feat(live): executor a 2 gambe per i pairs (PairsExecutionClient, shadow) — pronto, spento
PairsExecutionClient (compone ExecutionClient): open_pair (2 market long A/short B, verifica per-gamba, LEG-RISK unwind se una sola filla), close_pair (2 reduce-only via close_amount, MAI close_position), register_contract (fetch spec USDC). Spec LTC/ADA/SOL aggiunti. PairsWorker: ledger reale shadow a 2 gambe resume-safe (_real_open_pair/ _real_close_pair, PnL per gamba dir A=+d/B=-d, doppio arrotondamento riportato). Runner: pairs_executor gated su execution.pairs_enabled (false di default). Validazione: test 92/92 (open/close, leg-risk unwind, resume) + smoke testnet end-to-end (open 2 gambe verificate, close reduce-only, PnL reale -0.039 vs sim -0.036, conto flat). Smoke ora aborta se ci sono posizioni di produzione + usa solo close_amount. NB incidente testnet documentato (diario): pulizia manuale con close_position ha flattato le quote shadow dei fade sul conto condiviso -> auto-riconciliazione al prossimo close. Lezione: mai close_position su strumenti condivisi. pairs_enabled resta FALSE: accendere con finestra a conto flat + osservazione. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
+88
-2
@@ -29,8 +29,12 @@ from src.live.cerbero_client import CerberoClient
|
||||
_CONTRACT: dict[str, dict[str, Any]] = {
|
||||
"BTC-PERPETUAL": {"linear": False, "min": 10.0, "step": 10.0, "tick": 0.5},
|
||||
"ETH-PERPETUAL": {"linear": False, "min": 1.0, "step": 1.0, "tick": 0.05},
|
||||
"BTC_USDC-PERPETUAL": {"linear": True, "min": 0.0001, "step": 0.0001, "tick": 0.5, "settle": "USDC"},
|
||||
"ETH_USDC-PERPETUAL": {"linear": True, "min": 0.001, "step": 0.001, "tick": 0.05, "settle": "USDC"},
|
||||
"BTC_USDC-PERPETUAL": {"linear": True, "min": 0.0001, "step": 0.0001, "tick": 0.5, "settle": "USDC"},
|
||||
"ETH_USDC-PERPETUAL": {"linear": True, "min": 0.001, "step": 0.001, "tick": 0.05, "settle": "USDC"},
|
||||
# lineari USDC per le gambe dei pairs (PairsExecutionClient, 2026-06-08)
|
||||
"LTC_USDC-PERPETUAL": {"linear": True, "min": 0.1, "step": 0.1, "tick": 0.01, "settle": "USDC"},
|
||||
"ADA_USDC-PERPETUAL": {"linear": True, "min": 0.2, "step": 0.2, "tick": 1e-05, "settle": "USDC"},
|
||||
"SOL_USDC-PERPETUAL": {"linear": True, "min": 0.1, "step": 0.1, "tick": 0.001, "settle": "USDC"},
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +42,24 @@ def contract_spec(instrument: str) -> dict[str, Any]:
|
||||
return _CONTRACT.get(instrument, {"linear": False, "min": 1.0, "step": 1.0})
|
||||
|
||||
|
||||
def register_contract(instrument: str, client) -> dict[str, Any]:
|
||||
"""Recupera lo spec di uno strumento USDC da Deribit (get_instruments) e lo
|
||||
registra in _CONTRACT. Fallback per strumenti pair non hardcodati; ritorna lo
|
||||
spec (o il default se non trovato). Idempotente."""
|
||||
if instrument in _CONTRACT:
|
||||
return _CONTRACT[instrument]
|
||||
try:
|
||||
for i in client.get_instruments(currency="USDC", kind="future", limit=300):
|
||||
if i.get("symbol") == instrument:
|
||||
step = float(i["native"].get("min_trade_amount"))
|
||||
_CONTRACT[instrument] = {"linear": True, "min": step, "step": step,
|
||||
"tick": float(i.get("tick_size") or 0), "settle": "USDC"}
|
||||
return _CONTRACT[instrument]
|
||||
except Exception:
|
||||
pass
|
||||
return contract_spec(instrument)
|
||||
|
||||
|
||||
def settlement_currency(instrument: str) -> str:
|
||||
"""Inverse → base-coin (BTC/ETH); lineari USDC → USDC. Usato per get_positions
|
||||
e per denominare la fee."""
|
||||
@@ -345,3 +367,67 @@ class ExecutionClient:
|
||||
return Fill(instrument, side, 0.0, 0.0, fill_price, fee_coin, fee_usd,
|
||||
order_id, resp.get("state"), verified, raw=resp,
|
||||
notes="" if verified else f"posizione non flat dopo close (pos={pos})")
|
||||
|
||||
|
||||
@dataclass
|
||||
class PairFill:
|
||||
"""Esito verificato di un'apertura/chiusura a 2 GAMBE."""
|
||||
verified: bool # entrambe le gambe eseguite e verificate
|
||||
leg_a: Fill
|
||||
leg_b: Fill
|
||||
unwound: bool = False # true se una gamba e' fallita e l'altra e' stata richiusa
|
||||
notes: str = ""
|
||||
|
||||
|
||||
@dataclass
|
||||
class PairsExecutionClient:
|
||||
"""Esecuzione REALE a 2 gambe (shadow) per i pairs market-neutral su Deribit.
|
||||
|
||||
Compone un ExecutionClient single-leg: apre/chiude le due gambe (long A / short B
|
||||
o viceversa) come market reduce_only-aware, con gestione del LEG-RISK:
|
||||
- open_pair: piazza entrambe; se UNA sola filla -> UNWIND (richiude la fillata
|
||||
reduce-only) per non restare con esposizione direzionale netta -> verified=False.
|
||||
- close_pair: chiude entrambe reduce-only (market); ritorna fee e prezzi reali.
|
||||
Strumenti = lineari USDC (payoff lineare == matematica del backtest a 2 gambe; fee
|
||||
in USDC). amount per gamba arrotondato allo step del rispettivo strumento (doppio
|
||||
arrotondamento: piccolo sbilanciamento di notional inevitabile, riportato).
|
||||
"""
|
||||
leg: "ExecutionClient" = field(default_factory=ExecutionClient)
|
||||
|
||||
def __post_init__(self):
|
||||
# registra gli spec USDC degli strumenti pair non hardcodati (LTC/ADA/SOL ci sono;
|
||||
# questo copre eventuali coppie future)
|
||||
self.client = self.leg.client
|
||||
|
||||
def ensure_specs(self, *instruments: str):
|
||||
for inst in instruments:
|
||||
register_contract(inst, self.client)
|
||||
|
||||
def open_pair(self, inst_a: str, inst_b: str, direction: int,
|
||||
notional_usd: float, label: str | None = None) -> PairFill:
|
||||
"""direction +1 = long A / short B; -1 = short A / long B. notional uguale per gamba."""
|
||||
self.ensure_specs(inst_a, inst_b)
|
||||
side_a = "buy" if direction == 1 else "sell"
|
||||
side_b = "sell" if direction == 1 else "buy"
|
||||
fa = self.leg.open(inst_a, side_a, notional_usd, label=label)
|
||||
fb = self.leg.open(inst_b, side_b, notional_usd, label=label)
|
||||
if fa.verified and fb.verified:
|
||||
return PairFill(True, fa, fb)
|
||||
# LEG-RISK: una sola gamba (o nessuna) verificata -> unwind la fillata
|
||||
unwound = False
|
||||
for f, inst in ((fa, inst_a), (fb, inst_b)):
|
||||
if f.verified and f.amount > 0:
|
||||
self.leg.close_amount(inst, f.side, f.amount, label=label)
|
||||
unwound = True
|
||||
return PairFill(False, fa, fb, unwound=unwound,
|
||||
notes=f"leg-fail (a={fa.verified} b={fb.verified}), unwound={unwound}")
|
||||
|
||||
def close_pair(self, inst_a: str, inst_b: str, side_a: str, side_b: str,
|
||||
amount_a: float, amount_b: float, label: str | None = None) -> PairFill:
|
||||
"""Chiude entrambe le gambe a mercato (reduce-only del lato opposto all'entrata).
|
||||
Ritorna PairFill con i Fill di chiusura (fee/prezzi reali). verified = entrambe chiuse."""
|
||||
ca = self.leg.close_amount(inst_a, side_a, amount_a, label=label)
|
||||
cb = self.leg.close_amount(inst_b, side_b, amount_b, label=label)
|
||||
return PairFill(ca.verified and cb.verified, ca, cb,
|
||||
notes="" if (ca.verified and cb.verified)
|
||||
else f"close parziale (a={ca.verified} b={cb.verified})")
|
||||
|
||||
+113
-1
@@ -38,6 +38,8 @@ class PairsWorker:
|
||||
fee_rt: float = 0.001, # per gamba RT; la coppia paga 2x
|
||||
name: str = "PR01_pairs_reversion",
|
||||
data_dir: Path = Path("data/paper_trades"),
|
||||
executor=None, # PairsExecutionClient: esecuzione REALE shadow a 2 gambe
|
||||
exec_instruments: dict | None = None, # {asset: instrument USDC}
|
||||
):
|
||||
self.asset_a = asset_a
|
||||
self.asset_b = asset_b
|
||||
@@ -74,6 +76,27 @@ class PairsWorker:
|
||||
self.last_bar_ts = 0
|
||||
self.started_at = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# --- esecuzione REALE shadow a 2 gambe (sim resta la verita' che guida) ---
|
||||
self.executor = executor
|
||||
self.exec_instruments = exec_instruments or {}
|
||||
self.inst_a = self.exec_instruments.get(asset_a)
|
||||
self.inst_b = self.exec_instruments.get(asset_b)
|
||||
self.execution_enabled = bool(executor and self.inst_a and self.inst_b)
|
||||
self.real_capital = capital
|
||||
self.real_in_position = False
|
||||
self.real_dir = 0
|
||||
self.real_side_a = "" # lato della gamba A all'apertura ("buy"/"sell")
|
||||
self.real_side_b = ""
|
||||
self.real_amount_a = 0.0 # amount eseguito per gamba (base-coin)
|
||||
self.real_amount_b = 0.0
|
||||
self.real_entry_a = 0.0 # prezzo di fill per gamba
|
||||
self.real_entry_b = 0.0
|
||||
self.real_notional_a = 0.0 # USD effettivi per gamba
|
||||
self.real_notional_b = 0.0
|
||||
self.real_entry_fee = 0.0
|
||||
self.real_trades = 0
|
||||
self.real_first_notified = False
|
||||
|
||||
self._load_state()
|
||||
self._save_state()
|
||||
|
||||
@@ -98,8 +121,24 @@ class PairsWorker:
|
||||
self.total_wins = s.get("total_wins", 0)
|
||||
self.last_bar_ts = s.get("last_bar_ts", 0)
|
||||
self.started_at = s.get("started_at", self.started_at)
|
||||
self.real_capital = s.get("real_capital", self.initial_capital)
|
||||
self.real_in_position = s.get("real_in_position", False)
|
||||
self.real_dir = s.get("real_dir", 0)
|
||||
self.real_side_a = s.get("real_side_a", "")
|
||||
self.real_side_b = s.get("real_side_b", "")
|
||||
self.real_amount_a = s.get("real_amount_a", 0.0)
|
||||
self.real_amount_b = s.get("real_amount_b", 0.0)
|
||||
self.real_entry_a = s.get("real_entry_a", 0.0)
|
||||
self.real_entry_b = s.get("real_entry_b", 0.0)
|
||||
self.real_notional_a = s.get("real_notional_a", 0.0)
|
||||
self.real_notional_b = s.get("real_notional_b", 0.0)
|
||||
self.real_entry_fee = s.get("real_entry_fee", 0.0)
|
||||
self.real_trades = s.get("real_trades", 0)
|
||||
self.real_first_notified = s.get("real_first_notified", False)
|
||||
self._log("RESUME", {"capital": round(self.capital, 2),
|
||||
"total_trades": self.total_trades, "in_position": self.in_position})
|
||||
"total_trades": self.total_trades, "in_position": self.in_position,
|
||||
"real_capital": round(self.real_capital, 2),
|
||||
"real_in_position": self.real_in_position})
|
||||
|
||||
def _save_state(self):
|
||||
state = {
|
||||
@@ -109,6 +148,13 @@ class PairsWorker:
|
||||
"bars_held": self.bars_held, "total_trades": self.total_trades,
|
||||
"total_wins": self.total_wins, "last_bar_ts": self.last_bar_ts,
|
||||
"started_at": self.started_at, "last_update": datetime.now(timezone.utc).isoformat(),
|
||||
"real_capital": round(self.real_capital, 4), "real_in_position": self.real_in_position,
|
||||
"real_dir": self.real_dir, "real_side_a": self.real_side_a, "real_side_b": self.real_side_b,
|
||||
"real_amount_a": self.real_amount_a, "real_amount_b": self.real_amount_b,
|
||||
"real_entry_a": self.real_entry_a, "real_entry_b": self.real_entry_b,
|
||||
"real_notional_a": self.real_notional_a, "real_notional_b": self.real_notional_b,
|
||||
"real_entry_fee": self.real_entry_fee, "real_trades": self.real_trades,
|
||||
"real_first_notified": self.real_first_notified,
|
||||
}
|
||||
with open(self.status_path, "w") as f:
|
||||
json.dump(state, f, indent=2)
|
||||
@@ -145,6 +191,70 @@ class PairsWorker:
|
||||
"entry_a": round(ca, 4), "entry_b": round(cb, 4), "z": round(z, 3),
|
||||
"capital": round(self.capital, 2)}
|
||||
self._log("OPEN", data); self._notify("OPENED", data)
|
||||
if self.execution_enabled:
|
||||
self._real_open_pair(d, ca, cb)
|
||||
|
||||
def _real_open_pair(self, d: int, sim_a: float, sim_b: float):
|
||||
"""Apertura REALE shadow a 2 gambe (long A/short B se d=1). Notional uguale per
|
||||
gamba = capital*pos*lev. Logga slippage e fee reali; gestisce il leg-fail."""
|
||||
notional = self.capital * self.position_size * self.leverage
|
||||
pf = self.executor.open_pair(self.inst_a, self.inst_b, d, notional, label=self.worker_id)
|
||||
data = {"dir": d, "inst_a": self.inst_a, "inst_b": self.inst_b,
|
||||
"notional_leg": round(notional, 2),
|
||||
"fill_a": pf.leg_a.fill_price, "fill_b": pf.leg_b.fill_price,
|
||||
"fee_usd": round(pf.leg_a.fee_usd + pf.leg_b.fee_usd, 5),
|
||||
"verified": pf.verified}
|
||||
if pf.verified:
|
||||
self.real_in_position = True
|
||||
self.real_dir = d
|
||||
self.real_side_a, self.real_side_b = pf.leg_a.side, pf.leg_b.side
|
||||
self.real_amount_a, self.real_amount_b = pf.leg_a.amount, pf.leg_b.amount
|
||||
self.real_entry_a = pf.leg_a.fill_price or sim_a
|
||||
self.real_entry_b = pf.leg_b.fill_price or sim_b
|
||||
self.real_notional_a = pf.leg_a.amount * self.real_entry_a
|
||||
self.real_notional_b = pf.leg_b.amount * self.real_entry_b
|
||||
self.real_entry_fee = pf.leg_a.fee_usd + pf.leg_b.fee_usd
|
||||
self._log("REAL_OPEN_PAIR", data)
|
||||
if not self.real_first_notified:
|
||||
self._notify("REAL_EXEC_LIVE", data); self.real_first_notified = True
|
||||
else:
|
||||
self._log("REAL_OPEN_FAIL", {**data, "note": pf.notes})
|
||||
self._notify("REAL_OPEN_FAIL", {**data, "note": pf.notes})
|
||||
self._save_state() # persisti subito il ledger reale (resume-safe sui crash)
|
||||
|
||||
def _real_close_pair(self, sim_a: float, sim_b: float, reason: str, sim_pnl: float):
|
||||
"""Chiusura REALE shadow: richiude entrambe le gambe reduce-only, riconcilia
|
||||
PnL reale (per gamba) e fee, aggiorna il ledger reale parallelo."""
|
||||
if not self.real_in_position:
|
||||
return
|
||||
pf = self.executor.close_pair(self.inst_a, self.inst_b, self.real_side_a,
|
||||
self.real_side_b, self.real_amount_a, self.real_amount_b,
|
||||
label=self.worker_id)
|
||||
exit_a = pf.leg_a.fill_price or sim_a
|
||||
exit_b = pf.leg_b.fill_price or sim_b
|
||||
# PnL per gamba: dir A = +d (long ratio compra A), dir B = -d
|
||||
da, db = self.real_dir, -self.real_dir
|
||||
gross = (da * (exit_a - self.real_entry_a) / self.real_entry_a * self.real_notional_a
|
||||
+ db * (exit_b - self.real_entry_b) / self.real_entry_b * self.real_notional_b)
|
||||
exit_fee = pf.leg_a.fee_usd + pf.leg_b.fee_usd
|
||||
real_pnl = gross - self.real_entry_fee - exit_fee
|
||||
self.real_capital += real_pnl
|
||||
self.real_trades += 1
|
||||
self._log("REAL_CLOSE_PAIR", {
|
||||
"reason": reason, "exit_a": exit_a, "exit_b": exit_b,
|
||||
"real_pnl_usd": round(real_pnl, 4), "sim_pnl_usd": round(sim_pnl, 4),
|
||||
"entry_fee": round(self.real_entry_fee, 5), "exit_fee": round(exit_fee, 5),
|
||||
"real_capital": round(self.real_capital, 4), "verified": pf.verified})
|
||||
if not pf.verified:
|
||||
self._notify("REAL_CLOSE_FAILED", {"worker": self.worker_id, "note": pf.notes})
|
||||
self.real_in_position = False
|
||||
self.real_dir = 0
|
||||
self.real_side_a = self.real_side_b = ""
|
||||
self.real_amount_a = self.real_amount_b = 0.0
|
||||
self.real_entry_a = self.real_entry_b = 0.0
|
||||
self.real_notional_a = self.real_notional_b = 0.0
|
||||
self.real_entry_fee = 0.0
|
||||
self._save_state()
|
||||
|
||||
def _close(self, ca: float, cb: float, z: float, reason: str):
|
||||
if not self.in_position:
|
||||
@@ -166,6 +276,8 @@ class PairsWorker:
|
||||
"capital": round(self.capital, 2), "bars_held": self.bars_held,
|
||||
"win": bool(is_win), "total_trades": self.total_trades, "accuracy": round(acc, 1)}
|
||||
self._log("CLOSE", data); self._notify("CLOSED", data)
|
||||
if self.execution_enabled:
|
||||
self._real_close_pair(ca, cb, reason, pnl)
|
||||
self.in_position = False
|
||||
self.direction = 0
|
||||
self.entry_a = self.entry_b = self.entry_z = 0.0
|
||||
|
||||
@@ -27,6 +27,7 @@ NOTIFY_EVENTS = {
|
||||
# testnet: sim entra su un prezzo fantasma, il reale sul book)
|
||||
"REAL_DSL_CANCEL_FAIL", # cancel del disaster-SL fallita dopo retry: possibile
|
||||
# stop ORFANO sul book -> verificare a mano
|
||||
"REAL_CLOSE_FAILED", # chiusura reale a 2 gambe (pairs) non verificata su una gamba
|
||||
}
|
||||
|
||||
|
||||
|
||||
+21
-4
@@ -49,16 +49,19 @@ def pos_for_spec(sid: str, global_ps: float, family_overrides: dict[str, float])
|
||||
|
||||
def build_worker_for(spec: SleeveSpec, alloc_capital: float, leverage: float,
|
||||
data_dir: Path = DATA_DIR, position_size: float = 0.15,
|
||||
executor=None, exec_instrument: str | None = None):
|
||||
executor=None, exec_instrument: str | None = None,
|
||||
pairs_executor=None, exec_instruments: dict | None = None):
|
||||
"""Costruisce il worker esecutore per uno sleeve con capitale = quota allocata.
|
||||
|
||||
executor/exec_instrument: se valorizzati (solo per i fade single-leg abilitati),
|
||||
lo StrategyWorker affianca al fill simulato un ordine REALE su Deribit (shadow)."""
|
||||
executor/exec_instrument: per i fade single-leg, StrategyWorker affianca al fill sim
|
||||
un ordine REALE (shadow). pairs_executor/exec_instruments: idem per i PairsWorker
|
||||
(esecuzione reale a 2 gambe)."""
|
||||
if spec.kind == "pairs":
|
||||
return PairsWorker(
|
||||
asset_a=spec.a, asset_b=spec.b, tf=spec.tf, params=spec.params,
|
||||
capital=alloc_capital, position_size=position_size, leverage=leverage,
|
||||
fee_rt=0.001, name="PR01_pairs_reversion", data_dir=data_dir,
|
||||
executor=pairs_executor, exec_instruments=exec_instruments,
|
||||
)
|
||||
if spec.kind == "basket":
|
||||
pr = spec.params
|
||||
@@ -245,7 +248,9 @@ def run(config_path: str = "portfolios.yml"):
|
||||
exec_enabled = bool(_exec_cfg.get("enabled"))
|
||||
exec_sleeves = set(_exec_cfg.get("sleeves", []))
|
||||
exec_instr = _exec_cfg.get("instruments", {}) or {}
|
||||
pairs_exec_enabled = bool(_exec_cfg.get("pairs_enabled")) # esecuzione reale 2 gambe
|
||||
executor = None
|
||||
pairs_executor = None
|
||||
if exec_enabled:
|
||||
from src.live.execution import ExecutionClient
|
||||
executor = ExecutionClient(client=client)
|
||||
@@ -253,6 +258,10 @@ def run(config_path: str = "portfolios.yml"):
|
||||
executor.disaster_sl_pct = float(_exec_cfg.get("disaster_sl_pct", 0.30) or 0) or None
|
||||
print(f"[runner] ESECUZIONE REALE attiva (shadow) — sleeve={sorted(exec_sleeves)} "
|
||||
f"strumenti={exec_instr} disaster_sl={executor.disaster_sl_pct}")
|
||||
if pairs_exec_enabled:
|
||||
from src.live.execution import PairsExecutionClient
|
||||
pairs_executor = PairsExecutionClient(leg=executor)
|
||||
print(f"[runner] ESECUZIONE REALE PAIRS (2 gambe) attiva — strumenti={exec_instr}")
|
||||
|
||||
def _exec_for(s):
|
||||
"""(executor, exec_instrument) per uno sleeve, solo se fade single-leg abilitato."""
|
||||
@@ -260,6 +269,12 @@ def run(config_path: str = "portfolios.yml"):
|
||||
return None, None
|
||||
return executor, exec_instr.get(s.asset)
|
||||
|
||||
def _pairs_exec_for(s):
|
||||
"""(pairs_executor, {asset: instrument}) per uno sleeve pairs, se abilitato."""
|
||||
if not pairs_exec_enabled or s.kind != "pairs":
|
||||
return None, None
|
||||
return pairs_executor, exec_instr
|
||||
|
||||
dr = sleeve_returns_df(live_ids)
|
||||
weights = W.weight_vector(p.weighting, live_ids, dr, weights=p.weights,
|
||||
caps=p.caps, clusters=clusters, lookback=p.vol_lookback)
|
||||
@@ -267,9 +282,11 @@ def run(config_path: str = "portfolios.yml"):
|
||||
workers = {}
|
||||
for s in live_specs:
|
||||
ex, inst = _exec_for(s)
|
||||
pex, pinst = _pairs_exec_for(s)
|
||||
workers[s.sid] = build_worker_for(s, alloc[s.sid], p.leverage,
|
||||
position_size=pos_for_spec(s.sid, position_size, ps_family),
|
||||
executor=ex, exec_instrument=inst)
|
||||
executor=ex, exec_instrument=inst,
|
||||
pairs_executor=pex, exec_instruments=pinst)
|
||||
if ps_family:
|
||||
print(f"[runner] position_size globale={position_size} override famiglia={ps_family}")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user