book_execute: chiama set_open_cap (U1) se il trader lo espone, prima di rebalance_signed

hasattr perche' U1 e U2 si uniscono dopo questo rilascio: DeribitTrader oggi non ha
set_open_cap, e l'hasattr evita che l'unione dei due rami rompa in un senso o nell'altro.
Si toglie l'hasattr al merge.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-27 13:41:07 +00:00
parent 65a7f0d8fc
commit 9ce924d97c
2 changed files with 63 additions and 0 deletions
+58
View File
@@ -399,3 +399,61 @@ def test_parse_cron_log_legge_il_fill_di_un_RebalanceParziale(monkeypatch, capsy
assert len(runs) == 1 and len(runs[0].fills) == 1
f = runs[0].fills[0]
assert f.asset == "BTC" and f.side == "sell" and abs(f.qty - 0.002) < 1e-9
# ---------------------------------------------------------------------------
# set_open_cap (da U1, stesso rilascio): chiamato SOLO se il trader lo espone (hasattr — U1 e
# U2 si uniscono dopo questo rilascio), subito prima di rebalance_signed.
# ---------------------------------------------------------------------------
def test_set_open_cap_chiamato_se_il_trader_lo_espone(monkeypatch):
mod = carica_script("scripts/live/book_execute.py")
class _TraderConCap:
def __init__(self):
self.cap_visti = []
def set_open_cap(self, inst, cap_usd, mark):
self.cap_visti.append((inst, cap_usd, mark))
def rebalance_signed(self, inst, net, mark, min_usd=5.0):
return []
def position_usd(self, inst):
return 0.0
def ensure_disaster_sl(self, inst, sl_pct):
return {"state": "ok", "stop": 1.0, "amount": 0.0}
trader = _TraderConCap()
canned = _canned_report(cap_per_asset=300.0, scala=1.25)
monkeypatch.setattr(mod, "book_report", lambda **k: canned)
monkeypatch.setattr(mod, "notify", lambda t, d=None: None)
monkeypatch.setattr(mod, "DeribitTrader", lambda *a, **k: trader)
monkeypatch.setattr(mod, "load_config",
lambda: dict(execution_enabled=True, min_order_usd=5.0,
disaster_sl_pct=0.30, max_data_age_days=2.0,
skh_feed_max_age_min=30.0))
monkeypatch.setattr(sys, "argv", ["book_execute.py", "--execute"])
mod._run()
assert trader.cap_visti == [("BTC_USDC-PERPETUAL", 300.0 * 1.25, 60000.0)] # solo BTC ha un ordine
def test_set_open_cap_non_richiesto_se_il_trader_non_lo_espone(monkeypatch):
"""hasattr: un trader senza set_open_cap (oggi, prima del merge con U1) non deve rompersi."""
mod = carica_script("scripts/live/book_execute.py")
class _TraderSenzaCap:
def rebalance_signed(self, inst, net, mark, min_usd=5.0):
return []
def position_usd(self, inst):
return 0.0
def ensure_disaster_sl(self, inst, sl_pct):
return {"state": "ok", "stop": 1.0, "amount": 0.0}
canned = _canned_report()
monkeypatch.setattr(mod, "book_report", lambda **k: canned)
monkeypatch.setattr(mod, "notify", lambda t, d=None: None)
monkeypatch.setattr(mod, "DeribitTrader", lambda *a, **k: _TraderSenzaCap())
monkeypatch.setattr(mod, "load_config",
lambda: dict(execution_enabled=True, min_order_usd=5.0,
disaster_sl_pct=0.30, max_data_age_days=2.0,
skh_feed_max_age_min=30.0))
monkeypatch.setattr(sys, "argv", ["book_execute.py", "--execute"])
mod._run() # non deve sollevare AttributeError