malkhut(T4): Strategy DSL v2 + generator + supporting modules
Strategy DSL v2 (dsl.py): 40+ action primitives, 40+ market sensors, 12 comparison operators, 16 builtins, full parser. Strategy Generator (generator.py): genetic programming evolution — crossover, mutation, tournament selection, pool management. Supporting: discrepancy tracking, execution quality, hooks, feature importance, observability, parallel eval, auto-rollback, stress testing, structured observations, trajectory recording.
This commit is contained in:
75
MALKHUT/malkhut/training/hooks.py
Normal file
75
MALKHUT/malkhut/training/hooks.py
Normal file
@@ -0,0 +1,75 @@
|
||||
"""
|
||||
Execution Hooks — entry/exit interfaces for future BingX integration.
|
||||
|
||||
Prepares hooks that will be called when connecting to live execution systems.
|
||||
All hooks are no-ops now but define the interface for future implementation.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable, Optional, Protocol
|
||||
|
||||
from malkhut.state import MarketWorldState
|
||||
from malkhut.actions import FulfilmentAction
|
||||
|
||||
|
||||
class ExecutionIntentHook(Protocol):
|
||||
"""Hook called when an execution intent is submitted."""
|
||||
|
||||
def on_intent(self, intent_id: str, target: str, urgency: float) -> None: ...
|
||||
|
||||
|
||||
class FillCallbackHook(Protocol):
|
||||
"""Hook called after a fill is received."""
|
||||
|
||||
def on_fill(self, fill_price: float, fill_qty: float, side: str, is_maker: bool) -> None: ...
|
||||
|
||||
|
||||
class VenueTelemetryHook(Protocol):
|
||||
"""Hook for venue state publishing to Zinc."""
|
||||
|
||||
def on_venue_state(self, state: Mapping[str, Any]) -> None: ...
|
||||
|
||||
|
||||
class AccountReconciliationHook(Protocol):
|
||||
"""Hook to compare internal state vs venue state."""
|
||||
|
||||
def reconcile(self, internal_state: MarketWorldState, venue_state: Mapping[str, Any]) -> list[str]: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class ExecutionHooks:
|
||||
"""
|
||||
Collection of hooks for future execution integration.
|
||||
|
||||
All hooks are no-ops by default. Override when connecting to live systems.
|
||||
"""
|
||||
on_intent: Optional[Callable] = None
|
||||
on_fill: Optional[Callable] = None
|
||||
on_venue_state: Optional[Callable] = None
|
||||
reconcile: Optional[Callable] = None
|
||||
|
||||
def submit_intent(self, intent_id: str, target: str, urgency: float) -> None:
|
||||
"""Submit an execution intent (DSL primitive)."""
|
||||
if self.on_intent:
|
||||
self.on_intent(intent_id, target, urgency)
|
||||
|
||||
def report_fill(self, fill_price: float, fill_qty: float, side: str, is_maker: bool) -> None:
|
||||
"""Report a fill (DSL primitive)."""
|
||||
if self.on_fill:
|
||||
self.on_fill(fill_price, fill_qty, side, is_maker)
|
||||
|
||||
def publish_venue_state(self, state: Mapping[str, Any]) -> None:
|
||||
"""Publish venue state to Zinc."""
|
||||
if self.on_venue_state:
|
||||
self.on_venue_state(state)
|
||||
|
||||
def reconcile_state(self, internal: MarketWorldState, venue: Mapping[str, Any]) -> list[str]:
|
||||
"""Reconcile internal vs venue state."""
|
||||
if self.reconcile:
|
||||
return self.reconcile(internal, venue)
|
||||
return []
|
||||
|
||||
|
||||
# Default hooks (no-ops)
|
||||
DEFAULT_HOOKS = ExecutionHooks()
|
||||
Reference in New Issue
Block a user