-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_institutional.py
More file actions
258 lines (191 loc) · 7.58 KB
/
test_institutional.py
File metadata and controls
258 lines (191 loc) · 7.58 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
"""
Test Script for Institutional Trading System
Run this to verify all components are working
"""
import sys
import traceback
from datetime import datetime
def test_imports():
"""Test that all modules can be imported"""
print("Testing module imports...")
try:
from strategy.intraday_engine import IntradayMomentumEngine
print("✓ Strategy module imported")
from regime.ofras import OFRASRegimeDetector
print("✓ Regime module imported")
from risk.risk_manager import RiskManager
print("✓ Risk module imported")
from execution.execution_engine import ExecutionEngine
print("✓ Execution module imported")
from backtest.backtest_engine import BacktestEngine
print("✓ Backtest module imported")
from metrics.analytics import MetricsCalculator
print("✓ Metrics module imported")
from trading_orchestrator import TradingOrchestrator
print("✓ Trading orchestrator imported")
return True
except Exception as e:
print(f"✗ Import failed: {e}")
traceback.print_exc()
return False
def test_strategy_engine():
"""Test strategy engine with sample data"""
print("\nTesting strategy engine...")
try:
import pandas as pd
import numpy as np
from strategy.intraday_engine import IntradayMomentumEngine
# Create sample data
dates = pd.date_range(start='2024-01-01', periods=100, freq='5min')
prices = 50000 + np.cumsum(np.random.randn(100) * 100)
df = pd.DataFrame({
'timestamp': dates,
'open': prices + np.random.randn(100) * 10,
'high': prices + abs(np.random.randn(100) * 20),
'low': prices - abs(np.random.randn(100) * 20),
'close': prices,
'volume': np.random.randint(1000, 10000, 100)
})
engine = IntradayMomentumEngine()
signal = engine.generate_signal(df)
print(f"✓ Strategy engine working")
print(f" Signal: {signal.signal}, Strength: {signal.strength:.1f}")
return True
except Exception as e:
print(f"✗ Strategy engine test failed: {e}")
traceback.print_exc()
return False
def test_regime_detector():
"""Test regime detection"""
print("\nTesting regime detector...")
try:
import pandas as pd
import numpy as np
from regime.ofras import OFRASRegimeDetector
# Create sample data
dates = pd.date_range(start='2024-01-01', periods=100, freq='5min')
prices = 50000 + np.cumsum(np.random.randn(100) * 100)
df = pd.DataFrame({
'timestamp': dates,
'open': prices + np.random.randn(100) * 10,
'high': prices + abs(np.random.randn(100) * 20),
'low': prices - abs(np.random.randn(100) * 20),
'close': prices,
'volume': np.random.randint(1000, 10000, 100)
})
detector = OFRASRegimeDetector()
regime = detector.detect_regime(df, funding_rate=0.0001, oi_change=0.05)
print(f"✓ Regime detector working")
print(f" Regime: {regime.regime.value}, Confidence: {regime.confidence:.1f}")
return True
except Exception as e:
print(f"✗ Regime detector test failed: {e}")
traceback.print_exc()
return False
def test_risk_manager():
"""Test risk manager"""
print("\nTesting risk manager...")
try:
from risk.risk_manager import RiskManager
rm = RiskManager(initial_equity=10000.0)
# Test position sizing
size, margin, can_trade = rm.calculate_position_size(
entry_price=50000.0,
stop_loss=49500.0,
atr=500.0,
symbol="cmt_btcusdt"
)
print(f"✓ Risk manager working")
print(f" Position size: {size:.4f}, Margin: ${margin:.2f}, Can trade: {can_trade}")
# Test portfolio risk
portfolio = rm.get_portfolio_risk()
print(f" Portfolio equity: ${portfolio.total_equity:.2f}")
print(f" Can trade: {portfolio.can_trade}")
return True
except Exception as e:
print(f"✗ Risk manager test failed: {e}")
traceback.print_exc()
return False
def test_backtest_engine():
"""Test backtest engine"""
print("\nTesting backtest engine...")
try:
from backtest.backtest_engine import BacktestEngine, BacktestConfig
config = BacktestConfig(initial_capital=10000.0)
engine = BacktestEngine(config)
# Test position operations
opened = engine.open_backtest_position(
symbol="cmt_btcusdt",
direction="LONG",
entry_price=50000.0,
stop_loss=49500.0,
take_profit=51000.0,
timestamp=datetime.now()
)
if opened:
print(f"✓ Backtest engine working")
print(f" Test position opened successfully")
# Close position
trade = engine.close_backtest_position(
exit_price=50500.0,
timestamp=datetime.now(),
exit_reason="Test"
)
if trade:
print(f" Test position closed: PnL ${trade.pnl:.2f}")
return True
except Exception as e:
print(f"✗ Backtest engine test failed: {e}")
traceback.print_exc()
return False
def test_metrics():
"""Test metrics calculator"""
print("\nTesting metrics calculator...")
try:
import numpy as np
from metrics.analytics import MetricsCalculator
# Sample returns
returns = np.random.randn(100) * 0.01
sharpe = MetricsCalculator.calculate_sharpe_ratio(returns)
sortino = MetricsCalculator.calculate_sortino_ratio(returns)
print(f"✓ Metrics calculator working")
print(f" Sharpe: {sharpe:.2f}, Sortino: {sortino:.2f}")
return True
except Exception as e:
print(f"✗ Metrics calculator test failed: {e}")
traceback.print_exc()
return False
def main():
"""Run all tests"""
print("="*60)
print("INSTITUTIONAL TRADING SYSTEM - COMPONENT TESTS")
print("="*60)
print(f"Test started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print()
results = []
# Run tests
results.append(("Imports", test_imports()))
results.append(("Strategy Engine", test_strategy_engine()))
results.append(("Regime Detector", test_regime_detector()))
results.append(("Risk Manager", test_risk_manager()))
results.append(("Backtest Engine", test_backtest_engine()))
results.append(("Metrics", test_metrics()))
# Summary
print("\n" + "="*60)
print("TEST SUMMARY")
print("="*60)
passed = sum(1 for _, result in results if result)
total = len(results)
for name, result in results:
status = "✓ PASS" if result else "✗ FAIL"
print(f"{name:.<40} {status}")
print()
print(f"Tests passed: {passed}/{total}")
if passed == total:
print("\n🎉 All tests passed! System is ready for deployment.")
return 0
else:
print("\n⚠️ Some tests failed. Please fix issues before deployment.")
return 1
if __name__ == "__main__":
sys.exit(main())