malkhut(wire): OrderType as three orthogonal dimensions — Fable's corrections
CRITICAL REFACTOR based on Fable's review (S9 roadmap item):
Before: flat enum conflating order types with TIF/instructions
OrderType had MARKET, LIMIT, IOC, FOK, POST_ONLY, REDUCE_ONLY, etc.
After: three orthogonal dimensions (FIX-aligned):
1. OrderType (Tag 40): what the order IS
LIMIT, MARKET, STOP_MARKET, STOP_LIMIT, TRIGGER_MARKET, TRIGGER_LIMIT,
TRAILING_STOP, OCO, TP_SL
2. TimeInForce (Tag 59): how long it LIVES
GTC, IOC, FOK, GTD
3. Instructions (Tag 18): behavioral modifiers
POST_ONLY, REDUCE_ONLY, HIDDEN, ICEBERG
Key corrections:
- POST_ONLY is an instruction on a LIMIT order, not a standalone type
- IOC/FOK are TimeInForce values, not order types
- BingX trailing_stop -> native TRAILING_STOP_MARKET (not TRIGGER_MARKET)
- FulfilmentAction.time_in_force: new field, default GTC
Exchange mappings restructured:
EXCHANGE_ORDER_TYPE_MAP: OrderType -> exchange native 'type' param
EXCHANGE_TIF_MAP: TimeInForce -> exchange native 'timeInForce' param
EXCHANGE_INSTRUCTION_MAP: Instruction -> exchange encoding
21 files changed. 380+ tests pass. Backward compatible.
This commit is contained in:
@@ -41,7 +41,7 @@ class TestPostOnlyRejection:
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
# price = best_bid - (-10)*tick = 50000 + 1.0 = 50001.0 = best_ask
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, -10, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, -10, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.account.equity == s.account.equity
|
||||
|
||||
@@ -49,14 +49,14 @@ class TestPostOnlyRejection:
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
# price = best_ask + (-10)*tick = 50001 - 1.0 = 50000.0 = best_bid
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.POST_ONLY, -10, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.LIMIT, -10, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.account.equity == s.account.equity
|
||||
|
||||
def test_buy_inside_spread_accepted(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
# Should be in open orders (passive placement)
|
||||
assert any(o.side == Side.BUY for o in r.open_orders)
|
||||
@@ -64,7 +64,7 @@ class TestPostOnlyRejection:
|
||||
def test_sell_inside_spread_accepted(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.POST_ONLY, 0, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.LIMIT, 0, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert any(o.side == Side.SELL for o in r.open_orders)
|
||||
|
||||
@@ -73,21 +73,21 @@ class TestCrossSpread:
|
||||
def test_cross_buy_fills_at_best_ask(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.1, 50)
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.account.equity < s.account.equity # fees paid
|
||||
|
||||
def test_cross_sell_fills_at_best_bid(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.SELL, OrderType.IOC, 0, 0.1, 50)
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.SELL, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.account.equity <= s.account.equity
|
||||
|
||||
def test_cross_spread_taker_fee_applied(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.1, 50)
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r = cwm.transition(s, (a,))
|
||||
fee = s.venue.taker_fee_bps
|
||||
assert fee > 0
|
||||
@@ -97,7 +97,7 @@ class TestCancelOrder:
|
||||
def test_cancel_removes_order(self):
|
||||
oo = OpenOrderState(
|
||||
client_order_id="c1", venue_order_id="v1", symbol="BTCUSDT",
|
||||
side=Side.BUY, order_type=OrderType.POST_ONLY, price=50000.0,
|
||||
side=Side.BUY, order_type=OrderType.LIMIT, price=50000.0,
|
||||
qty=0.001, remaining_qty=0.001, queue_ahead_estimate=0.001,
|
||||
created_ts_ns=1_000_000_000, last_update_ts_ns=1_000_000_000,
|
||||
post_only=True,
|
||||
@@ -111,7 +111,7 @@ class TestCancelOrder:
|
||||
def test_cancel_wrong_id_keeps_order(self):
|
||||
oo = OpenOrderState(
|
||||
client_order_id="c1", venue_order_id="v1", symbol="BTCUSDT",
|
||||
side=Side.BUY, order_type=OrderType.POST_ONLY, price=50000.0,
|
||||
side=Side.BUY, order_type=OrderType.LIMIT, price=50000.0,
|
||||
qty=0.001, remaining_qty=0.001, queue_ahead_estimate=0.001,
|
||||
created_ts_ns=1_000_000_000, last_update_ts_ns=1_000_000_000,
|
||||
post_only=True,
|
||||
@@ -125,7 +125,7 @@ class TestCancelOrder:
|
||||
def test_cancel_replace_removes_old_adds_new(self):
|
||||
oo = OpenOrderState(
|
||||
client_order_id="c1", venue_order_id="v1", symbol="BTCUSDT",
|
||||
side=Side.BUY, order_type=OrderType.POST_ONLY, price=50000.0,
|
||||
side=Side.BUY, order_type=OrderType.LIMIT, price=50000.0,
|
||||
qty=0.001, remaining_qty=0.001, queue_ahead_estimate=0.001,
|
||||
created_ts_ns=1_000_000_000, last_update_ts_ns=1_000_000_000,
|
||||
post_only=True,
|
||||
@@ -133,7 +133,7 @@ class TestCancelOrder:
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state(open_orders=(oo,))
|
||||
a = FulfilmentAction(
|
||||
ActionKind.CANCEL_REPLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.25,
|
||||
ActionKind.CANCEL_REPLACE, Side.BUY, OrderType.LIMIT, 0, 0.25,
|
||||
200, cancel_order_id="c1", post_only=True,
|
||||
)
|
||||
r = cwm.transition(s, (a,))
|
||||
@@ -145,7 +145,7 @@ class TestAccountUpdate:
|
||||
def test_buy_increases_position(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.1, 50)
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r = cwm.transition(s, (a,))
|
||||
pos = r.account.positions.get("BTCUSDT")
|
||||
assert pos is not None
|
||||
@@ -165,7 +165,7 @@ class TestAccountUpdate:
|
||||
available_balance=10000.0, margin_used=0.0, total_notional=5000.0,
|
||||
positions={"BTCUSDT": pos},
|
||||
))
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.SELL, OrderType.IOC, 0, 0.1, 50)
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.SELL, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r = cwm.transition(s, (a,))
|
||||
new_pos = r.account.positions.get("BTCUSDT")
|
||||
assert new_pos.qty < pos.qty
|
||||
@@ -173,7 +173,7 @@ class TestAccountUpdate:
|
||||
def test_fees_reduce_equity(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.1, 50)
|
||||
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.account.equity < s.account.equity
|
||||
|
||||
@@ -188,7 +188,7 @@ class TestAccountUpdate:
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
# Both maker and taker should apply their respective fees
|
||||
a_cross = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.1, 50)
|
||||
a_cross = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
||||
r_cross = cwm.transition(s, (a_cross,))
|
||||
assert s.venue.taker_fee_bps > s.venue.maker_fee_bps
|
||||
|
||||
@@ -197,7 +197,7 @@ class TestPassivePlacement:
|
||||
def test_passive_order_added_to_book(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 1, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert len(r.open_orders) == 1
|
||||
assert r.open_orders[0].side == Side.BUY
|
||||
@@ -205,14 +205,14 @@ class TestPassivePlacement:
|
||||
def test_passive_order_price_correct(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 1, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.open_orders[0].price == 49999.9
|
||||
|
||||
def test_passive_order_symbol_matches(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.1, 200, post_only=True)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200, post_only=True)
|
||||
r = cwm.transition(s, (a,))
|
||||
assert r.open_orders[0].symbol == "BTCUSDT"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user