Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion technical/vendor/qtpylib/indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
11 changes: 11 additions & 0 deletions tests/test_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't cover actual problems (constant prices return NaN here - ta-lib returns 0.0).
There's also floating point errors depending on values - so it's may be matching ta-lib for this particular scenario - but that statement doesn't generalize.

It's also tons of times slower (as data amount increases).

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)