fix: reconcile не закрывает позиции при API ошибке fetch_positions
get_position() теперь возвращает API_ERROR sentinel при ошибке API (вместо None). reconcile различает "нет позиции" от "API упал" и пропускает reconcile при ошибке. Предотвращает ложное закрытие реальных позиций в БД. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -71,7 +71,12 @@ def reconcile_positions(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
||||
|
||||
if cfg.is_futures(asset_cfg):
|
||||
# Futures: check position via fetch_positions
|
||||
from .futures import FuturesExecutor
|
||||
fut_pos = executor.get_position(asset_cfg.symbol)
|
||||
if fut_pos == FuturesExecutor.API_ERROR:
|
||||
log.warning(f"{ticker}: reconcile skipped — position API error")
|
||||
logs.append(f"{ticker}: RECONCILE skipped — position API error")
|
||||
continue
|
||||
if fut_pos is None:
|
||||
coin_balance = 0.0 # no position = closed
|
||||
else:
|
||||
@@ -729,7 +734,7 @@ def _check_signal(conn, ticker, prices, dates, asset, asset_cfg, cfg):
|
||||
if cfg.is_futures(asset_cfg):
|
||||
# Futures: pos_size from exchange position
|
||||
fut_pos = executor.get_position(asset_cfg.symbol)
|
||||
if fut_pos and fut_pos["size"] > 0:
|
||||
if isinstance(fut_pos, dict) and fut_pos["size"] > 0:
|
||||
update_position(conn, pos_id, pos_size=fut_pos["size"])
|
||||
pos_size = fut_pos["size"]
|
||||
else:
|
||||
@@ -901,7 +906,7 @@ def update_stops(conn: sqlite3.Connection, cfg: Config) -> list[str]:
|
||||
|
||||
if cfg.is_futures(asset_cfg):
|
||||
fut_pos = executor.get_position(asset_cfg.symbol)
|
||||
safety_size = fut_pos["size"] if fut_pos else pos["pos_size"]
|
||||
safety_size = fut_pos["size"] if isinstance(fut_pos, dict) else pos["pos_size"]
|
||||
side = "long" if direction == 1 else "short"
|
||||
new_safety = executor.place_safety_stop(
|
||||
asset_cfg.symbol, side, safety_size, active_stop)
|
||||
|
||||
@@ -80,10 +80,16 @@ class FuturesExecutor:
|
||||
log.error(f"Balance fetch failed: {e}")
|
||||
return None
|
||||
|
||||
def get_position(self, symbol: str) -> dict | None:
|
||||
# Sentinel: get_position returns this on API error (vs None = no position)
|
||||
API_ERROR = "API_ERROR"
|
||||
|
||||
def get_position(self, symbol: str) -> dict | str | None:
|
||||
"""Fetch open position for symbol.
|
||||
|
||||
Returns dict with keys: size, side, entryPrice, or None if no position.
|
||||
Returns:
|
||||
dict with keys: size, side, entryPrice — if position exists
|
||||
None — if no position (confirmed by API)
|
||||
API_ERROR — if API call failed (DO NOT treat as "no position")
|
||||
"""
|
||||
self._ensure_markets()
|
||||
try:
|
||||
@@ -100,7 +106,7 @@ class FuturesExecutor:
|
||||
return None
|
||||
except Exception as e:
|
||||
log.error(f"Fetch position {symbol} failed: {e}")
|
||||
return None
|
||||
return self.API_ERROR
|
||||
|
||||
def get_price(self, symbol: str) -> float | None:
|
||||
"""Get current market price."""
|
||||
|
||||
Reference in New Issue
Block a user