QuantumLearner commited on
Commit
3d8dc02
·
verified ·
1 Parent(s): a916406

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -94,7 +94,6 @@ with st.sidebar.expander("Moving Average Parameters", expanded=False):
94
  ma_short_period = st.number_input('Short-term Moving Average Period', min_value=1, value=20, help="Set the period for the short-term moving average.")
95
  ma_long_period = st.number_input('Long-term Moving Average Period', min_value=1, value=50, help="Set the period for the long-term moving average.")
96
 
97
-
98
  # Place the Run Analysis button above the candlestick pattern checkboxes
99
  if st.sidebar.button('Run Analysis'):
100
  run_analysis = True
@@ -106,23 +105,30 @@ with st.sidebar.expander("Candlestick Patterns", expanded=True):
106
  selected_patterns = {name: st.checkbox(name, value=True) for name, func in pattern_funcs}
107
 
108
  if run_analysis:
109
- data = yf.download(symbol, start=start_date, end=end_date)
110
-
 
 
 
111
  if not data.empty:
112
- # Calculate moving averages based on user input
113
  data[f'MA{ma_short_period}'] = talib.SMA(data['Close'], timeperiod=ma_short_period)
114
  data[f'MA{ma_long_period}'] = talib.SMA(data['Close'], timeperiod=ma_long_period)
115
 
116
  for pattern_name, pattern_func in pattern_funcs:
117
  if selected_patterns[pattern_name]:
 
118
  data[pattern_name] = pattern_func(data['Open'], data['High'], data['Low'], data['Close'])
119
- pattern_dates = data[data[pattern_name] != 0].index
120
 
121
  if len(pattern_dates) == 0:
 
122
  continue
123
 
 
124
  fig = go.Figure()
125
 
 
126
  fig.add_trace(go.Candlestick(
127
  x=data.index,
128
  open=data['Open'],
@@ -133,6 +139,7 @@ if run_analysis:
133
  yaxis='y2'
134
  ))
135
 
 
136
  fig.add_trace(go.Scatter(
137
  x=data.index,
138
  y=data[f'MA{ma_short_period}'],
@@ -142,6 +149,7 @@ if run_analysis:
142
  yaxis='y2'
143
  ))
144
 
 
145
  fig.add_trace(go.Scatter(
146
  x=data.index,
147
  y=data[f'MA{ma_long_period}'],
@@ -151,6 +159,7 @@ if run_analysis:
151
  yaxis='y2'
152
  ))
153
 
 
154
  fig.add_trace(go.Bar(
155
  x=data.index,
156
  y=data['Volume'],
@@ -160,9 +169,11 @@ if run_analysis:
160
  opacity=0.5
161
  ))
162
 
 
163
  for date in pattern_dates:
164
  fig.add_vline(x=date, line=dict(color='red', width=2, dash='dash'), name=pattern_name)
165
 
 
166
  fig.update_layout(
167
  title=f"{symbol} Price and {pattern_name} Pattern Detection",
168
  xaxis_title="Date",
@@ -170,12 +181,13 @@ if run_analysis:
170
  yaxis2=dict(title='Price', overlaying='y', side='right'),
171
  legend_title="Legend",
172
  xaxis_rangeslider_visible=False,
173
- template='plotly_white'
 
174
  )
175
 
176
- st.plotly_chart(fig)
177
  else:
178
- st.write("No data found for the given ticker and date range.")
179
 
180
  # Hide the Streamlit style
181
  hide_streamlit_style = """
@@ -184,4 +196,4 @@ hide_streamlit_style = """
184
  footer {visibility: hidden;}
185
  </style>
186
  """
187
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
94
  ma_short_period = st.number_input('Short-term Moving Average Period', min_value=1, value=20, help="Set the period for the short-term moving average.")
95
  ma_long_period = st.number_input('Long-term Moving Average Period', min_value=1, value=50, help="Set the period for the long-term moving average.")
96
 
 
97
  # Place the Run Analysis button above the candlestick pattern checkboxes
98
  if st.sidebar.button('Run Analysis'):
99
  run_analysis = True
 
105
  selected_patterns = {name: st.checkbox(name, value=True) for name, func in pattern_funcs}
106
 
107
  if run_analysis:
108
+ # Fetch data with yfinance adjustments
109
+ data = yf.download(symbol, start=start_date, end=end_date, auto_adjust=False)
110
+ if isinstance(data.columns, pd.MultiIndex):
111
+ data.columns = data.columns.get_level_values(0)
112
+
113
  if not data.empty:
114
+ # Calculate moving averages based on user input with NaN handling
115
  data[f'MA{ma_short_period}'] = talib.SMA(data['Close'], timeperiod=ma_short_period)
116
  data[f'MA{ma_long_period}'] = talib.SMA(data['Close'], timeperiod=ma_long_period)
117
 
118
  for pattern_name, pattern_func in pattern_funcs:
119
  if selected_patterns[pattern_name]:
120
+ # Calculate pattern with TALib and handle potential NaNs
121
  data[pattern_name] = pattern_func(data['Open'], data['High'], data['Low'], data['Close'])
122
+ pattern_dates = data[data[pattern_name].notna() & (data[pattern_name] != 0)].index
123
 
124
  if len(pattern_dates) == 0:
125
+ st.write(f"No {pattern_name} patterns detected for {symbol} in the selected date range.")
126
  continue
127
 
128
+ # Create Plotly figure
129
  fig = go.Figure()
130
 
131
+ # Candlestick chart
132
  fig.add_trace(go.Candlestick(
133
  x=data.index,
134
  open=data['Open'],
 
139
  yaxis='y2'
140
  ))
141
 
142
+ # Short-term moving average
143
  fig.add_trace(go.Scatter(
144
  x=data.index,
145
  y=data[f'MA{ma_short_period}'],
 
149
  yaxis='y2'
150
  ))
151
 
152
+ # Long-term moving average
153
  fig.add_trace(go.Scatter(
154
  x=data.index,
155
  y=data[f'MA{ma_long_period}'],
 
159
  yaxis='y2'
160
  ))
161
 
162
+ # Volume bars
163
  fig.add_trace(go.Bar(
164
  x=data.index,
165
  y=data['Volume'],
 
169
  opacity=0.5
170
  ))
171
 
172
+ # Add vertical lines for pattern detection
173
  for date in pattern_dates:
174
  fig.add_vline(x=date, line=dict(color='red', width=2, dash='dash'), name=pattern_name)
175
 
176
+ # Update layout
177
  fig.update_layout(
178
  title=f"{symbol} Price and {pattern_name} Pattern Detection",
179
  xaxis_title="Date",
 
181
  yaxis2=dict(title='Price', overlaying='y', side='right'),
182
  legend_title="Legend",
183
  xaxis_rangeslider_visible=False,
184
+ template='plotly_white',
185
+ height=600
186
  )
187
 
188
+ st.plotly_chart(fig, use_container_width=True)
189
  else:
190
+ st.error(f"No data found for {symbol} in the given date range ({start_date} to {end_date}).")
191
 
192
  # Hide the Streamlit style
193
  hide_streamlit_style = """
 
196
  footer {visibility: hidden;}
197
  </style>
198
  """
199
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)