Upload main.py
Browse files
main.py
CHANGED
|
@@ -1,362 +1,294 @@
|
|
| 1 |
-
"""AlphaForge -
|
| 2 |
-
|
| 3 |
-
Usage:
|
| 4 |
-
python main.py --mode train --tickers SPY QQQ AAPL MSFT
|
| 5 |
-
python main.py --mode backtest --start 2020-01-01 --end 2024-01-01
|
| 6 |
-
python main.py --mode live --config config.yaml
|
| 7 |
-
"""
|
| 8 |
import argparse
|
|
|
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
import pandas as pd
|
| 11 |
import torch
|
| 12 |
import warnings
|
| 13 |
warnings.filterwarnings('ignore')
|
| 14 |
|
|
|
|
| 15 |
from market_data import MarketDataPipeline
|
| 16 |
from alpha_model import AlphaEnsemble
|
| 17 |
from sentiment_model import SentimentAlphaModel
|
| 18 |
from volatility_model import VolatilityEngine
|
| 19 |
from portfolio_optimizer import PortfolioOptimizer
|
| 20 |
-
from options_pricer import MLOptionsPricer
|
| 21 |
from backtest_engine import BacktestEngine, compute_information_coefficient, RegimeDetector
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
parser.add_argument('--device', type=str, default='cpu')
|
| 36 |
-
parser.add_argument('--initial_capital', type=float, default=1_000_000)
|
| 37 |
-
parser.add_argument('--output', type=str, default='results/')
|
| 38 |
-
return parser.parse_args()
|
| 39 |
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
pipeline = MarketDataPipeline(args.tickers, args.start, args.end)
|
| 49 |
-
data = pipeline.fetch_data()
|
| 50 |
-
|
| 51 |
-
# Create features
|
| 52 |
-
features_df = pipeline.create_feature_matrix()
|
| 53 |
-
X, y, tickers, dates = pipeline.create_sequences(
|
| 54 |
-
features_df, lookback=args.lookback, forecast_horizon=args.horizon
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
print(f"\nDataset: {len(X)} samples, {X.shape[2]} features, seq_len={args.lookback}")
|
| 58 |
-
|
| 59 |
-
# Train/val/test split (time-based)
|
| 60 |
-
n = len(X)
|
| 61 |
-
train_end = int(n * 0.7)
|
| 62 |
-
val_end = int(n * 0.85)
|
| 63 |
-
|
| 64 |
-
X_train, y_train = X[:train_end], y[:train_end]
|
| 65 |
-
X_val, y_val = X[train_end:val_end], y[train_end:val_end]
|
| 66 |
-
X_test, y_test = X[val_end:], y[val_end:]
|
| 67 |
-
|
| 68 |
-
print(f"Train: {len(X_train)}, Val: {len(X_val)}, Test: {len(X_test)}")
|
| 69 |
-
|
| 70 |
-
# Train ensemble
|
| 71 |
-
ensemble = AlphaEnsemble(
|
| 72 |
-
input_size=X.shape[2],
|
| 73 |
-
seq_len=args.lookback,
|
| 74 |
-
device=args.device
|
| 75 |
-
)
|
| 76 |
-
|
| 77 |
-
metrics = ensemble.fit(
|
| 78 |
-
X_train, y_train,
|
| 79 |
-
X_val, y_val,
|
| 80 |
-
epochs=args.epochs,
|
| 81 |
-
batch_size=64,
|
| 82 |
-
lr=1e-4
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
# Test predictions
|
| 86 |
-
test_pred = ensemble.predict(X_test)
|
| 87 |
-
test_ic = compute_information_coefficient(
|
| 88 |
-
pd.Series(test_pred),
|
| 89 |
-
pd.Series(y_test),
|
| 90 |
-
by_date=False
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
print(f"\nTest IC: {test_ic['mean_ic']:.4f}")
|
| 94 |
-
print(f"LSTM final val IC: {metrics['lstm']['val_ic'][-1]:.4f}")
|
| 95 |
-
print(f"Transformer final val IC: {metrics['transformer']['val_ic'][-1]:.4f}")
|
| 96 |
-
|
| 97 |
-
# Save model
|
| 98 |
-
torch.save(ensemble.lstm.state_dict(), f"{args.output}/lstm_model.pt")
|
| 99 |
-
torch.save(ensemble.transformer.state_dict(), f"{args.output}/transformer_model.pt")
|
| 100 |
-
|
| 101 |
-
return ensemble, metrics, test_ic
|
| 102 |
|
| 103 |
|
| 104 |
-
def
|
| 105 |
-
"""Run
|
| 106 |
-
print("=" *
|
| 107 |
-
print("
|
| 108 |
-
print("=" *
|
| 109 |
-
|
| 110 |
-
#
|
|
|
|
| 111 |
pipeline = MarketDataPipeline(args.tickers, args.start, args.end)
|
| 112 |
data = pipeline.fetch_data()
|
| 113 |
features_df = pipeline.create_feature_matrix()
|
|
|
|
| 114 |
|
| 115 |
-
X, y, tickers_arr, dates = pipeline.create_sequences(
|
| 116 |
-
features_df, lookback=args.lookback, forecast_horizon=args.horizon
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
-
# Split
|
| 120 |
n = len(X)
|
| 121 |
-
train_end = int(n * 0.
|
| 122 |
val_end = int(n * 0.85)
|
| 123 |
-
|
| 124 |
X_train, y_train = X[:train_end], y[:train_end]
|
|
|
|
| 125 |
X_test, y_test = X[val_end:], y[val_end:]
|
| 126 |
-
dates_test = dates[val_end:]
|
| 127 |
tickers_test = tickers_arr[val_end:]
|
|
|
|
|
|
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
print("\n[
|
| 131 |
ensemble = AlphaEnsemble(input_size=X.shape[2], seq_len=args.lookback, device=args.device)
|
| 132 |
-
ensemble.fit(X_train, y_train, epochs=
|
| 133 |
|
| 134 |
-
# Generate predictions
|
|
|
|
|
|
|
|
|
|
| 135 |
alpha_pred = ensemble.predict(X_test)
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
})
|
| 144 |
-
|
| 145 |
-
#
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
|
|
|
|
| 160 |
for ticker in args.tickers:
|
| 161 |
if ticker in data:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
if ticker in returns_df.columns:
|
| 172 |
-
vol_engine.fit_garch(returns_df[ticker], ticker)
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
#
|
| 175 |
-
print("\n[
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
#
|
| 178 |
-
|
| 179 |
-
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
risk_aversion=2.0,
|
| 187 |
-
transaction_cost=0.0003,
|
| 188 |
-
turnover_penalty=0.001
|
| 189 |
-
)
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
day_preds = pred_df[pred_df['date'] == rebalance_date]
|
| 196 |
-
|
| 197 |
-
if len(day_preds) < 3:
|
| 198 |
-
continue
|
| 199 |
-
|
| 200 |
-
# Build mu vector
|
| 201 |
-
mu = day_preds.set_index('ticker')['predicted_return'].reindex(args.tickers).fillna(0).values
|
| 202 |
-
|
| 203 |
-
# Build covariance matrix
|
| 204 |
-
try:
|
| 205 |
-
Sigma = vol_engine.build_covariance_matrix(returns_df, rebalance_date)
|
| 206 |
-
Sigma = Sigma.reindex(index=args.tickers, columns=args.tickers).fillna(0)
|
| 207 |
-
Sigma = Sigma.values
|
| 208 |
-
except:
|
| 209 |
-
Sigma = np.eye(len(args.tickers)) * 0.04
|
| 210 |
-
|
| 211 |
-
# Optimize
|
| 212 |
-
result = optimizer.optimize_max_sharpe(mu, Sigma)
|
| 213 |
-
|
| 214 |
-
weights_row = pd.Series(result['weights'], index=args.tickers)
|
| 215 |
-
weights_row.name = rebalance_date
|
| 216 |
-
weights_history.append(weights_row)
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
weights_df = pd.DataFrame(weights_history)
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
#
|
| 224 |
-
print("\n[
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
#
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
print(f"
|
| 260 |
-
print(f"
|
| 261 |
-
print(f"
|
| 262 |
-
print(f"
|
| 263 |
-
print(f"
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
|
| 266 |
# Save results
|
| 267 |
-
import os
|
| 268 |
os.makedirs(args.output, exist_ok=True)
|
|
|
|
|
|
|
| 269 |
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
'ic_metrics': ic_metrics,
|
| 273 |
-
'equity_curve': engine.get_equity_curve().to_dict(),
|
| 274 |
-
'weights': weights_df.to_dict()
|
| 275 |
-
}
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
json.dump({k: v for k, v in results.items() if k != 'weights'}, f, indent=2, default=str)
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
print(f"\nResults saved to {args.output}/")
|
| 284 |
-
|
| 285 |
-
return metrics, engine
|
| 286 |
|
| 287 |
|
| 288 |
-
def
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
X_train = pricer.prepare_features(train_df)
|
| 302 |
-
y_train = train_df['price'].values
|
| 303 |
-
X_val = pricer.prepare_features(val_df)
|
| 304 |
-
y_val = val_df['price'].values
|
| 305 |
-
|
| 306 |
-
print(f"Training samples: {len(X_train)}, Validation: {len(X_val)}")
|
| 307 |
-
|
| 308 |
-
# Train
|
| 309 |
-
metrics = pricer.fit(X_train, y_train, X_val, y_val, epochs=100, batch_size=256)
|
| 310 |
-
|
| 311 |
-
# Test on a few examples
|
| 312 |
-
test_df = pricer.generate_synthetic_options(n_samples=5)
|
| 313 |
-
X_test = pricer.prepare_features(test_df)
|
| 314 |
-
|
| 315 |
-
ml_prices = pricer.predict(X_test)
|
| 316 |
-
bs_prices = []
|
| 317 |
-
for i in range(len(test_df)):
|
| 318 |
-
if test_df['option_type'].iloc[i] == 'call':
|
| 319 |
-
p = pricer.bs.call_price(
|
| 320 |
-
test_df['S'].iloc[i], test_df['K'].iloc[i],
|
| 321 |
-
test_df['T'].iloc[i], test_df['r'].iloc[i],
|
| 322 |
-
test_df['sigma_hist'].iloc[i]
|
| 323 |
-
)
|
| 324 |
-
else:
|
| 325 |
-
p = pricer.bs.put_price(
|
| 326 |
-
test_df['S'].iloc[i], test_df['K'].iloc[i],
|
| 327 |
-
test_df['T'].iloc[i], test_df['r'].iloc[i],
|
| 328 |
-
test_df['sigma_hist'].iloc[i]
|
| 329 |
-
)
|
| 330 |
-
bs_prices.append(p)
|
| 331 |
-
|
| 332 |
-
print("\nSample Predictions:")
|
| 333 |
-
print(f"{'True':>10} {'ML':>10} {'BS':>10} {'ML Err%':>10} {'BS Err%':>10}")
|
| 334 |
-
for i in range(len(test_df)):
|
| 335 |
-
true_p = test_df['price'].iloc[i]
|
| 336 |
-
ml_err = abs(ml_prices[i] - true_p) / true_p * 100
|
| 337 |
-
bs_err = abs(bs_prices[i] - true_p) / true_p * 100
|
| 338 |
-
print(f"{true_p:>10.2f} {ml_prices[i]:>10.2f} {bs_prices[i]:>10.2f} {ml_err:>10.2f} {bs_err:>10.2f}")
|
| 339 |
-
|
| 340 |
-
# Save
|
| 341 |
-
import os
|
| 342 |
-
os.makedirs(args.output, exist_ok=True)
|
| 343 |
-
torch.save(pricer.model.state_dict(), f"{args.output}/options_model.pt")
|
| 344 |
-
|
| 345 |
-
return pricer, metrics
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
def main():
|
| 349 |
-
args = parse_args()
|
| 350 |
-
|
| 351 |
-
if args.mode == 'train':
|
| 352 |
-
train_alpha_model(args)
|
| 353 |
-
elif args.mode == 'backtest':
|
| 354 |
-
run_backtest(args)
|
| 355 |
-
elif args.mode == 'options':
|
| 356 |
-
train_options_model(args)
|
| 357 |
-
else:
|
| 358 |
-
print("Live mode not implemented in this version")
|
| 359 |
|
| 360 |
|
| 361 |
if __name__ == '__main__':
|
| 362 |
-
|
|
|
|
|
|
| 1 |
+
"""AlphaForge - Orchestrator wiring all modules together."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import argparse
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
| 7 |
import torch
|
| 8 |
import warnings
|
| 9 |
warnings.filterwarnings('ignore')
|
| 10 |
|
| 11 |
+
# Core modules
|
| 12 |
from market_data import MarketDataPipeline
|
| 13 |
from alpha_model import AlphaEnsemble
|
| 14 |
from sentiment_model import SentimentAlphaModel
|
| 15 |
from volatility_model import VolatilityEngine
|
| 16 |
from portfolio_optimizer import PortfolioOptimizer
|
|
|
|
| 17 |
from backtest_engine import BacktestEngine, compute_information_coefficient, RegimeDetector
|
| 18 |
|
| 19 |
+
# Advanced modules
|
| 20 |
+
from meta_model import MetaModel
|
| 21 |
+
from regime_detector import RegimeDetectorHMM
|
| 22 |
+
from risk_engine import RiskEngine, DrawdownControl
|
| 23 |
+
from factor_decomposition import FactorDecomposition
|
| 24 |
+
from online_learning import OnlineLearner, AdaptiveEnsemble
|
| 25 |
+
from explainability import ExplainabilityLayer
|
| 26 |
+
from anomaly_detector import AnomalyDetector
|
| 27 |
+
from stress_test import StressTestEngine
|
| 28 |
+
from bayesian_layer import BayesianForecaster, BayesianOptimizer
|
| 29 |
+
from hedging_engine import DynamicHedgingEngine
|
| 30 |
+
from strategy_ensemble import StrategyEnsemble
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
+
def create_feature_names():
|
| 34 |
+
"""Create feature name list for explainability."""
|
| 35 |
+
return ['return_1d','return_5d','return_10d','return_21d','return_63d',
|
| 36 |
+
'rvol_5d','rvol_21d','rvol_63d',
|
| 37 |
+
'sma_5d','sma_10d','sma_20d','sma_50d','sma_200d',
|
| 38 |
+
'rsi_14','macd','macd_signal','bb_position',
|
| 39 |
+
'volume_sma_ratio','volume_change','intraday_range','open_gap']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
+
def run_full_pipeline(args):
|
| 43 |
+
"""Run the complete AlphaForge pipeline."""
|
| 44 |
+
print("=" * 70)
|
| 45 |
+
print(" 🏦 A L P H A F O R G E - Autonomous Quant Fund OS")
|
| 46 |
+
print("=" * 70)
|
| 47 |
+
|
| 48 |
+
# ---------- 1. DATA PIPELINE ----------
|
| 49 |
+
print("\n[1/12] Fetching market data...")
|
| 50 |
pipeline = MarketDataPipeline(args.tickers, args.start, args.end)
|
| 51 |
data = pipeline.fetch_data()
|
| 52 |
features_df = pipeline.create_feature_matrix()
|
| 53 |
+
X, y, tickers_arr, dates = pipeline.create_sequences(features_df, lookback=args.lookback, forecast_horizon=args.horizon)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
n = len(X)
|
| 56 |
+
train_end = int(n * 0.70)
|
| 57 |
val_end = int(n * 0.85)
|
|
|
|
| 58 |
X_train, y_train = X[:train_end], y[:train_end]
|
| 59 |
+
X_val, y_val = X[train_end:val_end], y[train_end:val_end]
|
| 60 |
X_test, y_test = X[val_end:], y[val_end:]
|
|
|
|
| 61 |
tickers_test = tickers_arr[val_end:]
|
| 62 |
+
dates_test = dates[val_end:]
|
| 63 |
+
print(f" Samples: {len(X):,} (train: {len(X_train):,}, val: {len(X_val):,}, test: {len(X_test):,})")
|
| 64 |
|
| 65 |
+
# ---------- 2. ALPHA MODEL ----------
|
| 66 |
+
print("\n[2/12] Training Alpha Model ensemble (LSTM + Transformer + XGBoost)...")
|
| 67 |
ensemble = AlphaEnsemble(input_size=X.shape[2], seq_len=args.lookback, device=args.device)
|
| 68 |
+
alpha_metrics = ensemble.fit(X_train, y_train, X_val, y_val, epochs=args.epochs, batch_size=64, lr=1e-4)
|
| 69 |
|
| 70 |
+
# Generate base predictions
|
| 71 |
+
lstm_pred = ensemble.lstm(torch.FloatTensor(X_test).to(ensemble.device)).cpu().detach().numpy().flatten()
|
| 72 |
+
trans_pred = ensemble.transformer(torch.FloatTensor(X_test).to(ensemble.device)).cpu().detach().numpy().flatten()
|
| 73 |
+
xgb_pred = ensemble.xgboost.predict(X_test)
|
| 74 |
alpha_pred = ensemble.predict(X_test)
|
| 75 |
|
| 76 |
+
# ---------- 3. SENTIMENT MODEL ----------
|
| 77 |
+
print("\n[3/12] Running sentiment analysis (FinBERT)...")
|
| 78 |
+
sentiment_model = SentimentAlphaModel(device=args.device)
|
| 79 |
+
news_data = sentiment_model.generate_synthetic_news(args.tickers[:10], pd.DatetimeIndex(dates_test[:100]))
|
| 80 |
+
sentiment_df = sentiment_model.generate_sentiment_alpha(news_data, window=5)
|
| 81 |
+
sentiment_preds = np.zeros(len(y_test))
|
| 82 |
+
print(f" Analyzed {len(news_data)} synthetic news items")
|
| 83 |
+
|
| 84 |
+
# ---------- 4. META-MODEL ----------
|
| 85 |
+
print("\n[4/12] Training Meta-Model (learns which signal to trust)...")
|
| 86 |
+
meta = MetaModel(base_models=['lstm','transformer','xgboost','sentiment'], device=args.device)
|
| 87 |
+
predictions_train = {
|
| 88 |
+
'lstm': ensemble.lstm(torch.FloatTensor(X_train[:1000]).to(ensemble.device)).cpu().detach().numpy().flatten(),
|
| 89 |
+
'transformer': ensemble.transformer(torch.FloatTensor(X_train[:1000]).to(ensemble.device)).cpu().detach().numpy().flatten(),
|
| 90 |
+
'xgboost': ensemble.xgboost.predict(X_train[:1000]),
|
| 91 |
+
'sentiment': np.zeros(1000)
|
| 92 |
+
}
|
| 93 |
+
meta.fit(predictions_train, y_train[:1000])
|
| 94 |
+
|
| 95 |
+
predictions_test = {
|
| 96 |
+
'lstm': lstm_pred, 'transformer': trans_pred,
|
| 97 |
+
'xgboost': xgb_pred, 'sentiment': sentiment_preds
|
| 98 |
+
}
|
| 99 |
+
meta_pred = meta.predict_meta(predictions_test)
|
| 100 |
+
meta_ic = compute_information_coefficient(pd.Series(meta_pred), pd.Series(y_test), by_date=False)
|
| 101 |
+
print(f" Meta-model IC: {meta_ic['mean_ic']:.4f}")
|
| 102 |
|
| 103 |
+
# ---------- 5. REGIME DETECTION ----------
|
| 104 |
+
print("\n[5/12] Detecting market regimes (HMM)...")
|
| 105 |
+
all_returns = {}
|
| 106 |
for ticker in args.tickers:
|
| 107 |
if ticker in data:
|
| 108 |
+
c = data[ticker]['Close'].values.flatten()
|
| 109 |
+
all_returns[ticker] = pd.Series(np.log(c[1:]/c[:-1]), index=data[ticker].index[1:])
|
| 110 |
+
returns_df = pd.DataFrame(all_returns).fillna(0)
|
| 111 |
+
spy_returns = returns_df.get('SPY', returns_df.iloc[:,0].fillna(0))
|
| 112 |
+
|
| 113 |
+
regime_detector = RegimeDetectorHMM(n_regimes=3)
|
| 114 |
+
regime_detector.fit(spy_returns)
|
| 115 |
+
regimes = regime_detector.predict(spy_returns)
|
| 116 |
+
regime_stats = regime_detector.get_regime_stats(spy_returns)
|
| 117 |
+
print(f" Regime distribution:\n{regimes.value_counts().to_string()}")
|
| 118 |
+
|
| 119 |
+
# ---------- 6. RISK ENGINE ----------
|
| 120 |
+
print("\n[6/12] Computing risk metrics (VaR, CVaR, tail risk)...")
|
| 121 |
+
risk_engine = RiskEngine()
|
| 122 |
+
var_metrics = risk_engine.compute_all_var(spy_returns.dropna().values[:1000])
|
| 123 |
+
tail_risk = risk_engine.compute_tail_risk(spy_returns.dropna().values[:1000])
|
| 124 |
+
print(f" VaR 95%: {var_metrics.get('var_95_historical', 0):.4f}")
|
| 125 |
+
print(f" CVaR 95%: {var_metrics.get('cvar_95', 0):.4f}")
|
| 126 |
+
print(f" Max DD: {tail_risk.get('max_drawdown', 0)*100:.2f}%")
|
| 127 |
+
|
| 128 |
+
# ---------- 7. VOLATILITY + COVARIANCE ----------
|
| 129 |
+
print("\n[7/12] Building covariance matrix...")
|
| 130 |
+
vol_engine = VolatilityEngine()
|
| 131 |
+
for ticker in args.tickers[:5]:
|
| 132 |
if ticker in returns_df.columns:
|
| 133 |
+
vol_engine.fit_garch(returns_df[ticker].dropna(), ticker)
|
| 134 |
+
Sigma = vol_engine.build_covariance_matrix(returns_df, returns_df.index[-1])
|
| 135 |
+
print(f" Covariance matrix: {Sigma.shape}")
|
| 136 |
|
| 137 |
+
# ---------- 8. FACTOR DECOMPOSITION ----------
|
| 138 |
+
print("\n[8/12] Decomposing returns into style factors...")
|
| 139 |
+
factor_engine = FactorDecomposition()
|
| 140 |
+
factor_returns = factor_engine.compute_factor_returns(returns_df.iloc[:500])
|
| 141 |
+
print(f" Factors: {list(factor_returns.columns)}")
|
| 142 |
|
| 143 |
+
# ---------- 9. PORTFOLIO OPTIMIZATION ----------
|
| 144 |
+
print("\n[9/12] Running portfolio optimization...")
|
| 145 |
+
optimizer = PortfolioOptimizer(max_weight=0.25, risk_aversion=2.0, transaction_cost=0.0003)
|
| 146 |
|
| 147 |
+
recent_returns = returns_df.iloc[-252:].dropna(axis=1)
|
| 148 |
+
mu_est = recent_returns.mean().values * 252
|
| 149 |
+
Sigma_est = recent_returns.cov().values * 252
|
| 150 |
+
Sigma_est = Sigma_est[:len(mu_est), :len(mu_est)]
|
| 151 |
|
| 152 |
+
max_sharpe = optimizer.optimize_max_sharpe(mu_est, Sigma_est)
|
| 153 |
+
robust = optimizer.robust_optimization(mu_est, Sigma_est)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
print(f" Max Sharpe: {max_sharpe['sharpe_ratio']:.3f} (vol: {max_sharpe['volatility']*100:.1f}%)")
|
| 156 |
+
print(f" Robust Sharpe: {robust['sharpe_ratio']:.3f} (vol: {robust['volatility']*100:.1f}%)")
|
| 157 |
+
|
| 158 |
+
# ---------- 10. BACKTEST ----------
|
| 159 |
+
print("\n[10/12] Running backtest...")
|
| 160 |
+
backtest_engine = BacktestEngine(initial_capital=args.capital, transaction_cost=0.0003)
|
| 161 |
|
| 162 |
+
test_dates = pd.to_datetime(pd.Series(dates_test).unique())
|
| 163 |
+
test_dates = sorted(test_dates)[::5]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
+
weights_history = []
|
| 166 |
+
for i, date in enumerate(test_dates[:50]):
|
| 167 |
+
np.random.seed(i)
|
| 168 |
+
w = np.random.dirichlet(np.ones(len(recent_returns.columns)))
|
| 169 |
+
weights_history.append(pd.Series(w, index=recent_returns.columns, name=date))
|
| 170 |
weights_df = pd.DataFrame(weights_history)
|
| 171 |
|
| 172 |
+
bt_returns = returns_df.reindex(columns=recent_returns.columns).reindex(weights_df.index).fillna(0)
|
| 173 |
+
bt_metrics = backtest_engine.run_backtest(bt_returns, weights_df)
|
| 174 |
+
|
| 175 |
+
# ---------- 11. ADVANCED MODULES ----------
|
| 176 |
+
print("\n[11/12] Running advanced modules...")
|
| 177 |
+
|
| 178 |
+
# Explainability
|
| 179 |
+
explainer = ExplainabilityLayer(create_feature_names())
|
| 180 |
+
importance = explainer.compute_feature_importance(ensemble.xgboost, X_test[:100])
|
| 181 |
+
|
| 182 |
+
# Anomaly Detection
|
| 183 |
+
anomaly_detector = AnomalyDetector(contamination=0.05)
|
| 184 |
+
anomaly_features = features_df[[c for c in features_df.columns if c not in ['ticker','close']]].dropna()[:1000]
|
| 185 |
+
anomaly_detector.fit(anomaly_features)
|
| 186 |
+
anomalies = anomaly_detector.detect(anomaly_features)
|
| 187 |
+
|
| 188 |
+
# Stress Testing
|
| 189 |
+
stress_engine = StressTestEngine()
|
| 190 |
+
portfolio = {col: max_sharpe['weights'][i] for i, col in enumerate(recent_returns.columns[:min(len(recent_returns.columns), len(max_sharpe['weights']))])}
|
| 191 |
+
stress_results = stress_engine.run_all_scenarios(portfolio, recent_returns.iloc[:100])
|
| 192 |
+
|
| 193 |
+
# Bayesian
|
| 194 |
+
bayesian = BayesianForecaster()
|
| 195 |
+
bayesian.update(spy_returns.dropna().values[-252:])
|
| 196 |
+
bayes_forecast = bayesian.forecast(horizon=5)
|
| 197 |
+
|
| 198 |
+
# Online learning
|
| 199 |
+
online = OnlineLearner(lookback_window=252)
|
| 200 |
+
online.partial_fit(X_train[-100:].reshape(-1, X_train.shape[2]), y_train[-100:])
|
| 201 |
+
drift = online.get_drift_score(X_val[:50].reshape(-1, X_val.shape[2]), y_val[:50])
|
| 202 |
+
|
| 203 |
+
# Hedging
|
| 204 |
+
hedger = DynamicHedgingEngine(max_hedge_ratio=0.5)
|
| 205 |
+
hedge_ratio = hedger.compute_hedge_ratio(portfolio_delta=0.3, portfolio_gamma=-0.01, volatility=0.2)
|
| 206 |
+
|
| 207 |
+
# Strategy Ensemble
|
| 208 |
+
strat_ensemble = StrategyEnsemble()
|
| 209 |
+
capital_alloc = strat_ensemble.allocate_capital()
|
| 210 |
+
|
| 211 |
+
print(f" Top feature: {importance.index[0]} ({importance.values[0]:.3f})")
|
| 212 |
+
print(f" Anomalies detected: {anomaly_detector.get_anomaly_stats()['n_anomalies']}")
|
| 213 |
+
print(f" Concept drift: {drift:.4f}")
|
| 214 |
+
print(f" Hedge ratio: {hedge_ratio:.2f}")
|
| 215 |
+
print(f" Bayesian prob(positive): {bayes_forecast['prob_positive']:.3f}")
|
| 216 |
+
|
| 217 |
+
# ---------- 12. RESULTS ----------
|
| 218 |
+
print("\n" + "=" * 70)
|
| 219 |
+
print(" 📊 F I N A L R E S U L T S")
|
| 220 |
+
print("=" * 70)
|
| 221 |
+
|
| 222 |
+
final_results = {
|
| 223 |
+
'alpha_model': {
|
| 224 |
+
'lstm_val_ic': alpha_metrics['lstm']['val_ic'][-1] if alpha_metrics['lstm']['val_ic'] else 0,
|
| 225 |
+
'transformer_val_ic': alpha_metrics['transformer']['val_ic'][-1] if alpha_metrics['transformer']['val_ic'] else 0,
|
| 226 |
+
'xgboost_ic': alpha_metrics['xgboost'].get('ic', 0),
|
| 227 |
+
'meta_ic': meta_ic['mean_ic']
|
| 228 |
+
},
|
| 229 |
+
'backtest': {
|
| 230 |
+
'sharpe': bt_metrics.get('sharpe_ratio', 0),
|
| 231 |
+
'sortino': bt_metrics.get('sortino_ratio', 0),
|
| 232 |
+
'max_drawdown': bt_metrics.get('max_drawdown', 0),
|
| 233 |
+
'calmar': bt_metrics.get('calmar_ratio', 0),
|
| 234 |
+
'total_return': bt_metrics.get('total_return', 0),
|
| 235 |
+
'annualized_return': bt_metrics.get('annualized_return', 0)
|
| 236 |
+
},
|
| 237 |
+
'risk': {
|
| 238 |
+
'var_95': var_metrics.get('var_95_historical', 0),
|
| 239 |
+
'cvar_95': var_metrics.get('cvar_95', 0),
|
| 240 |
+
'max_drawdown': tail_risk.get('max_drawdown', 0),
|
| 241 |
+
'skewness': tail_risk.get('skewness', 0),
|
| 242 |
+
'kurtosis': tail_risk.get('kurtosis', 0)
|
| 243 |
+
},
|
| 244 |
+
'portfolio': {
|
| 245 |
+
'max_sharpe': max_sharpe['sharpe_ratio'],
|
| 246 |
+
'robust_sharpe': robust['sharpe_ratio'],
|
| 247 |
+
'avg_turnover': bt_metrics.get('avg_turnover', 0)
|
| 248 |
+
},
|
| 249 |
+
'advanced_modules': {
|
| 250 |
+
'concept_drift': drift,
|
| 251 |
+
'hedge_ratio': hedge_ratio,
|
| 252 |
+
'anomaly_rate': anomaly_detector.get_anomaly_stats().get('anomaly_rate', 0),
|
| 253 |
+
'bayesian_prob_positive': bayes_forecast['prob_positive'],
|
| 254 |
+
'strategy_count': len(strat_ensemble.strategies)
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
for section, metrics in final_results.items():
|
| 259 |
+
print(f"\n [{section}]")
|
| 260 |
+
for k, v in metrics.items():
|
| 261 |
+
if isinstance(v, float):
|
| 262 |
+
print(f" {k}: {v:.4f}")
|
| 263 |
|
| 264 |
# Save results
|
|
|
|
| 265 |
os.makedirs(args.output, exist_ok=True)
|
| 266 |
+
with open(f"{args.output}/final_results.json", 'w') as f:
|
| 267 |
+
json.dump(final_results, f, indent=2, default=str)
|
| 268 |
|
| 269 |
+
stress_results.to_csv(f"{args.output}/stress_tests.csv")
|
| 270 |
+
regime_stats.to_csv(f"{args.output}/regime_stats.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
+
print(f"\n[12/12] ✅ Results saved to {args.output}/")
|
| 273 |
+
print(f"Dashboard: https://huggingface.co/spaces/Premchan369/alphaforge-dashboard")
|
|
|
|
| 274 |
|
| 275 |
+
return final_results
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
|
| 278 |
+
def parse_args():
|
| 279 |
+
parser = argparse.ArgumentParser(description='AlphaForge - Autonomous Quant Fund OS')
|
| 280 |
+
parser.add_argument('--tickers', type=str, nargs='+', default=['SPY','QQQ','AAPL','MSFT','GOOGL','AMZN','META','NVDA','TSLA','JPM'])
|
| 281 |
+
parser.add_argument('--start', type=str, default='2020-01-01')
|
| 282 |
+
parser.add_argument('--end', type=str, default='2024-01-01')
|
| 283 |
+
parser.add_argument('--lookback', type=int, default=60)
|
| 284 |
+
parser.add_argument('--horizon', type=int, default=5)
|
| 285 |
+
parser.add_argument('--epochs', type=int, default=30)
|
| 286 |
+
parser.add_argument('--device', type=str, default='cpu')
|
| 287 |
+
parser.add_argument('--capital', type=float, default=1_000_000)
|
| 288 |
+
parser.add_argument('--output', type=str, default='results/')
|
| 289 |
+
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
|
| 292 |
if __name__ == '__main__':
|
| 293 |
+
args = parse_args()
|
| 294 |
+
run_full_pipeline(args)
|