-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_data_test.py
More file actions
93 lines (78 loc) · 2.71 KB
/
quick_data_test.py
File metadata and controls
93 lines (78 loc) · 2.71 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
#!/usr/bin/env python
"""
Quick test to generate minimal data and test backtest
"""
import os
import sys
import django
from datetime import datetime, timedelta
from decimal import Decimal
# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
django.setup()
from market_data.models import HistoricalData
from strategies.models import Strategy
from strategies.backtest_engine import BacktestEngine
def create_minimal_data():
"""Create minimal test data"""
print("🔍 [QUICK] Creating minimal test data...")
# Clear existing data
HistoricalData.objects.filter(symbol='ES', timeframe='1m').delete()
# Create 1000 records of test data
base_price = 4000
current_time = datetime(2024, 1, 1, 9, 30) # Start of trading day
records = []
for i in range(1000):
# Simple random walk
change = (i % 100 - 50) * 0.1 # Oscillating price
price = base_price + change
records.append(HistoricalData(
symbol='ES',
timeframe='1m',
date=current_time + timedelta(minutes=i),
open_price=Decimal(str(price)),
high_price=Decimal(str(price + 0.5)),
low_price=Decimal(str(price - 0.5)),
close_price=Decimal(str(price + 0.1)),
volume=1000
))
HistoricalData.objects.bulk_create(records)
print(f"✅ [QUICK] Created {len(records)} test records")
def test_backtest():
"""Test backtest with minimal data"""
print("🔍 [QUICK] Testing backtest...")
# Create test strategy
strategy = Strategy(
name='Quick Test Strategy',
description='Test strategy',
symbol='ES',
timeframe='1m',
initial_capital=100000,
entry_rules={'rsi_oversold': 30},
exit_rules={'time_based': True},
stop_loss_type='percentage',
stop_loss_value=2.0,
take_profit_type='percentage',
take_profit_value=4.0
)
# Test backtest engine
engine = BacktestEngine()
try:
result = engine.run_backtest(
strategy=strategy,
start_date=datetime(2024, 1, 1),
end_date=datetime(2024, 1, 2),
initial_capital=Decimal('100000'),
commission=Decimal('4.00'),
slippage=Decimal('0.5')
)
print(f"✅ [QUICK] Backtest completed!")
print(f"✅ [QUICK] Total trades: {result.total_trades}")
print(f"✅ [QUICK] Total return: {result.total_return}")
except Exception as e:
print(f"❌ [QUICK] Backtest failed: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
create_minimal_data()
test_backtest()