How to get same cci values from Trading view in Golang?

659 views Asked by At

I'm trying to replicate values from pine script cci() function in golang. I've found this lib https://github.com/markcheno/go-talib/blob/master/talib.go#L1821 but it gives totally different values than cci function does

pseudo code how do I use the lib

cci := talib.Cci(latest14CandlesHighArray, latest14CandlesLowArray, latest14CandlesCloseArray, 14)

The lib gives me the following data

Timestamp: 2021-05-22 18:59:27.675, Symbol: BTCUSDT, Interval: 5m, Open: 38193.78000000, Close: 38122.16000000, High: 38283.55000000, Low: 38067.92000000, StartTime: 2021-05-22 18:55:00.000, EndTime: 2021-05-22 18:59:59.999, Sma: 38091.41020000, Cci0: -16.63898084, Cci1: -53.92565811,

While current cci values on TradingView are: cci0 - -136, cci1 - -49

What do I miss?

P.S. cci0 - current candle cci, cci1 - previous candle cci

1

There are 1 answers

0
bajaco On BEST ANSWER

PineScript has really great reference when looking for functions, usually even supplying the pine code to recreate it.

https://www.tradingview.com/pine-script-reference/v4/#fun_cci

The code wasn't provided for cci, but a step-by-step explanation was. Here is how I managed to recreate the cci function using Pine, following the steps in the reference:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bajaco

//@version=4
study("CCI Breakdown", overlay=false, precision=16)

cci_breakdown(src, p) =>
    // The CCI (commodity channel index) is calculated as the 
    // 1. difference between the typical price of a commodity and its simple moving average, 
    // divided by the 
    // 2. mean absolute deviation of the typical price. 
    // 3. The index is scaled by an inverse factor of 0.015 
    // to provide more readable numbers

    // 1. diff
    ma = sma(src,p)
    diff = src - ma
    
    // 2. mad
    s = 0.0
    for i = 0 to p - 1
        s := s + abs(src[i] - ma)
    mad = s / p
    
    // 3. Scaling
    mcci = diff/mad / 0.015
    mcci
    
plot(cci(close, 100))
plot(cci_breakdown(close,100))

I didn't know what mean absolute deviation meant, but at least in their implementation it appears to be taking the difference from the mean for each value in the range, but NOT changing the mean value as you go back.

I don't know Go but that's the logic.