Skip to content

POC for atomic transaction, currently not violating other orders #547

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
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