def test_full_fill_on_unacked_cr_with_acked_new_order():
    n = NewOrderCommand(1, 1234235.123, 2342, "user_x", MARKET, BID_SIDE, FAR, Price("34.52"), 1000)
    oec = OrderEventChain(n, LOGGER, MonotonicIntID())
    # now ack it
    ack = AcknowledgementReport(2, 1234235.123, 2342, "user_x", MARKET, n, Price("34.52"), 1000, 1000)
    oec.apply_acknowledgement_report(ack)
    assert oec.visible_qty() == 1000
    assert oec.current_exposure().qty() == 1000
    assert oec.current_exposure().price() == Price("34.52")
    assert len(oec.open_exposure_requests()) == 0
    assert oec.is_open()

    cr = CancelReplaceCommand(3, 1234236.842, 2342, "user_x", MARKET, BID_SIDE, Price("34.56"), 800)
    oec.apply_cancel_replace_command(cr)
    # now should have 2 open exposures
    assert oec.visible_qty() == 1000
    assert oec.current_exposure().qty() == 1000
    assert oec.current_exposure().price() == Price("34.52")
    assert oec.is_open()
    assert len(oec.open_exposure_requests()) == 1
    assert oec.most_recent_requested_exposure() == Exposure(Price("34.56"), 800, 3)

    full_fill = FullFillReport(4, 1234237.123, 2342, "user_x", MARKET, cr, 800, Price("34.56"), BID_SIDE, 12345)
    oec.apply_full_fill_report(full_fill)
    assert oec.visible_qty() == 0
    assert oec.current_exposure().price() is None
    assert oec.current_exposure().qty() == 0
    assert oec.current_exposure().causing_event_id() == 4
    assert len(oec.open_exposure_requests()) == 0
    assert oec.is_open() is False
def test_close_exposure_cancel_closes_all():
    id_gen = MonotonicIntID(seed=23043, increment=1)
    n = NewOrderCommand(id_gen.id(), 1234235.123, 2342, "user_x", MARKET, BID_SIDE, FAR, Price("34.52"), 1000)
    oec = OrderEventChain(n, LOGGER, MonotonicIntID())
    # should have 1 open exposure
    assert len(oec.open_exposure_requests()) == 1
    assert oec.most_recent_requested_exposure() == Exposure(Price("34.52"), 1000, id_gen.last_id())

    cr = CancelReplaceCommand(id_gen.id(), 1234235.863, 2342, "user_x", MARKET, BID_SIDE, Price("34.51"), 800)
    oec.apply_cancel_replace_command(cr)
    # now should have 2 open exposures
    assert len(oec.open_exposure_requests()) == 2
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, id_gen.last_id())
    assert oec.most_recent_requested_exposure() == Exposure(Price("34.51"), 800, id_gen.last_id())

    cancel_command = CancelCommand(id_gen.id(), 1234274.663, 2342, "user_x", MARKET, CancelReasons.USER_CANCEL)
    oec.apply_cancel_command(cancel_command)
    # now should have 3 open exposures
    assert len(oec.open_exposure_requests()) == 3
    assert oec.open_exposure_requests()[2] == Exposure(None, 0, id_gen.last_id())
    assert oec.most_recent_requested_exposure() == Exposure(None, 0, id_gen.last_id())

    cancel_confirm = CancelReport(id_gen.id(), 1234278.663, 2342, "user_x", MARKET, cancel_command, CancelReasons.USER_CANCEL)
    oec.apply_cancel_report(cancel_confirm)
    # all exposures should be closed now
    assert len(oec.open_exposure_requests()) == 0
    assert oec.most_recent_requested_exposure() is None
    assert oec.has_partial_fill() is False
def test_cancel_replace_not_allowed_on_fak_or_fok():
    # FAK
    n = NewOrderCommand(121234, 1234235.123, 2342, "user_x", MARKET, BID_SIDE, FAK, Price("34.52"), 1000)
    oec = OrderEventChain(n, LOGGER, MonotonicIntID())
    cr = CancelReplaceCommand(121235, 1234235.324, 2342, "user_x", MARKET, BID_SIDE, Price("43.01"), 234)
    with pytest.raises(AssertionError):
        oec.apply_cancel_replace_command(cr)
    # FOK
    n = NewOrderCommand(121234, 1234235.123, 2342, "user_x", MARKET, BID_SIDE, FOK, Price("34.52"), 1000)
    oec = OrderEventChain(n, LOGGER, MonotonicIntID())
    cr = CancelReplaceCommand(121235, 1234235.324, 2342, "user_x", MARKET, BID_SIDE, Price("43.01"), 234)
    with pytest.raises(AssertionError):
        oec.apply_cancel_replace_command(cr)
def test_partial_fill_on_multiple_unacked_requests():
    n = NewOrderCommand(1, 1234235.123, 2342, "user_x", MARKET, BID_SIDE, FAR, Price("34.52"), 1000)
    oec = OrderEventChain(n, LOGGER, MonotonicIntID())
    # should have 1 open exposure
    assert len(oec.open_exposure_requests()) == 1
    assert oec.most_recent_requested_exposure() == Exposure(Price("34.52"), 1000, 1)

    cr1 = CancelReplaceCommand(2, 1234235.863, 2342, "user_x", MARKET, BID_SIDE, Price("34.51"), 800)
    oec.apply_cancel_replace_command(cr1)
    # now should have 2 open exposures
    assert len(oec.open_exposure_requests()) == 2
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.52"), 1000, 1)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, 2)

    cr2 = CancelReplaceCommand(3, 1234236.842, 2342, "user_x", MARKET, BID_SIDE, Price("34.55"), 800)
    oec.apply_cancel_replace_command(cr2)
    # now should have 2 open exposures
    assert len(oec.open_exposure_requests()) == 3
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.52"), 1000, 1)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, 2)
    assert oec.open_exposure_requests()[2] == Exposure(Price("34.55"), 800, 3)

    cr3 = CancelReplaceCommand(4, 1234236.842, 2342, "user_x", MARKET, BID_SIDE, Price("34.56"), 800)
    oec.apply_cancel_replace_command(cr3)
    # now should have 2 open exposures
    assert len(oec.open_exposure_requests()) == 4
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.52"), 1000, 1)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, 2)
    assert oec.open_exposure_requests()[2] == Exposure(Price("34.55"), 800, 3)
    assert oec.open_exposure_requests()[3] == Exposure(Price("34.56"), 800, 4)

    # a partial fill should should only impact the one the partial fill is for
    # partially filling orderid 3 (cr2)
    pf1 = PartialFillReport(5, 1234237.123, 2342, "user_x", MARKET, cr2, 10, Price("34.55"),
                            BID_SIDE, 999, 790)
    oec.apply_partial_fill_report(pf1)
    assert len(oec.open_exposure_requests()) == 4
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.52"), 1000, 1)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, 2)
    assert oec.open_exposure_requests()[2] == Exposure(Price("34.55"), 790, 3)
    assert oec.open_exposure_requests()[3] == Exposure(Price("34.56"), 800, 4)

    # and again
    pf2 = PartialFillReport(6, 1234237.123, 2342, "user_x", MARKET, cr2, 10, Price("34.55"),
                            BID_SIDE, 1000, 780)
    oec.apply_partial_fill_report(pf2)
    assert len(oec.open_exposure_requests()) == 4
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.52"), 1000, 1)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, 2)
    assert oec.open_exposure_requests()[2] == Exposure(Price("34.55"), 780, 3)
    assert oec.open_exposure_requests()[3] == Exposure(Price("34.56"), 800, 4)

    # and now I can fill order id 4 (cr 3)
    pf3 = PartialFillReport(6, 1234237.123, 2342, "user_x", MARKET, cr3, 50, Price("34.56"),
                            BID_SIDE, 1001, 750)
    oec.apply_partial_fill_report(pf3)
    assert len(oec.open_exposure_requests()) == 4
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.52"), 1000, 1)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.51"), 800, 2)
    assert oec.open_exposure_requests()[2] == Exposure(Price("34.55"), 780, 3)
    assert oec.open_exposure_requests()[3] == Exposure(Price("34.56"), 750, 4)

    # now start acking them
    ack1 = AcknowledgementReport(10, 1234235.123, 2342, "user_x", MARKET, n, Price("34.52"), 1000, None)
    oec.apply_acknowledgement_report(ack1)
    assert len(oec.open_exposure_requests()) == 3
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.51"), 800, 2)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.55"), 780, 3)
    assert oec.open_exposure_requests()[2] == Exposure(Price("34.56"), 750, 4)
    assert oec.current_exposure() == Exposure(Price("34.52"), 1000, 10)

    ack2 = AcknowledgementReport(11, 1234235.123, 2342, "user_x", MARKET, cr1, Price("34.51"), 800, None)
    oec.apply_acknowledgement_report(ack2)
    assert len(oec.open_exposure_requests()) == 2
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.55"), 780, 3)
    assert oec.open_exposure_requests()[1] == Exposure(Price("34.56"), 750, 4)
    assert oec.current_exposure() == Exposure(Price("34.51"), 800, 11)

    ack3 = AcknowledgementReport(12, 1234235.123, 2342, "user_x", MARKET, cr2, Price("34.55"), 780, None)
    oec.apply_acknowledgement_report(ack3)
    assert len(oec.open_exposure_requests()) == 1
    print(oec.open_exposure_requests()[0])
    assert oec.open_exposure_requests()[0] == Exposure(Price("34.56"), 750, 4)
    assert oec.current_exposure() == Exposure(Price("34.55"), 780, 12)

    ack4 = AcknowledgementReport(13, 1234235.123, 2342, "user_x", MARKET, cr3, Price("34.56"), 750, None)
    oec.apply_acknowledgement_report(ack4)
    assert len(oec.open_exposure_requests()) == 0
    assert oec.current_exposure() == Exposure(Price("34.56"), 750, 13)