malkhut: standardized order types — FIX/CCXT-aligned, multi-exchange mapping

order_types.py: Five-layer taxonomy normalized to industry standards:
  Layer 1: Base types (FIX Tag 40) — MARKET, LIMIT
  Layer 2: Time-in-force (FIX Tag 59) — GTC, IOC, FOK, GTD
  Layer 3: Conditional/Trigger (FIX Tag 3/4+MIT) — STOP_MARKET, STOP_LIMIT,
    TRIGGER_MARKET, TRIGGER_LIMIT, TRAILING_STOP
  Layer 4: Instructions (FIX Tag 18) — POST_ONLY, REDUCE_ONLY, HIDDEN, ICEBERG
  Layer 5: Compound (exchange-specific) — OCO, TP_SL

Cross-exchange mapping: BingX ↔ Binance ↔ Bybit (from CCXT source code).
Standards: FIX 4.4 Tag 40/59/18, CCXT unified API, ISO 10383 (MIC).

Transferability: strategy PARAMETERS transfer. ORDER TYPE NAMES are
venue-specific but semantics identical (LIMIT = LIMIT everywhere).

14 tests. README updated with full mapping table and standards references.
This commit is contained in:
Codex
2026-07-14 12:02:01 +02:00
parent 13811cc789
commit 53e02c84ec
3 changed files with 251 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
"""
Standardized Order Types — FIX/CCXT-aligned, multi-exchange compatible.
Five-layer taxonomy normalized to industry standards:
Layer 1: Base types (FIX Tag 40 OrdType)
Layer 2: Time-in-force (FIX Tag 59)
Layer 3: Conditional/Trigger (FIX Tag 3/4 + MIT)
Layer 4: Instructions (FIX Tag 18)
Layer 5: Compound (exchange-specific)
Transferability: strategy PARAMETERS transfer across exchanges.
Order type NAMES are venue-specific but semantics are identical.
The venue adapter translates normalized → exchange-native at submission.
"""
from __future__ import annotations
from enum import Enum
from typing import Dict, Optional
# ==============================================================================
# Layer 1: Base Types (FIX Tag 40 OrdType)
# ==============================================================================
class BaseType(str, Enum):
MARKET = "market" # FIX: 1 — immediate execution at best available price
LIMIT = "limit" # FIX: 2 — execute only at specified price or better
# ==============================================================================
# Layer 2: Time-in-Force (FIX Tag 59)
# ==============================================================================
class TimeInForce(str, Enum):
GTC = "gtc" # FIX: 1 — Good Till Cancel
IOC = "ioc" # FIX: 3 — Immediate or Cancel (partial fill OK)
FOK = "fok" # FIX: 4 — Fill or Kill (all-or-nothing)
GTD = "gtd" # FIX: 6 — Good Till Date
# ==============================================================================
# Layer 3: Conditional/Trigger (FIX Tag 3/4 + MIT)
# ==============================================================================
class ConditionalType(str, Enum):
STOP_MARKET = "stop_market" # FIX: 3 — trigger → market
STOP_LIMIT = "stop_limit" # FIX: 4 — trigger → limit
TRIGGER_MARKET = "trigger_market" # FIX: J (MIT) — market if touched
TRIGGER_LIMIT = "trigger_limit" # FIX: 4 — limit if touched
TRAILING_STOP = "trailing_stop" # FIX: P + ExecInst — trailing stop
# ==============================================================================
# Layer 4: Instructions (FIX Tag 18 ExecInst)
# ==============================================================================
class OrderInstruction(str, Enum):
POST_ONLY = "post_only" # FIX: ExecInst — maker-only, reject if crossing
REDUCE_ONLY = "reduce_only" # FIX: ExecInst — only reduce existing position
HIDDEN = "hidden" # FIX: ExecInst — iceberg/hidden order
ICEBERG = "iceberg" # FIX: ExecInst — visible portion only
# ==============================================================================
# Layer 5: Compound (exchange-specific, no FIX equivalent)
# ==============================================================================
class CompoundOrder(str, Enum):
OCO = "oco" # One-Cancels-Other (linked orders)
TP_SL = "tp_sl" # Take-Profit / Stop-Loss (attached to position)
# ==============================================================================
# Exchange-specific mapping tables
# ==============================================================================
EXCHANGE_ORDER_MAP: Dict[str, Dict[str, str]] = {
"binance": {
"limit": "LIMIT", "market": "MARKET",
"stop_market": "STOP_MARKET", "stop_limit": "STOP_LOSS_LIMIT",
"trigger_market": "TAKE_PROFIT", "trigger_limit": "TAKE_PROFIT_LIMIT",
"trailing_stop": "TRAILING_STOP_MARKET",
"post_only": "LIMIT_MAKER", "ioc": "IOC", "fok": "FOK",
"reduce_only": "REDUCE_ONLY",
},
"bingx": {
"limit": "LIMIT", "market": "MARKET",
"stop_market": "TRIGGER_MARKET", "stop_limit": "TRIGGER_LIMIT",
"trigger_market": "TRIGGER_MARKET", "trigger_limit": "TRIGGER_LIMIT",
"trailing_stop": "TRIGGER_MARKET",
"post_only": "POST_ONLY", "ioc": "IOC", "fok": "FOK",
"reduce_only": "REDUCE_ONLY",
},
"bybit": {
"limit": "LIMIT", "market": "MARKET",
"stop_market": "STOP_MARKET", "stop_limit": "STOP_LIMIT",
"trigger_market": "TAKE_PROFIT_MARKET", "trigger_limit": "TAKE_PROFIT_LIMIT",
"trailing_stop": "TRAILING_STOP",
"post_only": "POST_ONLY", "ioc": "IOC", "fok": "FOK",
"reduce_only": "REDUCE_ONLY",
},
}
def normalize_to_exchange(normalized_type: str, exchange_id: str) -> Optional[str]:
"""Convert a normalized order type to exchange-native string."""
mapping = EXCHANGE_ORDER_MAP.get(exchange_id, {})
return mapping.get(normalized_type)
def get_supported_types(exchange_id: str) -> list[str]:
"""Get all normalized order types supported by an exchange."""
mapping = EXCHANGE_ORDER_MAP.get(exchange_id, {})
return list(mapping.keys())
def is_type_available(normalized_type: str, exchange_id: str) -> bool:
"""Check if a normalized order type is available on an exchange."""
mapping = EXCHANGE_ORDER_MAP.get(exchange_id, {})
return normalized_type in mapping