Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,14 +9,42 @@ def load_data(ticker, start_date, end_date):
|
|
9 |
data.reset_index(inplace=True)
|
10 |
return data
|
11 |
|
12 |
-
# Function to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def plot_candlestick_chart(data):
|
14 |
fig = go.Figure(data=[go.Candlestick(
|
15 |
x=data['Date'],
|
16 |
open=data['Open'], high=data['High'],
|
17 |
low=data['Low'], close=data['Close'],
|
|
|
18 |
name='Candlestick')])
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
return fig
|
21 |
|
22 |
# Streamlit user interface
|
@@ -30,12 +58,13 @@ button = st.sidebar.button('Analyze')
|
|
30 |
st.title('Fight the Tiger Trading Strategy Visualization')
|
31 |
st.markdown("""
|
32 |
This app analyzes and visualizes the "Fight the Tiger" trading strategy using historical stock data fetched from Yahoo Finance.
|
33 |
-
|
34 |
""")
|
35 |
|
36 |
if button:
|
37 |
if start_date < end_date:
|
38 |
data = load_data(ticker, start_date, end_date)
|
|
|
39 |
fig = plot_candlestick_chart(data)
|
40 |
st.plotly_chart(fig, use_container_width=True)
|
41 |
else:
|
|
|
9 |
data.reset_index(inplace=True)
|
10 |
return data
|
11 |
|
12 |
+
# Function to identify buy and sell signals
|
13 |
+
def identify_signals(data):
|
14 |
+
# Assume bearish and bullish candles that are significantly larger than usual as signals
|
15 |
+
data['Signal'] = 'None' # Default no signal
|
16 |
+
avg_body_size = (abs(data['Open'] - data['Close'])).mean() # Average body size of the candles
|
17 |
+
|
18 |
+
for i in range(1, len(data)):
|
19 |
+
body_size = abs(data['Open'][i] - data['Close'][i])
|
20 |
+
if body_size > avg_body_size * 1.5: # Threshold of 150% of average body size
|
21 |
+
if data['Close'][i] < data['Open'][i]:
|
22 |
+
data['Signal'][i] = 'Sell' # Bearish candle
|
23 |
+
elif data['Close'][i] > data['Open'][i]:
|
24 |
+
data['Signal'][i] = 'Buy' # Bullish candle
|
25 |
+
|
26 |
+
return data
|
27 |
+
|
28 |
+
# Function to plot candlestick chart with signals
|
29 |
def plot_candlestick_chart(data):
|
30 |
fig = go.Figure(data=[go.Candlestick(
|
31 |
x=data['Date'],
|
32 |
open=data['Open'], high=data['High'],
|
33 |
low=data['Low'], close=data['Close'],
|
34 |
+
increasing_line_color='green', decreasing_line_color='red',
|
35 |
name='Candlestick')])
|
36 |
+
|
37 |
+
# Add signals to the plot
|
38 |
+
buys = data[data['Signal'] == 'Buy']
|
39 |
+
sells = data[data['Signal'] == 'Sell']
|
40 |
+
for i in buys.index:
|
41 |
+
fig.add_annotation(x=buys['Date'][i], y=buys['High'][i],
|
42 |
+
text='Buy', showarrow=True, arrowhead=1, arrowcolor='green', arrowsize=3)
|
43 |
+
for i in sells.index:
|
44 |
+
fig.add_annotation(x=sells['Date'][i], y=sells['Low'][i],
|
45 |
+
text='Sell', showarrow=True, arrowhead=1, arrowcolor='red', arrowsize=3)
|
46 |
+
|
47 |
+
fig.update_layout(title='Candlestick Chart with Buy and Sell Signals', xaxis_rangeslider_visible=False)
|
48 |
return fig
|
49 |
|
50 |
# Streamlit user interface
|
|
|
58 |
st.title('Fight the Tiger Trading Strategy Visualization')
|
59 |
st.markdown("""
|
60 |
This app analyzes and visualizes the "Fight the Tiger" trading strategy using historical stock data fetched from Yahoo Finance.
|
61 |
+
Enter a stock ticker and select a date range to view the candlestick chart with potential buy and sell signals based on significant candlestick formations.
|
62 |
""")
|
63 |
|
64 |
if button:
|
65 |
if start_date < end_date:
|
66 |
data = load_data(ticker, start_date, end_date)
|
67 |
+
data = identify_signals(data) # Call to identify signals
|
68 |
fig = plot_candlestick_chart(data)
|
69 |
st.plotly_chart(fig, use_container_width=True)
|
70 |
else:
|