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.
165 lines
6.9 KiB
Python
165 lines
6.9 KiB
Python
"""
|
|
Property-based tests using Hypothesis.
|
|
|
|
Invariant tests:
|
|
- CWM always produces valid states
|
|
- Planner always returns probability distribution summing to 1
|
|
- Codec always produces valid params
|
|
- Risk gate always returns valid decisions
|
|
"""
|
|
import hypothesis
|
|
from hypothesis import given, strategies as st, assume, settings
|
|
import math
|
|
import pytest
|
|
from malkhut.state import (
|
|
AccountState, FulfilmentPolicyParams, MarketWorldState, Mode,
|
|
OrderBookState, PriceLevel, VenueRules,
|
|
)
|
|
from malkhut.cwm.core import MinimalCryptoLOBCWM, materialize_price_from_action
|
|
from malkhut.actions import ActionKind, FulfilmentAction, OrderType, Side
|
|
from malkhut.training.cma_trainer import CMAParameterCodec
|
|
from malkhut.planner.sm_mcts import DecoupledUCBPlanner
|
|
from malkhut.counterparties import default_counterparty_ecology
|
|
|
|
|
|
def _venue():
|
|
return VenueRules(
|
|
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,
|
|
)
|
|
|
|
|
|
def _book(bid_price, ask_price):
|
|
assume(bid_price < ask_price)
|
|
assume(bid_price > 0)
|
|
return OrderBookState(
|
|
ts_ns=1_000_000_000, symbol="BTCUSDT",
|
|
bids=(PriceLevel(bid_price, 1.0),),
|
|
asks=(PriceLevel(ask_price, 1.0),),
|
|
)
|
|
|
|
|
|
def _params():
|
|
return FulfilmentPolicyParams(
|
|
version="hypo", ucb_c=1.414, max_sims=32, max_depth=2,
|
|
rollout_depth=1, root_temperature=0.5, min_root_entropy=0.25,
|
|
quote_offsets_ticks=(0, 1), quote_size_fractions=(0.25,),
|
|
passive_ttl_ms=200, aggressive_ttl_ms=50,
|
|
maker_edge_min_bps=0.5, cross_spread_edge_min_bps=5.0,
|
|
adverse_toxicity_cancel_threshold=0.5, queue_churn_cancel_threshold=0.5,
|
|
mae_tail_cut_bps=50.0, mfe_giveback_cut_fraction=0.5,
|
|
max_time_in_loss_s=300.0, failed_recovery_cut_count=3,
|
|
recovery_velocity_min_bps_per_s=0.0,
|
|
max_symbol_notional_fraction=0.20, max_single_order_notional_fraction=0.05,
|
|
reduce_when_global_up_fraction=0.30, session_profit_lock_fraction=0.02,
|
|
w_expected_pnl=1.0, w_fill_probability=0.5, w_adverse_selection=2.0,
|
|
w_queue_priority=0.5, w_inventory_risk=1.5, w_tail_loss=5.0,
|
|
w_fee_quality=0.5, w_time_decay=0.3, w_policy_entropy=0.5,
|
|
robust_tail_weight=2.0, toxic_counterparty_weight=3.0,
|
|
low_liquidity_weight=2.0, latency_stress_weight=1.0,
|
|
)
|
|
|
|
|
|
class TestCWMProperties:
|
|
@settings(max_examples=50, deadline=None)
|
|
@given(bid=st.floats(min_value=1.0, max_value=100000.0),
|
|
ask=st.floats(min_value=1.0, max_value=100000.0))
|
|
def test_transition_never_crashes(self, bid, ask):
|
|
assume(bid < ask)
|
|
cwm = MinimalCryptoLOBCWM()
|
|
book = _book(bid, ask)
|
|
s = MarketWorldState(
|
|
ts_ns=1_000_000_000, mode=Mode.REPLAY_NO_IMPACT,
|
|
venue=_venue(), book=book,
|
|
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.NOOP, None, None, 0, 0.0, 0)
|
|
r = cwm.transition(s, (a,))
|
|
assert r.ts_ns >= s.ts_ns
|
|
assert r.account.equity >= 0
|
|
|
|
@settings(max_examples=50, deadline=None)
|
|
@given(bid=st.floats(min_value=100.0, max_value=100000.0),
|
|
ask=st.floats(min_value=100.0, max_value=100000.0))
|
|
def test_book_invariants(self, bid, ask):
|
|
assume(bid < ask)
|
|
book = _book(bid, ask)
|
|
assert book.best_bid == bid
|
|
assert book.best_ask == ask
|
|
assert book.spread == ask - bid
|
|
assert book.spread_bps > 0
|
|
assert book.mid == (bid + ask) / 2
|
|
|
|
@settings(max_examples=50, deadline=None)
|
|
@given(bid=st.floats(min_value=100.0, max_value=100000.0),
|
|
ask=st.floats(min_value=100.0, max_value=100000.0),
|
|
offset=st.integers(min_value=0, max_value=20))
|
|
def test_price_materialization_bounded(self, bid, ask, offset):
|
|
assume(bid < ask)
|
|
book = _book(bid, ask)
|
|
s = MarketWorldState(
|
|
ts_ns=1, mode=Mode.REPLAY_NO_IMPACT, venue=_venue(), book=book,
|
|
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.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
|
|
|
|
|
|
class TestCodecProperties:
|
|
def test_decode_always_returns_valid_params(self):
|
|
codec = CMAParameterCodec()
|
|
import random
|
|
for _ in range(50):
|
|
lows, highs = codec.bounds()
|
|
x = [random.uniform(lo, hi) for lo, hi in zip(lows, highs)]
|
|
p = codec.decode(x, f"rand_{_}")
|
|
assert isinstance(p, FulfilmentPolicyParams)
|
|
assert p.version.startswith("rand_")
|
|
|
|
def test_decode_bounds_respected(self):
|
|
codec = CMAParameterCodec()
|
|
lows, highs = codec.bounds()
|
|
import random
|
|
for _ in range(50):
|
|
x = [random.uniform(lo, hi) for lo, hi in zip(lows, highs)]
|
|
p = codec.decode(x, "b")
|
|
for spec in codec.SPECS:
|
|
val = getattr(p, spec.name)
|
|
if spec.kind == "float":
|
|
assert spec.low - 1e-9 <= val <= spec.high + 1e-9
|
|
|
|
|
|
class TestPlannerProperties:
|
|
@settings(max_examples=30, deadline=None)
|
|
@given(seed=st.integers(min_value=0, max_value=2**31))
|
|
def test_planner_always_returns_valid_distribution(self, seed):
|
|
from malkhut.state import ExecutionIntent, IntentKind
|
|
cwm = MinimalCryptoLOBCWM()
|
|
intent = ExecutionIntent(
|
|
intent_id="h", ts_ns=1_000_000_000, symbol="BTCUSDT",
|
|
kind=IntentKind.ENTER_LONG, target_qty=0.01, max_notional=500.0,
|
|
urgency=0.5, alpha_horizon_s=60.0, alpha_bps=2.0,
|
|
max_slippage_bps=5.0, prefer_maker=True, reduce_only=False,
|
|
ttl_s=300.0, reason="hypo",
|
|
)
|
|
s = MarketWorldState(
|
|
ts_ns=1_000_000_000, mode=Mode.REPLAY_NO_IMPACT, venue=_venue(),
|
|
book=_book(50000.0, 50001.0),
|
|
account=AccountState(ts_ns=1, equity=10000.0, wallet_balance=10000.0,
|
|
available_balance=10000.0, margin_used=0.0, total_notional=0.0),
|
|
intent=intent,
|
|
)
|
|
planner = DecoupledUCBPlanner(
|
|
cwm=cwm, counterparties=default_counterparty_ecology(), rng_seed=seed,
|
|
)
|
|
result = planner.plan(root_state=s, params=_params(), budget_ms=10)
|
|
total = sum(result.probabilities)
|
|
assert abs(total - 1.0) < 1e-6
|
|
assert all(p >= 0 for p in result.probabilities)
|