-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbacktest.py
executable file
·432 lines (348 loc) · 13.7 KB
/
backtest.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
from market import Market, Order, Trade
import os
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
"""
Given below are the columns and dtypes corresponding to the different source
types 'BOOK', 'TRADES' and 'NEWS'.
- 'BOOK' is based on TRTH book data and includes timestamp (UTC) as well as
limit order book levels 1-10, each with bid price, bid size, ask price and
ask size.
"TIMESTAMP_UTC": pd.Timestamp,
"L1-BidPrice": float, "L1-BidSize": int,
"L1-AskPrice": float, "L1-AskSize": int,
"L2-BidPrice": float, "L2-BidSize": int,
"L2-AskPrice": float, "L2-AskSize": int,
"L3-BidPrice": float, "L3-BidSize": int,
"L3-AskPrice": float, "L3-AskSize": int,
"L4-BidPrice": float, "L4-BidSize": int,
"L4-AskPrice": float, "L4-AskSize": int,
"L5-BidPrice": float, "L5-BidSize": int,
"L5-AskPrice": float, "L5-AskSize": int,
"L6-BidPrice": float, "L6-BidSize": int,
"L6-AskPrice": float, "L6-AskSize": int,
"L7-BidPrice": float, "L7-BidSize": int,
"L7-AskPrice": float, "L7-AskSize": int,
"L8-BidPrice": float, "L8-BidSize": int,
"L8-AskPrice": float, "L8-AskSize": int,
"L9-BidPrice": float, "L9-BidSize": int,
"L9-AskPrice": float, "L9-AskSize": int,
"L10-BidPrice": float, "L10-BidSize": int,
"L10-AskPrice": float, "L10-AskSize": int,
- 'TRADES' is based on TRTH trades data and includes timestamp (UTC) as well
as price and volume.
"TIMESTAMP_UTC": pd.Timestamp,
"Price": [<float>, *],
"Volume": [<int>, *],
- 'NEWS' is based on scraped twitter data related to the observed stocks and
includes timestamp (UTC) as well as language of a tweet, the tweet itself, the
number of retweets, favorites, comments and times quoted.
"TIMESTAMP_UTC": pd.Timestamp,
"language": [<str>, *],
"text": [<str>, *],
"retweets": [<int>, *],
"favorites": [<int>, *],
"comments": [<int>, *],
"quoted": [<int>, *],
"""
class Backtest:
timestamp_global = None # most recent timestamp across all sources
def __init__(self, agent, generator):
"""
Backtest evaluates a trading agent based on a data generator instance
that yields events based on a set of sources.
:param agent:
Agent, trading agent instance
:param generator
Generator, data generator instance
"""
# from arguments
self.agent = agent
self.generator = generator
# data updated chunk-wise by the generator
self.sources = None
self.monitor = None
# identify symbols as market_ids
symbols = set(source_id.split(".")[0] for source_id
in self.generator.sources
)
# setup market instances
for market_id in symbols:
Market(market_id)
# access market instances
self.markets = Market.instances
def market_step(self, market_id, step):
"""
Update market state and match standing orders.
:param market_id:
str, market identifier
:param step:
int, backtest step
"""
# get corresponding book, trades source
source_book = self.sources[f"{market_id}.BOOK"]
source_trades = self.sources[f"{market_id}.TRADES"]
# update market state based on historical data
self.markets[market_id].update(
book_state=source_book.iloc[step, :],
trades_state=source_trades.iloc[step, :],
)
# match standing agent orders against pre-trade state
self.markets[market_id].match()
def agent_step(self, source_id, step):
"""
Alert agent by sending any event through the corresponding method.
:param source_id:
str, source identifier
:param step:
int, backtest step
"""
# get market_id
market_id = source_id.split(".")[0]
# get event required to alert agent
source_state = self.sources[source_id].iloc[step, :]
# alert agent every time that book is updated
if source_id.endswith("BOOK"):
self.agent.on_quote(
market_id,
book_state=source_state,
)
# alert agent every time that trades happen
if source_id.endswith("TRADES"):
self.agent.on_trade(
market_id,
trades_state=source_state,
)
# alert agent every time that news happen
if source_id.endswith("NEWS"):
self.agent.on_news(
market_id,
news_state=source_state,
)
# alert agent with time interval between this and next step
try:
self.agent.on_time(
timestamp=self.monitor.iloc[step, 0],
timestamp_next=self.monitor.iloc[step+1, 0],
)
# will fail for the last step per monitor frame
except:
pass
def run(self, verbose=True, interval=100):
"""
Iterate over sources and update market_state.
:param verbose:
bool, print updates with each iteration, default is True
"""
# print before backtest ..
print("\n(INFO) start backtest ...\n")
# ...
for sources, monitor in self.generator:
# update data
self.sources = sources
self.monitor = monitor
# ...
for step, timestamp, *monitor_state in self.monitor.itertuples():
# update global timestamp
self.__class__.timestamp_global = timestamp
# get source_id per updated source
updated_sources = (monitor
.iloc[:, 1:]
.columns[monitor_state]
.values
)
# get market_id for book event (trades event is optional)
updated_markets = [col.split(".")[0] for col in updated_sources
if col.endswith("BOOK")
]
# step 1: update book_state -> based on original data
# step 2: match standing orders -> based on pre-trade state
for market_id in updated_markets:
self.market_step(market_id, step)
# step 3: alert agent -> based on original data
for source_id in updated_sources:
self.agent_step(source_id, step)
# print agent status
if verbose and not step % interval:
print(self.agent)
# run reset routine before each data update
for _, market in self.markets.items():
market.reset()
# print after backtest ...
print("\n(INFO) pnl_realized: {pnl_1}, pnl_unrealized: {pnl_2}\n".format(
pnl_1=self.agent.pnl_realized,
pnl_2=self.agent.pnl_unrealized,
))
class Generator:
def __init__(self, sources, start_date, end_date):
"""
Generator class that yields source data one day at a time, in order to
save memory.
:param sources:
list, [<source_id>, *]
:param start_date:
str, date string, default is "2016-01-01"
:param end_date:
str, date string, default is "2016-03-31"
"""
# from arguments
self.sources = sources
self.start_date = pd.Timestamp(start_date)
self.end_date = pd.Timestamp(end_date)
# ...
self.directory = os.path.join(
os.path.dirname(__file__), # preprend to get absolute path
"data" # instead of "./data"
)
self.time_delta = pd.Timedelta(1, "D")
@staticmethod
def _load_sources(directory, sources, date):
"""
Load .csv(.gz) and .json files into dataframes and store them in a
dictionary together with their corresponding key.
:param directory:
str, directory to load files from
:param sources:
dict, [<source_id>, *]
:param date:
pd.Timestamp, date to filter data by
:return sources_output:
dict, {<source_id>: <pd.DataFrame>, *}
"""
datetime = "TIMESTAMP_UTC"
sources_output = dict()
# identify all paths available in directory
path_list = [os.path.join(pre, f) for pre, _, sub
in os.walk(directory) for f in sub if not f.startswith((".", "_"))
]
# ...
for source_id in sources:
# identify matching criteria
market_id, event_id = source_id.split(".")
date_string = str(date.date()).replace("-", "")
path_filter = path_list
# require matching market_id
path_filter = filter(
lambda path: market_id.lower() in path.lower(), path_filter)
# require matching event_id
path_filter = filter(
lambda path: event_id.lower() in path.lower(), path_filter)
# require matching date
path_filter = filter(
lambda path: date_string in path, path_filter)
path_filter = list(path_filter)
# there should be exactly one matching path
if path_filter:
source_path = path_filter[0]
# otherwise, raise Exception
else:
raise Exception("(ERROR) found no data for {source_id}".format(
source_id=source_id,
))
# load event_id 'BOOK' as .csv(.gz)
if "BOOK" in source_id:
df = pd.read_csv(source_path, parse_dates=[datetime])
# load event_id 'TRADES' as .json
if "TRADES" in source_id:
df = pd.read_json(source_path, convert_dates=True)
# load event_id 'NEWS' as .json
if "NEWS" in source_id:
df = pd.read_json(source_path, convert_dates=True)
# if dataframe is empty, break
if not len(df.index) > 0:
break
# make timestamp timezone-unaware
df[datetime] = pd.DatetimeIndex(df[datetime]).tz_localize(None)
# add dataframe to output dictionary
sources_output[source_id] = df
return sources_output
@staticmethod
def _align_sources(sources):
"""
Consolidate and split again all sources so that each source dataframe
contains a state for each ocurring timestamp across all sources.
:param sources:
dict, {<source_id>: <pd.DataFrame>, *}, only original timestamps
:return sources:
dict, {<source_id>: <pd.DataFrame>, *}, aligned timestamps
"""
datetime = "TIMESTAMP_UTC"
# unpack dictionary
id_list, df_list = zip(*sources.items())
# rename columns and use id as prefix, exclude timestamp
add_prefix = lambda id, df: df.rename(columns={x: f"{id}__{x}"
for x in df.columns[1:]
})
df_list = list(map(add_prefix, id_list, df_list))
# merge sources horizontally using full outer join
df_merged = pd.concat([
df.set_index(datetime) for df in df_list
], axis=1, join="outer").reset_index()
# split merged_df into original df_list
df_list = [pd.concat([
df_merged[[datetime]], # timestamp
df_merged[[x for x in df_merged.columns if id in x]
]], axis=1) for id in id_list]
# rename columns and remove prefix, exclude timestamp
del_prefix = lambda df: df.rename(columns={x: x.split("__")[1]
for x in df.columns[1:]
})
df_list = list(map(del_prefix, df_list))
# pack dictionary
sources = dict(zip(id_list, df_list))
return sources
@staticmethod
def _monitor_sources(sources):
"""
In addition to the sources dict, return a monitor dataframe that keeps
track of changes in state across all sources.
:param sources:
dict, {<source_id>: <pd.DataFrame>, *}, with aligned timestamp
:return monitor:
pd.DataFrame, changes per source and timestamp
"""
datetime = "TIMESTAMP_UTC"
# setup dictionary based on timestamp
datetime_index = list(sources.values())[0][datetime]
monitor = {datetime: datetime_index}
# track changes per source and timestamp in series
for key, df in sources.items():
monitor[key] = ~ df.iloc[:, 1:].isna().all(axis=1)
# build monitor as dataframe from series
monitor = pd.DataFrame(monitor)
return monitor
def __iter__(self):
"""
Iterate over each single date between the specified date_start and
date_end, and yield both sources dictionary and monitor dataframe.
:yield sources:
dict, {<source_id>: <pd.DataFrame>, *}, with aligned timestamp
:yield monitor:
pd.DataFrame, changes per source and timestamp
"""
while self.start_date <= self.end_date:
# try to load, process and yield data
try:
print("(INFO) load data for {date} ...".format(
date=self.start_date.date(),
))
sources = self._load_sources(
directory=self.directory,
sources=self.sources,
date=self.start_date,
)
sources = self._align_sources(sources)
monitor = self._monitor_sources(sources)
yield sources, monitor
# should no data be available, pass
except:
pass
# continue with next date
finally:
self.start_date += self.time_delta
def __next__(self):
return self