def test_order_initialized_event_to_from_dict_and_str_repr(self): # Arrange uuid = UUID4() event = OrderInitialized( trader_id=TraderId("TRADER-001"), strategy_id=StrategyId("SCALPER-001"), instrument_id=InstrumentId(Symbol("BTC/USDT"), Venue("BINANCE")), client_order_id=ClientOrderId("O-2020872378423"), order_side=OrderSide.BUY, order_type=OrderType.LIMIT, quantity=Quantity.from_str("0.561000"), time_in_force=TimeInForce.DAY, reduce_only=True, options={"price": "15200.10"}, order_list_id=OrderListId("1"), parent_order_id=None, child_order_ids=[ClientOrderId("O-2020872378424")], contingency=ContingencyType.OTO, contingency_ids=[ClientOrderId("O-2020872378424")], tags="ENTRY", event_id=uuid, ts_init=0, ) # Act, Assert assert OrderInitialized.from_dict( OrderInitialized.to_dict(event)) == event assert ( str(event) == f"OrderInitialized(instrument_id=BTC/USDT.BINANCE, client_order_id=O-2020872378423, side=BUY, type=LIMIT, quantity=0.561000, time_in_force=DAY, reduce_only=True, options={{'price': '15200.10'}}, order_list_id=1, parent_order_id=None, child_order_ids=['O-2020872378424'], contingency=OTO, contingency_ids=['O-2020872378424'], tags=ENTRY)" # noqa ) assert ( repr(event) == f"OrderInitialized(trader_id=TRADER-001, strategy_id=SCALPER-001, instrument_id=BTC/USDT.BINANCE, client_order_id=O-2020872378423, side=BUY, type=LIMIT, quantity=0.561000, time_in_force=DAY, reduce_only=True, options={{'price': '15200.10'}}, order_list_id=1, parent_order_id=None, child_order_ids=['O-2020872378424'], contingency=OTO, contingency_ids=['O-2020872378424'], tags=ENTRY, event_id={uuid}, ts_init=0)" # noqa )
def test_serialize_and_deserialize_stop_market_order_initialized_events(self): # Arrange options = { "trigger_price": "1.0005", } event = OrderInitialized( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.SELL, OrderType.STOP_MARKET, Quantity(100000, precision=0), TimeInForce.DAY, post_only=False, reduce_only=True, options=options, order_list_id=OrderListId("1"), contingency_type=ContingencyType.OTO, linked_order_ids=[ClientOrderId("O-123457"), ClientOrderId("O-123458")], parent_order_id=ClientOrderId("O-123455"), tags=None, event_id=UUID4(), ts_init=0, ) # Act serialized = self.serializer.serialize(event) deserialized = self.serializer.deserialize(serialized) # Assert assert deserialized == event assert deserialized.options == options
def test_pack_and_unpack_trailing_stop_limit_orders_with_no_initial_prices(self): # Arrange order = TrailingStopLimitOrder( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.BUY, Quantity(100000, precision=0), price=None, trigger_price=None, trigger_type=TriggerType.MARK, limit_offset=Decimal("50"), trailing_offset=Decimal("50"), offset_type=TrailingOffsetType.TICKS, time_in_force=TimeInForce.GTD, expire_time=UNIX_EPOCH + timedelta(minutes=1), init_id=UUID4(), ts_init=0, ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_pack_and_unpack_trailing_stop_market_orders_with_expiration(self): # Arrange order = TrailingStopMarketOrder( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.BUY, Quantity(100000, precision=0), trigger_price=Price(1.00000, precision=5), trigger_type=TriggerType.DEFAULT, trailing_offset=Decimal("0.00010"), offset_type=TrailingOffsetType.PRICE, time_in_force=TimeInForce.GTD, expire_time=UNIX_EPOCH + timedelta(minutes=1), init_id=UUID4(), ts_init=0, ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_serialize_and_deserialize_market_order_initialized_events(self): # Arrange event = OrderInitialized( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.SELL, OrderType.MARKET, Quantity(100000, precision=0), TimeInForce.FOK, reduce_only=True, options={}, order_list_id=OrderListId("1"), parent_order_id=ClientOrderId("O-123455"), child_order_ids=[ ClientOrderId("O-123457"), ClientOrderId("O-123458") ], contingency=ContingencyType.OTO, contingency_ids=[ ClientOrderId("O-123457"), ClientOrderId("O-123458") ], tags="ENTRY", event_id=UUID4(), ts_init=0, ) # Act serialized = self.serializer.serialize(event) deserialized = self.serializer.deserialize(serialized) # Assert assert deserialized == event
def deserialize_order_initialised(data: Dict) -> OrderInitialized: for k in ("price", "quantity"): data[k] = str(data[k]) options_fields = orjson.loads( NAUTILUS_PARQUET_SCHEMA[OrderInitialized].metadata[b"options_fields"]) data["options"] = orjson.dumps( {k: data.pop(k, None) for k in options_fields}) return OrderInitialized.from_dict(data)
def serialize_order_initialized(event: OrderInitialized): caster = { "quantity": float, "price": float, } data = event.to_dict(event) data.update(orjson.loads(data.pop("options", "{}"))) # noqa: P103 data = { k: caster[k](v) if (k in caster and v is not None) else v for k, v in data.items() } return data
def test_pack_and_unpack_market_orders(self): # Arrange order = self.order_factory.market( AUDUSD_SIM.id, OrderSide.BUY, Quantity(100000, precision=0), ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_pack_and_unpack_limit_orders(self): # Arrange order = self.order_factory.limit( AUDUSD_SIM.id, OrderSide.BUY, Quantity(100000, precision=0), Price(1.00000, precision=5), TimeInForce.DAY, display_qty=Quantity(50000, precision=0), ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_serialize_and_deserialize_stop_limit_order_initialized_events( self): # Arrange options = { "ExpireTime": None, "Price": "1.0005", "Trigger": "1.0010", "PostOnly": True, "Hidden": False, } event = OrderInitialized( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.SELL, OrderType.STOP_LIMIT, Quantity(100000, precision=0), TimeInForce.DAY, reduce_only=True, options=options, order_list_id=OrderListId("1"), parent_order_id=ClientOrderId("O-123455"), child_order_ids=[ ClientOrderId("O-123457"), ClientOrderId("O-123458") ], contingency=ContingencyType.OTO, contingency_ids=[ ClientOrderId("O-123457"), ClientOrderId("O-123458") ], tags="entry,bulk", event_id=UUID4(), ts_init=0, ) # Act serialized = self.serializer.serialize(event) deserialized = self.serializer.deserialize(serialized) # Assert assert deserialized == event assert deserialized.options == options assert deserialized.tags == "entry,bulk"
def test_pack_and_unpack_market_to_limit__orders(self): # Arrange order = MarketToLimitOrder( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.BUY, Quantity(100000, precision=0), time_in_force=TimeInForce.GTD, # <-- invalid expire_time=UNIX_EPOCH + timedelta(minutes=1), init_id=UUID4(), ts_init=0, ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_pack_and_unpack_stop_market_orders_with_expire_time(self): # Arrange order = StopMarketOrder( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.BUY, Quantity(100000, precision=0), price=Price(1.00000, precision=5), time_in_force=TimeInForce.GTD, expire_time=UNIX_EPOCH, init_id=UUID4(), ts_init=0, ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_pack_and_unpack_market_if_touched_orders(self): # Arrange order = MarketIfTouchedOrder( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.BUY, Quantity(100000, precision=0), trigger_price=Price(1.00000, precision=5), trigger_type=TriggerType.DEFAULT, time_in_force=TimeInForce.GTD, expire_time=UNIX_EPOCH + timedelta(minutes=1), init_id=UUID4(), ts_init=0, ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
def test_pack_and_unpack_limit_if_touched_orders(self): # Arrange order = LimitIfTouchedOrder( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.BUY, Quantity(100000, precision=0), price=Price(1.00000, precision=5), trigger_price=Price(1.00010, precision=5), trigger_type=TriggerType.BID_ASK, time_in_force=TimeInForce.GTC, expire_time=None, init_id=UUID4(), ts_init=0, ) # Act packed = OrderInitialized.to_dict(order.last_event) unpacked = self.unpacker.unpack(packed) # Assert assert unpacked == order
# Arrange options = { "expire_time_ns": 1_000_000_000, "price": "1.0010", } event = OrderInitialized( self.trader_id, self.strategy_id, AUDUSD_SIM.id, ClientOrderId("O-123456"), OrderSide.SELL, OrderType.LIMIT, Quantity(100000, precision=0), TimeInForce.DAY, post_only=True, reduce_only=False, options=options, order_list_id=OrderListId("1"), contingency_type=ContingencyType.OTO, linked_order_ids=[ClientOrderId("O-123457"), ClientOrderId("O-123458")], parent_order_id=ClientOrderId("O-123455"), tags=None, event_id=UUID4(), ts_init=0, ) # Act serialized = self.serializer.serialize(event) deserialized = self.serializer.deserialize(serialized) # Assert