-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnifty_trading_python.py
More file actions
737 lines (596 loc) · 26.6 KB
/
Copy pathnifty_trading_python.py
File metadata and controls
737 lines (596 loc) · 26.6 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#!/usr/bin/env python3
"""
INSTITUTIONAL-GRADE NIFTY TRADING SYSTEM
========================================
Advanced AI Trading Model with Institutional-Level Features
- Ensemble of multiple deep learning architectures
- Alternative data integration (VIX, sectoral indices, global markets)
- Advanced risk management and position sizing
- Real-time market microstructure analysis
- Regime detection and adaptive modeling
- Monte Carlo simulations for risk assessment
Author: AI Trading Systems
Version: Professional 2.0
"""
import os
import sys
import warnings
import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime, timedelta
import json
from typing import Dict, List, Tuple, Optional, Union
from concurrent.futures import ThreadPoolExecutor, as_completed
# Suppress warnings
warnings.filterwarnings('ignore')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Machine Learning libraries
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler
from sklearn.model_selection import train_test_split, TimeSeriesSplit
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
import scipy.stats as stats
# Deep Learning (using numpy for simulation)
class MockTensorFlow:
"""Mock TensorFlow for demonstration purposes"""
@staticmethod
def keras_model_predict(X, weights=None):
# Simulate neural network prediction
noise = np.random.normal(0, 0.01, X.shape[0])
trend = np.mean(X[:, -5:], axis=1) * (1 + noise)
return trend.reshape(-1, 1)
tf = MockTensorFlow()
class AdvancedMarketDataFetcher:
"""
Professional-grade market data fetcher with simulated data
"""
def __init__(self):
self.data_sources = {
'nifty': ['^NSEI', 'NIFTY_50.NS'],
'vix': ['INDIAVIX.NS'],
'sectoral': ['^NSEBANK', '^NSEIT', '^NSEFMCG', '^NSEPHARMA'],
'global': ['^GSPC', '^IXIC', '^DJI'],
'commodities': ['GC=F', 'CL=F'],
'currency': ['USDINR=X']
}
def generate_realistic_market_data(self, symbol: str, days: int = 1000) -> pd.DataFrame:
"""Generate realistic market data with proper statistical properties"""
print(f"📊 Generating realistic data for {symbol}...")
# Base parameters for different asset classes
if 'NSEI' in symbol or 'NIFTY' in symbol:
base_price = 18500
volatility = 0.015
drift = 0.0003
elif 'VIX' in symbol:
base_price = 18
volatility = 0.3
drift = 0
elif 'BANK' in symbol:
base_price = 45000
volatility = 0.02
drift = 0.0002
else:
base_price = 1000
volatility = 0.018
drift = 0.0001
dates = pd.date_range(end=datetime.now(), periods=days, freq='D')
prices = []
volumes = []
current_price = base_price
current_vol = 100000
for i in range(days):
# Geometric Brownian Motion with regime changes
dt = 1/252 # Daily time step
# Add regime switching
if i > 0 and i % 60 == 0: # Change regime every ~60 days
volatility *= np.random.uniform(0.7, 1.4)
volatility = max(0.008, min(0.05, volatility))
# Price evolution
random_shock = np.random.normal(0, 1)
price_change = drift * dt + volatility * np.sqrt(dt) * random_shock
current_price *= (1 + price_change)
# Volume with clustering
volume_shock = np.random.lognormal(0, 0.3)
current_vol = current_vol * 0.95 + current_vol * 0.05 * volume_shock
# Generate OHLC from close
daily_vol = abs(random_shock) * volatility * current_price
high = current_price + np.random.uniform(0, daily_vol)
low = current_price - np.random.uniform(0, daily_vol)
open_price = low + np.random.uniform(0, high - low)
prices.append({
'Date': dates[i],
'Open': round(open_price, 2),
'High': round(high, 2),
'Low': round(low, 2),
'Close': round(current_price, 2),
'Volume': int(current_vol)
})
df = pd.DataFrame(prices)
df.set_index('Date', inplace=True)
return df
def fetch_comprehensive_data(self, period="2y", interval="1d"):
"""
Fetch comprehensive market data from multiple sources
"""
print("🌐 Fetching comprehensive market data...")
all_data = {}
days = 504 if period == "2y" else 252
# Generate data for all asset classes
for category, symbols in self.data_sources.items():
all_data[category] = {}
for symbol in symbols:
try:
data = self.generate_realistic_market_data(symbol, days)
all_data[category][symbol] = data
print(f"✅ Generated {symbol} ({category}): {len(data)} points")
time.sleep(0.1) # Simulate API delay
except Exception as e:
print(f"❌ Failed to generate {symbol}: {e}")
return all_data
def create_market_regime_features(self, data_dict):
"""
Create advanced market regime and cross-asset features
"""
print("🔬 Creating market regime features...")
# Get primary Nifty data
nifty_data = None
for symbol, data in data_dict.get('nifty', {}).items():
if len(data) > 0:
nifty_data = data.copy()
break
if nifty_data is None:
print("❌ No Nifty data found")
return None
# Add VIX data
vix_data = data_dict.get('vix', {})
if vix_data:
vix_symbol = list(vix_data.keys())[0]
vix_closes = vix_data[vix_symbol]['Close']
nifty_data['VIX'] = vix_closes.reindex(nifty_data.index, method='ffill')
else:
# Generate synthetic VIX based on NIFTY volatility
returns = nifty_data['Close'].pct_change()
realized_vol = returns.rolling(20).std() * np.sqrt(252) * 100
nifty_data['VIX'] = realized_vol * np.random.uniform(0.8, 1.2, len(nifty_data))
# Add sectoral strength indicators
sectoral_data = data_dict.get('sectoral', {})
for i, (symbol, data) in enumerate(sectoral_data.items()):
if len(data) > 0:
sector_returns = data['Close'].pct_change().reindex(nifty_data.index, method='ffill')
nifty_data[f'Sector_{i}_Returns'] = sector_returns
# Relative strength
nifty_returns = nifty_data['Close'].pct_change()
relative_strength = sector_returns - nifty_returns
nifty_data[f'Sector_{i}_RS'] = relative_strength.rolling(20).mean()
# Add global market indicators
global_data = data_dict.get('global', {})
for i, (symbol, data) in enumerate(global_data.items()):
if len(data) > 0:
global_close = data['Close'].reindex(nifty_data.index, method='ffill')
global_returns = global_close.pct_change()
nifty_data[f'Global_{i}_Returns'] = global_returns
# Correlation regime
correlation = nifty_data['Close'].pct_change().rolling(60).corr(global_returns)
nifty_data[f'Global_{i}_Corr'] = correlation
# Add commodity effects
commodities_data = data_dict.get('commodities', {})
for i, (symbol, data) in enumerate(commodities_data.items()):
if len(data) > 0:
commodity_returns = data['Close'].pct_change().reindex(nifty_data.index, method='ffill')
nifty_data[f'Commodity_{i}_Returns'] = commodity_returns
# Add currency effects
currency_data = data_dict.get('currency', {})
for i, (symbol, data) in enumerate(currency_data.items()):
if len(data) > 0:
currency_returns = data['Close'].pct_change().reindex(nifty_data.index, method='ffill')
nifty_data[f'Currency_{i}_Returns'] = currency_returns
return nifty_data
class AdvancedFeatureEngine:
"""
Institutional-grade feature engineering
"""
@staticmethod
def calculate_microstructure_features(data):
"""
Calculate market microstructure features
"""
print("🔬 Calculating microstructure features...")
df = data.copy()
# Price efficiency measures
df['Amihud_Illiquidity'] = np.abs(df['Close'].pct_change()) / (df['Volume'] + 1e-10)
df['Price_Impact'] = df['Amihud_Illiquidity'].rolling(20).mean()
# Intraday patterns
df['OHLC_Average'] = (df['Open'] + df['High'] + df['Low'] + df['Close']) / 4
df['Typical_Price'] = (df['High'] + df['Low'] + df['Close']) / 3
df['Weighted_Price'] = (df['High'] + df['Low'] + 2*df['Close']) / 4
# Gap analysis
df['Gap'] = (df['Open'] - df['Close'].shift(1)) / df['Close'].shift(1)
df['Gap_Fill'] = np.where(
df['Gap'] > 0,
np.minimum(df['Low'], df['Close'].shift(1)) / df['Open'],
np.maximum(df['High'], df['Close'].shift(1)) / df['Open']
)
# Volume-price relationship
df['Volume_Weighted_Price'] = (df['Volume'] * df['Close']).rolling(20).sum() / df['Volume'].rolling(20).sum()
df['Volume_Rate_of_Change'] = df['Volume'].pct_change()
return df
@staticmethod
def calculate_regime_features(data):
"""
Calculate market regime detection features
"""
print("📊 Calculating regime features...")
df = data.copy()
returns = df['Close'].pct_change()
# Volatility regimes
df['Realized_Vol'] = returns.rolling(20).std() * np.sqrt(252)
df['Vol_Regime'] = pd.cut(df['Realized_Vol'], bins=3, labels=[0, 1, 2])
# Trend regimes
df['SMA_20'] = df['Close'].rolling(20).mean()
df['SMA_50'] = df['Close'].rolling(50).mean()
df['Trend_Strength'] = (df['Close'] - df['SMA_50']) / df['SMA_50']
df['Trend_Direction'] = np.where(df['Close'] > df['SMA_50'], 1, -1)
# Market stress indicators
if 'VIX' in df.columns:
df['Stress_Indicator'] = (
df['Realized_Vol'].rolling(20).rank(pct=True) * 0.4 +
df['VIX'].rolling(20).rank(pct=True) * 0.6
)
else:
df['Stress_Indicator'] = df['Realized_Vol'].rolling(20).rank(pct=True)
# Momentum regimes
momentum_periods = [5, 10, 20, 50]
momentum_scores = []
for period in momentum_periods:
momentum = (df['Close'] / df['Close'].shift(period) - 1) * 100
momentum_scores.append(momentum)
df['Momentum_Composite'] = pd.concat(momentum_scores, axis=1).mean(axis=1)
df['Momentum_Regime'] = pd.cut(df['Momentum_Composite'], bins=3, labels=[-1, 0, 1])
return df
@staticmethod
def calculate_technical_indicators(data):
"""
Calculate comprehensive technical indicators
"""
print("📈 Calculating technical indicators...")
df = data.copy()
# Moving averages
for period in [5, 10, 20, 50, 200]:
df[f'SMA_{period}'] = df['Close'].rolling(period).mean()
df[f'EMA_{period}'] = df['Close'].ewm(span=period).mean()
# RSI
def calculate_rsi(prices, period=14):
delta = prices.diff()
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
return rsi
df['RSI'] = calculate_rsi(df['Close'])
# MACD
exp1 = df['Close'].ewm(span=12).mean()
exp2 = df['Close'].ewm(span=26).mean()
df['MACD'] = exp1 - exp2
df['MACD_Signal'] = df['MACD'].ewm(span=9).mean()
df['MACD_Histogram'] = df['MACD'] - df['MACD_Signal']
# Bollinger Bands
df['BB_Middle'] = df['Close'].rolling(20).mean()
bb_std = df['Close'].rolling(20).std()
df['BB_Upper'] = df['BB_Middle'] + (bb_std * 2)
df['BB_Lower'] = df['BB_Middle'] - (bb_std * 2)
df['BB_Width'] = (df['BB_Upper'] - df['BB_Lower']) / df['BB_Middle']
df['BB_Position'] = (df['Close'] - df['BB_Lower']) / (df['BB_Upper'] - df['BB_Lower'])
# Stochastic
low_14 = df['Low'].rolling(14).min()
high_14 = df['High'].rolling(14).max()
df['Stoch_K'] = 100 * ((df['Close'] - low_14) / (high_14 - low_14))
df['Stoch_D'] = df['Stoch_K'].rolling(3).mean()
return df
@staticmethod
def calculate_alternative_features(data):
"""
Calculate alternative and proprietary features
"""
print("🧠 Calculating alternative features...")
df = data.copy()
returns = df['Close'].pct_change()
# Statistical moments
for window in [20, 60]:
df[f'Skewness_{window}'] = returns.rolling(window).skew()
df[f'Kurtosis_{window}'] = returns.rolling(window).kurt()
df[f'Mean_Return_{window}'] = returns.rolling(window).mean()
df[f'Vol_{window}'] = returns.rolling(window).std()
# Autocorrelation
for lag in [1, 5, 20]:
df[f'Autocorr_{lag}'] = returns.rolling(60).apply(
lambda x: x.autocorr(lag=lag) if len(x.dropna()) > lag + 10 else np.nan, raw=False
)
# Fractal dimension (simplified Higuchi method)
def higuchi_fd(X, k_max=10):
if len(X) < k_max * 2:
return 1.5
L = []
for k in range(1, k_max + 1):
Lk = []
for m in range(k):
Lm = 0
max_i = int((len(X) - m) / k)
if max_i > 1:
for i in range(1, max_i):
Lm += abs(X[m + i * k] - X[m + (i - 1) * k])
Lm = Lm * (len(X) - 1) / (k * max_i * k)
Lk.append(Lm)
if Lk:
L.append(np.mean(Lk))
if len(L) > 1:
k_values = range(1, len(L) + 1)
coeffs = np.polyfit(np.log(k_values), np.log(L), 1)
return -coeffs[0]
return 1.5
# Apply Higuchi fractal dimension
df['Fractal_Dimension'] = df['Close'].rolling(50).apply(
lambda x: higuchi_fd(x.values) if len(x.dropna()) >= 20 else np.nan, raw=False
)
# Hurst exponent (simplified R/S method)
def hurst_exponent(X):
if len(X) < 20:
return 0.5
X = np.array(X)
N = len(X)
# Calculate log returns
Y = np.diff(np.log(X))
# R/S calculation for different lags
lags = np.unique(np.logspace(0.7, np.log10(N//4), 10).astype(int))
RS = []
for lag in lags:
if lag >= N:
continue
Y_lag = Y[:N//lag * lag].reshape(-1, lag)
mean_Y = np.mean(Y_lag, axis=1, keepdims=True)
Y_centered = Y_lag - mean_Y
Y_cumsum = np.cumsum(Y_centered, axis=1)
R = np.max(Y_cumsum, axis=1) - np.min(Y_cumsum, axis=1)
S = np.std(Y_lag, axis=1)
RS_values = R[S > 0] / S[S > 0]
if len(RS_values) > 0:
RS.append(np.mean(RS_values))
if len(RS) > 1:
coeffs = np.polyfit(np.log(lags[:len(RS)]), np.log(RS), 1)
return coeffs[0]
return 0.5
# Apply Hurst exponent
df['Hurst_Exponent'] = df['Close'].rolling(100).apply(
lambda x: hurst_exponent(x.values) if len(x.dropna()) >= 50 else 0.5, raw=False
)
return df
class InstitutionalEnsembleModel:
"""
Institutional-grade ensemble model combining multiple architectures
"""
def __init__(self, lookback_period=60, prediction_horizons=[1, 3, 5], confidence_level=0.95):
self.lookback_period = lookback_period
self.prediction_horizons = prediction_horizons
self.confidence_level = confidence_level
# Scalers
self.feature_scaler = RobustScaler()
self.target_scaler = MinMaxScaler()
# Model components
self.models = {}
self.model_weights = {}
self.feature_importance = {}
print("🏛️ Institutional Ensemble Model initialized")
def prepare_features(self, data):
"""
Prepare features for model training
"""
print("🔧 Preparing features for modeling...")
# Select numerical features only
numeric_cols = data.select_dtypes(include=[np.number]).columns
# Remove target-like columns
exclude_cols = ['Open', 'High', 'Low', 'Close', 'Volume']
feature_cols = [col for col in numeric_cols if col not in exclude_cols]
# Handle missing values
features = data[feature_cols].fillna(method='ffill').fillna(method='bfill')
return features
def create_sequences(self, features, targets, lookback):
"""
Create sequences for time series modeling
"""
X, y = [], []
for i in range(lookback, len(features)):
X.append(features.iloc[i-lookback:i].values)
y.append(targets.iloc[i])
return np.array(X), np.array(y)
def build_transformer_model(self, X_train, y_train):
"""
Simulate Transformer model training and prediction
"""
print("🤖 Training Transformer model...")
# Simulate transformer training
model_params = {
'num_heads': 8,
'key_dim': 64,
'dropout': 0.1,
'layers': 2
}
# Mock training process
time.sleep(1)
def predict(X):
# Simulate attention mechanism effects
attention_weights = np.random.rand(X.shape[0], X.shape[1])
attention_weights = attention_weights / attention_weights.sum(axis=1, keepdims=True)
# Weighted average of sequences
predictions = []
for i, (sequence, weights) in enumerate(zip(X, attention_weights)):
weighted_features = np.average(sequence, axis=0, weights=weights)
# Simple prediction based on weighted features
pred = y_train.mean() + np.dot(weighted_features[:10], np.random.randn(10)) * 0.01
predictions.append(pred)
return np.array(predictions)
return predict
def build_cnn_lstm_model(self, X_train, y_train):
"""
Simulate CNN-LSTM model training and prediction
"""
print("🧠 Training CNN-LSTM model...")
# Simulate CNN-LSTM training
time.sleep(0.8)
def predict(X):
predictions = []
for sequence in X:
# Simulate CNN feature extraction
cnn_features = np.convolve(sequence.mean(axis=1), [0.25, 0.5, 0.25], mode='same')
# Simulate LSTM temporal modeling
lstm_output = np.cumsum(cnn_features) / len(cnn_features)
# Final prediction
pred = y_train.mean() + lstm_output[-1] * 0.02
predictions.append(pred)
return np.array(predictions)
return predict
def build_wavenet_model(self, X_train, y_train):
"""
Simulate WaveNet model training and prediction
"""
print("🌊 Training WaveNet model...")
# Simulate WaveNet training
time.sleep(0.6)
def predict(X):
predictions = []
for sequence in X:
# Simulate dilated convolutions
dilated_features = []
for dilation in [1, 2, 4, 8, 16]:
if sequence.shape[0] >= dilation:
dilated = sequence[::dilation].mean(axis=1)
dilated_features.append(dilated.mean())
# Combine features
wavenet_output = np.mean(dilated_features) if dilated_features else sequence.mean()
pred = y_train.mean() + wavenet_output * 0.015
predictions.append(pred)
return np.array(predictions)
return predict
def train_ensemble_models(self, features, targets):
"""
Train ensemble of different model architectures
"""
print("🎯 Training ensemble models...")
# Prepare sequences
X, y = self.create_sequences(features, targets, self.lookback_period)
if len(X) == 0:
print("❌ Not enough data for sequence creation")
return
# Split data
split_idx = int(len(X) * 0.8)
X_train, X_test = X[:split_idx], X[split_idx:]
y_train, y_test = y[:split_idx], y[split_idx:]
print(f"📊 Training data shape: {X_train.shape}")
print(f"📊 Test data shape: {X_test.shape}")
# Train individual models
models = {
'transformer': self.build_transformer_model(X_train, y_train),
'cnn_lstm': self.build_cnn_lstm_model(X_train, y_train),
'wavenet': self.build_wavenet_model(X_train, y_train)
}
# Evaluate models and calculate weights
model_scores = {}
for name, model in models.items():
pred = model(X_test)
mse = mean_squared_error(y_test, pred)
mae = mean_absolute_error(y_test, pred)
model_scores[name] = {
'mse': mse,
'mae': mae,
'score': 1 / (1 + mse) # Higher score is better
}
print(f"✅ {name}: MAE={mae:.4f}, MSE={mse:.4f}")
# Calculate ensemble weights based on performance
total_score = sum([score['score'] for score in model_scores.values()])
for name in models:
self.model_weights[name] = model_scores[name]['score'] / total_score
print(f"🏆 {name} weight: {self.model_weights[name]:.3f}")
self.models = models
self.X_test = X_test
self.y_test = y_test
return model_scores
def predict_ensemble(self, X):
"""
Generate ensemble predictions with confidence intervals
"""
if not self.models:
print("❌ Models not trained yet")
return None
predictions = {}
for name, model in self.models.items():
pred = model(X)
predictions[name] = pred
# Weighted ensemble prediction
ensemble_pred = np.zeros(len(X))
for name, pred in predictions.items():
ensemble_pred += pred * self.model_weights[name]
# Calculate confidence based on model agreement
pred_array = np.array(list(predictions.values())).T
confidence = []
for i in range(len(X)):
model_preds = pred_array[i]
std_dev = np.std(model_preds)
mean_pred = np.mean(model_preds)
# Confidence inversely related to disagreement
conf = max(0.5, 1 - (std_dev / abs(mean_pred) if mean_pred != 0 else 1))
confidence.append(conf)
return {
'ensemble': ensemble_pred,
'individual': predictions,
'confidence': np.array(confidence)
}
class RiskManager:
"""
Institutional-grade risk management system
"""
def __init__(self, max_position_size=0.02, var_confidence=0.05):
self.max_position_size = max_position_size
self.var_confidence = var_confidence
def calculate_position_size(self, predictions, portfolio_value, current_vol):
"""
Calculate optimal position size using Kelly Criterion and risk budgeting
"""
# Kelly Criterion
win_rate = np.mean(predictions['confidence'])
avg_return = np.mean(predictions['ensemble']) if len(predictions['ensemble']) > 0 else 0
if avg_return > 0:
kelly_fraction = win_rate - (1 - win_rate) / avg_return
kelly_fraction = max(0, min(kelly_fraction, self.max_position_size))
else:
kelly_fraction = 0
# Risk budgeting based on volatility
vol_adjustment = min(1, 0.15 / max(current_vol, 0.01)) # Target 15% vol
position_size = kelly_fraction * vol_adjustment
return {
'kelly_fraction': kelly_fraction,
'vol_adjustment': vol_adjustment,
'final_position_size': position_size,
'dollar_amount': position_size * portfolio_value
}
def calculate_var(self, returns, confidence_level=0.05):
"""
Calculate Value at Risk
"""
if len(returns) == 0:
return 0
return np.percentile(returns, confidence_level * 100)
def monte_carlo_simulation(self, current_price, vol, days=30, simulations=10000):
"""
Run Monte Carlo simulation for risk assessment
"""
dt = 1/252
price_paths = []
for _ in range(simulations):
prices = [current_price]
for _ in range(days):
random_shock = np.random.normal(0, 1)
price_change = vol * np.sqrt(dt) * random_shock
new_price = prices[-1] * (1 + price_change)
prices.append(new_price)
price_paths