Moved not_quoting to model_state (where external components and orderchain elements can both access it). Added a state bag to model_state too.

This commit is contained in:
Geoff Taylor 2021-08-26 09:19:51 +01:00
parent d888fe010c
commit b89a072cdd
3 changed files with 18 additions and 9 deletions

View File

@ -50,16 +50,22 @@ class MarketMaker:
self.buy_client_ids: typing.List[int] = []
self.sell_client_ids: typing.List[int] = []
self.not_quoting: bool = False
def pulse(self, context: mango.Context, model_state: ModelState):
if self.not_quoting:
return True
try:
payer = mango.CombinableInstructions.from_wallet(self.wallet)
desired_orders = self.desired_orders_chain.process(context, model_state)
# This is here to give the orderchain the chance to look at state and set `not_quoting`. Any
# element in the orderchain can set this, rather than just return an empty list of desired
# orders, knowing it won't be accidentally changed by subsequent elements returning orders.
#
# It also gives the opportunity to code outside the orderchain to set `not_quoting` if that
# code has access to the `model_state`.
if model_state.not_quoting:
self.logger.info(f"[{context.name}] Market-maker not quoting - model_state.not_quoting is set.")
return
existing_orders = self.order_tracker.existing_orders(model_state)
reconciled = self.order_reconciler.reconcile(model_state, existing_orders, desired_orders)

View File

@ -47,6 +47,9 @@ class ModelState:
self.bids_watcher: mango.Watcher[typing.Sequence[mango.Order]] = bids
self.asks_watcher: mango.Watcher[typing.Sequence[mango.Order]] = asks
self.not_quoting: bool = False
self.state: typing.Dict[str, typing.Any] = {}
@property
def group(self) -> mango.Group:
return self.group_watcher.latest

View File

@ -33,9 +33,9 @@ class PreventPostOnlyCrossingBookElement(Element):
new_orders: typing.List[mango.Order] = []
for order in orders:
if order.order_type == mango.OrderType.POST_ONLY:
top_bid = model_state.top_bid
top_ask = model_state.top_ask
if order.side == mango.Side.BUY and top_ask is not None and order.price >= top_ask.price:
top_bid: typing.Optional[mango.Order] = model_state.top_bid
top_ask: typing.Optional[mango.Order] = model_state.top_ask
if order.side == mango.Side.BUY and top_bid is not None and top_ask is not None and order.price >= top_ask.price:
new_buy_price: Decimal = top_bid.price - model_state.market.lot_size_converter.tick_size
new_buy: mango.Order = order.with_price(new_buy_price)
self.logger.debug(
@ -43,7 +43,7 @@ class PreventPostOnlyCrossingBookElement(Element):
Old: {order}
New: {new_buy}""")
new_orders += [new_buy]
elif order.side == mango.Side.SELL and top_bid is not None and order.price <= top_bid.price:
elif order.side == mango.Side.SELL and top_bid is not None and top_ask is not None and order.price <= top_bid.price:
new_sell_price: Decimal = top_ask.price + model_state.market.lot_size_converter.tick_size
new_sell: mango.Order = order.with_price(new_sell_price)
self.logger.debug(