-
Notifications
You must be signed in to change notification settings - Fork 543
/
Copy pathdatabento_option_greeks.py
321 lines (266 loc) · 9.33 KB
/
databento_option_greeks.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# ---
# jupyter:
# jupytext:
# formats: py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.4
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# ## imports
# %%
# Note: Use the python extension jupytext to be able to open this python file in jupyter as a notebook
# %%
from nautilus_trader.adapters.databento.data_utils import data_path
from nautilus_trader.adapters.databento.data_utils import databento_data
from nautilus_trader.adapters.databento.data_utils import load_catalog
from nautilus_trader.backtest.node import BacktestNode
from nautilus_trader.common.enums import LogColor
from nautilus_trader.config import BacktestDataConfig
from nautilus_trader.config import BacktestEngineConfig
from nautilus_trader.config import BacktestRunConfig
from nautilus_trader.config import BacktestVenueConfig
from nautilus_trader.config import ImportableActorConfig
from nautilus_trader.config import ImportableStrategyConfig
from nautilus_trader.config import LoggingConfig
from nautilus_trader.config import StrategyConfig
from nautilus_trader.config import StreamingConfig
from nautilus_trader.core.datetime import unix_nanos_to_iso8601
from nautilus_trader.model.data import Bar
from nautilus_trader.model.data import BarType
from nautilus_trader.model.data import QuoteTick
from nautilus_trader.model.enums import OrderSide
from nautilus_trader.model.greeks import GreeksData
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.model.identifiers import Venue
from nautilus_trader.model.objects import Price
from nautilus_trader.model.objects import Quantity
from nautilus_trader.risk.greeks import GreeksCalculator
from nautilus_trader.risk.greeks import GreeksCalculatorConfig
from nautilus_trader.risk.greeks import InterestRateProvider
from nautilus_trader.risk.greeks import InterestRateProviderConfig
from nautilus_trader.trading.strategy import Strategy
# %% [markdown]
# ## parameters
# %%
# import nautilus_trader.adapters.databento.data_utils as db_data_utils
# from nautilus_trader.adapters.databento.data_utils import init_databento_client
# from option_trader import DATA_PATH # personal library, use your own value here
# db_data_utils.DATA_PATH = DATA_PATH
catalog_folder = "options_catalog"
catalog = load_catalog(catalog_folder)
future_symbols = ["ESM4"]
option_symbols = ["ESM4 P5230", "ESM4 P5250"]
start_time = "2024-05-09T10:00"
end_time = "2024-05-09T10:05"
# a valid databento key can be entered here (or as an env variable of the same name)
# DATABENTO_API_KEY = None
# init_databento_client(DATABENTO_API_KEY)
# https://databento.com/docs/schemas-and-data-formats/whats-a-schema
futures_data = databento_data(
future_symbols,
start_time,
end_time,
"ohlcv-1m",
"futures",
catalog_folder,
)
options_data = databento_data(
option_symbols,
start_time,
end_time,
"mbp-1",
"options",
catalog_folder,
)
# %% [markdown]
# ## strategy
# %%
class OptionConfig(StrategyConfig, frozen=True):
future_id: InstrumentId
option_id: InstrumentId
option_id2: InstrumentId
load_greeks: bool = False
class OptionStrategy(Strategy):
"""
An options trading strategy that calculates and displays the portfolio greeks.
The strategy subscribes to quotes for two options and a futures contract, and
initializes a portfolio with some trades. It can optionally load greeks from a
catalog, or compute them on the fly. The strategy logs the portfolio greeks at
regular intervals.
"""
def __init__(self, config: OptionConfig):
super().__init__(config=config)
self.start_orders_done = False
def on_start(self):
self.subscribe_quote_ticks(self.config.option_id)
self.subscribe_quote_ticks(self.config.option_id2)
bar_type = BarType.from_str(f"{self.config.future_id}-1-MINUTE-LAST-EXTERNAL")
self.subscribe_bars(bar_type)
def init_portfolio(self):
self.submit_market_order(instrument_id=self.config.option_id, quantity=-10)
self.submit_market_order(instrument_id=self.config.option_id2, quantity=10)
self.submit_market_order(instrument_id=self.config.future_id, quantity=1)
self.start_orders_done = True
def on_bar(self, bar):
self.user_log(f"bar ts_init = {unix_nanos_to_iso8601(bar.ts_init)}")
if not self.start_orders_done:
self.user_log("Initializing the portfolio with some trades")
self.init_portfolio()
return
if self.config.load_greeks:
# when greeks are loaded from a catalog a small delay is needed so all greeks are updated
# note that loading greeks is not required, it's actually faster to just compute them every time
self.clock.set_time_alert(
"display greeks",
self.clock.utc_now().replace(microsecond=100),
self.display_greeks,
override=True,
)
else:
self.display_greeks()
def display_greeks(self, alert=None):
portfolio_greeks = self.portfolio_greeks()
self.user_log(f"{portfolio_greeks=}")
def submit_market_order(self, instrument_id, quantity):
order = self.order_factory.market(
instrument_id=instrument_id,
order_side=(OrderSide.BUY if quantity > 0 else OrderSide.SELL),
quantity=Quantity.from_int(abs(quantity)),
)
self.submit_order(order)
def submit_limit_order(self, instrument_id, price, quantity):
order = self.order_factory.limit(
instrument_id=instrument_id,
order_side=(OrderSide.BUY if quantity > 0 else OrderSide.SELL),
quantity=Quantity.from_int(abs(quantity)),
price=Price(price),
)
self.submit_order(order)
def user_log(self, msg):
self.log.warning(str(msg), color=LogColor.GREEN)
# %% [markdown]
# ## backtest node
# %%
# BacktestEngineConfig
# for saving and loading custom data greeks, use False, True then True, False below
load_greeks, stream_data = False, False
actors = [
ImportableActorConfig(
actor_path=GreeksCalculator.fully_qualified_name(),
config_path=GreeksCalculatorConfig.fully_qualified_name(),
config={
"load_greeks": load_greeks,
},
),
ImportableActorConfig(
actor_path=InterestRateProvider.fully_qualified_name(),
config_path=InterestRateProviderConfig.fully_qualified_name(),
config={
"interest_rates_file": str(data_path(catalog_folder, "usd_short_term_rate.xml")),
},
),
]
strategies = [
ImportableStrategyConfig(
strategy_path=OptionStrategy.fully_qualified_name(),
config_path=OptionConfig.fully_qualified_name(),
config={
"future_id": InstrumentId.from_str(f"{future_symbols[0]}.GLBX"),
"option_id": InstrumentId.from_str(f"{option_symbols[0]}.GLBX"),
"option_id2": InstrumentId.from_str(f"{option_symbols[1]}.GLBX"),
"load_greeks": load_greeks,
},
),
]
streaming = StreamingConfig(
catalog_path=catalog.path,
fs_protocol="file",
include_types=[GreeksData],
)
logging = LoggingConfig(
bypass_logging=False,
log_colors=True,
log_level="WARN",
log_level_file="WARN",
log_directory=".",
log_file_format=None, # 'json' or None
log_file_name="databento_option_greeks",
)
engine_config = BacktestEngineConfig(
actors=actors,
strategies=strategies,
streaming=(streaming if stream_data else None),
logging=logging,
)
# BacktestRunConfig
data = [
BacktestDataConfig(
data_cls=Bar,
catalog_path=catalog.path,
instrument_id=InstrumentId.from_str(f"{future_symbols[0]}.GLBX"),
bar_spec="1-MINUTE-LAST",
# start_time=start_time,
# end_time=end_time,
),
BacktestDataConfig(
data_cls=QuoteTick,
catalog_path=catalog.path,
),
]
if load_greeks:
data.append(
BacktestDataConfig(
data_cls=GreeksData.fully_qualified_name(),
catalog_path=catalog.path,
client_id="GreeksDataProvider",
metadata={"instrument_id": "ES"},
),
)
venues = [
BacktestVenueConfig(
name="GLBX",
oms_type="NETTING",
account_type="MARGIN",
base_currency="USD",
starting_balances=["1_000_000 USD"],
),
]
configs = [
BacktestRunConfig(
engine=engine_config,
data=data,
venues=venues,
chunk_size=None, # use None when loading custom data
),
]
node = BacktestNode(configs=configs)
# %%
results = node.run(raise_exception=True)
# %%
if stream_data:
# 'overwrite_or_ignore' keeps existing data intact, 'delete_matching' overwrites everything, see in pyarrow/dataset.py
catalog.convert_stream_to_data(
results[0].instance_id,
GreeksData,
basename_template="part-{i}.parquet",
partitioning=["date"],
existing_data_behavior="overwrite_or_ignore",
)
# %% [markdown]
# ## backtest results
# %%
engine = node.get_engine(configs[0].id)
engine.trader.generate_order_fills_report()
# %%
engine.trader.generate_positions_report()
# %%
engine.trader.generate_account_report(Venue("GLBX"))
# %%
node.dispose()