Skip to content

Commit

Permalink
POC for atomic transaction, currently not violating other orders
Browse files Browse the repository at this point in the history
  • Loading branch information
liampauling committed Jan 7, 2022
1 parent 8353cbb commit d6b7273
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 11 additions & 2 deletions flumine/execution/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ class Transaction:
t.place_order(order) # both executed on transaction __exit__
"""

def __init__(self, market, id_: int, async_place_orders: bool):
def __init__(
self, market, id_: int, async_place_orders: bool, atomic: bool = False
):
self.market = market
self.atomic = atomic
self._id = id_ # unique per market only
self._async_place_orders = async_place_orders
self._pending_orders = False
self._pending_place = [] # list of (<Order>, market_version)
self._pending_cancel = [] # list of (<Order>, None)
self._pending_update = [] # list of (<Order>, None)
self._pending_replace = [] # list of (<Order>, market_version)
self._control_error = False

def place_order(
self,
Expand Down Expand Up @@ -169,6 +173,7 @@ def _validate_controls(self, order, package_type: OrderPackageType) -> bool:
for control in self.market.flumine.client.trading_controls:
control(order, package_type)
except ControlError:
self._control_error = True
return False
else:
return True
Expand Down Expand Up @@ -200,8 +205,12 @@ def _create_order_package(
return packages

def __enter__(self):
self._control_error = False # reset
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self._pending_orders:
self.execute()
if self.atomic and self._control_error:
logger.error("Transaction not executed due to control exception")
else:
self.execute()
9 changes: 7 additions & 2 deletions flumine/markets/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,17 @@ def close_market(self) -> None:
extra=self.info,
)

def transaction(self, async_place_orders: bool = None) -> Transaction:
def transaction(
self, async_place_orders: bool = None, atomic: bool = False
) -> Transaction:
if async_place_orders is None:
async_place_orders = config.async_place_orders
self._transaction_id += 1
return Transaction(
self, id_=self._transaction_id, async_place_orders=async_place_orders
self,
id_=self._transaction_id,
async_place_orders=async_place_orders,
atomic=atomic,
)

# order
Expand Down

0 comments on commit d6b7273

Please sign in to comment.