"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(indicators): stop rounding Bollinger and MACD to zero for sub-penny prices
Rudy Celekli5906fb563caa
diff --git a/agent/src/tools/technical_indicator_tool.py b/agent/src/tools/technical_indicator_tool.pyindex e9f87fb36..f3902cfd7 100644--- a/agent/src/tools/technical_indicator_tool.py+++ b/agent/src/tools/technical_indicator_tool.py@@ -10,6 +10,7 @@ from __future__ import annotationsimport hashlibimport jsonimport logging+import mathfrom datetime import date, datetime, timedeltafrom typing import Any@@ -192,6 +193,20 @@ def _compute_rsi(close: pd.Series, period: int = _RSI_PERIOD) -> float | None:return float(100.0 - (100.0 / (1.0 + rs)))+def _round_keeping_precision(value: float, min_decimals: int, reference: float | None = None) -> float:+ """Round to ``min_decimals`` places, widening for tiny magnitudes.++ A fixed 2- or 4-decimal round collapses the output of a sub-penny asset+ (e.g. a 1.2e-5 crypto price) to 0.0. Keep at least four significant digits+ of ``reference`` (default ``value``); ordinary prices keep ``min_decimals``.+ """+ ref = abs(value if reference is None else reference)+ if not math.isfinite(ref) or ref == 0:+ return round(value, min_decimals)+ decimals = max(min_decimals, 3 - math.floor(math.log10(ref)))+ return round(value, min(decimals, 15))++def _compute_macd(close: pd.Series,fast: int = _MACD_FAST,@@ -206,10 +221,12 @@ def _compute_macd(macd_line = ema_fast - ema_slowsignal_line = macd_line.ewm(span=signal, adjust=False).mean()histogram = macd_line - signal_line+ macd_last = float(macd_line.iloc[-1])+ signal_last = float(signal_line.iloc[-1])return {- "macd_line": round(float(macd_line.iloc[-1]), 4),- "signal_line": round(float(signal_line.iloc[-1]), 4),- "histogram": round(float(histogram.iloc[-1]), 4),+ "macd_line": _round_keeping_precision(macd_last, 4, reference=macd_last),+ "signal_line": _round_keeping_precision(signal_last, 4, reference=macd_last),+ "histogram": _round_keeping_precision(float(histogram.iloc[-1]), 4, reference=macd_last),}@@ -223,10 +240,12 @@ def _compute_bollinger(return Nonesma = close.rolling(window=period).mean()std = close.rolling(window=period).std()+ middle = float(sma.iloc[-1])+ spread = num_std * float(std.iloc[-1])return {- "upper": round(float(sma.iloc[-1] + num_std * std.iloc[-1]), 2),- "middle": round(float(sma.iloc[-1]), 2),- "lower": round(float(sma.iloc[-1] - num_std * std.iloc[-1]), 2),+ "upper": _round_keeping_precision(middle + spread, 2, reference=middle),+ "middle": _round_keeping_precision(middle, 2, reference=middle),+ "lower": _round_keeping_precision(middle - spread, 2, reference=middle),}diff --git a/agent/tests/test_technical_indicator_small_price_rounding.py b/agent/tests/test_technical_indicator_small_price_rounding.pynew file mode 100644index 000000000..4d32de1ef--- /dev/null+++ b/agent/tests/test_technical_indicator_small_price_rounding.py@@ -0,0 +1,36 @@+"""Sub-penny assets must not have their Bollinger/MACD output rounded to zero."""++from __future__ import annotations++import numpy as np+import pandas as pd++from src.tools.technical_indicator_tool import _compute_bollinger, _compute_macd+++def _series(scale: float) -> pd.Series:+ rng = np.random.RandomState(0)+ return pd.Series(scale * np.cumprod(1 + rng.normal(0, 0.02, 80)))+++def test_bollinger_keeps_precision_for_sub_penny_price() -> None:+ bands = _compute_bollinger(_series(1.2e-5))+ assert bands is not None+ assert bands["lower"] > 0+ assert bands["lower"] < bands["middle"] < bands["upper"]+++def test_macd_keeps_precision_for_sub_penny_price() -> None:+ macd = _compute_macd(_series(1.2e-5))+ assert macd is not None+ assert macd["macd_line"] != 0.0+ assert macd["signal_line"] != 0.0+++def test_ordinary_price_rounding_is_unchanged() -> None:+ close = _series(150.0)+ bands = _compute_bollinger(close)+ macd = _compute_macd(close)+ assert bands is not None and macd is not None+ assert all(round(v, 2) == v for v in bands.values())+ assert all(round(v, 4) == v for v in macd.values())