-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
299 lines (259 loc) · 9.92 KB
/
main.py
File metadata and controls
299 lines (259 loc) · 9.92 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
import asyncio
from pathlib import Path
import pandas as pd
from pprint import pprint
from datetime import datetime
from nautilus_trader.backtest.config import (
BacktestDataConfig,
BacktestEngineConfig,
BacktestRunConfig,
BacktestVenueConfig,
)
from nautilus_trader.backtest.node import BacktestNode
from nautilus_trader.config import ImportableStrategyConfig
from nautilus_trader.config import LoggingConfig, RiskEngineConfig
from nautilus_trader.persistence.catalog import ParquetDataCatalog
from nautilus_trader.persistence.wranglers import BarDataWrangler
from nautilus_trader.model.enums import (
PriceType,
BarAggregation,
AggregationSource,
)
from nautilus_trader.model.data import Bar, BarType, BarSpecification
from nautilus_trader.model.instruments.base import Instrument
from nautilus_trader.test_kit.providers import TestInstrumentProvider
from nautilus_trader.persistence.config import DataCatalogConfig
from utils.binance_data import get_combined_dataframe
from nautilus_trader.config import DataEngineConfig
def create_bars_from_df(
df: pd.DataFrame,
instrument: Instrument,
bar_type: BarType,
) -> list[Bar]:
# Read CSV and convert open_time to datetime index
df["timestamp"] = pd.to_datetime(df["open_time"], unit="ms")
df.set_index("timestamp", inplace=True)
# Select and rename required columns
df = df[["open", "high", "low", "close", "volume"]]
wrangler = BarDataWrangler(bar_type=bar_type, instrument=instrument)
bars = wrangler.process(data=df)
return bars
async def run_backtest(
quote_symbol: str,
base_symbol: str,
start_date: str,
end_date: str,
backtest_start_date: str | None = None,
interval: str = "1h",
):
# Validate backtest_start_date if provided
if backtest_start_date is not None:
start = datetime.strptime(start_date, "%Y-%m-%d").date()
end = datetime.strptime(end_date, "%Y-%m-%d").date()
backtest_start = datetime.strptime(backtest_start_date, "%Y-%m-%d").date()
if not (start <= backtest_start <= end):
raise ValueError(
f"backtest_start_date {backtest_start_date} must be between "
f"start_date {start_date} and end_date {end_date}"
)
# Create data directory if it doesn't exist
data_dir = Path("data")
data_dir.mkdir(exist_ok=True)
symbol = f"{quote_symbol}{base_symbol}"
# Set up the catalog for storing data
catalog_path = Path("catalog")
if catalog_path.exists():
import shutil
shutil.rmtree(catalog_path)
catalog_path.mkdir()
catalog = ParquetDataCatalog(str(catalog_path))
# Instruments define the trading pair on a given exchange
# Include info like symbol, size precision, price precision, maker/taker fee, etc.
instrument = TestInstrumentProvider.btcusdt_perp_binance()
# BarSpecification defines the time step and aggregation of the bar data
# Step is the time step of the bar (e.g. 1 hour)
# Aggregation is the type of aggregation (e.g. last price)
# PriceType is the type of price (e.g. last, mid, bid, ask)
bar_spec = BarSpecification(
step=1,
aggregation=BarAggregation.HOUR,
price_type=PriceType.LAST,
)
# BarType is the type of bar data
# InstrumentId is the instrument id (e.g. BTCUSDT-PERP)
# BarSpec is the bar specification
# AggregationSource is the source of the aggregation (EXTERNAL or INTERNAL)
bar_type = BarType(
instrument_id=instrument.id,
bar_spec=bar_spec,
aggregation_source=AggregationSource.EXTERNAL,
)
combined_df = await get_combined_dataframe(
symbol=symbol,
interval=interval,
start_date=start_date,
end_date=end_date,
)
# Create bar data
bars = create_bars_from_df(
df=combined_df,
instrument=instrument,
bar_type=bar_type,
)
catalog.write_data([instrument])
catalog.write_data(bars)
# Print first 5 bar timestamps
print("\nFirst 5 bar timestamps:")
for i, bar in enumerate(catalog.bars(bar_types=[str(bar_type)])[:5]):
print(f"Bar {i + 1}: {bar.ts_init}")
# Configure the backtest data
# Represents the data configuration for one specific backtest run.
# Note: It's possible to use instrument_ids and bar_types instead of instrument_id and bar_spec
data_config = BacktestDataConfig(
catalog_path=catalog.path,
data_cls=Bar,
instrument_id=instrument.id,
bar_spec=bar_spec,
)
catalogs = [
DataCatalogConfig(
path=catalog.path,
),
]
default_leverage: float = 10.0
# Configure the backtest venue
# Add book type if dealing with order book data (won't work with bars only)
venue_config = BacktestVenueConfig(
name="BINANCE",
oms_type="HEDGING", # HEDGING allows for both long and short positions on a given contract, while NETTING only allows for one position per contract
account_type="MARGIN",
base_currency="USDT", # None for multi-currency accounts
default_leverage=default_leverage,
starting_balances=["10000000 USDT"],
reject_stop_orders=False, # Stop orders are not rejected on submission if trigger price is in the market
bar_execution=True,
bar_adaptive_high_low_ordering=True,
)
# Define strategies
strategies = [
ImportableStrategyConfig(
strategy_path="strategies.EMACrossBracket:EMACrossBracket",
config_path="strategies.EMACrossBracket:EMACrossBracketConfig",
config={
"instrument_id": instrument.id,
"bar_type": bar_type,
"trade_size": 0.01,
"atr_period": 20,
"fast_ema_period": 10,
"slow_ema_period": 20,
"bracket_distance_atr": 3.0,
"historical_start_time": start_date,
"historical_end_time": backtest_start_date,
},
),
]
data_engine = DataEngineConfig(
time_bars_origins={
BarAggregation.HOUR: pd.Timedelta(seconds=0),
},
)
logging = LoggingConfig(
bypass_logging=False,
log_colors=True,
log_level="ERROR",
log_level_file="DEBUG",
log_directory="logs",
log_file_format=None, # "json" or None
log_file_name="backtest",
clear_log_file=True,
print_config=False,
use_pyo3=False,
)
# Configure the backtest engine
# trader_id : TraderId
# The trader ID for the node (must be a name and ID tag separated by a hyphen).
# log_level : str, default "INFO"
# The stdout log level for the node.
# loop_debug : bool, default False
# If the asyncio event loop should be in debug mode.
# cache : CacheConfig, optional
# The cache configuration.
# data_engine : DataEngineConfig, optional
# The live data engine configuration.
# risk_engine : RiskEngineConfig, optional
# The live risk engine configuration.
# exec_engine : ExecEngineConfig, optional
# The live execution engine configuration.
# streaming : StreamingConfig, optional
# The configuration for streaming to feather files.
# strategies : list[ImportableStrategyConfig]
# The strategy configurations for the kernel.
# actors : list[ImportableActorConfig]
# The actor configurations for the kernel.
# exec_algorithms : list[ImportableExecAlgorithmConfig]
# The execution algorithm configurations for the kernel.
# controller : ImportableControllerConfig, optional
# The trader controller for the kernel.
# load_state : bool, default True
# If trading strategy state should be loaded from the database on start.
# save_state : bool, default True
# If trading strategy state should be saved to the database on stop.
# bypass_logging : bool, default False
# If logging should be bypassed.
# run_analysis : bool, default True
# If post backtest performance analysis should be run.
backtest_engine_config = BacktestEngineConfig(
strategies=strategies,
logging=logging,
risk_engine=RiskEngineConfig(
bypass=True, # Example of bypassing pre-trade risk checks for backtests
),
catalogs=catalogs,
data_engine=data_engine,
)
# Create backtest configuration
config = BacktestRunConfig(
engine=backtest_engine_config,
data=[data_config],
venues=[venue_config],
start=backtest_start_date,
end=end_date,
)
# Create backtest node
node = BacktestNode(configs=[config])
# Run the backtest
print(f"Running backtest for {symbol} from {backtest_start_date} to {end_date}...")
results = node.run()
# Generate reports
print("Generating reports...")
engine = node.get_engine(config.id)
if engine:
# Generate and save order fills report if available as DataFrame
fills_report = None
if hasattr(engine.trader, "generate_order_fills_report"):
fills_report = engine.trader.generate_order_fills_report()
if isinstance(fills_report, pd.DataFrame):
fills_report.to_csv("order_fills_report.csv")
# Generate and save positions report if available as DataFrame
positions_report = None
if hasattr(engine.trader, "generate_positions_report"):
positions_report = engine.trader.generate_positions_report()
if isinstance(positions_report, pd.DataFrame):
positions_report.to_csv("positions_report.csv")
print("Backtest complete!")
return results
def main():
# Run the backtest
results = asyncio.run(
run_backtest(
base_symbol="USDT",
quote_symbol="BTC",
interval="1h",
start_date="2024-01-01",
end_date="2024-12-01",
backtest_start_date="2024-01-03",
)
)
pprint(results)
if __name__ == "__main__":
main()