fix(live): quantizzazione amount senza artefatti float (Decimal) — 72*0.001=0.07200000000000001 causava 'Invalid params' su Deribit

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-06-04 16:10:28 +00:00
parent 4f5bd32c38
commit 74670dae05
+10 -2
View File
@@ -17,6 +17,7 @@ from __future__ import annotations
import time import time
from dataclasses import dataclass, field from dataclasses import dataclass, field
from decimal import Decimal
from typing import Any from typing import Any
from src.live.cerbero_client import CerberoClient from src.live.cerbero_client import CerberoClient
@@ -46,6 +47,13 @@ def settlement_currency(instrument: str) -> str:
return instrument.split("-")[0].split("_")[0] return instrument.split("-")[0].split("_")[0]
def _quantize_step(value: float, step: float, mn: float) -> float:
"""Arrotonda `value` al multiplo di `step` SENZA artefatti float
(es. 72*0.001 = 0.07200000000000001 → Deribit 'Invalid params')."""
n_steps = round(value / step)
return float(max(n_steps * Decimal(str(step)), Decimal(str(mn))))
def notional_to_amount(instrument: str, notional_usd: float, def notional_to_amount(instrument: str, notional_usd: float,
price: float | None = None) -> float: price: float | None = None) -> float:
"""Notional USD → `amount` Deribit, arrotondato allo step e clampato al minimo. """Notional USD → `amount` Deribit, arrotondato allo step e clampato al minimo.
@@ -59,10 +67,10 @@ def notional_to_amount(instrument: str, notional_usd: float,
units = notional_usd / price # base-coin richiesti units = notional_usd / price # base-coin richiesti
if units < step / 2: if units < step / 2:
return 0.0 return 0.0
return max(round(units / step) * step, mn) return _quantize_step(units, step, mn)
if notional_usd < step / 2: if notional_usd < step / 2:
return 0.0 return 0.0
return max(round(notional_usd / step) * step, mn) return _quantize_step(notional_usd, step, mn)
@dataclass @dataclass