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
This commit is contained in:
170
MALKHUT/malkhut/tests/test_cwm.py
Normal file
170
MALKHUT/malkhut/tests/test_cwm.py
Normal file
@@ -0,0 +1,170 @@
|
||||
"""
|
||||
Unit tests: CWM determinism and exchange mechanics.
|
||||
|
||||
Mutation litmus: if we flip a comparison in CWM transition,
|
||||
these tests MUST go RED.
|
||||
"""
|
||||
import math
|
||||
import pytest
|
||||
from malkhut.state import (
|
||||
AccountState, ExecutionIntent, FulfilmentPolicyParams, IntentKind,
|
||||
MarketWorldState, Mode, OpenOrderState, OrderBookState, PositionState,
|
||||
PriceLevel, Side, VenueRules,
|
||||
)
|
||||
from malkhut.cwm import MinimalCryptoLOBCWM, materialize_price_from_action
|
||||
from malkhut.actions import ActionKind, FulfilmentAction, OrderType
|
||||
|
||||
|
||||
def _default_venue() -> VenueRules:
|
||||
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 _default_book() -> OrderBookState:
|
||||
return 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)),
|
||||
)
|
||||
|
||||
|
||||
def _default_account() -> AccountState:
|
||||
return 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,
|
||||
)
|
||||
|
||||
|
||||
def _default_state() -> MarketWorldState:
|
||||
return MarketWorldState(
|
||||
ts_ns=1_000_000_000, mode=Mode.REPLAY_NO_IMPACT,
|
||||
venue=_default_venue(), book=_default_book(), account=_default_account(),
|
||||
)
|
||||
|
||||
|
||||
def _default_params() -> FulfilmentPolicyParams:
|
||||
return FulfilmentPolicyParams(
|
||||
version="test_v1", ucb_c=1.414, max_sims=64, max_depth=2,
|
||||
rollout_depth=2, 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 TestCWNDeterminism:
|
||||
def test_same_state_action_seed_same_next_state(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(ActionKind.NOOP, None, None, 0, 0.0, 0)
|
||||
|
||||
r1 = cwm.transition(state, (action,))
|
||||
r2 = cwm.transition(state, (action,))
|
||||
assert r1.ts_ns == r2.ts_ns
|
||||
assert r1.book.best_bid == r2.book.best_bid
|
||||
|
||||
def test_transition_does_not_mutate_input_state(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(ActionKind.NOOP, None, None, 0, 0.0, 0)
|
||||
|
||||
orig_ts = state.ts_ns
|
||||
orig_bid = state.book.best_bid
|
||||
cwm.transition(state, (action,))
|
||||
assert state.ts_ns == orig_ts
|
||||
assert state.book.best_bid == orig_bid
|
||||
|
||||
def test_noop_does_not_change_account(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(ActionKind.NOOP, None, None, 0, 0.0, 0)
|
||||
|
||||
result = cwm.transition(state, (action,))
|
||||
assert result.account.equity == state.account.equity
|
||||
|
||||
|
||||
class TestExchangeMechanics:
|
||||
def test_tick_rounding_buy(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
state = _default_state()
|
||||
action = FulfilmentAction(
|
||||
ActionKind.PLACE, Side.BUY, OrderType.POST_ONLY, 0, 0.10, 200,
|
||||
post_only=True,
|
||||
)
|
||||
result = cwm.transition(state, (action,))
|
||||
assert result.account.equity <= state.account.equity
|
||||
|
||||
def test_cancel_removes_open_order(self):
|
||||
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,
|
||||
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,
|
||||
)
|
||||
state = MarketWorldState(
|
||||
ts_ns=1_000_000_000, mode=Mode.REPLAY_NO_IMPACT,
|
||||
venue=_default_venue(), book=_default_book(),
|
||||
account=_default_account(), open_orders=(oo,),
|
||||
)
|
||||
action = FulfilmentAction(
|
||||
ActionKind.CANCEL, Side.BUY, None, 0, 0.0, 0,
|
||||
cancel_order_id="test_123",
|
||||
)
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
result = cwm.transition(state, (action,))
|
||||
assert len(result.open_orders) == 0
|
||||
|
||||
def test_post_only_rejects_crossing_buy(self):
|
||||
cwm = MinimalCryptoLOBCWM()
|
||||
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,
|
||||
post_only=True,
|
||||
)
|
||||
result = cwm.transition(state, (action,))
|
||||
# No fill should occur, order should not be in book at crossing price
|
||||
assert result.account.equity == state.account.equity
|
||||
|
||||
|
||||
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,
|
||||
)
|
||||
price = materialize_price_from_action(state, action)
|
||||
assert price == 50001.0
|
||||
|
||||
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,
|
||||
)
|
||||
price = materialize_price_from_action(state, action)
|
||||
assert price == 50000.0
|
||||
|
||||
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,
|
||||
)
|
||||
price = materialize_price_from_action(state, action)
|
||||
assert price == 50000.0
|
||||
Reference in New Issue
Block a user