-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace_backspace.py
197 lines (189 loc) · 5.24 KB
/
namespace_backspace.py
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
# Create namespace for backtest endpoints
from flask_restx import Namespace, fields
backtest_ns = Namespace(
'backtest',
description='Trading strategy backtesting operations',
path='/backtest'
)
# Import the strategy mappings
STRATEGIES = {
'MACD Crossover': 'MACD-based trading strategy with customizable parameters',
'Simple Moving Average': 'Moving average crossover strategy with customizable periods'
}
# Models for strategy parameters
macd_params = backtest_ns.model('MACDParameters', {
'fast_period': fields.Integer(
required=True,
description='Fast period for MACD calculation',
example=12,
min=1
),
'slow_period': fields.Integer(
required=True,
description='Slow period for MACD calculation',
example=26,
min=1
),
'signal_period': fields.Integer(
required=True,
description='Signal period for MACD calculation',
example=9,
min=1
)
})
ma_params = backtest_ns.model('MovingAverageParameters', {
'short_period': fields.Integer(
required=True,
description='Short period for MA calculation',
example=50,
min=1
),
'long_period': fields.Integer(
required=True,
description='Long period for MA calculation',
example=200,
min=1
)
})
# Main backtest request model
backtest_request = backtest_ns.model('BacktestRequest', {
'strategy': fields.String(
required=True,
description='Trading strategy to backtest',
enum=list(STRATEGIES.keys()),
example='MACD Crossover'
),
'asset': fields.String(
required=True,
description='Trading asset symbol (e.g., BTC/USDT)',
example='BTC/USDT'
),
'start_date': fields.String(
required=True,
description='Start date for backtest (YYYY-MM-DD)',
example='2023-01-01'
),
'end_date': fields.String(
required=True,
description='End date for backtest (YYYY-MM-DD)',
example='2023-12-31'
),
'interval': fields.String(
required=True,
description='Trading interval',
enum=['1m', '5m', '15m', '1h', '4h', '1d', '1w'],
example='1h'
),
'initial_capital': fields.Float(
required=True,
description='Initial capital for backtesting',
example=10000.0,
min=0
),
'strategy_params': fields.Raw(
description='Strategy-specific parameters (MACD or MA)',
example={
'fast_period': 12,
'slow_period': 26,
'signal_period': 9
}
)
})
# Trade model for individual trades in the backtest
trade_model = backtest_ns.model('Trade', {
'date': fields.String(
description='Trade timestamp',
example='2023-01-01 14:30:00'
),
'portfolio_value': fields.Float(
description='Portfolio value after trade',
example=10500.75
),
'position': fields.Float(
description='Position size',
example=0.5
),
'price': fields.Float(
description='Asset price at trade',
example=45000.00
),
'capital': fields.Float(
description='Available capital',
example=5000.00
),
'asset': fields.Float(
description='Asset value',
example=5500.75
),
'trade': fields.String(
description='Trade type',
enum=['BUY', 'SELL', 'HOLD'],
example='BUY'
)
})
# Backtest response model
backtest_response = backtest_ns.model('BacktestResponse', {
'backtest_id': fields.String(
description='Unique identifier for the backtest',
example='456e4567-e89b-12d3-a456-426614174000'
),
'trades': fields.List(
fields.Nested(trade_model),
description='List of trades executed'
),
'final_portfolio_value': fields.Float(
description='Final portfolio value',
example=12500.50
),
'total_return_percentage': fields.Float(
description='Total return percentage',
example=25.0
),
'sharpe_ratio': fields.Float(
description='Sharpe ratio',
example=1.5
),
'interval': fields.String(
description='Trading interval used',
example='1h'
),
'initial_capital': fields.Float(
description='Initial capital used',
example=10000.0
)
})
# Backtest summary for list endpoint
backtest_summary = backtest_ns.model('BacktestSummary', {
'id': fields.String(
description='Backtest identifier',
example='456e4567-e89b-12d3-a456-426614174000'
),
'strategy': fields.String(
description='Strategy name',
example='MACD Crossover'
),
'asset': fields.String(
description='Trading asset',
example='BTC/USDT'
),
'interval': fields.String(
description='Trading interval',
example='1h'
),
'start_date': fields.String(
description='Start date',
example='2023-01-01'
),
'end_date': fields.String(
description='End date',
example='2023-12-31'
),
'initial_capital': fields.Float(
description='Initial capital',
example=10000.0
),
'final_portfolio_value': fields.Float(
description='Final portfolio value',
example=12500.50
)
})