fix(xsec): cast numpy->Python in _save/_close_book — int64 rompeva json.dumps e bloccava il runner in error-streak

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-06-10 21:23:52 +00:00
parent 5e557fe998
commit 61fcc29c5b
+11 -8
View File
@@ -67,11 +67,13 @@ class CrossSectionalWorker:
def _save(self): def _save(self):
self.status_path.write_text(json.dumps({ self.status_path.write_text(json.dumps({
"capital": round(self.capital, 2), "in_position": self.in_position, "capital": round(float(self.capital), 2), "in_position": bool(self.in_position),
"weights": {a: round(v, 5) for a, v in self.weights.items()}, "weights": {a: round(float(v), 5) for a, v in self.weights.items()},
"entry_px": self.entry_px, "bars_held": self.bars_held, "cooldown": self.cooldown, "entry_px": {a: float(v) for a, v in self.entry_px.items()},
"total_trades": self.total_trades, "total_wins": self.total_wins, "bars_held": int(self.bars_held), "cooldown": int(self.cooldown),
"last_bar_ts": self.last_bar_ts, "last_update": datetime.now(timezone.utc).isoformat(), "total_trades": int(self.total_trades), "total_wins": int(self.total_wins),
"last_bar_ts": int(self.last_bar_ts),
"last_update": datetime.now(timezone.utc).isoformat(),
}, indent=2)) }, indent=2))
def _log(self, event, data=None): def _log(self, event, data=None):
@@ -113,11 +115,12 @@ class CrossSectionalWorker:
book = 0.0 book = 0.0
for k, a in enumerate(self.universe): for k, a in enumerate(self.universe):
book += self.weights[a] * np.log(closes_now[k] / self.entry_px[a]) book += self.weights[a] * np.log(closes_now[k] / self.entry_px[a])
net = book - 2 * self.fee_rt # cast a tipi Python: i numpy (float64/int64/bool_) rompono json.dumps in _save
pnl = self.capital * self.position_size * self.leverage * net net = float(book - 2 * self.fee_rt)
pnl = float(self.capital * self.position_size * self.leverage * net)
self.capital = max(self.capital + pnl, 10.0) self.capital = max(self.capital + pnl, 10.0)
self.total_trades += 1 self.total_trades += 1
self.total_wins += net > 0 self.total_wins += 1 if net > 0 else 0
acc = self.total_wins / self.total_trades * 100 if self.total_trades else 0 acc = self.total_wins / self.total_trades * 100 if self.total_trades else 0
self._log("CLOSE", {"book_ret": round(book * 100, 3), "net": round(net * 100, 3), self._log("CLOSE", {"book_ret": round(book * 100, 3), "net": round(net * 100, 3),
"pnl": round(pnl, 2), "capital": round(self.capital, 2), "pnl": round(pnl, 2), "capital": round(self.capital, 2),