Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,8 +74,8 @@ def trading_strategy(data, base_window=60, base_alpha=1.0, beta=0.1, trend_windo
|
|
| 74 |
positions = []
|
| 75 |
trend = data.rolling(window=trend_window).mean()
|
| 76 |
|
| 77 |
-
# For indices where the trend is not fully defined, append NaNs and no position.
|
| 78 |
for i in range(len(data)):
|
|
|
|
| 79 |
if i < trend_window - 1:
|
| 80 |
buy_signals.append(np.nan)
|
| 81 |
sell_signals.append(np.nan)
|
|
@@ -85,7 +85,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 |
-
#
|
| 89 |
price = float(data.iloc[i])
|
| 90 |
mu_value = float(mu.iloc[-1])
|
| 91 |
sigma_value = float(sigma.iloc[-1])
|
|
@@ -106,8 +106,10 @@ 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 |
def calculate_performance(data, positions):
|
| 110 |
-
|
|
|
|
| 111 |
strategy_returns = np.array(positions[:-1]) * returns
|
| 112 |
equity_curve = np.cumprod(1 + strategy_returns) * 100 # starting equity of 100
|
| 113 |
return equity_curve
|
|
|
|
| 74 |
positions = []
|
| 75 |
trend = data.rolling(window=trend_window).mean()
|
| 76 |
|
|
|
|
| 77 |
for i in range(len(data)):
|
| 78 |
+
# For early indices where the trend isn't fully defined, append NaNs and no position
|
| 79 |
if i < trend_window - 1:
|
| 80 |
buy_signals.append(np.nan)
|
| 81 |
sell_signals.append(np.nan)
|
|
|
|
| 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 |
|
| 107 |
return buy_signals, sell_signals, positions, trend
|
| 108 |
|
| 109 |
+
# Updated calculate_performance: Convert data to NumPy array explicitly
|
| 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
|
| 115 |
return equity_curve
|