def test_completed_order_recovered_from_json_has_completed_event_updated(self): order_json = { "client_order_id": self.client_order_id, "exchange_order_id": self.exchange_order_id, "trading_pair": self.trading_pair, "order_type": OrderType.LIMIT.name, "trade_type": TradeType.BUY.name, "price": "1.0", "amount": "1000.0", "executed_amount_base": "1000.0", "executed_amount_quote": "1100.0", "fee_asset": None, "fee_paid": "0", "last_state": "0", "leverage": "1", "position": "NIL", "creation_timestamp": 1640001112.0, } expected_order: InFlightOrder = InFlightOrder( client_order_id=self.client_order_id, exchange_order_id=self.exchange_order_id, trading_pair=self.trading_pair, order_type=OrderType.LIMIT, trade_type=TradeType.BUY, amount=Decimal("1000.0"), creation_timestamp=1640001112.0, price=Decimal("1.0"), ) expected_order.executed_amount_base = Decimal("1000") expected_order.executed_amount_quote = Decimal("1100") order_from_json = InFlightOrder.from_json(order_json) self.assertEqual(expected_order, order_from_json) self.assertTrue(order_from_json.completely_filled_event.is_set())
def test_from_json_does_not_fail_when_order_fills_not_present(self): order_json = { "client_order_id": self.client_order_id, "exchange_order_id": self.exchange_order_id, "trading_pair": self.trading_pair, "order_type": OrderType.LIMIT.name, "trade_type": TradeType.BUY.name, "price": "1.0", "amount": "1000.0", "executed_amount_base": "0", "executed_amount_quote": "0", "fee_asset": None, "fee_paid": "0", "last_state": "0", "leverage": "1", "position": PositionAction.NIL.value, "creation_timestamp": 1640001112 } expected_order: InFlightOrder = InFlightOrder( client_order_id=self.client_order_id, exchange_order_id=self.exchange_order_id, trading_pair=self.trading_pair, order_type=OrderType.LIMIT, trade_type=TradeType.BUY, amount=Decimal("1000.0"), creation_timestamp=1640001112.0, price=Decimal("1.0"), ) order_from_json = InFlightOrder.from_json(order_json) self.assertEqual(expected_order, order_from_json) self.assertFalse(order_from_json.completely_filled_event.is_set())
def test_from_json(self): order_json = { "client_order_id": self.client_order_id, "exchange_order_id": self.exchange_order_id, "trading_pair": self.trading_pair, "order_type": OrderType.LIMIT.name, "trade_type": TradeType.BUY.name, "price": "1.0", "amount": "1000.0", "executed_amount_base": "0", "executed_amount_quote": "0", "fee_asset": None, "fee_paid": "0", "last_state": "0", "leverage": "1", "position": PositionAction.NIL.value, } expected_order: InFlightOrder = InFlightOrder( client_order_id=self.client_order_id, exchange_order_id=self.exchange_order_id, trading_pair=self.trading_pair, order_type=OrderType.LIMIT, trade_type=TradeType.BUY, amount=Decimal("1000.0"), price=Decimal("1.0"), ) order_from_json = InFlightOrder.from_json(order_json) self.assertEqual(expected_order, order_from_json)
def restore_tracking_states(self, tracking_states: Dict[str, any]): """ Restore in-flight orders from saved tracking states. :param tracking_states: a dictionary associating order ids with the serialized order (JSON format). """ for serialized_order in tracking_states.values(): order = InFlightOrder.from_json(serialized_order) if order.is_open: self.start_tracking_order(order)
def test_from_json(self): fee = AddedToCostTradeFee( percent=Decimal("0.5"), percent_token=self.quote_asset ) trade_update = TradeUpdate( trade_id="12345", client_order_id=self.client_order_id, exchange_order_id="EOID1", trading_pair=self.trading_pair, fill_timestamp=1640001112, fill_price=Decimal("1000.11"), fill_base_amount=Decimal("2"), fill_quote_amount=Decimal("2000.22"), fee=fee, ) order_json = { "client_order_id": self.client_order_id, "exchange_order_id": self.exchange_order_id, "trading_pair": self.trading_pair, "order_type": OrderType.LIMIT.name, "trade_type": TradeType.BUY.name, "price": "1.0", "amount": "1000.0", "executed_amount_base": "0", "executed_amount_quote": "0", "fee_asset": None, "fee_paid": "0", "last_state": "0", "leverage": "1", "position": "NIL", "creation_timestamp": 1640001112.0, "order_fills": {"1": trade_update.to_json()} } expected_order: InFlightOrder = InFlightOrder( client_order_id=self.client_order_id, exchange_order_id=self.exchange_order_id, trading_pair=self.trading_pair, order_type=OrderType.LIMIT, trade_type=TradeType.BUY, amount=Decimal("1000.0"), creation_timestamp=1640001112.0, price=Decimal("1.0"), ) order_from_json = InFlightOrder.from_json(order_json) self.assertEqual(expected_order, order_from_json) self.assertFalse(order_from_json.completely_filled_event.is_set()) self.assertIn("1", order_from_json.order_fills) self.assertEqual(trade_update, order_from_json.order_fills["1"])