QuantumLearner commited on
Commit
f048ab2
·
verified ·
1 Parent(s): 1b2c776

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -54,8 +54,13 @@ with st.sidebar.expander("Asset Settings", expanded=True):
54
  # Function to download data
55
  @st.cache_data
56
  def get_data(ticker, start, end):
57
- data = yf.download(ticker, start=start, end=end)
58
- # Squeeze the 'Close' column to ensure a 1D Series (fixes new yfinance behavior)
 
 
 
 
 
59
  return data['Close'].squeeze()
60
 
61
  # Exponential Moving Average based OU parameters
@@ -155,29 +160,32 @@ def grid_search(data, param_grid):
155
  run_button = st.sidebar.button("Run Strategy")
156
 
157
  if run_button:
158
- # Get historical data
159
- data = get_data(ticker, start_date, end_date)
160
- param_grid = {
161
- 'base_window': [30, 50, 70, 90],
162
- 'base_alpha': [0.5, 1.0, 1.5],
163
- 'beta': [0.05, 0.1, 0.15],
164
- 'trend_window': [100, 200, 300]
165
- }
166
- best_params, best_performance, best_positions, best_trend = grid_search(data, param_grid)
167
- st.session_state['data'] = data
168
- st.session_state['best_params'] = best_params
169
- st.session_state['best_positions'] = best_positions
170
- st.session_state['best_trend'] = best_trend
171
-
172
- # Display best parameters in JSON format
173
- st.json({
174
- "Best Parameters": {
175
- "Base Window": best_params[0],
176
- "Base Alpha": best_params[1],
177
- "Beta": best_params[2],
178
- "Trend Window": best_params[3]
179
  }
180
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  # If the session state has the optimized data, allow updating the signal threshold and other parameters without re-running the optimization
183
  if 'best_params' in st.session_state:
@@ -257,4 +265,4 @@ hide_streamlit_style = """
257
  footer {visibility: hidden;}
258
  </style>
259
  """
260
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
54
  # Function to download data
55
  @st.cache_data
56
  def get_data(ticker, start, end):
57
+ data = yf.download(ticker, start=start, end=end, auto_adjust=False)
58
+ if isinstance(data.columns, pd.MultiIndex):
59
+ data.columns = data.columns.get_level_values(0)
60
+ if data.empty:
61
+ raise ValueError(f"No data retrieved for {ticker}")
62
+ if len(data) < 300: # Ensure enough data for largest trend window (300)
63
+ raise ValueError(f"Insufficient data points for {ticker}. Need at least 300 days.")
64
  return data['Close'].squeeze()
65
 
66
  # Exponential Moving Average based OU parameters
 
160
  run_button = st.sidebar.button("Run Strategy")
161
 
162
  if run_button:
163
+ try:
164
+ # Get historical data
165
+ data = get_data(ticker, start_date, end_date)
166
+ param_grid = {
167
+ 'base_window': [30, 50, 70, 90],
168
+ 'base_alpha': [0.5, 1.0, 1.5],
169
+ 'beta': [0.05, 0.1, 0.15],
170
+ 'trend_window': [100, 200, 300]
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  }
172
+ best_params, best_performance, best_positions, best_trend = grid_search(data, param_grid)
173
+ st.session_state['data'] = data
174
+ st.session_state['best_params'] = best_params
175
+ st.session_state['best_positions'] = best_positions
176
+ st.session_state['best_trend'] = best_trend
177
+
178
+ # Display best parameters in JSON format
179
+ st.json({
180
+ "Best Parameters": {
181
+ "Base Window": best_params[0],
182
+ "Base Alpha": best_params[1],
183
+ "Beta": best_params[2],
184
+ "Trend Window": best_params[3]
185
+ }
186
+ })
187
+ except Exception as e:
188
+ st.error(f"An error occurred while running the analysis: {e}")
189
 
190
  # If the session state has the optimized data, allow updating the signal threshold and other parameters without re-running the optimization
191
  if 'best_params' in st.session_state:
 
265
  footer {visibility: hidden;}
266
  </style>
267
  """
268
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)