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:
@@ -132,7 +132,7 @@ class TestRiskGateAdversarial:
|
||||
gate = RiskGate()
|
||||
state = _state_with_intent()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, -10, 0.1, 200, post_only=True,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, -10, 0.1, 200, post_only=True,
|
||||
)
|
||||
planned = PlannedPolicy(actions=(action,), probabilities=(1.0,),
|
||||
selected_action=action, diagnostics={})
|
||||
|
||||
@@ -98,7 +98,7 @@ class TestOrderPlacement:
|
||||
def test_place_cross_spread_market(self):
|
||||
adapter = BingXVenueAdapter()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.1, 50,
|
||||
ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50,
|
||||
)
|
||||
state = _state()
|
||||
adapter.execute(state, _decision(action=action))
|
||||
|
||||
@@ -103,7 +103,7 @@ class TestExchangeMechanics:
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.10, 200,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.10, 200,
|
||||
post_only=True,
|
||||
)
|
||||
result = cwm.transition(state, (action,))
|
||||
@@ -113,7 +113,7 @@ class TestExchangeMechanics:
|
||||
from malkhut.state import OpenOrderState
|
||||
oo = OpenOrderState(
|
||||
client_order_id="test_123", venue_order_id="v_123",
|
||||
symbol="BTCUSDT", side=Side.BUY, order_type=OrderType.POST_ONLY,
|
||||
symbol="BTCUSDT", 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,
|
||||
@@ -136,7 +136,7 @@ class TestExchangeMechanics:
|
||||
state = _default_state()
|
||||
# Post-only buy at best_ask should be rejected
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, -1, 0.10, 200,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, -1, 0.10, 200,
|
||||
post_only=True,
|
||||
)
|
||||
result = cwm.transition(state, (action,))
|
||||
@@ -148,7 +148,7 @@ class TestPriceMaterialization:
|
||||
def test_cross_spread_buy_returns_best_ask(self):
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.CROSS_SPREAD, Side.BUY, OrderType.IOC, 0, 0.10, 50,
|
||||
ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.10, 50,
|
||||
)
|
||||
price = materialize_price_from_action(state, action)
|
||||
assert price == 50001.0
|
||||
@@ -156,7 +156,7 @@ class TestPriceMaterialization:
|
||||
def test_cross_spread_sell_returns_best_bid(self):
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.CROSS_SPREAD, Side.SELL, OrderType.IOC, 0, 0.10, 50,
|
||||
ActionKind.CROSS_SPREAD, Side.SELL, OrderType.LIMIT, 0, 0.10, 50,
|
||||
)
|
||||
price = materialize_price_from_action(state, action)
|
||||
assert price == 50000.0
|
||||
@@ -164,7 +164,7 @@ class TestPriceMaterialization:
|
||||
def test_buy_offset_0_returns_best_bid(self):
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.10, 200,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.10, 200,
|
||||
)
|
||||
price = materialize_price_from_action(state, action)
|
||||
assert price == 50000.0
|
||||
|
||||
@@ -110,32 +110,32 @@ class TestCWMDeterminism:
|
||||
class TestPriceMaterialization:
|
||||
def test_cross_spread_buy_returns_best_ask(self):
|
||||
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)
|
||||
assert materialize_price_from_action(s, a) == 50001.0
|
||||
|
||||
def test_cross_spread_sell_returns_best_bid(self):
|
||||
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)
|
||||
assert materialize_price_from_action(s, a) == 50000.0
|
||||
|
||||
def test_buy_offset_0_returns_best_bid(self):
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200)
|
||||
assert materialize_price_from_action(s, a) == 50000.0
|
||||
|
||||
def test_buy_offset_1_one_tick_behind(self):
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 1, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.1, 200)
|
||||
assert materialize_price_from_action(s, a) == 49999.9
|
||||
|
||||
def test_sell_offset_0_returns_best_ask(self):
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.POST_ONLY, 0, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.LIMIT, 0, 0.1, 200)
|
||||
assert materialize_price_from_action(s, a) == 50001.0
|
||||
|
||||
def test_sell_offset_1_one_tick_behind(self):
|
||||
s = _state()
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.POST_ONLY, 1, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.LIMIT, 1, 0.1, 200)
|
||||
assert materialize_price_from_action(s, a) == 50001.1
|
||||
|
||||
def test_none_side_returns_none(self):
|
||||
@@ -145,12 +145,12 @@ class TestPriceMaterialization:
|
||||
|
||||
def test_wide_spread_offsets(self):
|
||||
s = _state(bid=49000.0, ask=51000.0)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 5, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 5, 0.1, 200)
|
||||
assert materialize_price_from_action(s, a) == 48999.5
|
||||
|
||||
def test_tight_spread_one_tick(self):
|
||||
s = _state(bid=50000.0, ask=50000.1)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200)
|
||||
assert materialize_price_from_action(s, a) == 50000.0
|
||||
|
||||
|
||||
@@ -190,7 +190,7 @@ class TestRewardFunction:
|
||||
params = self._params(w_fee_quality=1.0)
|
||||
s = _state()
|
||||
a = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 1, 0.10, 200, post_only=True,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.10, 200, post_only=True,
|
||||
)
|
||||
r = cwm.transition(s, (a,))
|
||||
reward = cwm.reward(s, a, r, params)
|
||||
@@ -200,7 +200,7 @@ class TestRewardFunction:
|
||||
def test_cross_spread_penalty(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,))
|
||||
reward = cwm.reward(s, a, r, self._params())
|
||||
assert reward < 0
|
||||
|
||||
@@ -86,14 +86,14 @@ def _noop():
|
||||
def _place(side, offset=0, frac=0.1, post_only=False, reduce_only=False):
|
||||
return FulfilmentAction(
|
||||
ActionKind.PLACE, side,
|
||||
OrderType.POST_ONLY if post_only else OrderType.LIMIT,
|
||||
OrderType.LIMIT if post_only else OrderType.LIMIT,
|
||||
offset, frac, 200,
|
||||
post_only=post_only, reduce_only=reduce_only,
|
||||
)
|
||||
|
||||
|
||||
def _cross(side, frac=0.1):
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.IOC, 0, frac, 50)
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.LIMIT, 0, frac, 50)
|
||||
|
||||
|
||||
def _cancel(order_id):
|
||||
@@ -103,7 +103,7 @@ def _cancel(order_id):
|
||||
def _oo(cid="c1", price=50000.0, qty=0.001, side=Side.BUY, ts=1_000_000_000):
|
||||
return OpenOrderState(
|
||||
client_order_id=cid, venue_order_id="v1", symbol="BTCUSDT",
|
||||
side=side, order_type=OrderType.POST_ONLY, price=price,
|
||||
side=side, order_type=OrderType.LIMIT, price=price,
|
||||
qty=qty, remaining_qty=qty, queue_ahead_estimate=qty * 0.5,
|
||||
created_ts_ns=ts, last_update_ts_ns=ts, post_only=True,
|
||||
)
|
||||
|
||||
@@ -223,7 +223,7 @@ def _noop():
|
||||
|
||||
|
||||
def _cross(side, frac):
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.IOC, 0, frac, 50)
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.LIMIT, 0, frac, 50)
|
||||
|
||||
|
||||
def _place(side, offset=0, frac=0.1):
|
||||
|
||||
@@ -409,4 +409,4 @@ def _noop():
|
||||
|
||||
def _cross(side, frac=0.1):
|
||||
from malkhut.actions import OrderType
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.IOC, 0, frac, 50)
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.LIMIT, 0, frac, 50)
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -359,7 +359,7 @@ def _noop():
|
||||
|
||||
|
||||
def _cross(side, frac):
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.IOC, 0, frac, 50)
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.LIMIT, 0, frac, 50)
|
||||
|
||||
|
||||
def _place(side, offset=0, frac=0.1):
|
||||
|
||||
@@ -105,7 +105,7 @@ class TestCWMProperties:
|
||||
account=AccountState(ts_ns=1, equity=10000.0, wallet_balance=10000.0,
|
||||
available_balance=10000.0, margin_used=0.0, total_notional=0.0),
|
||||
)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, offset, 0.1, 200)
|
||||
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, offset, 0.1, 200)
|
||||
price = materialize_price_from_action(s, a)
|
||||
assert price is not None
|
||||
assert price <= book.best_bid # buy offset should be <= best bid
|
||||
|
||||
@@ -1,83 +1,135 @@
|
||||
"""
|
||||
Tests for standardized order types — FIX/CCXT-aligned, multi-exchange.
|
||||
Three orthogonal dimensions: OrderType, TimeInForce, OrderInstruction.
|
||||
"""
|
||||
import pytest
|
||||
from malkhut.training.order_types import (
|
||||
BaseType, TimeInForce, ConditionalType, OrderInstruction, CompoundOrder,
|
||||
EXCHANGE_ORDER_MAP, normalize_to_exchange, get_supported_types, is_type_available,
|
||||
OrderType, TimeInForce, OrderInstruction,
|
||||
EXCHANGE_ORDER_TYPE_MAP, EXCHANGE_TIF_MAP, EXCHANGE_INSTRUCTION_MAP,
|
||||
normalize_type_to_exchange, normalize_tif_to_exchange,
|
||||
get_supported_types, is_type_available, decompose_order,
|
||||
)
|
||||
|
||||
|
||||
class TestOrderTypeEnums:
|
||||
def test_base_types(self):
|
||||
assert BaseType.MARKET.value == "market"
|
||||
assert BaseType.LIMIT.value == "limit"
|
||||
def test_core_order_types(self):
|
||||
assert OrderType.MARKET.value == "MARKET"
|
||||
assert OrderType.LIMIT.value == "LIMIT"
|
||||
assert OrderType.STOP_MARKET.value == "STOP_MARKET"
|
||||
assert OrderType.STOP_LIMIT.value == "STOP_LIMIT"
|
||||
assert OrderType.TRIGGER_MARKET.value == "TRIGGER_MARKET"
|
||||
assert OrderType.TRIGGER_LIMIT.value == "TRIGGER_LIMIT"
|
||||
assert OrderType.TRAILING_STOP.value == "TRAILING_STOP"
|
||||
|
||||
def test_time_in_force(self):
|
||||
assert TimeInForce.GTC.value == "gtc"
|
||||
assert TimeInForce.IOC.value == "ioc"
|
||||
assert TimeInForce.FOK.value == "fok"
|
||||
|
||||
def test_conditional_types(self):
|
||||
assert ConditionalType.STOP_MARKET.value == "stop_market"
|
||||
assert ConditionalType.STOP_LIMIT.value == "stop_limit"
|
||||
assert ConditionalType.TRAILING_STOP.value == "trailing_stop"
|
||||
assert TimeInForce.GTC.value == "GTC"
|
||||
assert TimeInForce.IOC.value == "IOC"
|
||||
assert TimeInForce.FOK.value == "FOK"
|
||||
assert TimeInForce.GTD.value == "GTD"
|
||||
|
||||
def test_instructions(self):
|
||||
assert OrderInstruction.POST_ONLY.value == "post_only"
|
||||
assert OrderInstruction.REDUCE_ONLY.value == "reduce_only"
|
||||
assert OrderInstruction.POST_ONLY.value == "POST_ONLY"
|
||||
assert OrderInstruction.REDUCE_ONLY.value == "REDUCE_ONLY"
|
||||
assert OrderInstruction.HIDDEN.value == "HIDDEN"
|
||||
assert OrderInstruction.ICEBERG.value == "ICEBERG"
|
||||
|
||||
def test_compound(self):
|
||||
assert CompoundOrder.OCO.value == "oco"
|
||||
assert CompoundOrder.TP_SL.value == "tp_sl"
|
||||
def test_post_only_is_not_order_type(self):
|
||||
"""POST_ONLY is an instruction, not an order type."""
|
||||
assert not hasattr(OrderType, "POST_ONLY")
|
||||
assert hasattr(OrderInstruction, "POST_ONLY")
|
||||
|
||||
def test_ioc_is_not_order_type(self):
|
||||
"""IOC is a TimeInForce, not an order type."""
|
||||
assert not hasattr(OrderType, "IOC")
|
||||
assert hasattr(TimeInForce, "IOC")
|
||||
|
||||
|
||||
class TestExchangeMapping:
|
||||
class TestExchangeOrderTypeMapping:
|
||||
def test_binance_has_all_types(self):
|
||||
types = get_supported_types("binance")
|
||||
assert "limit" in types
|
||||
assert "market" in types
|
||||
assert "stop_market" in types
|
||||
assert "post_only" in types
|
||||
assert "LIMIT" in types
|
||||
assert "MARKET" in types
|
||||
assert "STOP_MARKET" in types
|
||||
assert "TRAILING_STOP" in types
|
||||
|
||||
def test_bingx_has_all_types(self):
|
||||
types = get_supported_types("bingx")
|
||||
assert "limit" in types
|
||||
assert "market" in types
|
||||
assert "trigger_market" in types
|
||||
assert "LIMIT" in types
|
||||
assert "MARKET" in types
|
||||
assert "TRIGGER_MARKET" in types
|
||||
assert "TRAILING_STOP" in types
|
||||
|
||||
def test_bybit_has_all_types(self):
|
||||
types = get_supported_types("bybit")
|
||||
assert "limit" in types
|
||||
assert "market" in types
|
||||
assert "stop_market" in types
|
||||
assert "LIMIT" in types
|
||||
assert "MARKET" in types
|
||||
assert "STOP_MARKET" in types
|
||||
|
||||
def test_normalize_limit_all_exchanges(self):
|
||||
"""LIMIT should map to LIMIT on all exchanges."""
|
||||
for ex in ["binance", "bingx", "bybit"]:
|
||||
assert normalize_to_exchange("limit", ex) == "LIMIT"
|
||||
assert normalize_type_to_exchange(OrderType.LIMIT, ex) == "LIMIT"
|
||||
|
||||
def test_normalize_market_all_exchanges(self):
|
||||
"""MARKET should map to MARKET on all exchanges."""
|
||||
for ex in ["binance", "bingx", "bybit"]:
|
||||
assert normalize_to_exchange("market", ex) == "MARKET"
|
||||
assert normalize_type_to_exchange(OrderType.MARKET, ex) == "MARKET"
|
||||
|
||||
def test_normalize_post_only_varies(self):
|
||||
"""POST_ONLY maps differently across exchanges."""
|
||||
assert normalize_to_exchange("post_only", "binance") == "LIMIT_MAKER"
|
||||
assert normalize_to_exchange("post_only", "bingx") == "POST_ONLY"
|
||||
assert normalize_to_exchange("post_only", "bybit") == "POST_ONLY"
|
||||
def test_bingx_stop_market(self):
|
||||
assert normalize_type_to_exchange(OrderType.STOP_MARKET, "bingx") == "TRIGGER_MARKET"
|
||||
|
||||
def test_normalize_unknown_type(self):
|
||||
assert normalize_to_exchange("invalid_type", "binance") is None
|
||||
def test_bingx_trailing_stop(self):
|
||||
assert normalize_type_to_exchange(OrderType.TRAILING_STOP, "bingx") == "TRAILING_STOP_MARKET"
|
||||
|
||||
def test_binance_trailing_stop(self):
|
||||
assert normalize_type_to_exchange(OrderType.TRAILING_STOP, "binance") == "TRAILING_STOP_MARKET"
|
||||
|
||||
def test_normalize_tif(self):
|
||||
assert normalize_tif_to_exchange(TimeInForce.IOC, "bingx") == "IOC"
|
||||
assert normalize_tif_to_exchange(TimeInForce.FOK, "bingx") == "FOK"
|
||||
assert normalize_tif_to_exchange(TimeInForce.GTC, "bingx") == "GTC"
|
||||
|
||||
def test_is_type_available(self):
|
||||
assert is_type_available("limit", "binance")
|
||||
assert is_type_available("stop_market", "bingx")
|
||||
assert not is_type_available("invalid", "binance")
|
||||
assert is_type_available(OrderType.LIMIT, "binance")
|
||||
assert is_type_available(OrderType.STOP_MARKET, "bingx")
|
||||
assert is_type_available(OrderType.TRAILING_STOP, "bybit")
|
||||
|
||||
def test_get_supported_types_unique(self):
|
||||
"""Each exchange should support at least 8 types."""
|
||||
for ex in ["binance", "bingx", "bybit"]:
|
||||
types = get_supported_types(ex)
|
||||
assert len(types) >= 8
|
||||
assert len(types) >= 7
|
||||
|
||||
|
||||
class TestDecomposeOrder:
|
||||
def test_decompose_ioc(self):
|
||||
ot, tif, inst = decompose_order("IOC")
|
||||
assert ot == "LIMIT"
|
||||
assert tif == "IOC"
|
||||
assert inst is None
|
||||
|
||||
def test_decompose_fok(self):
|
||||
ot, tif, inst = decompose_order("FOK")
|
||||
assert ot == "LIMIT"
|
||||
assert tif == "FOK"
|
||||
|
||||
def test_decompose_post_only(self):
|
||||
ot, tif, inst = decompose_order("POST_ONLY")
|
||||
assert ot == "LIMIT"
|
||||
assert tif == "GTC"
|
||||
assert inst == "POST_ONLY"
|
||||
|
||||
def test_decompose_reduce_only(self):
|
||||
ot, tif, inst = decompose_order("REDUCE_ONLY")
|
||||
assert ot == "MARKET"
|
||||
assert tif == "GTC"
|
||||
assert inst == "REDUCE_ONLY"
|
||||
|
||||
def test_decompose_market(self):
|
||||
ot, tif, inst = decompose_order("MARKET")
|
||||
assert ot == "MARKET"
|
||||
assert tif == "GTC"
|
||||
assert inst is None
|
||||
|
||||
def test_decompose_limit(self):
|
||||
ot, tif, inst = decompose_order("LIMIT")
|
||||
assert ot == "LIMIT"
|
||||
assert tif == "GTC"
|
||||
assert inst is None
|
||||
|
||||
@@ -67,7 +67,7 @@ def _noop():
|
||||
|
||||
|
||||
def _cross(side, frac=0.1):
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.IOC, 0, frac, 50)
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.LIMIT, 0, frac, 50)
|
||||
|
||||
|
||||
# ══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
@@ -69,7 +69,7 @@ class TestRiskGate:
|
||||
gate = RiskGate()
|
||||
# offset=-10 => price = best_bid + 10*tick = 50001.0 >= best_ask => crosses
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, -10, 0.10, 200,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, -10, 0.10, 200,
|
||||
post_only=True,
|
||||
)
|
||||
planned = PlannedPolicy(actions=(action,), probabilities=(1.0,),
|
||||
@@ -98,7 +98,7 @@ class TestRiskGate:
|
||||
def test_approved_action_passes(self):
|
||||
gate = RiskGate()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 1, 0.10, 200,
|
||||
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.10, 200,
|
||||
post_only=True,
|
||||
)
|
||||
planned = PlannedPolicy(actions=(action,), probabilities=(1.0,),
|
||||
|
||||
@@ -145,7 +145,7 @@ def _intent():
|
||||
|
||||
|
||||
def _cross(side, frac):
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.IOC, 0, frac, 50)
|
||||
return FulfilmentAction(ActionKind.CROSS_SPREAD, side, OrderType.LIMIT, 0, frac, 50)
|
||||
|
||||
|
||||
def _place(side, offset=0, frac=0.1):
|
||||
|
||||
@@ -124,7 +124,8 @@ class TestEnums:
|
||||
|
||||
def test_order_type_values(self):
|
||||
assert OrderType.LIMIT.value == "LIMIT"
|
||||
assert OrderType.POST_ONLY.value == "POST_ONLY"
|
||||
assert OrderType.MARKET.value == "MARKET"
|
||||
assert OrderType.STOP_MARKET.value == "STOP_MARKET"
|
||||
|
||||
|
||||
class TestTradePathState:
|
||||
|
||||
Reference in New Issue
Block a user