Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backtest-optimization): reduce overhead of calls in Orders #472

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
create order model to make sure I still have to db functionality
  • Loading branch information
yakir4123 committed Sep 2, 2024
commit cfeda67887caa79ce85f63d70a994e2a83f822d4
30 changes: 0 additions & 30 deletions jesse/exchanges/sandbox/Sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ def __init__(self, name='Sandbox'):
self.name = name

def market_order(self, symbol: str, qty: float, current_price: float, side: str, reduce_only: bool) -> Order:
# order = Order({
# 'id': jh.generate_unique_id(),
# 'symbol': symbol,
# 'exchange': self.name,
# 'side': side,
# 'type': order_types.MARKET,
# 'reduce_only': reduce_only,
# 'qty': jh.prepare_qty(qty, side),
# 'price': current_price,
# })
order = Order(
id=jh.generate_unique_id(),
symbol=symbol,
Expand All @@ -40,16 +30,6 @@ def market_order(self, symbol: str, qty: float, current_price: float, side: str,
return order

def limit_order(self, symbol: str, qty: float, price: float, side: str, reduce_only: bool) -> Order:
# order = Order({
# 'id': jh.generate_unique_id(),
# 'symbol': symbol,
# 'exchange': self.name,
# 'side': side,
# 'type': order_types.LIMIT,
# 'reduce_only': reduce_only,
# 'qty': jh.prepare_qty(qty, side),
# 'price': price,
# })
order = Order(
id=jh.generate_unique_id(),
symbol=symbol,
Expand All @@ -66,16 +46,6 @@ def limit_order(self, symbol: str, qty: float, price: float, side: str, reduce_o
return order

def stop_order(self, symbol: str, qty: float, price: float, side: str, reduce_only: bool) -> Order:
# order = Order({
# 'id': jh.generate_unique_id(),
# 'symbol': symbol,
# 'exchange': self.name,
# 'side': side,
# 'type': order_types.STOP,
# 'reduce_only': reduce_only,
# 'qty': jh.prepare_qty(qty, side),
# 'price': price,
# })
order = Order(
id=jh.generate_unique_id(),
symbol=symbol,
Expand Down
22 changes: 11 additions & 11 deletions jesse/factories/order_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def fake_order(attributes: dict = None) -> Order:
status = order_statuses.ACTIVE
created_at = first_timestamp

return Order({
"id": jh.generate_unique_id(),
'symbol': attributes.get('symbol', symbol),
'exchange': attributes.get('exchange', exchange),
'side': attributes.get('side', side),
'type': attributes.get('type', order_type),
'qty': attributes.get('qty', qty),
'price': attributes.get('price', price),
'status': attributes.get('status', status),
'created_at': attributes.get('created_at', created_at),
})
return Order(
id=jh.generate_unique_id(),
symbol=attributes.get('symbol', symbol),
exchange=attributes.get('exchange', exchange),
side=attributes.get('side', side),
type=attributes.get('type', order_type),
qty=attributes.get('qty', qty),
price=attributes.get('price', price),
status=attributes.get('status', status),
created_at=attributes.get('created_at', created_at),
)
111 changes: 46 additions & 65 deletions jesse/models/Order.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,47 @@
database.open_connection()


# class Order(Model):
# # id generated by Jesse for database usage
# id = UUIDField(primary_key=True)
# trade_id = UUIDField(index=True, null=True)
# session_id = UUIDField(index=True)
#
# # id generated by market, used in live-trade mode
# exchange_id = CharField(null=True)
# # some exchanges might require even further info
# vars = JSONField(default={})
# symbol = CharField()
# exchange = CharField()
# side = CharField()
# type = CharField()
# reduce_only = BooleanField()
# qty = FloatField()
# filled_qty = FloatField(default=0)
# price = FloatField(null=True)
# status = CharField(default=order_statuses.ACTIVE)
# created_at = BigIntegerField()
# executed_at = BigIntegerField(null=True)
# canceled_at = BigIntegerField(null=True)
#
# # needed in Jesse, but no need to store in database(?)
# submitted_via = None
#
# class Meta:
# from jesse.services.db import database
#
# database = database.db
# indexes = ((('trade_id', 'exchange', 'symbol', 'status', 'created_at'), False),)
#
# def __init__(self, attributes: dict = None, should_silent=False, **kwargs) -> None:
# Model.__init__(self, attributes=attributes, **kwargs)
#
# if attributes is None:
# attributes = {}
#
# for a, value in attributes.items():
# setattr(self, a, value)
#
# if self.created_at is None:
# self.created_at = jh.now_to_timestamp()
#
# # if jh.is_live():
# # from jesse.store import store
# # self.session_id = store.app.session_id
# # self.save(force_insert=True)
#
# if not should_silent:
# if jh.is_live():
# self.notify_submission()
#
# if jh.is_debuggable('order_submission') and (self.is_active or self.is_queued):
# txt = f'{"QUEUED" if self.is_queued else "SUBMITTED"} order: {self.symbol}, {self.type}, {self.side}, {self.qty}'
# if self.price:
# txt += f', ${self.price}'
# logger.info(txt)
#
# # handle exchange balance for ordered asset
# e = selectors.get_exchange(self.exchange)
# e.on_order_submission(self)
class OrderModel(Model):
# id generated by Jesse for database usage
id = UUIDField(primary_key=True)
trade_id = UUIDField(index=True, null=True)
session_id = UUIDField(index=True)

# id generated by market, used in live-trade mode
exchange_id = CharField(null=True)
# some exchanges might require even further info
vars = JSONField(default={})
symbol = CharField()
exchange = CharField()
side = CharField()
type = CharField()
reduce_only = BooleanField()
qty = FloatField()
filled_qty = FloatField(default=0)
price = FloatField(null=True)
status = CharField(default=order_statuses.ACTIVE)
created_at = BigIntegerField()
executed_at = BigIntegerField(null=True)
canceled_at = BigIntegerField(null=True)

# needed in Jesse, but no need to store in database(?)
submitted_via = None

class Meta:
from jesse.services.db import database

database = database.db
indexes = ((('trade_id', 'exchange', 'symbol', 'status', 'created_at'), False),)

def __init__(self, attributes: dict = None, should_silent=False, **kwargs) -> None:
Model.__init__(self, attributes=attributes, **kwargs)

if attributes is None:
attributes = {}

for a, value in attributes.items():
setattr(self, a, value)


def get_session_id():
from jesse.store import store
Expand All @@ -91,8 +70,8 @@ class Order:
exchange: str
side: str
type: str
reduce_only: bool
qty: float
reduce_only: bool = False
exchange_id: str | None = None
vars: dict = field(default_factory=dict)
filled_qty: float = 0.0
Expand Down Expand Up @@ -333,8 +312,10 @@ def execute_partially(self, silent=False) -> None:
if p:
p._on_executed_order(self)

def to_model(self) -> OrderModel:
return OrderModel(self.to_dict)


# if database is open, create the table
if database.is_open():
pass
# Order.create_table()
OrderModel.create_table()
2 changes: 1 addition & 1 deletion jesse/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .ClosedTrade import ClosedTrade
from .Exchange import Exchange
from .FuturesExchange import FuturesExchange
from .Order import Order
from .Order import Order, OrderModel
from .Position import Position
from .Route import Route
from .SpotExchange import SpotExchange
Expand Down
3 changes: 2 additions & 1 deletion jesse/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np

import jesse.helpers as jh
from jesse.models import OrderModel
from jesse.services import logger


Expand Down Expand Up @@ -171,7 +172,7 @@ def store_order_into_db(order) -> None:
}

def async_save() -> None:
Order.insert(**d).execute()
OrderModel.insert(**d).execute()
if jh.is_debugging():
logger.info(f'Stored the executed order record for {order.exchange}-{order.symbol} into database.')

Expand Down
20 changes: 10 additions & 10 deletions jesse/modes/backtest_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,16 +588,16 @@ def _check_for_liquidations(candle: np.ndarray, exchange: str, symbol: str) -> N
closing_order_side = jh.closing_side(p.type)

# create the market order that is used as the liquidation order
order = Order({
'id': jh.generate_unique_id(),
'symbol': symbol,
'exchange': exchange,
'side': closing_order_side,
'type': order_types.MARKET,
'reduce_only': True,
'qty': jh.prepare_qty(p.qty, closing_order_side),
'price': p.bankruptcy_price
})
order = Order(
id=jh.generate_unique_id(),
symbol=symbol,
exchange=exchange,
side=closing_order_side,
type=order_types.MARKET,
reduce_only=True,
qty=jh.prepare_qty(p.qty, closing_order_side),
price=p.bankruptcy_price
)

store.orders.add_order(order)

Expand Down
5 changes: 2 additions & 3 deletions jesse/services/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ def run():
_exchange_api_keys(migrator)

# create initial tables
from jesse.models import Candle, ClosedTrade, Log, Order, Option
# database.db.create_tables([Candle, ClosedTrade, Log, Order])
database.db.create_tables([Candle, ClosedTrade, Log])
from jesse.models import Candle, ClosedTrade, Log, OrderModel, Option
database.db.create_tables([Candle, ClosedTrade, Log, OrderModel])

database.close_connection()

Expand Down
Loading