QuantumLearner commited on
Commit
1892fea
·
verified ·
1 Parent(s): ac4f445

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -10
app.py CHANGED
@@ -111,13 +111,33 @@ def calculate_kmeans_clusters(data, n_days, num_clusters):
111
  cluster_centers = kmeans.cluster_centers_[:, 1] * (np.max(filtered_data['Close']) - np.min(filtered_data['Close'])) + np.min(filtered_data['Close'])
112
  return cluster_centers
113
 
114
- # Run analysis only if the user clicks the "Run Analysis" button
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  if st.sidebar.button('Run Analysis'):
116
  data = get_data(ticker, start_date, end_date)
117
 
118
  if not data.empty:
 
 
119
  if analysis_type == "Pivot Points":
120
- df_pivot = calculate_pivot_points(data.copy(), window_period)
121
  df_pivot = df_pivot.dropna()
122
 
123
  st.write("### Pivot Points")
@@ -148,8 +168,7 @@ if st.sidebar.button('Run Analysis'):
148
  st.plotly_chart(fig1, use_container_width=True)
149
 
150
  elif analysis_type == "Support and Resistance":
151
- support, resistance = find_levels(data.copy(), window_period)
152
- breaks_above_resistance, breaks_below_support = check_significant_break(data.copy(), support, resistance)
153
 
154
  st.write("### Rolling Midpoint Range")
155
  st.markdown("""
@@ -176,7 +195,7 @@ if st.sidebar.button('Run Analysis'):
176
  st.plotly_chart(fig2, use_container_width=True)
177
 
178
  elif analysis_type == "Swing Highs and Lows":
179
- data_with_trendlines = prepare_data_for_trendlines(data.copy(), lookback_period)
180
 
181
  st.write("### Swing Highs and Lows")
182
  st.markdown("""
@@ -200,7 +219,7 @@ if st.sidebar.button('Run Analysis'):
200
  st.plotly_chart(fig3, use_container_width=True)
201
 
202
  elif analysis_type == "Fibonacci Retracement Levels":
203
- fib_levels, levels = calculate_fibonacci_levels(data.copy(), lookback_period)
204
 
205
  st.write("### Fibonacci Retracement Levels")
206
  st.markdown("""
@@ -224,7 +243,7 @@ if st.sidebar.button('Run Analysis'):
224
  st.plotly_chart(fig4, use_container_width=True)
225
 
226
  elif analysis_type == "Trendlines":
227
- data_with_trendlines = prepare_data_for_trendlines(data.copy(), lookback_period)
228
 
229
  st.write("### Trendlines with Regression Analysis")
230
  st.markdown("""
@@ -261,7 +280,7 @@ if st.sidebar.button('Run Analysis'):
261
  st.plotly_chart(fig5, use_container_width=True)
262
 
263
  elif analysis_type == "Volume Profile":
264
- price_bins, volume_profile = calculate_volume_profile(data.copy(), n_days)
265
 
266
  st.write("### Volume Profile")
267
  st.markdown("""
@@ -295,7 +314,7 @@ if st.sidebar.button('Run Analysis'):
295
  st.pyplot(fig6, use_container_width=True)
296
 
297
  elif analysis_type == "KMeans Clustering":
298
- cluster_centers = calculate_kmeans_clusters(data.copy(), n_days, num_clusters)
299
 
300
  st.write("### KMeans Clusters")
301
  st.markdown("""
@@ -330,4 +349,4 @@ hide_streamlit_style = """
330
  footer {visibility: hidden;}
331
  </style>
332
  """
333
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
111
  cluster_centers = kmeans.cluster_centers_[:, 1] * (np.max(filtered_data['Close']) - np.min(filtered_data['Close'])) + np.min(filtered_data['Close'])
112
  return cluster_centers
113
 
114
+ # Cache results for different analyses
115
+ def get_analysis_results(analysis_type, data, window_period, lookback_period, n_days, num_clusters):
116
+ if analysis_type == "Pivot Points":
117
+ return calculate_pivot_points(data.copy(), window_period)
118
+ elif analysis_type == "Support and Resistance":
119
+ support, resistance = find_levels(data.copy(), window_period)
120
+ breaks_above_resistance, breaks_below_support = check_significant_break(data.copy(), support, resistance)
121
+ return support, resistance, breaks_above_resistance, breaks_below_support
122
+ elif analysis_type == "Swing Highs and Lows":
123
+ return prepare_data_for_trendlines(data.copy(), lookback_period)
124
+ elif analysis_type == "Fibonacci Retracement Levels":
125
+ return calculate_fibonacci_levels(data.copy(), lookback_period)
126
+ elif analysis_type == "Volume Profile":
127
+ return calculate_volume_profile(data.copy(), n_days)
128
+ elif analysis_type == "KMeans Clustering":
129
+ return calculate_kmeans_clusters(data.copy(), n_days, num_clusters)
130
+ return None
131
+
132
+ # Run analysis and display results
133
  if st.sidebar.button('Run Analysis'):
134
  data = get_data(ticker, start_date, end_date)
135
 
136
  if not data.empty:
137
+ results = get_analysis_results(analysis_type, data, window_period, lookback_period, n_days, num_clusters)
138
+
139
  if analysis_type == "Pivot Points":
140
+ df_pivot = results
141
  df_pivot = df_pivot.dropna()
142
 
143
  st.write("### Pivot Points")
 
168
  st.plotly_chart(fig1, use_container_width=True)
169
 
170
  elif analysis_type == "Support and Resistance":
171
+ support, resistance, breaks_above_resistance, breaks_below_support = results
 
172
 
173
  st.write("### Rolling Midpoint Range")
174
  st.markdown("""
 
195
  st.plotly_chart(fig2, use_container_width=True)
196
 
197
  elif analysis_type == "Swing Highs and Lows":
198
+ data_with_trendlines = results
199
 
200
  st.write("### Swing Highs and Lows")
201
  st.markdown("""
 
219
  st.plotly_chart(fig3, use_container_width=True)
220
 
221
  elif analysis_type == "Fibonacci Retracement Levels":
222
+ fib_levels, levels = results
223
 
224
  st.write("### Fibonacci Retracement Levels")
225
  st.markdown("""
 
243
  st.plotly_chart(fig4, use_container_width=True)
244
 
245
  elif analysis_type == "Trendlines":
246
+ data_with_trendlines = results
247
 
248
  st.write("### Trendlines with Regression Analysis")
249
  st.markdown("""
 
280
  st.plotly_chart(fig5, use_container_width=True)
281
 
282
  elif analysis_type == "Volume Profile":
283
+ price_bins, volume_profile = results
284
 
285
  st.write("### Volume Profile")
286
  st.markdown("""
 
314
  st.pyplot(fig6, use_container_width=True)
315
 
316
  elif analysis_type == "KMeans Clustering":
317
+ cluster_centers = results
318
 
319
  st.write("### KMeans Clusters")
320
  st.markdown("""
 
349
  footer {visibility: hidden;}
350
  </style>
351
  """
352
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)