from __future__ import annotations import json import math from dataclasses import asdict, dataclass, field from typing import Any from crypto_spot_bot.config import Settings from crypto_spot_bot.models import Candle @dataclass(slots=True) class TimeSeriesForecast: enabled: bool usable: bool model: str volatility_model: str expected_return_percent: float expected_price: float volatility_percent: float probability_up: float confidence_adjustment: float block_entry: bool validation_mae_percent: float baseline_mae_percent: float skill: float horizon: int reason: str candidates: list[dict[str, Any]] = field(default_factory=list) def as_dict(self) -> dict[str, Any]: return asdict(self) class TimeSeriesForecaster: def __init__(self, settings: Settings): self.settings = settings self._lstm_artifact_mtime: float | None = None self._lstm_artifact: dict[str, Any] = {} def forecast(self, candles: list[Candle], symbol: str | None = None) -> TimeSeriesForecast: if not self.settings.time_series_forecast_enabled: return _empty_forecast(False, "прогноз временных рядов выключен") closes = [float(candle.close) for candle in candles if candle.close > 0] min_candles = max(30, self.settings.time_series_min_candles) if len(closes) < min_candles: return _empty_forecast(True, "недостаточно свечей для PyTorch прогноза") returns = _log_returns(closes) if len(returns) < 20: return _empty_forecast(True, "недостаточно доходностей для PyTorch прогноза") artifact = self._load_lstm_artifact() model = _torch_recurrent_model_name(symbol, artifact) if not model or not _can_use_torch_recurrent(returns, symbol, artifact): return _empty_forecast(True, "нет валидной PyTorch LSTM/GRU модели для пары") entry = _torch_recurrent_entry(symbol, artifact) prediction = _torch_recurrent_predict(returns, symbol, artifact) if entry is None or prediction is None: return _empty_forecast(True, "PyTorch LSTM/GRU модель не смогла построить прогноз") horizon = max(1, self.settings.time_series_forecast_horizon) expected_return = prediction * horizon expected_price = closes[-1] * math.exp(expected_return) model_mae = _torch_validation_mae(entry, returns) baseline_mae = max(_float_entry(entry, "baseline_mae_percent", model_mae * 100) / 100, model_mae) uncertainty_one_step = max(model_mae, _return_scale(returns) * 0.25, 1e-9) uncertainty = uncertainty_one_step * math.sqrt(horizon) volatility_percent = uncertainty * 100 expected_return_percent = (math.exp(expected_return) - 1) * 100 probability_up = _normal_cdf(expected_return / max(uncertainty, 1e-9)) skill = _clamp(_float_entry(entry, "skill", 0.0), -1.0, 1.0) min_edge = max(0.0, self.settings.time_series_min_edge_percent) confidence_adjustment = _confidence_adjustment( expected_return_percent=expected_return_percent, probability_up=probability_up, skill=skill, min_edge=min_edge, max_adjustment=self.settings.time_series_max_adjustment, ) block_entry = bool(expected_return_percent <= -min_edge and probability_up <= 0.45) reason = _reason( model=model, expected_return_percent=expected_return_percent, probability_up=probability_up, skill=skill, block_entry=block_entry, ) return TimeSeriesForecast( enabled=True, usable=True, model=model, volatility_model="torch validation MAE", expected_return_percent=round(expected_return_percent, 4), expected_price=round(expected_price, 8), volatility_percent=round(volatility_percent, 4), probability_up=round(probability_up, 4), confidence_adjustment=round(confidence_adjustment, 4), block_entry=block_entry, validation_mae_percent=round(model_mae * 100, 4), baseline_mae_percent=round(baseline_mae * 100, 4), skill=round(skill, 4), horizon=horizon, reason=reason, candidates=[{"model": model, "mae_percent": round(model_mae * 100, 4)}], ) def _load_lstm_artifact(self) -> dict[str, Any]: if not self.settings.time_series_lstm_enabled: return {} path = self.settings.time_series_lstm_model_path try: stat = path.stat() except OSError: self._lstm_artifact_mtime = None self._lstm_artifact = {} return {} if self._lstm_artifact_mtime == stat.st_mtime: return self._lstm_artifact try: data = json.loads(path.read_text(encoding="utf-8")) except (OSError, json.JSONDecodeError): data = {} self._lstm_artifact = data if isinstance(data, dict) else {} self._lstm_artifact_mtime = stat.st_mtime return self._lstm_artifact def _empty_forecast(enabled: bool, reason: str) -> TimeSeriesForecast: return TimeSeriesForecast( enabled=enabled, usable=False, model="none", volatility_model="none", expected_return_percent=0.0, expected_price=0.0, volatility_percent=0.0, probability_up=0.5, confidence_adjustment=0.0, block_entry=False, validation_mae_percent=0.0, baseline_mae_percent=0.0, skill=0.0, horizon=0, reason=reason, ) def _log_returns(closes: list[float]) -> list[float]: return [math.log(closes[index] / closes[index - 1]) for index in range(1, len(closes))] def _torch_recurrent_model_name(symbol: str | None, artifact: dict[str, Any]) -> str | None: entry = _torch_recurrent_entry(symbol, artifact) if not entry: return None architecture = str(entry.get("architecture", "")).strip().lower() if architecture in {"lstm", "gru"}: return f"torch_{architecture}" model = str(entry.get("model", "")).strip().lower() return model if model in {"torch_lstm", "torch_gru"} else None def _torch_recurrent_entry(symbol: str | None, artifact: dict[str, Any]) -> dict[str, Any] | None: if artifact.get("type") != "pytorch_recurrent_forecaster": return None symbols = artifact.get("symbols") entry = symbols.get(symbol.upper()) if symbol and isinstance(symbols, dict) else None if not isinstance(entry, dict): default = artifact.get("default") entry = default if isinstance(default, dict) else None if not isinstance(entry, dict): return None if not isinstance(entry.get("state_dict"), dict): return None return entry def _can_use_torch_recurrent(returns: list[float], symbol: str | None, artifact: dict[str, Any]) -> bool: entry = _torch_recurrent_entry(symbol, artifact) if not entry: return False lookback = int(_clamp(_float_entry(entry, "lookback", 0.0), 4.0, 512.0)) hidden_size = int(_clamp(_float_entry(entry, "hidden_size", 0.0), 1.0, 512.0)) num_layers = int(_clamp(_float_entry(entry, "num_layers", 1.0), 1.0, 8.0)) return len(returns) >= lookback + 1 and hidden_size > 0 and num_layers > 0 def _torch_recurrent_predict( returns: list[float], symbol: str | None, artifact: dict[str, Any], ) -> float | None: entry = _torch_recurrent_entry(symbol, artifact) model_name = _torch_recurrent_model_name(symbol, artifact) if not entry or not model_name: return None lookback = int(_clamp(_float_entry(entry, "lookback", 0.0), 4.0, 512.0)) hidden_size = int(_clamp(_float_entry(entry, "hidden_size", 0.0), 1.0, 512.0)) num_layers = int(_clamp(_float_entry(entry, "num_layers", 1.0), 1.0, 8.0)) mean = _float_entry(entry, "mean", 0.0) scale = max(_float_entry(entry, "scale", _return_scale(returns)), 1e-8) clip = _clamp(_float_entry(entry, "clip", 8.0), 1.0, 50.0) if len(returns) < lookback: return None normalized = [_clamp((value - mean) / scale, -clip, clip) for value in returns[-lookback:]] try: hidden = _torch_recurrent_hidden( normalized, entry=entry, model_name=model_name, hidden_size=hidden_size, num_layers=num_layers, ) if hidden is None: return None head_weight = _float_vector(entry.get("head_weight")) head_bias = _float_entry(entry, "head_bias", 0.0) if len(head_weight) != hidden_size: return None normalized_prediction = sum(weight * value for weight, value in zip(head_weight, hidden)) + head_bias if not math.isfinite(normalized_prediction): return None prediction = _clamp(normalized_prediction, -clip, clip) * scale + mean except (IndexError, KeyError, TypeError, ValueError, OverflowError): return None recent_abs = sorted(abs(value) for value in returns[-48:]) if len(returns) >= 8 else [0.01] cap = max(recent_abs[int(len(recent_abs) * 0.9)], 0.0002) return _clamp(prediction, -cap, cap) def _torch_recurrent_hidden( normalized: list[float], *, entry: dict[str, Any], model_name: str, hidden_size: int, num_layers: int, ) -> list[float] | None: state = entry.get("state_dict") if not isinstance(state, dict): return None h_layers = [[0.0 for _ in range(hidden_size)] for _ in range(num_layers)] c_layers = [[0.0 for _ in range(hidden_size)] for _ in range(num_layers)] for value in normalized: layer_input = [value] for layer in range(num_layers): if model_name == "torch_lstm": next_hidden, next_cell = _torch_lstm_step(layer_input, h_layers[layer], c_layers[layer], state, layer) h_layers[layer] = next_hidden c_layers[layer] = next_cell elif model_name == "torch_gru": h_layers[layer] = _torch_gru_step(layer_input, h_layers[layer], state, layer) else: return None layer_input = h_layers[layer] return h_layers[-1] def _torch_lstm_step( inputs: list[float], hidden: list[float], cell: list[float], state: dict[str, Any], layer: int, ) -> tuple[list[float], list[float]]: hidden_size = len(hidden) gates = _torch_gate_values(inputs, hidden, state, layer, gate_count=4) input_gate = [_sigmoid(value) for value in gates[0]] forget_gate = [_sigmoid(value) for value in gates[1]] cell_gate = [math.tanh(value) for value in gates[2]] output_gate = [_sigmoid(value) for value in gates[3]] next_cell = [ forget_gate[index] * cell[index] + input_gate[index] * cell_gate[index] for index in range(hidden_size) ] next_hidden = [ output_gate[index] * math.tanh(next_cell[index]) for index in range(hidden_size) ] return next_hidden, next_cell def _torch_gru_step( inputs: list[float], hidden: list[float], state: dict[str, Any], layer: int, ) -> list[float]: hidden_size = len(hidden) weight_ih = _float_matrix(state[f"weight_ih_l{layer}"]) weight_hh = _float_matrix(state[f"weight_hh_l{layer}"]) bias_ih = _float_vector(state[f"bias_ih_l{layer}"]) bias_hh = _float_vector(state[f"bias_hh_l{layer}"]) def gate_input(gate: int) -> list[float]: start = gate * hidden_size output = [] for index in range(hidden_size): row = start + index output.append(_dot(weight_ih[row], inputs) + bias_ih[row]) return output def gate_hidden(gate: int) -> list[float]: start = gate * hidden_size output = [] for index in range(hidden_size): row = start + index output.append(_dot(weight_hh[row], hidden) + bias_hh[row]) return output reset_input = gate_input(0) update_input = gate_input(1) new_input = gate_input(2) reset_hidden = gate_hidden(0) update_hidden = gate_hidden(1) new_hidden = gate_hidden(2) reset_gate = [_sigmoid(reset_input[index] + reset_hidden[index]) for index in range(hidden_size)] update_gate = [_sigmoid(update_input[index] + update_hidden[index]) for index in range(hidden_size)] candidate = [ math.tanh(new_input[index] + reset_gate[index] * new_hidden[index]) for index in range(hidden_size) ] return [ (1 - update_gate[index]) * candidate[index] + update_gate[index] * hidden[index] for index in range(hidden_size) ] def _torch_gate_values( inputs: list[float], hidden: list[float], state: dict[str, Any], layer: int, gate_count: int, ) -> list[list[float]]: hidden_size = len(hidden) weight_ih = _float_matrix(state[f"weight_ih_l{layer}"]) weight_hh = _float_matrix(state[f"weight_hh_l{layer}"]) bias_ih = _float_vector(state[f"bias_ih_l{layer}"]) bias_hh = _float_vector(state[f"bias_hh_l{layer}"]) gates: list[list[float]] = [] for gate in range(gate_count): values = [] start = gate * hidden_size for index in range(hidden_size): row = start + index values.append(_dot(weight_ih[row], inputs) + _dot(weight_hh[row], hidden) + bias_ih[row] + bias_hh[row]) gates.append(values) return gates def _torch_validation_mae(entry: dict[str, Any], returns: list[float]) -> float: mae_percent = _float_entry(entry, "validation_mae_percent", 0.0) if mae_percent > 0: return mae_percent / 100 return _return_scale(returns) def _float_entry(data: dict[str, Any], key: str, default: float) -> float: value = data.get(key) if isinstance(value, (int, float)): return float(value) if isinstance(value, str): try: return float(value) except ValueError: return default return default def _float_vector(data: Any) -> list[float]: if not isinstance(data, list): return [] return [float(value) for value in data] def _float_matrix(data: Any) -> list[list[float]]: if not isinstance(data, list): return [] return [_float_vector(row) for row in data] def _dot(left: list[float], right: list[float]) -> float: return sum(left[index] * right[index] for index in range(min(len(left), len(right)))) def _return_scale(returns: list[float]) -> float: recent = returns[-120:] if len(returns) > 120 else returns values = sorted(abs(value) for value in recent if math.isfinite(value)) if not values: return 0.0005 median = values[len(values) // 2] mean = sum(values) / len(values) return max(max(median, mean * 0.5), 1e-5) def _sigmoid(value: float) -> float: if value >= 40: return 1.0 if value <= -40: return 0.0 return 1 / (1 + math.exp(-value)) def _confidence_adjustment( *, expected_return_percent: float, probability_up: float, skill: float, min_edge: float, max_adjustment: float, ) -> float: edge = abs(expected_return_percent) - min_edge if edge <= 0: return 0.0 direction = 1.0 if expected_return_percent > 0 and probability_up >= 0.55 else -1.0 if direction < 0 and probability_up > 0.45: return 0.0 strength = _clamp(edge / max(min_edge, 0.05), 0.0, 1.0) probability_strength = _clamp(abs(probability_up - 0.5) / 0.25, 0.0, 1.0) skill_strength = _clamp((skill + 0.03) / 0.18, 0.25, 1.0) return direction * _clamp(max_adjustment, 0.0, 0.18) * strength * probability_strength * skill_strength def _reason( *, model: str, expected_return_percent: float, probability_up: float, skill: float, block_entry: bool, ) -> str: if block_entry: return f"модель {model}: ожидаемое движение вниз {expected_return_percent:.3f}%, P(рост)={probability_up:.2f}" return f"модель {model}: прогноз {expected_return_percent:.3f}%, P(рост)={probability_up:.2f}, skill={skill:.3f}" def _normal_cdf(value: float) -> float: return 0.5 * (1 + math.erf(value / math.sqrt(2))) def _clamp(value: float, low: float, high: float) -> float: return max(low, min(high, value))