Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -22,7 +22,7 @@ def fetch_data(ticker, period):
|
|
22 |
data = yf.download(ticker, period=periods[period])
|
23 |
return data
|
24 |
|
25 |
-
def plot_technical_analysis(ticker, period, analysis_type, ma_length,
|
26 |
data = fetch_data(ticker, period)
|
27 |
|
28 |
fig = go.Figure()
|
@@ -34,8 +34,7 @@ def plot_technical_analysis(ticker, period, analysis_type, ma_length, candle_set
|
|
34 |
low=data['Low'],
|
35 |
close=data['Close'],
|
36 |
name='Candlestick',
|
37 |
-
|
38 |
-
decreasing_line_color=candle_settings['decreasing_color']))
|
39 |
|
40 |
elif analysis_type == "Moving Average":
|
41 |
data[f'MA{ma_length}'] = data['Close'].rolling(window=ma_length).mean()
|
@@ -50,41 +49,30 @@ def plot_technical_analysis(ticker, period, analysis_type, ma_length, candle_set
|
|
50 |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price'))
|
51 |
fig.add_trace(go.Scatter(x=data.index, y=data['upper'], mode='lines', name='Upper Band'))
|
52 |
fig.add_trace(go.Scatter(x=data.index, y=data['lower'], mode='lines', name='Lower Band'))
|
53 |
-
|
54 |
-
fig.update_layout(title=f"{analysis_type} Analysis for {ticker}", xaxis_title="Date", yaxis_title="Price", xaxis_rangeslider_visible=False)
|
55 |
-
return fig
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
loss = -delta.where(delta < 0, 0)
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
rs = avg_gain / avg_loss
|
67 |
-
rsi = 100 - (100 / (1 + rs))
|
68 |
-
|
69 |
-
fig = go.Figure()
|
70 |
-
fig.add_trace(go.Scatter(x=data.index, y=rsi, mode='lines', name='RSI'))
|
71 |
-
fig.update_layout(title=f"RSI for {ticker}", xaxis_title="Date", yaxis_title="RSI", xaxis_rangeslider_visible=False)
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
def plot_macd(ticker, period):
|
76 |
-
data = fetch_data(ticker, period)
|
77 |
|
78 |
-
|
79 |
-
exp2 = data['Close'].ewm(span=26, adjust=False).mean()
|
80 |
-
macd = exp1 - exp2
|
81 |
-
signal = macd.ewm(span=9, adjust=False).mean()
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
87 |
|
|
|
|
|
|
|
|
|
88 |
return fig
|
89 |
|
90 |
def plot_fundamental_analysis(ticker, analysis_type):
|
@@ -171,31 +159,26 @@ with gr.Blocks() as demo:
|
|
171 |
gr.Markdown("# Stock Analysis Tool")
|
172 |
|
173 |
with gr.Tab("Technical Analysis"):
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
candle_settings = gr.JSON(label="Candlestick Settings", value={"increasing_color": "green", "decreasing_color": "red"})
|
179 |
-
plot_button = gr.Button("Plot")
|
180 |
-
plot_output = gr.Plot()
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
with gr.Tab("RSI"):
|
185 |
-
ticker = gr.Textbox(label="Ticker Symbol", value="AAPL")
|
186 |
-
period = gr.Dropdown(label="Period", choices=list(periods.keys()), value="1 Year")
|
187 |
-
plot_button = gr.Button("Plot RSI")
|
188 |
-
plot_output = gr.Plot()
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
196 |
plot_output = gr.Plot()
|
197 |
|
198 |
-
plot_button.click(fn=
|
199 |
|
200 |
with gr.Tab("Fundamental Analysis"):
|
201 |
ticker = gr.Textbox(label="Ticker Symbol", value="AAPL")
|
|
|
22 |
data = yf.download(ticker, period=periods[period])
|
23 |
return data
|
24 |
|
25 |
+
def plot_technical_analysis(ticker, period, analysis_type, ma_length, candle_period):
|
26 |
data = fetch_data(ticker, period)
|
27 |
|
28 |
fig = go.Figure()
|
|
|
34 |
low=data['Low'],
|
35 |
close=data['Close'],
|
36 |
name='Candlestick',
|
37 |
+
xperiod=candle_period))
|
|
|
38 |
|
39 |
elif analysis_type == "Moving Average":
|
40 |
data[f'MA{ma_length}'] = data['Close'].rolling(window=ma_length).mean()
|
|
|
49 |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price'))
|
50 |
fig.add_trace(go.Scatter(x=data.index, y=data['upper'], mode='lines', name='Upper Band'))
|
51 |
fig.add_trace(go.Scatter(x=data.index, y=data['lower'], mode='lines', name='Lower Band'))
|
|
|
|
|
|
|
52 |
|
53 |
+
elif analysis_type == "RSI":
|
54 |
+
delta = data['Close'].diff(1)
|
55 |
+
gain = delta.where(delta > 0, 0)
|
56 |
+
loss = -delta.where(delta < 0, 0)
|
|
|
57 |
|
58 |
+
avg_gain = gain.rolling(window=14).mean()
|
59 |
+
avg_loss = loss.rolling(window=14).mean()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
rs = avg_gain / avg_loss
|
62 |
+
rsi = 100 - (100 / (1 + rs))
|
|
|
|
|
63 |
|
64 |
+
fig.add_trace(go.Scatter(x=data.index, y=rsi, mode='lines', name='RSI'))
|
|
|
|
|
|
|
65 |
|
66 |
+
elif analysis_type == "MACD":
|
67 |
+
exp1 = data['Close'].ewm(span=12, adjust=False).mean()
|
68 |
+
exp2 = data['Close'].ewm(span=26, adjust=False).mean()
|
69 |
+
macd = exp1 - exp2
|
70 |
+
signal = macd.ewm(span=9, adjust=False).mean()
|
71 |
|
72 |
+
fig.add_trace(go.Scatter(x=data.index, y=macd, mode='lines', name='MACD'))
|
73 |
+
fig.add_trace(go.Scatter(x=data.index, y=signal, mode='lines', name='Signal Line'))
|
74 |
+
|
75 |
+
fig.update_layout(title=f"{analysis_type} Analysis for {ticker}", xaxis_title="Date", yaxis_title="Price", xaxis_rangeslider_visible=False)
|
76 |
return fig
|
77 |
|
78 |
def plot_fundamental_analysis(ticker, analysis_type):
|
|
|
159 |
gr.Markdown("# Stock Analysis Tool")
|
160 |
|
161 |
with gr.Tab("Technical Analysis"):
|
162 |
+
with gr.Row():
|
163 |
+
ticker = gr.Textbox(label="Ticker Symbol", value="AAPL")
|
164 |
+
period = gr.Dropdown(label="Period", choices=list(periods.keys()), value="1 Year")
|
165 |
+
analysis_type = gr.Radio(label="Analysis Type", choices=["Candlestick", "Moving Average", "Bollinger Bands", "RSI", "MACD"], value="Candlestick")
|
|
|
|
|
|
|
166 |
|
167 |
+
ma_length = gr.Number(label="Moving Average Length", value=20, visible=False)
|
168 |
+
candle_period = gr.Number(label="Candlestick Period", value=1, visible=False)
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
+
def update_visibility(analysis_type):
|
171 |
+
return (
|
172 |
+
gr.update(visible=analysis_type == "Moving Average"),
|
173 |
+
gr.update(visible=analysis_type == "Candlestick")
|
174 |
+
)
|
175 |
+
|
176 |
+
analysis_type.change(fn=update_visibility, inputs=analysis_type, outputs=[ma_length, candle_period])
|
177 |
+
|
178 |
+
plot_button = gr.Button("Plot")
|
179 |
plot_output = gr.Plot()
|
180 |
|
181 |
+
plot_button.click(fn=plot_technical_analysis, inputs=[ticker, period, analysis_type, ma_length, candle_period], outputs=plot_output)
|
182 |
|
183 |
with gr.Tab("Fundamental Analysis"):
|
184 |
ticker = gr.Textbox(label="Ticker Symbol", value="AAPL")
|