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.
76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
"""
|
|
Action model — compact menu for simultaneous-move tree search.
|
|
|
|
Bad: enumerate every price tick x every quantity x every TTL x every TIF.
|
|
Good: 8-24 meaningful actions per player, 3-12 per counterparty role.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Mapping, Optional, Tuple
|
|
|
|
from malkhut.state import ActionKind, AgentRole, OrderType, Side
|
|
|
|
# Lazy import to avoid circular dependency (training -> actions -> training)
|
|
_TimeInForce = None
|
|
|
|
def _get_TimeInForce():
|
|
global _TimeInForce
|
|
if _TimeInForce is None:
|
|
from malkhut.training.order_types import TimeInForce
|
|
_TimeInForce = TimeInForce
|
|
return _TimeInForce
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class FulfilmentAction:
|
|
"""One atomic action candidate."""
|
|
kind: ActionKind
|
|
side: Optional[Side]
|
|
order_type: Optional[OrderType]
|
|
price_ticks_from_best: int
|
|
qty_fraction: float
|
|
ttl_ms: int
|
|
cancel_order_id: Optional[str] = None
|
|
reduce_only: bool = False
|
|
post_only: bool = False
|
|
time_in_force: str = "GTC"
|
|
metadata: Mapping[str, Any] = field(default_factory=dict)
|
|
|
|
@property
|
|
def time_in_force_enum(self):
|
|
TIF = _get_TimeInForce()
|
|
return TIF(self.time_in_force)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class CounterpartyAction:
|
|
"""Adversarially useful aggregate actions that alter book/fill outcomes."""
|
|
role: AgentRole
|
|
kind: ActionKind
|
|
side: Optional[Side]
|
|
price_ticks_from_best: int
|
|
qty_fraction_of_top: float
|
|
toxicity: float = 0.0
|
|
metadata: Mapping[str, Any] = field(default_factory=dict)
|
|
|
|
|
|
JointAction = Tuple[Any, ...] # (our_action, cp_action_1, cp_action_2, ...)
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class PlannedPolicy:
|
|
"""Output of the planner: distribution over actions + selected action."""
|
|
actions: Tuple[FulfilmentAction, ...]
|
|
probabilities: Tuple[float, ...]
|
|
selected_action: FulfilmentAction
|
|
diagnostics: Mapping[str, Any]
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class RiskDecision:
|
|
approved: bool
|
|
action: Optional[FulfilmentAction]
|
|
reason: str
|
|
adjusted: bool = False
|