Spaces:
Running
Running
import gradio as gr | |
import plotly.graph_objects as go | |
import yfinance as yf | |
import pandas as pd | |
def fetch_data(ticker, start_date, end_date): | |
data = yf.download(ticker, start=start_date, end=end_date) | |
return data | |
def plot_technical_analysis(ticker, start_date, end_date, analysis_type): | |
data = fetch_data(ticker, start_date, end_date) | |
fig = go.Figure() | |
if analysis_type == "Candlestick": | |
fig.add_trace(go.Candlestick(x=data.index, | |
open=data['Open'], | |
high=data['High'], | |
low=data['Low'], | |
close=data['Close'], | |
name='Candlestick')) | |
elif analysis_type == "Moving Average": | |
data['MA20'] = data['Close'].rolling(window=20).mean() | |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price')) | |
fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], mode='lines', name='20-day MA')) | |
elif analysis_type == "Bollinger Bands": | |
data['MA20'] = data['Close'].rolling(window=20).mean() | |
data['stddev'] = data['Close'].rolling(window=20).std() | |
data['upper'] = data['MA20'] + (data['stddev'] * 2) | |
data['lower'] = data['MA20'] - (data['stddev'] * 2) | |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price')) | |
fig.add_trace(go.Scatter(x=data.index, y=data['upper'], mode='lines', name='Upper Band')) | |
fig.add_trace(go.Scatter(x=data.index, y=data['lower'], mode='lines', name='Lower Band')) | |
fig.update_layout(title=f"{analysis_type} Analysis for {ticker}", xaxis_title="Date", yaxis_title="Price", xaxis_rangeslider_visible=False) | |
return fig | |
def plot_fundamental_analysis(ticker, analysis_type): | |
stock = yf.Ticker(ticker) | |
if analysis_type == "Financials": | |
data = stock.financials | |
elif analysis_type == "Balance Sheet": | |
data = stock.balance_sheet | |
elif analysis_type == "Cash Flow": | |
data = stock.cashflow | |
return data.to_html() | |
# Gradio Interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# Stock Analysis Tool") | |
with gr.Tab("Technical Analysis"): | |
ticker = gr.Textbox(label="Ticker Symbol", value="AAPL") | |
start_date = gr.Textbox(label="Start Date (YYYY-MM-DD)", value="2020-01-01") | |
end_date = gr.Textbox(label="End Date (YYYY-MM-DD)", value="2023-01-01") | |
analysis_type = gr.Radio(label="Analysis Type", choices=["Candlestick", "Moving Average", "Bollinger Bands"], value="Candlestick") | |
plot_button = gr.Button("Plot") | |
plot_output = gr.Plot() | |
plot_button.click(fn=plot_technical_analysis, inputs=[ticker, start_date, end_date, analysis_type], outputs=plot_output) | |
with gr.Tab("Fundamental Analysis"): | |
ticker = gr.Textbox(label="Ticker Symbol", value="AAPL") | |
analysis_type = gr.Radio(label="Analysis Type", choices=["Financials", "Balance Sheet", "Cash Flow"], value="Financials") | |
plot_button = gr.Button("Show Data") | |
table_output = gr.HTML() | |
plot_button.click(fn=plot_fundamental_analysis, inputs=[ticker, analysis_type], outputs=table_output) | |
# Launch the interface | |
demo.launch() |