betcode-org/flumine

Issue with replacing orders during market suspension

Closed this issue · 0 comments

class ExampleStrategy(BaseStrategy):
def start(self) -> None:
print("starting strategy 'ExampleStrategy'")

def process_market_book(self, market: Market, market_book: MarketBook) -> None:
    for i in range(len(market_book.runners)):
        runner = market_book.runners[i]

        LPT = runner.last_price_traded

        if runner.status == "ACTIVE" and (LPT is not None):
            if LPT < 2:
                trade = Trade(
                    market_id=market_book.market_id,
                    selection_id=runner.selection_id,
                    handicap=runner.handicap,
                    strategy=self
                )
                order = trade.create_order(
                    side="BACK",
                    order_type=LimitOrder(price=LPT, size=2.00)
                )

                res = market.place_order(order)

                if res:
                    print("Order placed: ", market_book.market_id, runner.selection_id, LPT)

def process_orders(self, market: Market, orders: list) -> None:
    if not((market.market_book.status == "OPEN") and (not market.market_book.inplay)):
        return

    for order in orders:
        if order.status == OrderStatus.EXECUTABLE:
            if order.size_remaining == 2.00:
                market.cancel_order(order, 0.02)  # reduce size to 1.98

            if order.order_type.persistence_type == "LAPSE":
                market.update_order(order, "PERSIST")

            if order.size_remaining > 0:
                market.replace_order(order, new_price=1.02)

I had run this strategy on a specific market, but then the following error was raised:
Traceback (most recent call last):
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/simulation/simulation.py", line 97, in run
self._process_market_books(
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/simulation/simulation.py", line 117, in _process_market_books
self._check_pending_packages(market_id)
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/simulation/simulation.py", line 189, in _check_pending_packages
order_package.client.execution.handler(order_package)
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/execution/simulatedexecution.py", line 31, in handler
func(order_package, http_session=None)
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/execution/simulatedexecution.py", line 142, in execute_replace
place_instruction_report = replacement_order.simulated.place(
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/simulation/simulatedorder.py", line 70, in place
return self._create_place_response(
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/simulation/simulatedorder.py", line 181, in _create_place_response
if self.size_remaining == 0:
File "/home/alessandro/venv/lib/python3.8/site-packages/flumine/simulation/simulatedorder.py", line 395, in size_remaining
size
TypeError: unsupported operand type(s) for -: 'NoneType' and @'int'

It seems that the execution is broken while the framework is trying to replace an order: first it tries to cancel the order, but then it discovers that the market.status is not OPEN and raise the error with

return SimulatedCancelResponse( status="FAILURE", error_code="ERROR_IN_ORDER", )

in flumine/simulation/simulatedorder.py, line 194. I don't understand why it is happening, because the check_market_book functions should already have checked that the market is OPEN. Also, in this specific case, the market is SUSPENDED before the race.

From conversation on slack, Liam suggested to look here

order.executable() # todo do not carry out replace
due to a replace occurring during market suspension.

Attached you can find a file to reproduce the error.
@liampauling

1.200016927.zip