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

all_or_nothing transactions #590

Open
wants to merge 5 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
47 changes: 42 additions & 5 deletions flumine/execution/transaction.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from collections import defaultdict
from typing import Optional

from ..order.orderpackage import OrderPackageType, BetfairOrderPackage
from ..events import events
Expand Down Expand Up @@ -28,9 +29,27 @@ class Transaction:
..
t.cancel_order(order)
t.place_order(order) # both executed on transaction __exit__

When all_or_nothing==True, orders within a transaction will
only be placed if the all pass validation.
For example, supposed we have:

with market.transaction() as t:
jsphon marked this conversation as resolved.
Show resolved Hide resolved
t.place_order(order1) # validation passes
t.place_order(order2) # validation does not pass and raises a ControlError

Neither order1 nor order2 will be executed, as order2 failed validation.

"""

def __init__(self, market, id_: int, async_place_orders: bool, client):
def __init__(
self,
market,
id_: int,
async_place_orders: bool,
client,
all_or_nothing: Optional[bool] = False,
jsphon marked this conversation as resolved.
Show resolved Hide resolved
):
self.market = market
self._client = client
self._id = id_ # unique per market only
Expand All @@ -40,6 +59,7 @@ def __init__(self, market, id_: int, async_place_orders: bool, client):
self._pending_cancel = [] # list of (<Order>, None)
self._pending_update = [] # list of (<Order>, None)
self._pending_replace = [] # list of (<Order>, market_version)
self.all_or_nothing = all_or_nothing

def place_order(
self,
Expand All @@ -52,6 +72,7 @@ def place_order(
if (
execute
and not force
and not self.all_or_nothing
and self._validate_controls(order, OrderPackageType.PLACE) is False
):
return False
Expand Down Expand Up @@ -94,6 +115,7 @@ def cancel_order(
)
if (
not force
and not self.all_or_nothing
and self._validate_controls(order, OrderPackageType.CANCEL) is False
):
return False
Expand All @@ -114,6 +136,7 @@ def update_order(
)
if (
not force
and not self.all_or_nothing
and self._validate_controls(order, OrderPackageType.UPDATE) is False
):
return False
Expand All @@ -134,6 +157,7 @@ def replace_order(
)
if (
not force
and not self.all_or_nothing
and self._validate_controls(order, OrderPackageType.REPLACE) is False
):
return False
Expand Down Expand Up @@ -185,15 +209,18 @@ def execute(self) -> int:
def _validate_controls(self, order, package_type: OrderPackageType) -> bool:
# return False on violation
try:
for control in self.market.flumine.trading_controls:
control(order, package_type)
for control in self._client.trading_controls:
control(order, package_type)
self._do_validate_controls(order, package_type)
except ControlError:
return False
else:
return True

def _do_validate_controls(self, order, package_type):
for control in self.market.flumine.trading_controls:
control(order, package_type)
for control in self._client.trading_controls:
control(order, package_type)

def _create_order_package(
self, orders: list, package_type: OrderPackageType, async_: bool = False
) -> list:
Expand Down Expand Up @@ -224,5 +251,15 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self.all_or_nothing:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to below ln264 to keep things quick

for order in self._pending_place:
self._do_validate_controls(order, OrderPackageType.PLACE)
jsphon marked this conversation as resolved.
Show resolved Hide resolved
for order in self._pending_cancel:
self._do_validate_controls(order, OrderPackageType.CANCEL)
for order in self._pending_update:
self._do_validate_controls(order, OrderPackageType.UPDATE)
for order in self._pending_replace:
self._do_validate_controls(order, OrderPackageType.REPLACE)

if self._pending_orders:
self.execute()
8 changes: 7 additions & 1 deletion flumine/markets/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ def close_market(self) -> None:
extra=self.info,
)

def transaction(self, async_place_orders: bool = None, client=None) -> Transaction:
def transaction(
self,
async_place_orders: bool = None,
client=None,
all_or_nothing: Optional[bool] = False,
) -> Transaction:
if async_place_orders is None:
async_place_orders = config.async_place_orders
if client is None:
Expand All @@ -73,6 +78,7 @@ def transaction(self, async_place_orders: bool = None, client=None) -> Transacti
id_=self._transaction_id,
async_place_orders=async_place_orders,
client=client,
all_or_nothing=all_or_nothing,
)

# order
Expand Down
2 changes: 2 additions & 0 deletions tests/test_markets.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_transaction(self, mock_transaction):
id_=self.market._transaction_id,
async_place_orders=False,
client=self.market.flumine.clients.get_default(),
all_or_nothing=False,
)
self.assertEqual(transaction, mock_transaction())

Expand All @@ -172,6 +173,7 @@ def test_transaction_async(self, mock_transaction):
id_=self.market._transaction_id,
async_place_orders=True,
client=self.market.flumine.clients.get_default(),
all_or_nothing=False,
)
self.assertEqual(transaction, mock_transaction())

Expand Down
66 changes: 66 additions & 0 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,72 @@ def test_place_order(
)
mock_order.update_client.assert_called_with(self.transaction._client)

@mock.patch("flumine.execution.transaction.get_market_notes")
@mock.patch(
"flumine.execution.transaction.Transaction._do_validate_controls",
return_value=True,
)
def test_place_order_with_all_or_nothing_success(
self,
mock__do_validate_controls,
mock_get_market_notes,
):
self.transaction.all_or_nothing = True
self.transaction.market.blotter = mock.MagicMock()
self.transaction.market.blotter.has_trade.return_value = False
self.transaction.execute = mock.Mock()

mock_order1 = mock.Mock(id="123", lookup=(1, 2, 3))
mock_order1.trade.market_notes = None

mock_order2 = mock.Mock(id="123", lookup=(1, 2, 3))
mock_order2.trade.market_notes = None

with self.transaction:
self.assertTrue(self.transaction.place_order(mock_order1))
self.assertTrue(self.transaction.place_order(mock_order2))
self.assertEqual(0, self.transaction.execute.call_count)

self.assertEqual(1, self.transaction.execute.call_count)

@mock.patch("flumine.execution.transaction.get_market_notes")
def test_place_order_with_all_or_nothing_fail(
self,
mock_get_market_notes,
):
c = 0

def side_effect(*args, **kwargs):
nonlocal c
if c == 0:
c = c + 1
return

raise ControlError("Intentional")

self.transaction._do_validate_controls = mock.Mock(side_effect=side_effect)

self.transaction.all_or_nothing = True
self.transaction.market.blotter = mock.MagicMock()
self.transaction.market.blotter.has_trade.return_value = False
self.transaction.execute = mock.Mock()

mock_order1 = mock.Mock(id="123", lookup=(1, 2, 3))
mock_order1.trade.market_notes = None

mock_order2 = mock.Mock(id="123", lookup=(1, 2, 3))
mock_order2.trade.market_notes = None

"""
The second mock order will raise a ControlError
"""
with self.assertRaises(ControlError):
with self.transaction:
self.assertTrue(self.transaction.place_order(mock_order1))
self.assertTrue(self.transaction.place_order(mock_order2))

self.assertEqual(0, self.transaction.execute.call_count)

@mock.patch("flumine.execution.transaction.get_market_notes")
@mock.patch(
"flumine.execution.transaction.Transaction._validate_controls",
Expand Down