Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 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)
|