Add fallback_reference for deviation when insufficient data points

Add optional min_data_points (default 100) and fallback_reference config
fields to indicator definitions. When available daily data points are
below min_data_points, the fallback_reference value is used as the
deviation reference instead of the computed mean.

Applied to ratio, price, spread, and diff_pct indicator types.
Configured WTI/BRENT ratio with fallback_reference=0.96065.
This commit is contained in:
DiTus
2026-07-29 09:36:35 +02:00
parent 63bab43557
commit 8b88aee61f
3 changed files with 50 additions and 7 deletions

View File

@ -227,7 +227,12 @@ class IndicatorCalculator:
if ind_def.get("show_deviation", False):
ratios = self._get_all_ratio(num, den, "1d")
if ratios:
reference = sum(ratios) / len(ratios)
min_points = ind_def.get("min_data_points", 100)
fallback_ref = ind_def.get("fallback_reference")
if len(ratios) < min_points and fallback_ref is not None:
reference = fallback_ref
else:
reference = sum(ratios) / len(ratios)
deviation = self._format_change(current, reference)
return {"value": current, "reference": reference, "changes": changes, "deviation": deviation}
@ -250,7 +255,12 @@ class IndicatorCalculator:
if ind_def.get("show_deviation", False):
closes = self._get_all_closes(coin, "1d")
if closes:
reference = sum(closes) / len(closes)
min_points = ind_def.get("min_data_points", 100)
fallback_ref = ind_def.get("fallback_reference")
if len(closes) < min_points and fallback_ref is not None:
reference = fallback_ref
else:
reference = sum(closes) / len(closes)
deviation = self._format_change(current, reference)
return {"value": current, "reference": reference, "changes": changes, "deviation": deviation}
@ -281,7 +291,12 @@ class IndicatorCalculator:
if ind_def.get("show_deviation", False):
spreads = self._get_all_spread(num, den, "1d")
if spreads:
reference = sum(spreads) / len(spreads)
min_points = ind_def.get("min_data_points", 100)
fallback_ref = ind_def.get("fallback_reference")
if len(spreads) < min_points and fallback_ref is not None:
reference = fallback_ref
else:
reference = sum(spreads) / len(spreads)
deviation = self._format_change(current, reference)
return {"value": current, "reference": reference, "changes": changes, "deviation": deviation}
@ -312,7 +327,12 @@ class IndicatorCalculator:
if ind_def.get("show_deviation", False):
diffs = self._get_all_diff_pct(num, den, "1d")
if diffs:
reference = sum(diffs) / len(diffs)
min_points = ind_def.get("min_data_points", 100)
fallback_ref = ind_def.get("fallback_reference")
if len(diffs) < min_points and fallback_ref is not None:
reference = fallback_ref
else:
reference = sum(diffs) / len(diffs)
deviation = self._format_change(current, reference)
return {"value": current, "reference": reference, "changes": changes, "deviation": deviation}