From 1cbddc6b404cb84b294ed7b39d3d87bdb13b93d9 Mon Sep 17 00:00:00 2001 From: nkaz001 Date: Thu, 10 Aug 2023 10:58:17 -0400 Subject: [PATCH] Replace manual input for event type with predefined constant variables to prevent mistakes. --- hftbacktest/data/utils/binancefutures.py | 15 ++++++++------- hftbacktest/data/utils/binancehistmktdata.py | 9 +++++---- hftbacktest/data/utils/snapshot.py | 6 +++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/hftbacktest/data/utils/binancefutures.py b/hftbacktest/data/utils/binancefutures.py index cbdc798..b5e0b81 100644 --- a/hftbacktest/data/utils/binancefutures.py +++ b/hftbacktest/data/utils/binancefutures.py @@ -6,6 +6,7 @@ from numpy.typing import NDArray from .. import correct, validate_data +from ... import DEPTH_EVENT, DEPTH_CLEAR_EVENT, DEPTH_SNAPSHOT_EVENT, TRADE_EVENT def convert( @@ -76,15 +77,15 @@ def convert( qty = data['q'] side = -1 if data['m'] else 1 # trade initiator's side exch_timestamp = int(transaction_time) * 1000 - rows.append([2, exch_timestamp, local_timestamp, side, float(price), float(qty)]) + rows.append([TRADE_EVENT, exch_timestamp, local_timestamp, side, float(price), float(qty)]) elif evt == 'depthUpdate': # event_time = data['E'] transaction_time = data['T'] bids = data['b'] asks = data['a'] exch_timestamp = int(transaction_time) * 1000 - rows += [[1, exch_timestamp, local_timestamp, 1, float(bid[0]), float(bid[1])] for bid in bids] - rows += [[1, exch_timestamp, local_timestamp, -1, float(ask[0]), float(ask[1])] for ask in asks] + rows += [[DEPTH_EVENT, exch_timestamp, local_timestamp, 1, float(bid[0]), float(bid[1])] for bid in bids] + rows += [[DEPTH_EVENT, exch_timestamp, local_timestamp, -1, float(ask[0]), float(ask[1])] for ask in asks] elif evt == 'markPriceUpdate' and 'm' in opt: # event_time = data['E'] transaction_time = data['T'] @@ -115,11 +116,11 @@ def convert( ask_clear_upto = float(asks[-1][0]) exch_timestamp = int(transaction_time) * 1000 # clear the existing market depth upto the prices in the snapshot. - rows.append([3, exch_timestamp, local_timestamp, 1, bid_clear_upto, 0]) - rows.append([3, exch_timestamp, local_timestamp, -1, ask_clear_upto, 0]) + rows.append([DEPTH_CLEAR_EVENT, exch_timestamp, local_timestamp, 1, bid_clear_upto, 0]) + rows.append([DEPTH_CLEAR_EVENT, exch_timestamp, local_timestamp, -1, ask_clear_upto, 0]) # insert the snapshot. - rows += [[4, exch_timestamp, local_timestamp, 1, float(bid[0]), float(bid[1])] for bid in bids] - rows += [[4, exch_timestamp, local_timestamp, -1, float(ask[0]), float(ask[1])] for ask in asks] + rows += [[DEPTH_SNAPSHOT_EVENT, exch_timestamp, local_timestamp, 1, float(bid[0]), float(bid[1])] for bid in bids] + rows += [[DEPTH_SNAPSHOT_EVENT, exch_timestamp, local_timestamp, -1, float(ask[0]), float(ask[1])] for ask in asks] data = np.asarray(rows, np.float64) data = correct(data, base_latency=base_latency, method=method) diff --git a/hftbacktest/data/utils/binancehistmktdata.py b/hftbacktest/data/utils/binancehistmktdata.py index a92f30a..197b044 100644 --- a/hftbacktest/data/utils/binancehistmktdata.py +++ b/hftbacktest/data/utils/binancehistmktdata.py @@ -4,6 +4,7 @@ from numpy.typing import NDArray from .. import merge_on_local_timestamp, correct, validate_data +from ... import DEPTH_EVENT, TRADE_EVENT, DEPTH_SNAPSHOT_EVENT def convert_snapshot( @@ -49,7 +50,7 @@ def convert_snapshot( if side == 1: ss_bid.append([ - 4, + DEPTH_SNAPSHOT_EVENT, exch_timestamp, loc_timestamp, side, @@ -58,7 +59,7 @@ def convert_snapshot( ]) else: ss_ask.append([ - 4, + DEPTH_SNAPSHOT_EVENT, exch_timestamp, loc_timestamp, side, @@ -129,7 +130,7 @@ def convert( # Insert DEPTH_EVENT tmp_depth[row_num] = [ - 1, + DEPTH_EVENT, exch_timestamp, loc_timestamp, side, @@ -161,7 +162,7 @@ def convert( # Insert TRADE_EVENT tmp_trades[row_num] = [ - 2, + TRADE_EVENT, exch_timestamp, loc_timestamp, side, diff --git a/hftbacktest/data/utils/snapshot.py b/hftbacktest/data/utils/snapshot.py index b63c3e3..83abf28 100644 --- a/hftbacktest/data/utils/snapshot.py +++ b/hftbacktest/data/utils/snapshot.py @@ -5,7 +5,7 @@ from ... import HftBacktest, Linear, ConstantLatency from ...typing import DataCollection, Data -from ...reader import UNTIL_END_OF_DATA +from ...reader import UNTIL_END_OF_DATA, DEPTH_SNAPSHOT_EVENT def create_last_snapshot( @@ -47,7 +47,7 @@ def create_last_snapshot( snapshot = [] snapshot += [[ - 4, + DEPTH_SNAPSHOT_EVENT, hbt.last_timestamp, -1, 1, @@ -55,7 +55,7 @@ def create_last_snapshot( float(qty) ] for bid, qty in sorted(hbt.bid_depth.items(), key=lambda v: -float(v[0]))] snapshot += [[ - 4, + DEPTH_SNAPSHOT_EVENT, hbt.last_timestamp, -1, -1,