Use Torch forecast as primary strategy
This commit is contained in:
@@ -24,6 +24,15 @@ class SpotStrategy:
|
||||
account: dict | None = None,
|
||||
trend_candles: list[Candle] | None = None,
|
||||
) -> Signal:
|
||||
if self.settings.strategy_mode == "torch_forecast":
|
||||
return _torch_forecast_entry_signal(
|
||||
settings=self.settings,
|
||||
symbol=symbol,
|
||||
ticker=ticker,
|
||||
open_positions_for_symbol=open_positions_for_symbol,
|
||||
forecast=forecast or {},
|
||||
account=account,
|
||||
)
|
||||
if self.settings.strategy_mode == "trend_macd":
|
||||
return _trend_macd_entry_signal(
|
||||
settings=self.settings,
|
||||
@@ -354,6 +363,8 @@ class SpotStrategy:
|
||||
learning: dict | None = None,
|
||||
forecast: dict | None = None,
|
||||
) -> Signal:
|
||||
if self.settings.strategy_mode == "torch_forecast":
|
||||
return _torch_forecast_exit_signal(self.settings, position, candles, ticker, forecast or {})
|
||||
if self.settings.strategy_mode == "trend_macd":
|
||||
return _trend_macd_exit_signal(self.settings, position, candles, ticker)
|
||||
if ticker is None:
|
||||
@@ -600,6 +611,208 @@ def _trend_macd_exit_signal(
|
||||
return Signal(position.symbol, "HOLD", 0.35, "trend_macd: условия выхода не выполнены", diagnostics)
|
||||
|
||||
|
||||
def _torch_forecast_entry_signal(
|
||||
*,
|
||||
settings: Settings,
|
||||
symbol: str,
|
||||
ticker: Ticker | None,
|
||||
open_positions_for_symbol: int,
|
||||
forecast: dict,
|
||||
account: dict | None,
|
||||
) -> Signal:
|
||||
if ticker is None:
|
||||
return Signal(symbol, "HOLD", 0.0, "torch_forecast: no ticker data")
|
||||
if open_positions_for_symbol > 0:
|
||||
return Signal(symbol, "HOLD", 0.0, "torch_forecast: position for symbol is already open")
|
||||
|
||||
stop_loss_percent = _clamp(settings.stop_loss_percent, 0.003, 0.08)
|
||||
sizing = _torch_forecast_position_sizing(settings, account, stop_loss_percent, forecast)
|
||||
position_notional = float(sizing["notional_usdt"])
|
||||
expected_return = _safe_float(forecast.get("expected_return_percent"), 0.0)
|
||||
probability_up = _safe_float(forecast.get("probability_up"), 0.5)
|
||||
skill = _safe_float(forecast.get("skill"), 0.0)
|
||||
min_edge = max(0.0, settings.time_series_min_edge_percent)
|
||||
min_probability = _torch_min_probability(settings)
|
||||
confidence = _torch_forecast_confidence(settings, forecast)
|
||||
spread_ok = ticker.spread_percent <= settings.max_spread_percent
|
||||
liquidity_ok = ticker.turnover_24h >= settings.min_24h_turnover_usdt
|
||||
model_ok = _is_torch_forecast(forecast)
|
||||
checks = {
|
||||
"torch_model_ok": model_ok,
|
||||
"forecast_usable": bool(forecast.get("usable", False)),
|
||||
"forecast_not_blocked": not bool(forecast.get("block_entry", False)),
|
||||
"expected_edge_ok": expected_return >= min_edge,
|
||||
"probability_ok": probability_up >= min_probability,
|
||||
"skill_ok": skill > 0.0,
|
||||
"confidence_ok": confidence >= settings.min_signal_confidence,
|
||||
"spread_ok": spread_ok,
|
||||
"liquidity_ok": liquidity_ok,
|
||||
"risk_size_ok": position_notional >= settings.min_position_usdt,
|
||||
}
|
||||
diagnostics = {
|
||||
"strategy_mode": "torch_forecast",
|
||||
"trade_mode": "TORCH_FORECAST",
|
||||
"forecast": forecast,
|
||||
"position_notional_usdt": position_notional,
|
||||
"position_sizing": sizing,
|
||||
"stop_loss_percent": stop_loss_percent,
|
||||
"atr_trailing_multiplier": _clamp(settings.atr_trailing_multiplier, 0.5, 10.0),
|
||||
"expected_return_percent": expected_return,
|
||||
"min_edge_percent": min_edge,
|
||||
"probability_up": probability_up,
|
||||
"min_probability_up": min_probability,
|
||||
"skill": skill,
|
||||
"spread_percent": round(ticker.spread_percent, 5),
|
||||
"turnover_24h": ticker.turnover_24h,
|
||||
"checks": checks,
|
||||
"grid": {"enabled": False, "active": False},
|
||||
"rebound": {"enabled": False, "active": False},
|
||||
"learning": {},
|
||||
"llm": {},
|
||||
}
|
||||
if all(checks.values()):
|
||||
return Signal(
|
||||
symbol,
|
||||
"BUY",
|
||||
confidence,
|
||||
(
|
||||
"torch_forecast: PyTorch edge confirmed; "
|
||||
f"model={forecast.get('model')}, p_up={probability_up:.3f}, "
|
||||
f"expected={expected_return:.4f}%, size={position_notional:.2f} USDT"
|
||||
),
|
||||
diagnostics,
|
||||
)
|
||||
failed = ", ".join(name for name, ok in checks.items() if not ok)
|
||||
return Signal(symbol, "HOLD", confidence, f"torch_forecast: entry blocked ({failed})", diagnostics)
|
||||
|
||||
|
||||
def _torch_forecast_exit_signal(
|
||||
settings: Settings,
|
||||
position: Position,
|
||||
candles: list[Candle],
|
||||
ticker: Ticker | None,
|
||||
forecast: dict,
|
||||
) -> Signal:
|
||||
if ticker is None:
|
||||
return Signal(position.symbol, "HOLD", 0.0, "torch_forecast: no ticker data for exit")
|
||||
|
||||
latest = candles[-1] if candles else None
|
||||
price = ticker.last_price
|
||||
stop_loss_percent = _clamp(settings.stop_loss_percent, 0.003, 0.08)
|
||||
effective_stop_loss = max(position.stop_loss, position.entry_price * (1 - stop_loss_percent))
|
||||
atr_multiplier = _clamp(settings.atr_trailing_multiplier, 0.5, 10.0)
|
||||
atr_trailing_stop = None
|
||||
if latest and latest.atr_14 is not None and position.highest_price > position.entry_price:
|
||||
atr_trailing_stop = max(effective_stop_loss, position.highest_price - latest.atr_14 * atr_multiplier)
|
||||
|
||||
expected_return = _safe_float(forecast.get("expected_return_percent"), 0.0)
|
||||
probability_up = _safe_float(forecast.get("probability_up"), 0.5)
|
||||
skill = _safe_float(forecast.get("skill"), 0.0)
|
||||
min_edge = max(0.0, settings.time_series_min_edge_percent)
|
||||
min_probability = _torch_min_probability(settings)
|
||||
estimated_exit_net_percent = _estimated_exit_net_percent(position, price, settings)
|
||||
diagnostics = {
|
||||
"strategy_mode": "torch_forecast",
|
||||
"price": price,
|
||||
"entry_price": position.entry_price,
|
||||
"stop_loss": effective_stop_loss,
|
||||
"atr_trailing_stop": atr_trailing_stop,
|
||||
"atr_trailing_multiplier": atr_multiplier,
|
||||
"highest_price": position.highest_price,
|
||||
"forecast": forecast,
|
||||
"expected_return_percent": expected_return,
|
||||
"min_edge_percent": min_edge,
|
||||
"probability_up": probability_up,
|
||||
"min_probability_up": min_probability,
|
||||
"skill": skill,
|
||||
"estimated_exit_net_percent": round(estimated_exit_net_percent, 4),
|
||||
"atr_14": latest.atr_14 if latest else None,
|
||||
}
|
||||
if price <= effective_stop_loss:
|
||||
return Signal(position.symbol, "SELL", 1.0, "torch_forecast: stop-loss hit", diagnostics)
|
||||
if atr_trailing_stop is not None and price <= atr_trailing_stop:
|
||||
return Signal(position.symbol, "SELL", 0.94, "torch_forecast: ATR trailing stop hit", diagnostics)
|
||||
if not _is_torch_forecast(forecast):
|
||||
return Signal(position.symbol, "SELL", 0.78, "torch_forecast: no valid PyTorch forecast to hold", diagnostics)
|
||||
if bool(forecast.get("block_entry", False)) or expected_return <= 0.0 or probability_up <= 0.50:
|
||||
return Signal(
|
||||
position.symbol,
|
||||
"SELL",
|
||||
0.86,
|
||||
(
|
||||
"torch_forecast: PyTorch forecast turned negative; "
|
||||
f"p_up={probability_up:.3f}, expected={expected_return:.4f}%"
|
||||
),
|
||||
diagnostics,
|
||||
)
|
||||
weak_hold = expected_return < min_edge or probability_up < min_probability or skill <= 0.0
|
||||
if weak_hold and estimated_exit_net_percent >= 0:
|
||||
return Signal(
|
||||
position.symbol,
|
||||
"SELL",
|
||||
0.74,
|
||||
(
|
||||
"torch_forecast: PyTorch no longer confirms enough edge; "
|
||||
f"p_up={probability_up:.3f}, expected={expected_return:.4f}%"
|
||||
),
|
||||
diagnostics,
|
||||
)
|
||||
return Signal(position.symbol, "HOLD", 0.35, "torch_forecast: PyTorch hold confirmed", diagnostics)
|
||||
|
||||
|
||||
def _is_torch_forecast(forecast: dict) -> bool:
|
||||
model = str(forecast.get("model", "")).strip().lower()
|
||||
return bool(forecast.get("usable", False)) and model in {"torch_lstm", "torch_gru"}
|
||||
|
||||
|
||||
def _torch_min_probability(settings: Settings) -> float:
|
||||
return round(_clamp(settings.min_signal_confidence - 0.08, 0.52, 0.68), 4)
|
||||
|
||||
|
||||
def _torch_forecast_confidence(settings: Settings, forecast: dict) -> float:
|
||||
expected_return = max(0.0, _safe_float(forecast.get("expected_return_percent"), 0.0))
|
||||
probability_up = _safe_float(forecast.get("probability_up"), 0.5)
|
||||
skill = max(0.0, _safe_float(forecast.get("skill"), 0.0))
|
||||
min_edge = max(0.01, settings.time_series_min_edge_percent)
|
||||
edge_strength = _clamp(expected_return / max(min_edge * 4.0, 0.01), 0.0, 1.0)
|
||||
probability_strength = _clamp((probability_up - 0.50) / 0.25, 0.0, 1.0)
|
||||
skill_strength = _clamp(skill / 0.35, 0.0, 1.0)
|
||||
confidence = 0.45 + probability_strength * 0.30 + edge_strength * 0.20 + skill_strength * 0.10
|
||||
return round(_clamp(confidence, 0.0, 0.96), 4)
|
||||
|
||||
|
||||
def _torch_forecast_position_sizing(
|
||||
settings: Settings,
|
||||
account: dict | None,
|
||||
stop_loss_percent: float,
|
||||
forecast: dict,
|
||||
) -> dict[str, float | str]:
|
||||
base = _trend_position_sizing(settings, account, stop_loss_percent)
|
||||
base_notional = float(base["notional_usdt"])
|
||||
if base_notional <= 0:
|
||||
notional = 0.0
|
||||
edge_multiplier = probability_multiplier = skill_multiplier = 0.0
|
||||
else:
|
||||
expected_return = max(0.0, _safe_float(forecast.get("expected_return_percent"), 0.0))
|
||||
probability_up = _safe_float(forecast.get("probability_up"), 0.5)
|
||||
skill = max(0.0, _safe_float(forecast.get("skill"), 0.0))
|
||||
min_edge = max(0.01, settings.time_series_min_edge_percent)
|
||||
edge_multiplier = _clamp(expected_return / max(min_edge * 3.0, 0.01), 0.25, 1.15)
|
||||
probability_multiplier = _clamp(0.75 + (probability_up - 0.55) * 3.0, 0.50, 1.20)
|
||||
skill_multiplier = _clamp(0.85 + skill * 0.60, 0.60, 1.15)
|
||||
raw = base_notional * edge_multiplier * probability_multiplier * skill_multiplier
|
||||
notional = 0.0 if raw < settings.min_position_usdt else min(raw, settings.max_position_usdt)
|
||||
return {
|
||||
**base,
|
||||
"method": "torch_forecast_risk",
|
||||
"notional_usdt": round(notional, 2),
|
||||
"base_notional_usdt": base["notional_usdt"],
|
||||
"torch_edge_multiplier": round(edge_multiplier, 4),
|
||||
"torch_probability_multiplier": round(probability_multiplier, 4),
|
||||
"torch_skill_multiplier": round(skill_multiplier, 4),
|
||||
}
|
||||
|
||||
|
||||
def _has_trend_entry_indicators(current: Candle, previous: Candle, trend: Candle) -> bool:
|
||||
return all(
|
||||
value is not None
|
||||
|
||||
Reference in New Issue
Block a user