Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,10 +48,11 @@ with st.sidebar.expander("Asset Settings", expanded=True):
|
|
| 48 |
start_date = st.date_input("Start Date", value=pd.to_datetime("2020-01-01"), help="Select the start date for historical data.")
|
| 49 |
end_date = st.date_input("End Date", value=datetime.today() + pd.DateOffset(1), help="Select the end date for historical data.")
|
| 50 |
|
|
|
|
| 51 |
@st.cache_data
|
| 52 |
def get_data(ticker, start, end):
|
| 53 |
data = yf.download(ticker, start=start, end=end)
|
| 54 |
-
return data['Close']
|
| 55 |
|
| 56 |
def OU_parameters_ema(data, window):
|
| 57 |
window = int(window) # ensure window is scalar
|
|
@@ -75,7 +76,7 @@ def trading_strategy(data, base_window=60, base_alpha=1.0, beta=0.1, trend_windo
|
|
| 75 |
trend = data.rolling(window=trend_window).mean()
|
| 76 |
|
| 77 |
for i in range(len(data)):
|
| 78 |
-
# For early indices where the trend
|
| 79 |
if i < trend_window - 1:
|
| 80 |
buy_signals.append(np.nan)
|
| 81 |
sell_signals.append(np.nan)
|
|
@@ -85,7 +86,7 @@ def trading_strategy(data, base_window=60, base_alpha=1.0, beta=0.1, trend_windo
|
|
| 85 |
window = int(windows.iloc[i])
|
| 86 |
mu, sigma = OU_parameters_ema(data[:i+1], window=window)
|
| 87 |
alpha = base_alpha + beta * float(sigma.iloc[-1])
|
| 88 |
-
# Force each value to be a float to ensure scalar comparisons
|
| 89 |
price = float(data.iloc[i])
|
| 90 |
mu_value = float(mu.iloc[-1])
|
| 91 |
sigma_value = float(sigma.iloc[-1])
|
|
@@ -106,9 +107,9 @@ def trading_strategy(data, base_window=60, base_alpha=1.0, beta=0.1, trend_windo
|
|
| 106 |
|
| 107 |
return buy_signals, sell_signals, positions, trend
|
| 108 |
|
| 109 |
-
#
|
| 110 |
def calculate_performance(data, positions):
|
| 111 |
-
data_np = data.to_numpy()
|
| 112 |
returns = np.diff(data_np) / data_np[:-1]
|
| 113 |
strategy_returns = np.array(positions[:-1]) * returns
|
| 114 |
equity_curve = np.cumprod(1 + strategy_returns) * 100 # starting equity of 100
|
|
|
|
| 48 |
start_date = st.date_input("Start Date", value=pd.to_datetime("2020-01-01"), help="Select the start date for historical data.")
|
| 49 |
end_date = st.date_input("End Date", value=datetime.today() + pd.DateOffset(1), help="Select the end date for historical data.")
|
| 50 |
|
| 51 |
+
# Modified get_data: squeeze the 'Close' column to ensure a 1D Series
|
| 52 |
@st.cache_data
|
| 53 |
def get_data(ticker, start, end):
|
| 54 |
data = yf.download(ticker, start=start, end=end)
|
| 55 |
+
return data['Close'].squeeze()
|
| 56 |
|
| 57 |
def OU_parameters_ema(data, window):
|
| 58 |
window = int(window) # ensure window is scalar
|
|
|
|
| 76 |
trend = data.rolling(window=trend_window).mean()
|
| 77 |
|
| 78 |
for i in range(len(data)):
|
| 79 |
+
# For early indices where the trend is not fully defined, skip trading logic.
|
| 80 |
if i < trend_window - 1:
|
| 81 |
buy_signals.append(np.nan)
|
| 82 |
sell_signals.append(np.nan)
|
|
|
|
| 86 |
window = int(windows.iloc[i])
|
| 87 |
mu, sigma = OU_parameters_ema(data[:i+1], window=window)
|
| 88 |
alpha = base_alpha + beta * float(sigma.iloc[-1])
|
| 89 |
+
# Force each value to be a float to ensure scalar comparisons:
|
| 90 |
price = float(data.iloc[i])
|
| 91 |
mu_value = float(mu.iloc[-1])
|
| 92 |
sigma_value = float(sigma.iloc[-1])
|
|
|
|
| 107 |
|
| 108 |
return buy_signals, sell_signals, positions, trend
|
| 109 |
|
| 110 |
+
# Modified calculate_performance: explicitly convert the price Series to a 1D NumPy array.
|
| 111 |
def calculate_performance(data, positions):
|
| 112 |
+
data_np = data.to_numpy() # ensure 1D array
|
| 113 |
returns = np.diff(data_np) / data_np[:-1]
|
| 114 |
strategy_returns = np.array(positions[:-1]) * returns
|
| 115 |
equity_curve = np.cumprod(1 + strategy_returns) * 100 # starting equity of 100
|