-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathmodel_configs_example.py
More file actions
194 lines (174 loc) · 7.5 KB
/
model_configs_example.py
File metadata and controls
194 lines (174 loc) · 7.5 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
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------
from nautilus_trader.backtest.config import BacktestDataConfig
from nautilus_trader.backtest.config import BacktestEngineConfig
from nautilus_trader.backtest.config import BacktestRunConfig
from nautilus_trader.backtest.config import BacktestVenueConfig
from nautilus_trader.backtest.config import ImportableFeeModelConfig
from nautilus_trader.backtest.config import ImportableFillModelConfig
from nautilus_trader.backtest.config import ImportableLatencyModelConfig
from nautilus_trader.backtest.node import BacktestNode
from nautilus_trader.config import ImportableStrategyConfig
from nautilus_trader.config import LoggingConfig
from nautilus_trader.model.data import QuoteTick
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.model.identifiers import TraderId
if __name__ == "__main__":
# Example strategy configuration
strategy_config = ImportableStrategyConfig(
strategy_path="nautilus_trader.examples.strategies.ema_cross:EMACross",
config_path="nautilus_trader.examples.strategies.ema_cross:EMACrossConfig",
config={
"instrument_id": "AAPL.NASDAQ",
"bar_type": "AAPL.NASDAQ-1-MINUTE-LAST-EXTERNAL",
"fast_ema_period": 10,
"slow_ema_period": 20,
"trade_size": 100,
},
)
# Configure backtest engine
engine_config = BacktestEngineConfig(
trader_id=TraderId("BACKTESTER-001"),
logging=LoggingConfig(log_level="INFO"),
strategies=[strategy_config],
)
# Create importable fill model configs
fill_model_config = ImportableFillModelConfig(
fill_model_path="nautilus_trader.backtest.models:FillModel",
config_path="nautilus_trader.backtest.config:FillModelConfig",
config={
"prob_fill_on_limit": 0.95, # 95% chance of limit orders filling
"prob_slippage": 0.05, # 5% chance of slippage
"random_seed": 42, # For reproducibility
},
)
# Create importable latency model configs
latency_model_config = ImportableLatencyModelConfig(
latency_model_path="nautilus_trader.backtest.models:LatencyModel",
config_path="nautilus_trader.backtest.config:LatencyModelConfig",
config={
"base_latency_nanos": 5_000_000, # 5 milliseconds base latency
"insert_latency_nanos": 2_000_000, # Additional 2ms for inserts
"update_latency_nanos": 3_000_000, # Additional 3ms for updates
"cancel_latency_nanos": 1_000_000, # Additional 1ms for cancels
},
)
# Example of different importable fee models
maker_taker_fee_model_config = ImportableFeeModelConfig(
fee_model_path="nautilus_trader.backtest.models:MakerTakerFeeModel",
config_path="nautilus_trader.backtest.config:MakerTakerFeeModelConfig",
config={}, # Empty config for MakerTakerFeeModel as it doesn't require parameters
)
fixed_fee_model_config = ImportableFeeModelConfig(
fee_model_path="nautilus_trader.backtest.models:FixedFeeModel",
config_path="nautilus_trader.backtest.config:FixedFeeModelConfig",
config={
"commission": "1.50 USD",
"charge_commission_once": True,
},
)
per_contract_fee_model_config = ImportableFeeModelConfig(
fee_model_path="nautilus_trader.backtest.models:PerContractFeeModel",
config_path="nautilus_trader.backtest.config:PerContractFeeModelConfig",
config={
"commission": "0.01 USD",
},
)
# Another example with different parameters
custom_fixed_fee_model_config = ImportableFeeModelConfig(
fee_model_path="nautilus_trader.backtest.models:FixedFeeModel",
config_path="nautilus_trader.backtest.config:FixedFeeModelConfig",
config={
"commission": "2.00 USD",
"charge_commission_once": False,
},
)
# Create venue configs with different models
venue_config1 = BacktestVenueConfig(
name="NASDAQ",
oms_type="NETTING",
account_type="CASH",
base_currency="USD",
starting_balances=["1000000 USD"],
book_type="L1_MBP",
fill_model=fill_model_config,
latency_model=latency_model_config,
fee_model=maker_taker_fee_model_config,
)
venue_config2 = BacktestVenueConfig(
name="NYSE",
oms_type="NETTING",
account_type="CASH",
base_currency="USD",
starting_balances=["1000000 USD"],
book_type="L1_MBP",
fill_model=fill_model_config,
latency_model=latency_model_config,
fee_model=fixed_fee_model_config,
)
venue_config3 = BacktestVenueConfig(
name="CME",
oms_type="NETTING",
account_type="MARGIN",
base_currency="USD",
starting_balances=["1000000 USD"],
book_type="L1_MBP",
fill_model=fill_model_config,
latency_model=latency_model_config,
fee_model=per_contract_fee_model_config,
)
# Create venue config with custom fixed fee model
venue_config4 = BacktestVenueConfig(
name="BATS",
oms_type="NETTING",
account_type="CASH",
base_currency="USD",
starting_balances=["1000000 USD"],
book_type="L1_MBP",
fill_model=fill_model_config,
latency_model=latency_model_config,
fee_model=custom_fixed_fee_model_config,
)
# Create data config (this is just a placeholder - you would need actual data)
data_config = BacktestDataConfig(
catalog_path="./data",
data_cls=QuoteTick,
instrument_id=InstrumentId.from_str("AAPL.NASDAQ"),
)
# Create BacktestRunConfig
run_config = BacktestRunConfig(
engine=engine_config,
venues=[venue_config1, venue_config2, venue_config3, venue_config4],
data=[data_config],
)
# Create and run the backtest node
node = BacktestNode(configs=[run_config])
# Note: This example won't actually run without proper data
# results = node.run()
print("Example of using importable model configs in BacktestVenueConfig")
print(
f"Venue 1 uses ImportableFeeModelConfig with MakerTakerFeeModel: {venue_config1.fee_model}",
)
print(
f"Venue 2 uses ImportableFeeModelConfig with FixedFeeModel: {venue_config2.fee_model.config}",
)
print(
f"Venue 3 uses ImportableFeeModelConfig with PerContractFeeModel: {venue_config3.fee_model.config}",
)
print(
f"Venue 4 uses ImportableFeeModelConfig with custom FixedFeeModel: {venue_config4.fee_model.config}",
)
print(f"Fill model config: {venue_config1.fill_model.config}")
print(f"Latency model config: {venue_config1.latency_model.config}")