def test_order_state_is_new_after_update_exchange_order_id(self): order = CoinzoomInFlightOrder(client_order_id="OID1", exchange_order_id=None, trading_pair="BTC-USDT", order_type=OrderType.LIMIT, trade_type=TradeType.BUY, price=Decimal(45000), amount=Decimal(1)) order.update_exchange_order_id("EOID1") self.assertEqual("EOID1", order.exchange_order_id) self.assertFalse(order.is_local) self.assertFalse(order.is_done) self.assertFalse(order.is_failure) self.assertFalse(order.is_cancelled)
def restore_tracking_states(self, saved_states: Dict[str, any]): """ Restore in-flight orders from saved tracking states, this is st the connector can pick up on where it left off when it disconnects. :param saved_states: The saved tracking_states. """ self._in_flight_orders.update({ key: CoinzoomInFlightOrder.from_json(value) for key, value in saved_states.items() })
def test_order_is_local_after_creation(self): order = CoinzoomInFlightOrder(client_order_id="OID1", exchange_order_id="EOID1", trading_pair="BTC-USDT", order_type=OrderType.LIMIT, trade_type=TradeType.BUY, price=Decimal(45000), amount=Decimal(1)) self.assertTrue(order.is_local)
def start_tracking_order(self, order_id: str, exchange_order_id: str, trading_pair: str, trade_type: TradeType, price: Decimal, amount: Decimal, order_type: OrderType): """ Starts tracking an order by simply adding it into _in_flight_orders dictionary. """ self._in_flight_orders[order_id] = CoinzoomInFlightOrder( client_order_id=order_id, exchange_order_id=exchange_order_id, trading_pair=trading_pair, order_type=order_type, trade_type=trade_type, price=price, amount=amount)
async def _trigger_order_fill(self, tracked_order: CoinzoomInFlightOrder, update_msg: Dict[str, Any]): self.trigger_event( MarketEvent.OrderFilled, OrderFilledEvent( self.current_timestamp, tracked_order.client_order_id, tracked_order.trading_pair, tracked_order.trade_type, tracked_order.order_type, Decimal( str( update_msg.get("averagePrice", update_msg.get("price", "0")))), tracked_order.executed_amount_base, AddedToCostTradeFee(percent=update_msg["trade_fee"]), update_msg.get("exchange_trade_id", update_msg.get("id", update_msg.get("orderId"))))) if math.isclose(tracked_order.executed_amount_base, tracked_order.amount) or \ tracked_order.executed_amount_base >= tracked_order.amount or \ tracked_order.is_done: tracked_order.last_state = "FILLED" self.logger().info( f"The {tracked_order.trade_type.name} order " f"{tracked_order.client_order_id} has completed " f"according to order status API.") event_tag = MarketEvent.BuyOrderCompleted if tracked_order.trade_type is TradeType.BUY \ else MarketEvent.SellOrderCompleted event_class = BuyOrderCompletedEvent if tracked_order.trade_type is TradeType.BUY \ else SellOrderCompletedEvent await asyncio.sleep(0.1) self.trigger_event( event_tag, event_class(self.current_timestamp, tracked_order.client_order_id, tracked_order.base_asset, tracked_order.quote_asset, tracked_order.executed_amount_base, tracked_order.executed_amount_quote, tracked_order.order_type)) self.stop_tracking_order(tracked_order.client_order_id)