Skip to content

Commit

Permalink
Replace manual input for event type with predefined constant variable…
Browse files Browse the repository at this point in the history
…s to prevent mistakes.
  • Loading branch information
nkaz001 committed Aug 10, 2023
1 parent 0fcddbf commit 1cbddc6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
15 changes: 8 additions & 7 deletions hftbacktest/data/utils/binancefutures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions hftbacktest/data/utils/binancehistmktdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -49,7 +50,7 @@ def convert_snapshot(

if side == 1:
ss_bid.append([
4,
DEPTH_SNAPSHOT_EVENT,
exch_timestamp,
loc_timestamp,
side,
Expand All @@ -58,7 +59,7 @@ def convert_snapshot(
])
else:
ss_ask.append([
4,
DEPTH_SNAPSHOT_EVENT,
exch_timestamp,
loc_timestamp,
side,
Expand Down Expand Up @@ -129,7 +130,7 @@ def convert(

# Insert DEPTH_EVENT
tmp_depth[row_num] = [
1,
DEPTH_EVENT,
exch_timestamp,
loc_timestamp,
side,
Expand Down Expand Up @@ -161,7 +162,7 @@ def convert(

# Insert TRADE_EVENT
tmp_trades[row_num] = [
2,
TRADE_EVENT,
exch_timestamp,
loc_timestamp,
side,
Expand Down
6 changes: 3 additions & 3 deletions hftbacktest/data/utils/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -47,15 +47,15 @@ def create_last_snapshot(

snapshot = []
snapshot += [[
4,
DEPTH_SNAPSHOT_EVENT,
hbt.last_timestamp,
-1,
1,
float(bid * tick_size),
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,
Expand Down

0 comments on commit 1cbddc6

Please sign in to comment.