Files
sentiment-engine/MALKHUT/malkhut/tests/test_adversarial.py

240 lines
11 KiB
Python
Raw Normal View History

"""
Adversarial scenario tests.
These prove the core thesis: mixed policies survive diverse counterparty
ecologies better than pure deterministic quotes.
"""
import pytest
from malkhut.state import (
AccountState, ExecutionIntent, FulfilmentPolicyParams, IntentKind,
MarketWorldState, Mode, OrderBookState, PriceLevel, Side, VenueRules,
)
from malkhut.cwm.core import MinimalCryptoLOBCWM
from malkhut.planner.sm_mcts import DecoupledUCBPlanner
from malkhut.planner.action_menu import build_our_actions
from malkhut.risk.gate import RiskGate
from malkhut.counterparties import ToxicTakerPolicy, default_counterparty_ecology
from malkhut.actions import ActionKind, FulfilmentAction, OrderType, PlannedPolicy
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 _params(**kw):
d = dict(
version="adv", 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, 2), quote_size_fractions=(0.10, 0.25, 0.50),
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,
)
d.update(kw)
return FulfilmentPolicyParams(**d)
def _state_with_intent(**kw):
from malkhut.state import TradePathState, AccountState as AC
tp = kw.get("trade_path")
return MarketWorldState(
ts_ns=1_000_000_000, mode=Mode.REPLAY_NO_IMPACT, venue=_venue(),
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)),
),
account=AC(
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,
),
intent=ExecutionIntent(
intent_id="adv", 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="adversarial_test",
),
trade_path=tp,
)
class TestToxicTakerPicksOffStaleQuote:
def test_pure_stale_quote_vulnerable(self):
"""A pure 'always quote best bid' is predictable and gets picked off."""
state = _state_with_intent()
params = _params()
actions = build_our_actions(state, params)
# Pure strategy: always place at best bid, 25% size
pure_actions = [a for a in actions if a.kind == ActionKind.PLACE and a.price_ticks_from_best == 0]
assert len(pure_actions) > 0
# This action is predictable — toxic taker can target it
def test_mixed_policy_reduces_predictability(self):
"""SM-MCTS should return a mixed distribution, not a single action."""
cwm = MinimalCryptoLOBCWM()
planner = DecoupledUCBPlanner(
cwm=cwm, counterparties=default_counterparty_ecology(), rng_seed=42,
)
state = _state_with_intent()
params = _params()
result = planner.plan(root_state=state, params=params, budget_ms=15)
# Distribution should have multiple non-zero probabilities
nonzero = [p for p in result.probabilities if p > 0.01]
assert len(nonzero) >= 2, "Pure deterministic policy is exploitable"
def test_mixed_policy_includes_cancellation_option(self):
"""A good policy should have PASSIVE placement + NOOP as minimum diversity."""
cwm = MinimalCryptoLOBCWM()
planner = DecoupledUCBPlanner(
cwm=cwm, counterparties=default_counterparty_ecology(), rng_seed=42,
)
state = _state_with_intent()
params = _params()
result = planner.plan(root_state=state, params=params, budget_ms=15)
# Action set should include NOOP and at least one passive placement
all_kinds = set(a.kind for a in result.actions)
assert ActionKind.NOOP in all_kinds
assert ActionKind.PLACE in all_kinds
class TestRiskGateAdversarial:
def test_kill_switch_blocks_all(self):
gate = RiskGate()
gate._kill_switch_active = lambda: True
action = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200)
planned = PlannedPolicy(actions=(action,), probabilities=(1.0,),
selected_action=action, diagnostics={})
state = _state_with_intent()
decision = gate.validate(state, planned, _params())
assert not decision.approved
assert decision.reason == "kill_switch"
def test_post_only_cross_rejected(self):
gate = RiskGate()
state = _state_with_intent()
action = FulfilmentAction(
ActionKind.PLACE, Side.BUY, OrderType.LIMIT, -10, 0.1, 200, post_only=True,
)
planned = PlannedPolicy(actions=(action,), probabilities=(1.0,),
selected_action=action, diagnostics={})
decision = gate.validate(state, planned, _params())
assert not decision.approved
def test_leverage_exceeded_blocks(self):
gate = RiskGate()
from malkhut.state import AccountState
state = MarketWorldState(
ts_ns=1_000_000_000, mode=Mode.LIVE, venue=_venue(),
book=OrderBookState(ts_ns=1, symbol="BTCUSDT",
bids=(PriceLevel(50000.0, 1.0),),
asks=(PriceLevel(50001.0, 1.0),)),
account=AccountState(
ts_ns=1, equity=1000.0, wallet_balance=1000.0,
available_balance=1000.0, margin_used=0.0, total_notional=5000.0,
),
)
action = FulfilmentAction(ActionKind.PLACE, Side.BUY, OrderType.LIMIT, 0, 0.1, 200)
planned = PlannedPolicy(actions=(action,), probabilities=(1.0,),
selected_action=action, diagnostics={})
decision = gate.validate(state, planned, _params())
assert not decision.approved
assert decision.reason == "leverage_limit"
class TestCounterpartyAdversarial:
def test_toxic_taker_attacks_high_toxicity(self):
"""When orderflow toxicity is high, toxic taker should cross."""
from malkhut.state import TradePathState
tp = TradePathState(
symbol="BTCUSDT", side=Side.BUY, entry_ts_ns=0, now_ts_ns=100_000_000,
bars_held=10, seconds_held=100.0, pnl_bps=0.0, mae_bps=-10.0,
mfe_bps=15.0, distance_from_mfe_bps=15.0, distance_from_entry_bps=0.0,
time_to_mfe_s=30.0, time_in_loss_s=50.0, time_in_profit_s=50.0,
time_since_last_profit_s=10.0, time_since_deep_mae_s=20.0,
loss_to_profit_transitions=1, deep_loss_recoveries=0,
failed_recovery_count=0, recovery_velocity_bps_per_s=1.0,
adverse_velocity_bps_per_s=-0.5,
dolphin_regime_score=0.5, jericho_signal_strength=0.3,
volatility_bps=15.0, orderflow_toxicity=0.9,
queue_churn_score=0.2, book_imbalance=0.1, cross_venue_lead_score=0.1,
)
state = _state_with_intent(trade_path=tp)
toxic = ToxicTakerPolicy()
import random
action = toxic.rollout_action(state, random.Random(42))
assert action.kind == ActionKind.CROSS_SPREAD
def test_latency_arb_attacks_stale_quotes(self):
"""Latency arb crosses when cross-venue lead is strong."""
from malkhut.state import TradePathState
from malkhut.counterparties import LatencyArbPolicy
tp = TradePathState(
symbol="BTCUSDT", side=Side.BUY, entry_ts_ns=0, now_ts_ns=100_000_000,
bars_held=10, seconds_held=100.0, pnl_bps=0.0, mae_bps=-10.0,
mfe_bps=15.0, distance_from_mfe_bps=15.0, distance_from_entry_bps=0.0,
time_to_mfe_s=30.0, time_in_loss_s=50.0, time_in_profit_s=50.0,
time_since_last_profit_s=10.0, time_since_deep_mae_s=20.0,
loss_to_profit_transitions=1, deep_loss_recoveries=0,
failed_recovery_count=0, recovery_velocity_bps_per_s=1.0,
adverse_velocity_bps_per_s=-0.5,
dolphin_regime_score=0.5, jericho_signal_strength=0.3,
volatility_bps=15.0, orderflow_toxicity=0.3,
queue_churn_score=0.2, book_imbalance=0.1, cross_venue_lead_score=0.9,
)
state = _state_with_intent(trade_path=tp)
arb = LatencyArbPolicy()
import random
action = arb.rollout_action(state, random.Random(42))
assert action.kind == ActionKind.CROSS_SPREAD
class TestMixedPolicySurvivesEcology:
def test_noop_always_available(self):
"""NOOP must always be in the action set — sometimes the best quote is no quote."""
state = _state_with_intent()
params = _params()
actions = build_our_actions(state, params)
kinds = [a.kind for a in actions]
assert ActionKind.NOOP in kinds
def test_exit_available_under_tail_risk(self):
"""When path risk is high, FULL_EXIT must be available."""
from malkhut.state import TradePathState
tp = TradePathState(
symbol="BTCUSDT", side=Side.BUY, entry_ts_ns=0, now_ts_ns=100_000_000,
bars_held=10, seconds_held=100.0, pnl_bps=-30.0, mae_bps=-60.0,
mfe_bps=5.0, distance_from_mfe_bps=35.0, distance_from_entry_bps=30.0,
time_to_mfe_s=10.0, time_in_loss_s=90.0, time_in_profit_s=10.0,
time_since_last_profit_s=80.0, time_since_deep_mae_s=5.0,
loss_to_profit_transitions=0, deep_loss_recoveries=0,
failed_recovery_count=4, recovery_velocity_bps_per_s=-2.0,
adverse_velocity_bps_per_s=3.0,
dolphin_regime_score=0.2, jericho_signal_strength=0.1,
volatility_bps=30.0, orderflow_toxicity=0.7,
queue_churn_score=0.5, book_imbalance=0.3, cross_venue_lead_score=-0.5,
)
state = _state_with_intent(trade_path=tp)
params = _params()
actions = build_our_actions(state, params)
kinds = [a.kind for a in actions]
assert ActionKind.FULL_EXIT in kinds