Skip to content

Commit

Permalink
Merge pull request #467 from yakir4123/yakir/feat/get-execution-order…
Browse files Browse the repository at this point in the history
…s-2-active-orders

fix: update-active-orders another get-active-orders + use numba to ac…
  • Loading branch information
saleh-mir authored Jul 28, 2024
2 parents 08cb71c + f9a268e commit 02d9744
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
21 changes: 14 additions & 7 deletions jesse/models/FuturesExchange.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import numpy as np
from numba import njit

import jesse.helpers as jh
import jesse.services.logger as logger
from jesse.enums import sides, order_types
Expand Down Expand Up @@ -150,18 +152,15 @@ def on_order_cancellation(self, order: Order) -> None:
base_asset = jh.base_asset(order.symbol)

self.available_assets[base_asset] -= order.qty
# self.available_assets[quote_asset] += order.qty * order.price
if not order.reduce_only:
order_array = np.array([order.qty, order.price])
if order.side == sides.BUY:
item_index = np.where(np.all(self.buy_orders[base_asset].array == order_array, axis=1))[0]
if len(item_index) > 0:
index = item_index[0]
index = find_order_index(self.buy_orders[base_asset].array, order_array)
if index != -1:
self.buy_orders[base_asset].delete(index, axis=0)
else:
item_index = np.where(np.all(self.sell_orders[base_asset].array == order_array, axis=1))[0]
if len(item_index) > 0:
index = item_index[0]
index = find_order_index(self.sell_orders[base_asset].array, order_array)
if index != -1:
self.sell_orders[base_asset].delete(index, axis=0)

def update_from_stream(self, data: dict) -> None:
Expand All @@ -175,3 +174,11 @@ def update_from_stream(self, data: dict) -> None:
self._wallet_balance = data['wallet_balance']
if self._started_balance == 0:
self._started_balance = self._wallet_balance


@njit(cache=True)
def find_order_index(orders, order_array):
for i in range(len(orders)):
if np.all(orders[i] == order_array):
return i
return -1
2 changes: 1 addition & 1 deletion jesse/modes/backtest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ def _execute_market_orders():


def _get_executing_orders(exchange, symbol, real_candle):
orders = store.orders.get_orders(exchange, symbol)
orders = store.orders.get_active_orders(exchange, symbol)
return [
order
for order in orders
Expand Down

0 comments on commit 02d9744

Please sign in to comment.