Skip to content

Commit

Permalink
Fix the issue of the local order being overwritten when canceling an …
Browse files Browse the repository at this point in the history
…expired order that has the same order_id.
  • Loading branch information
nkaz001 committed Apr 7, 2023
1 parent 88eeb53 commit d17d249
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 255 deletions.
2 changes: 1 addition & 1 deletion hftbacktest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'Stat',
'validate_data', 'correct_local_timestamp', 'correct_exch_timestamp', 'correct',)

__version__ = '1.4.1'
__version__ = '1.4.2'


def HftBacktest(
Expand Down
252 changes: 0 additions & 252 deletions hftbacktest/data.py

This file was deleted.

3 changes: 3 additions & 0 deletions hftbacktest/proc/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def _process_data(self, row):
return 0

def submit_order(self, order_id, side, price, qty, order_type, time_in_force, current_timestamp):
if order_id in self.orders:
raise KeyError('Duplicate order_id')

price_tick = round(price / self.depth.tick_size)
order = Order(order_id, price_tick, self.depth.tick_size, qty, side, time_in_force, order_type)
order.req = NEW
Expand Down
11 changes: 10 additions & 1 deletion hftbacktest/proc/nopartialfillexchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def on_best_ask_update(self, prev_best, new_best, timestamp):
self.__fill(order, timestamp, True)

def __ack_new(self, order, timestamp):
if order.order_id in self.orders:
raise KeyError('order_id already exists')

if order.side == BUY:
# Check if the buy order price is greater than or equal to the current best ask.
if order.price_tick >= self.depth.best_ask_tick:
Expand Down Expand Up @@ -268,7 +271,8 @@ def __ack_cancel(self, order, timestamp):
order.status = EXPIRED
order.exch_timestamp = timestamp
local_recv_timestamp = timestamp + self.order_latency.response(timestamp, order, self)
self.orders_to.append(order.copy(), local_recv_timestamp)
# It can overwrite another existing order on the local side if order_id is the same. So, commented out.
# self.orders_to.append(order.copy(), local_recv_timestamp)
return local_recv_timestamp

# Delete the order.
Expand All @@ -293,6 +297,11 @@ def __fill(
exec_price_tick=0,
delete_order=True
):
if order.status == EXPIRED \
or order.status == CANCELED \
or order.status == FILLED:
raise ValueError('status')

order.maker = maker
order.exec_price_tick = order.price_tick if maker else exec_price_tick
order.exec_qty = order.leaves_qty
Expand Down
11 changes: 10 additions & 1 deletion hftbacktest/proc/partialfillexchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ def on_best_ask_update(self, prev_best, new_best, timestamp):
)

def __ack_new(self, order, timestamp):
if order.order_id in self.orders:
raise KeyError('order_id already exists')

if order.side == BUY:
# Check if the buy order price is greater than or equal to the current best ask.
if order.price_tick >= self.depth.best_ask_tick:
Expand Down Expand Up @@ -430,7 +433,8 @@ def __ack_cancel(self, order, timestamp):
order.status = EXPIRED
order.exch_timestamp = timestamp
local_recv_timestamp = timestamp + self.order_latency.response(timestamp, order, self)
self.orders_to.append(order.copy(), local_recv_timestamp)
# It can overwrite another existing order on the local side if order_id is the same. So, commented out.
# self.orders_to.append(order.copy(), local_recv_timestamp)
return local_recv_timestamp

# Delete the order.
Expand All @@ -456,6 +460,11 @@ def __fill(
exec_price_tick=0,
delete_order=True
):
if order.status == EXPIRED \
or order.status == CANCELED \
or order.status == FILLED:
raise ValueError('status')

order.maker = maker
order.exec_price_tick = order.price_tick if maker else exec_price_tick
order.exec_qty = exec_qty
Expand Down

0 comments on commit d17d249

Please sign in to comment.