fix: cooldown в часах (не днях) + one-way position mode для futures

Cooldown после стопа считал целые дни (.days), при 1h TF бот ждал 24ч
вместо 3ч (cooldown_bars=3). Теперь last_stop_date хранит ISO datetime,
cooldown считает в часах через total_seconds().

Добавлен set_position_mode(False) в FuturesExecutor для гарантии
one-way mode на Bybit (hedge mode ломает ордера без positionIdx).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-15 04:06:01 +10:00
co-authored by Claude Opus 4.6
parent 17ca885ae1
commit 37a213e6d1
2 changed files with 22 additions and 4 deletions
+15 -4
View File
@@ -439,7 +439,9 @@ def _check_position(conn, pos, prices, dates, asset, asset_cfg, cfg):
consecutive_losses=consecutive_losses,
)
if stopped:
updates["last_stop_date"] = last_date
# Store full ISO datetime for sub-daily cooldown calculation
from datetime import datetime as dt_cls2, timezone as tz2
updates["last_stop_date"] = dt_cls2.now(tz2.utc).isoformat()
update_asset(conn, asset["ticker"], **updates)
dir_str = "LONG" if direction == 1 else "SHORT"
@@ -496,12 +498,21 @@ def _check_signal(conn, ticker, prices, dates, asset, asset_cfg, cfg):
# Cooldown after stop
last_stop = asset.get("last_stop_date")
if last_stop:
from datetime import date as d
from datetime import datetime as dt_cls, timezone as tz
model_cfg = cfg.model_for(asset_cfg)
days_since_stop = (d.fromisoformat(str(dates[-1])) - d.fromisoformat(last_stop)).days
# Parse last_stop as ISO datetime (new format) or date (legacy)
try:
stop_dt = dt_cls.fromisoformat(last_stop)
if stop_dt.tzinfo is None:
stop_dt = stop_dt.replace(tzinfo=tz.utc)
except (ValueError, TypeError):
from datetime import date as d
stop_dt = dt_cls.combine(d.fromisoformat(last_stop), dt_cls.min.time(),
tzinfo=tz.utc)
tf_str = asset_cfg.timeframe or cfg.timeframe
tf_hours = int(tf_str.replace("h", "").replace("d", "")) if "d" not in tf_str else 24
candles_since_stop = days_since_stop * 24 // tf_hours
hours_since_stop = (dt_cls.now(tz.utc) - stop_dt).total_seconds() / 3600
candles_since_stop = int(hours_since_stop / tf_hours)
# Per-asset cooldown_bars; -1 = use shift as cooldown
cooldown = asset_cfg.cooldown_bars if asset_cfg.cooldown_bars >= 0 else model_cfg.shift
if candles_since_stop < cooldown:
+7
View File
@@ -43,6 +43,13 @@ class FuturesExecutor:
def _ensure_markets(self):
if not self._markets_loaded:
self.exchange.load_markets()
# Ensure one-way position mode (not hedge mode)
if not self.dry_run:
try:
self.exchange.set_position_mode(False) # one-way
except Exception as e:
if "not modified" not in str(e).lower():
log.warning(f"set_position_mode failed: {e}")
self._markets_loaded = True
def ensure_leverage(self, symbol: str, leverage: int | None = None):