From 7e037b1c65fd4e31fdd6ef8088a3009af6529454 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:37:35 +0530 Subject: [PATCH] fix: qtpylib cci uses mean deviation over the window np.std of the whole rolling-mean column put CCI on the wrong scale and made past bars change with later candles. --- technical/vendor/qtpylib/indicators.py | 3 ++- tests/test_indicators.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/technical/vendor/qtpylib/indicators.py b/technical/vendor/qtpylib/indicators.py index 2fd7fe26..6cbbdf09 100644 --- a/technical/vendor/qtpylib/indicators.py +++ b/technical/vendor/qtpylib/indicators.py @@ -556,7 +556,8 @@ def cci(series, window=14): """ price = typical_price(series) typical_mean = rolling_mean(price, window) - res = (price - typical_mean) / (0.015 * np.std(typical_mean)) + mean_deviation = price.rolling(window).apply(lambda x: np.abs(x - x.mean()).mean(), raw=True) + res = (price - typical_mean) / (0.015 * mean_deviation) return pd.Series(index=series.index, data=res) diff --git a/tests/test_indicators.py b/tests/test_indicators.py index 0783e991..5784e288 100644 --- a/tests/test_indicators.py +++ b/tests/test_indicators.py @@ -131,3 +131,14 @@ def test_rma_short_dataframe(): dataframe = DataFrame({"close": numpy.array([1.0, 2.0, 3.0])}) assert rma(dataframe, 5).isna().all() + + +def test_qtpylib_cci_matches_talib(testdata_1m_btc): + import talib + + from technical import qtpylib + + candles = testdata_1m_btc.iloc[:300] + result = qtpylib.cci(candles, 14) + expected = talib.CCI(candles["high"], candles["low"], candles["close"], 14) + numpy.testing.assert_allclose(result.iloc[13:], expected.iloc[13:], atol=1e-6)