malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
"""
|
|
|
|
|
Exchange mechanics — price-time priority, tick/lot rounding, fees,
|
|
|
|
|
partial fills, IOC/FOK, post-only rejection, reduce-only.
|
|
|
|
|
"""
|
|
|
|
|
import pytest
|
|
|
|
|
from malkhut.state import (
|
|
|
|
|
AccountState, MarketWorldState, Mode, OpenOrderState, OrderBookState,
|
|
|
|
|
PriceLevel, Side, VenueRules,
|
|
|
|
|
)
|
|
|
|
|
from malkhut.cwm.core import MinimalCryptoLOBCWM
|
|
|
|
|
from malkhut.actions import ActionKind, FulfilmentAction, OrderType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _venue(**kw):
|
|
|
|
|
d = dict(exchange="bingx", symbol="BTCUSDT", tick_size=0.1, lot_size=0.001,
|
|
|
|
|
min_qty=0.001, min_notional=5.0, maker_fee_bps=-0.2, taker_fee_bps=0.5,
|
|
|
|
|
post_only_supported=True, reduce_only_supported=True,
|
|
|
|
|
max_orders_per_second=100, max_cancels_per_minute=120)
|
|
|
|
|
d.update(kw); return VenueRules(**d)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _state(**kw):
|
|
|
|
|
book = kw.get("book", OrderBookState(
|
|
|
|
|
ts_ns=1_000_000_000, symbol="BTCUSDT",
|
|
|
|
|
bids=(PriceLevel(50000.0, 1.0), PriceLevel(49999.0, 2.0)),
|
|
|
|
|
asks=(PriceLevel(50001.0, 1.0), PriceLevel(50002.0, 2.0)),
|
|
|
|
|
))
|
|
|
|
|
return MarketWorldState(
|
|
|
|
|
ts_ns=kw.get("ts", 1_000_000_000), mode=Mode.REPLAY_NO_IMPACT,
|
|
|
|
|
venue=kw.get("venue", _venue()), book=book,
|
|
|
|
|
account=kw.get("account", AccountState(
|
|
|
|
|
ts_ns=1_000_000_000, equity=10000.0, wallet_balance=10000.0,
|
|
|
|
|
available_balance=10000.0, margin_used=0.0, total_notional=0.0,
|
|
|
|
|
)),
|
|
|
|
|
open_orders=kw.get("open_orders", ()),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPostOnlyRejection:
|
|
|
|
|
def test_buy_at_ask_rejected(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
|
|
|
|
# price = best_bid - (-10)*tick = 50000 + 1.0 = 50001.0 = best_ask
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, -10, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.account.equity == s.account.equity
|
|
|
|
|
|
|
|
|
|
def test_sell_at_bid_rejected(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
|
|
|
|
# price = best_ask + (-10)*tick = 50001 - 1.0 = 50000.0 = best_bid
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.LIMIT, -10, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.account.equity == s.account.equity
|
|
|
|
|
|
|
|
|
|
def test_buy_inside_spread_accepted(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
# Should be in open orders (passive placement)
|
|
|
|
|
assert any(o.side == Side.BUY for o in r.open_orders)
|
|
|
|
|
|
|
|
|
|
def test_sell_inside_spread_accepted(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.SELL, OrderType.LIMIT, 0, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert any(o.side == Side.SELL for o in r.open_orders)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCrossSpread:
|
|
|
|
|
def test_cross_buy_fills_at_best_ask(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
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()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.SELL, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.account.equity <= s.account.equity
|
|
|
|
|
|
|
|
|
|
def test_cross_spread_taker_fee_applied(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
fee = s.venue.taker_fee_bps
|
|
|
|
|
assert fee > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCancelOrder:
|
|
|
|
|
def test_cancel_removes_order(self):
|
|
|
|
|
oo = OpenOrderState(
|
|
|
|
|
client_order_id="c1", venue_order_id="v1", symbol="BTCUSDT",
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
side=Side.BUY, order_type=OrderType.LIMIT, price=50000.0,
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state(open_orders=(oo,))
|
|
|
|
|
a = FulfilmentAction(ActionKind.CANCEL, Side.BUY, None, 0, 0.0, 0, cancel_order_id="c1")
|
|
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert len(r.open_orders) == 0
|
|
|
|
|
|
|
|
|
|
def test_cancel_wrong_id_keeps_order(self):
|
|
|
|
|
oo = OpenOrderState(
|
|
|
|
|
client_order_id="c1", venue_order_id="v1", symbol="BTCUSDT",
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
side=Side.BUY, order_type=OrderType.LIMIT, price=50000.0,
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state(open_orders=(oo,))
|
|
|
|
|
a = FulfilmentAction(ActionKind.CANCEL, Side.BUY, None, 0, 0.0, 0, cancel_order_id="wrong_id")
|
|
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert len(r.open_orders) == 1
|
|
|
|
|
|
|
|
|
|
def test_cancel_replace_removes_old_adds_new(self):
|
|
|
|
|
oo = OpenOrderState(
|
|
|
|
|
client_order_id="c1", venue_order_id="v1", symbol="BTCUSDT",
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
side=Side.BUY, order_type=OrderType.LIMIT, price=50000.0,
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
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,
|
|
|
|
|
)
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state(open_orders=(oo,))
|
|
|
|
|
a = FulfilmentAction(
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
ActionKind.CANCEL_REPLACE, Side.BUY, OrderType.LIMIT, 0, 0.25,
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
200, cancel_order_id="c1", post_only=True,
|
|
|
|
|
)
|
|
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert len(r.open_orders) == 1
|
|
|
|
|
assert r.open_orders[0].client_order_id != "c1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestAccountUpdate:
|
|
|
|
|
def test_buy_increases_position(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
pos = r.account.positions.get("BTCUSDT")
|
|
|
|
|
assert pos is not None
|
|
|
|
|
assert pos.qty > 0
|
|
|
|
|
|
|
|
|
|
def test_sell_decreases_position(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
# Start with a long position
|
|
|
|
|
from malkhut.state import PositionState
|
|
|
|
|
pos = PositionState(
|
|
|
|
|
symbol="BTCUSDT", qty=0.1, avg_entry=50000.0,
|
|
|
|
|
unrealized_pnl=0.0, realized_pnl=0.0,
|
|
|
|
|
liquidation_price=None, leverage=0.5, side=Side.BUY,
|
|
|
|
|
)
|
|
|
|
|
s = _state(account=AccountState(
|
|
|
|
|
ts_ns=1_000_000_000, equity=10000.0, wallet_balance=10000.0,
|
|
|
|
|
available_balance=10000.0, margin_used=0.0, total_notional=5000.0,
|
|
|
|
|
positions={"BTCUSDT": pos},
|
|
|
|
|
))
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.SELL, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
new_pos = r.account.positions.get("BTCUSDT")
|
|
|
|
|
assert new_pos.qty < pos.qty
|
|
|
|
|
|
|
|
|
|
def test_fees_reduce_equity(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.account.equity < s.account.equity
|
|
|
|
|
|
|
|
|
|
def test_no_position_zero_notional(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
|
|
|
|
a = _noop()
|
|
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.account.total_notional == 0.0
|
|
|
|
|
|
|
|
|
|
def test_maker_fee_lower_than_taker(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
|
|
|
|
# Both maker and taker should apply their respective fees
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a_cross = FulfilmentAction(ActionKind.CROSS_SPREAD, Side.BUY, OrderType.LIMIT, 0, 0.1, 50)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r_cross = cwm.transition(s, (a_cross,))
|
|
|
|
|
assert s.venue.taker_fee_bps > s.venue.maker_fee_bps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPassivePlacement:
|
|
|
|
|
def test_passive_order_added_to_book(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert len(r.open_orders) == 1
|
|
|
|
|
assert r.open_orders[0].side == Side.BUY
|
|
|
|
|
|
|
|
|
|
def test_passive_order_price_correct(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 1, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.open_orders[0].price == 49999.9
|
|
|
|
|
|
|
|
|
|
def test_passive_order_symbol_matches(self):
|
|
|
|
|
cwm = MinimalCryptoLOBCWM()
|
|
|
|
|
s = _state()
|
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.
2026-07-14 14:46:44 +02:00
|
|
|
a = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200, post_only=True)
|
malkhut(tests): 1140 test functions across 46 test files
CWM (103): core mechanics, exhaustive edge cases, numba, exchange mechanics
Replay (118): exhaustive verification, microstructure, trajectory
Training (190): asset classification, phase0 extensive, pipeline, exhaustive
DSL (102): v2 syntax, expanded, new features
ASEx (33): validate-before-mutate, single-writer
Planner (48): MCTS, alternatives, hooks
Counterparties (19): 9 adversarial agent policies
Clock (30): event-driven reactor
BingX (28): venue adapter
IPC (8): Zinc SHM
Storage (9): ClickHouse
Risk (4): hard invariants
State (17): frozen dataclass invariants
Integration: E2E, concurrency, sync/async seams, hypothesis, fuzz, adversarial
2026-07-11 10:46:12 +02:00
|
|
|
r = cwm.transition(s, (a,))
|
|
|
|
|
assert r.open_orders[0].symbol == "BTCUSDT"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _noop():
|
|
|
|
|
return FulfilmentAction(ActionKind.NOOP, None, None, 0, 0.0, 0)
|