build 051: switch HTF analysis to canonical candles
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from math import isfinite
|
||||
|
||||
from src.core.numbers import safe_float
|
||||
from src.core.types import JsonDict
|
||||
from src.integrations.exchange.service import ExchangeService
|
||||
@@ -52,7 +54,7 @@ def htf_volatility_context(
|
||||
}
|
||||
|
||||
try:
|
||||
batch = ExchangeService().get_klines(
|
||||
candles = ExchangeService().get_candles(
|
||||
symbol=symbol,
|
||||
interval=service._htf_interval,
|
||||
limit=service._htf_limit,
|
||||
@@ -67,10 +69,7 @@ def htf_volatility_context(
|
||||
"htf_reason": f"HTF_KLINES_ERROR: {exc}",
|
||||
}
|
||||
|
||||
candles = batch.candles
|
||||
closes = [item.close_price for item in candles]
|
||||
|
||||
if len(candles) < service._min_candles or not closes:
|
||||
if len(candles) < service._min_candles:
|
||||
return {
|
||||
"htf_interval": service._htf_interval,
|
||||
"htf_atr_percent": None,
|
||||
@@ -80,10 +79,15 @@ def htf_volatility_context(
|
||||
"htf_reason": "HTF_NOT_ENOUGH_CANDLES",
|
||||
}
|
||||
|
||||
close_price = safe_float(closes[-1])
|
||||
close_price = safe_float(candles[-1].close_price)
|
||||
atr_value = atr(candles, service._atr_period)
|
||||
|
||||
if close_price is None or close_price <= 0 or atr_value is None:
|
||||
if (
|
||||
close_price is None
|
||||
or not isfinite(close_price)
|
||||
or close_price <= 0
|
||||
or atr_value is None
|
||||
):
|
||||
return {
|
||||
"htf_interval": service._htf_interval,
|
||||
"htf_atr_percent": None,
|
||||
@@ -150,7 +154,7 @@ def htf_trend_context(
|
||||
}
|
||||
|
||||
try:
|
||||
batch = ExchangeService().get_klines(
|
||||
candles = ExchangeService().get_candles(
|
||||
symbol=symbol,
|
||||
interval=service._htf_interval,
|
||||
limit=service._htf_limit,
|
||||
@@ -158,12 +162,19 @@ def htf_trend_context(
|
||||
except Exception as exc:
|
||||
return _htf_unknown_context(f"HTF_KLINES_ERROR: {exc}")
|
||||
|
||||
candles = batch.candles
|
||||
closes = [item.close_price for item in candles]
|
||||
|
||||
if len(candles) < service._min_candles:
|
||||
return _htf_unknown_context("HTF_NOT_ENOUGH_CANDLES")
|
||||
|
||||
closes: list[float] = []
|
||||
|
||||
for candle in candles:
|
||||
close_value = safe_float(candle.close_price)
|
||||
|
||||
if close_value is None or not isfinite(close_value):
|
||||
return _htf_unknown_context("HTF_INDICATORS_UNAVAILABLE")
|
||||
|
||||
closes.append(close_value)
|
||||
|
||||
close_price = closes[-1] if closes else None
|
||||
ema_fast = ema(closes, service._fast_ema_period)
|
||||
ema_slow = ema(closes, service._slow_ema_period)
|
||||
@@ -369,7 +380,7 @@ def safe_volatility_state(value: object) -> VolatilityState | None:
|
||||
return VolatilityState(str(value))
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def _htf_unknown_context(reason: str) -> JsonDict:
|
||||
return {
|
||||
@@ -484,4 +495,4 @@ def _htf_confirmation_score(
|
||||
if trend_efficiency is not None:
|
||||
score += (trend_efficiency - 0.3) * 0.15
|
||||
|
||||
return max(0.0, min(1.0, score))
|
||||
return max(0.0, min(1.0, score))
|
||||
|
||||
Reference in New Issue
Block a user