mr601s's picture
Create utils/chart_utils.py
8f182cb verified
import yfinance as yf
import plotly.graph_objects as go
from datetime import datetime, timedelta
class ChartUtils:
def __init__(self):
pass
def create_price_chart(self, ticker, period="6mo"):
"""Create a price chart for the ticker"""
try:
stock = yf.Ticker(ticker)
hist = stock.history(period=period)
if hist.empty:
return None
fig = go.Figure()
fig.add_trace(go.Scatter(
x=hist.index,
y=hist['Close'],
mode='lines',
name=f'{ticker} Close Price',
line=dict(color='#1f77b4', width=2)
))
fig.update_layout(
title=f'{ticker} Stock Price - Last {period}',
xaxis_title='Date',
yaxis_title='Price ($)',
hovermode='x unified',
showlegend=True,
height=400
)
return fig
except Exception as e:
return None