Files
sentiment-engine/MALKHUT/malkhut/planner/action_menu.py
Codex d24d9bc6bd 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

131 lines
4.1 KiB
Python

"""
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.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.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.LIMIT, price_ticks_from_best=0,
qty_fraction=frac, ttl_ms=params.aggressive_ttl_ms,
time_in_force="IOC",
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.MARKET, price_ticks_from_best=0,
qty_fraction=1.0, ttl_ms=0, reduce_only=True,
metadata={"reason": "path_risk_exit"},
))
return tuple(actions)