File size: 4,244 Bytes
cfa6eff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import re
with open('app_test.py', 'r') as f:
content = f.read()
old_future = """ if model_keys:
for mk in model_keys:
model_seed = int(hashlib.md5(mk.encode()).hexdigest(), 16) % (2**32)
np.random.seed(model_seed)
# Mock forecast projection
volatility = df['adj_close'].pct_change().std()
momentum = df['adj_close'].pct_change().tail(20).mean()
bias_dir = 1 if (model_seed % 2) == 0 else -1
bias = bias_dir * (0.00005 + ((model_seed % 100) / 1000000.0))
# Project 252 days
future_returns = np.random.normal(momentum + bias, volatility, 252)
future_prices = last_price * np.cumprod(1 + future_returns)
future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=252, freq='B')
m_name = mk.split('/')[-1]
fig_fcst.add_trace(go.Scatter(x=future_dates, y=future_prices, name=f"{m_name} Forecast", mode="lines", line=dict(dash="dot")))
for label, days in horizons:
idx = days - 1
pred_p = future_prices[idx]
ret = (pred_p / last_price) - 1
direction = "↑ Bullish" if ret > 0 else "↓ Bearish"
fcst_rows.append([m_name, label, f"${pred_p:.2f}", f"{ret:.2%}", direction, "Not provided"])"""
new_future = """ if model_keys:
future_prices_dict = {}
# Generate individual models
for mk in [m for m in model_keys if m != "system/meta-learner"]:
model_seed = int(hashlib.md5(mk.encode()).hexdigest(), 16) % (2**32)
np.random.seed(model_seed)
volatility = df['adj_close'].pct_change().std()
momentum = df['adj_close'].pct_change().tail(20).mean()
bias_dir = 1 if (model_seed % 2) == 0 else -1
bias = bias_dir * (0.00005 + ((model_seed % 100) / 1000000.0))
future_returns = np.random.normal(momentum + bias, volatility, 252)
future_prices = last_price * np.cumprod(1 + future_returns)
future_prices_dict[mk] = future_prices
future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=252, freq='B')
m_name = mk.split('/')[-1]
fig_fcst.add_trace(go.Scatter(x=future_dates, y=future_prices, name=f"{m_name} Forecast", mode="lines", line=dict(dash="dot")))
for label, days in horizons:
idx = days - 1
pred_p = future_prices[idx]
ret = (pred_p / last_price) - 1
direction = "↑ Bullish" if ret > 0 else "↓ Bearish"
fcst_rows.append([m_name, label, f"${pred_p:.2f}", f"{ret:.2%}", direction, "Not provided"])
# Generate meta-learner forecast
if "system/meta-learner" in model_keys and future_prices_dict:
# Use the final weights from the meta-learner logic
meta_future = np.zeros(252)
for j, mk in enumerate([m for m in model_keys if m != "system/meta-learner"]):
meta_future += weights[j] * future_prices_dict[mk]
fig_fcst.add_trace(go.Scatter(x=future_dates, y=meta_future, name="🧠 META-LEARNER", mode="lines", line=dict(color="#8b5cf6", width=3)))
for label, days in horizons:
idx = days - 1
pred_p = meta_future[idx]
ret = (pred_p / last_price) - 1
direction = "↑ Bullish" if ret > 0 else "↓ Bearish"
fcst_rows.append(["META-LEARNER", label, f"${pred_p:.2f}", f"{ret:.2%}", direction, "High"])"""
content = content.replace(old_future, new_future)
with open('app_test.py', 'w') as f:
f.write(content)
|