Skip to content

Commit

Permalink
Merge pull request #57 from nkaz001/fix_datautils
Browse files Browse the repository at this point in the history
Fixed a problem where a snapshot exists only for one side
  • Loading branch information
nkaz001 authored Nov 21, 2023
2 parents 599a4e0 + 92bbac9 commit a3632bf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
22 changes: 14 additions & 8 deletions hftbacktest/data/utils/binancefutures.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,21 @@ def convert(
transaction_time = message['T']
bids = message['bids']
asks = message['asks']
bid_clear_upto = float(bids[-1][0])
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([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 += [[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]
if len(bids) > 0:
bid_clear_upto = float(bids[-1][0])
# clears the existing market depth upto the prices in the snapshot.
rows.append([DEPTH_CLEAR_EVENT, exch_timestamp, local_timestamp, 1, bid_clear_upto, 0])
# inserts the snapshot.
rows += [[DEPTH_SNAPSHOT_EVENT, exch_timestamp, local_timestamp, 1, float(bid[0]), float(bid[1])]
for bid in bids]
if len(asks) > 0:
ask_clear_upto = float(asks[-1][0])
# clears the existing market depth upto the prices in the snapshot.
rows.append([DEPTH_CLEAR_EVENT, exch_timestamp, local_timestamp, -1, ask_clear_upto, 0])
# inserts the snapshot.
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)
Expand Down
55 changes: 28 additions & 27 deletions hftbacktest/data/utils/tardis.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,38 @@ def convert(
is_snapshot = False

# Add DEPTH_CLEAR_EVENT before refreshing the market depth by the snapshot.

ss_bid = ss_bid[:ss_bid_rn]
# Clear the bid market depth within the snapshot bid range.
tmp[row_num] = [
DEPTH_CLEAR_EVENT,
ss_bid[0, 1],
ss_bid[0, 2],
1,
ss_bid[-1, 4],
0
]
row_num += 1
# Add DEPTH_SNAPSHOT_EVENT for the bid snapshot
tmp[row_num:row_num + len(ss_bid)] = ss_bid[:]
row_num += len(ss_bid)
if len(ss_bid) > 0:
# Clear the bid market depth within the snapshot bid range.
tmp[row_num] = [
DEPTH_CLEAR_EVENT,
ss_bid[0, 1],
ss_bid[0, 2],
1,
ss_bid[-1, 4],
0
]
row_num += 1
# Add DEPTH_SNAPSHOT_EVENT for the bid snapshot
tmp[row_num:row_num + len(ss_bid)] = ss_bid[:]
row_num += len(ss_bid)
ss_bid = None

ss_ask = ss_ask[:ss_ask_rn]
# Clear the ask market depth within the snapshot ask range.
tmp[row_num] = [
DEPTH_CLEAR_EVENT,
ss_ask[0, 1],
ss_ask[0, 2],
-1,
ss_ask[-1, 4],
0
]
row_num += 1
# Add DEPTH_SNAPSHOT_EVENT for the ask snapshot
tmp[row_num:row_num + len(ss_ask)] = ss_ask[:]
row_num += len(ss_ask)
if len(ss_ask) > 0:
# Clear the ask market depth within the snapshot ask range.
tmp[row_num] = [
DEPTH_CLEAR_EVENT,
ss_ask[0, 1],
ss_ask[0, 2],
-1,
ss_ask[-1, 4],
0
]
row_num += 1
# Add DEPTH_SNAPSHOT_EVENT for the ask snapshot
tmp[row_num:row_num + len(ss_ask)] = ss_ask[:]
row_num += len(ss_ask)
ss_ask = None
# Insert DEPTH_EVENT
tmp[row_num] = [
Expand Down

0 comments on commit a3632bf

Please sign in to comment.