malkhut(T3): planner + adversarial counterparties
Planner: Decoupled UCB/UCT simultaneous-move MCTS (sm_mcts.py), compact action space (action_menu.py), planner alternatives (alternatives.py). Counterparties: 4+ adversarial agent ecology — ToxicTaker, LatencyArb, MarketMaker, NoiseTrader + extended: LiquidationFlow, WhaleOrder, MomentumFollower, SpoofDetector, QueueChaser.
This commit is contained in:
129
MALKHUT/malkhut/planner/action_menu.py
Normal file
129
MALKHUT/malkhut/planner/action_menu.py
Normal file
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
Action menu builder — reduces impossible action space to compact meaningful set.
|
||||
|
||||
Menu size target:
|
||||
our actions: 8-24
|
||||
each counterparty role: 3-12
|
||||
depth: 2-4
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Optional, Tuple
|
||||
|
||||
from malkhut.state import (
|
||||
ActionKind,
|
||||
FulfilmentPolicyParams,
|
||||
IntentKind,
|
||||
MarketWorldState,
|
||||
OpenOrderState,
|
||||
OrderType,
|
||||
Side,
|
||||
TradePathState,
|
||||
)
|
||||
from malkhut.actions import FulfilmentAction
|
||||
|
||||
|
||||
def _side_for_intent(intent_kind: IntentKind) -> Side:
|
||||
if intent_kind in (IntentKind.ENTER_LONG, IntentKind.ADD_LONG, IntentKind.REDUCE_SHORT, IntentKind.EXIT_SHORT):
|
||||
return Side.BUY
|
||||
return Side.SELL
|
||||
|
||||
|
||||
def _exit_side_for_position(state: MarketWorldState) -> Optional[Side]:
|
||||
path = state.trade_path
|
||||
if path is None:
|
||||
return None
|
||||
return Side.SELL if path.side == Side.BUY else Side.BUY
|
||||
|
||||
|
||||
def _path_risk_says_exit(state: MarketWorldState, params: FulfilmentPolicyParams) -> bool:
|
||||
path = state.trade_path
|
||||
if path is None:
|
||||
return False
|
||||
|
||||
if abs(path.mae_bps) >= params.mae_tail_cut_bps:
|
||||
if path.recovery_velocity_bps_per_s < params.recovery_velocity_min_bps_per_s:
|
||||
return True
|
||||
|
||||
if path.time_in_loss_s > params.max_time_in_loss_s:
|
||||
return True
|
||||
|
||||
if path.failed_recovery_count >= params.failed_recovery_cut_count:
|
||||
return True
|
||||
|
||||
if path.mfe_bps > 0:
|
||||
giveback = path.distance_from_mfe_bps / max(path.mfe_bps, 1e-12)
|
||||
if giveback >= params.mfe_giveback_cut_fraction:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def build_our_actions(
|
||||
state: MarketWorldState,
|
||||
params: FulfilmentPolicyParams,
|
||||
) -> Tuple[FulfilmentAction, ...]:
|
||||
intent = state.intent
|
||||
if intent is None:
|
||||
return (FulfilmentAction(ActionKind.NOOP, None, None, 0, 0.0, 0),)
|
||||
|
||||
side = _side_for_intent(intent)
|
||||
actions: List[FulfilmentAction] = []
|
||||
|
||||
# Always allow no-op
|
||||
actions.append(FulfilmentAction(
|
||||
kind=ActionKind.NOOP, side=None, order_type=None,
|
||||
price_ticks_from_best=0, qty_fraction=0.0, ttl_ms=100,
|
||||
))
|
||||
|
||||
# Existing order management
|
||||
for oo in state.open_orders:
|
||||
if oo.symbol != intent.symbol:
|
||||
continue
|
||||
|
||||
actions.append(FulfilmentAction(
|
||||
kind=ActionKind.CANCEL, side=oo.side, order_type=None,
|
||||
price_ticks_from_best=0, qty_fraction=0.0, ttl_ms=0,
|
||||
cancel_order_id=oo.client_order_id,
|
||||
))
|
||||
|
||||
for offset in params.quote_offsets_ticks:
|
||||
actions.append(FulfilmentAction(
|
||||
kind=ActionKind.CANCEL_REPLACE, side=side,
|
||||
order_type=OrderType.POST_ONLY if intent.prefer_maker else OrderType.LIMIT,
|
||||
price_ticks_from_best=offset, qty_fraction=0.25,
|
||||
ttl_ms=params.passive_ttl_ms, cancel_order_id=oo.client_order_id,
|
||||
post_only=intent.prefer_maker, reduce_only=intent.reduce_only,
|
||||
))
|
||||
|
||||
# Passive quote placements
|
||||
for offset in params.quote_offsets_ticks:
|
||||
for frac in params.quote_size_fractions:
|
||||
actions.append(FulfilmentAction(
|
||||
kind=ActionKind.PLACE, side=side,
|
||||
order_type=OrderType.POST_ONLY if intent.prefer_maker else OrderType.LIMIT,
|
||||
price_ticks_from_best=offset, qty_fraction=frac,
|
||||
ttl_ms=params.passive_ttl_ms,
|
||||
post_only=intent.prefer_maker, reduce_only=intent.reduce_only,
|
||||
))
|
||||
|
||||
# Aggressive crossing if urgency allows
|
||||
if intent.urgency > 0.65:
|
||||
for frac in (0.05, 0.10, 0.25):
|
||||
actions.append(FulfilmentAction(
|
||||
kind=ActionKind.CROSS_SPREAD, side=side,
|
||||
order_type=OrderType.IOC, price_ticks_from_best=0,
|
||||
qty_fraction=frac, ttl_ms=params.aggressive_ttl_ms,
|
||||
reduce_only=intent.reduce_only,
|
||||
))
|
||||
|
||||
# Path-risk exits
|
||||
if _path_risk_says_exit(state, params):
|
||||
actions.append(FulfilmentAction(
|
||||
kind=ActionKind.FULL_EXIT, side=_exit_side_for_position(state),
|
||||
order_type=OrderType.REDUCE_ONLY_MARKET, price_ticks_from_best=0,
|
||||
qty_fraction=1.0, ttl_ms=0, reduce_only=True,
|
||||
metadata={"reason": "path_risk_exit"},
|
||||
))
|
||||
|
||||
return tuple(actions)
|
||||
Reference in New Issue
Block a user