From d6b7273e2156dd6c67acac356e421e5aba5c3a12 Mon Sep 17 00:00:00 2001 From: liampauling Date: Fri, 7 Jan 2022 08:24:52 +0000 Subject: [PATCH] POC for atomic transaction, currently not violating other orders #542 --- flumine/execution/transaction.py | 13 +++++++++++-- flumine/markets/market.py | 9 +++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/flumine/execution/transaction.py b/flumine/execution/transaction.py index 57f6f114..2ce6fabe 100644 --- a/flumine/execution/transaction.py +++ b/flumine/execution/transaction.py @@ -30,8 +30,11 @@ 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 @@ -39,6 +42,7 @@ def __init__(self, market, id_: int, async_place_orders: bool): self._pending_cancel = [] # list of (, None) self._pending_update = [] # list of (, None) self._pending_replace = [] # list of (, market_version) + self._control_error = False def place_order( self, @@ -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 @@ -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() diff --git a/flumine/markets/market.py b/flumine/markets/market.py index 949603ac..33767872 100644 --- a/flumine/markets/market.py +++ b/flumine/markets/market.py @@ -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