dibend commited on
Commit
e5939e6
1 Parent(s): ac05b5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import plotly.graph_objects as go
3
+ import yfinance as yf
4
+ import pandas as pd
5
+
6
+ def fetch_data(ticker, start_date, end_date):
7
+ data = yf.download(ticker, start=start_date, end=end_date)
8
+ return data
9
+
10
+ def plot_technical_analysis(ticker, start_date, end_date, analysis_type):
11
+ data = fetch_data(ticker, start_date, end_date)
12
+
13
+ fig = go.Figure()
14
+
15
+ if analysis_type == "Candlestick":
16
+ fig.add_trace(go.Candlestick(x=data.index,
17
+ open=data['Open'],
18
+ high=data['High'],
19
+ low=data['Low'],
20
+ close=data['Close'],
21
+ name='Candlestick'))
22
+
23
+ elif analysis_type == "Moving Average":
24
+ data['MA20'] = data['Close'].rolling(window=20).mean()
25
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price'))
26
+ fig.add_trace(go.Scatter(x=data.index, y=data['MA20'], mode='lines', name='20-day MA'))
27
+
28
+ elif analysis_type == "Bollinger Bands":
29
+ data['MA20'] = data['Close'].rolling(window=20).mean()
30
+ data['stddev'] = data['Close'].rolling(window=20).std()
31
+ data['upper'] = data['MA20'] + (data['stddev'] * 2)
32
+ data['lower'] = data['MA20'] - (data['stddev'] * 2)
33
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price'))
34
+ fig.add_trace(go.Scatter(x=data.index, y=data['upper'], mode='lines', name='Upper Band'))
35
+ fig.add_trace(go.Scatter(x=data.index, y=data['lower'], mode='lines', name='Lower Band'))
36
+
37
+ fig.update_layout(title=f"{analysis_type} Analysis for {ticker}", xaxis_title="Date", yaxis_title="Price", xaxis_rangeslider_visible=False)
38
+ return fig
39
+
40
+ def plot_fundamental_analysis(ticker, analysis_type):
41
+ stock = yf.Ticker(ticker)
42
+
43
+ if analysis_type == "Financials":
44
+ data = stock.financials
45
+ elif analysis_type == "Balance Sheet":
46
+ data = stock.balance_sheet
47
+ elif analysis_type == "Cash Flow":
48
+ data = stock.cashflow
49
+
50
+ return data.to_html()
51
+
52
+ # Gradio Interface
53
+ with gr.Blocks() as demo:
54
+ gr.Markdown("# Stock Analysis Tool")
55
+
56
+ with gr.Tab("Technical Analysis"):
57
+ ticker = gr.Textbox(label="Ticker Symbol", value="AAPL")
58
+ start_date = gr.Textbox(label="Start Date (YYYY-MM-DD)", value="2020-01-01")
59
+ end_date = gr.Textbox(label="End Date (YYYY-MM-DD)", value="2023-01-01")
60
+ analysis_type = gr.Radio(label="Analysis Type", choices=["Candlestick", "Moving Average", "Bollinger Bands"], value="Candlestick")
61
+ plot_button = gr.Button("Plot")
62
+ plot_output = gr.Plot()
63
+
64
+ plot_button.click(fn=plot_technical_analysis, inputs=[ticker, start_date, end_date, analysis_type], outputs=plot_output)
65
+
66
+ with gr.Tab("Fundamental Analysis"):
67
+ ticker = gr.Textbox(label="Ticker Symbol", value="AAPL")
68
+ analysis_type = gr.Radio(label="Analysis Type", choices=["Financials", "Balance Sheet", "Cash Flow"], value="Financials")
69
+ plot_button = gr.Button("Show Data")
70
+ table_output = gr.HTML()
71
+
72
+ plot_button.click(fn=plot_fundamental_analysis, inputs=[ticker, analysis_type], outputs=table_output)
73
+
74
+ # Launch the interface
75
+ demo.launch()