如何在Golang中获取与TradingView相同的CCI值?

huangapple go评论81阅读模式
英文:

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

问题

我正在尝试在Golang中复制Pine Script的cci()函数的值。我找到了这个库https://github.com/markcheno/go-talib/blob/master/talib.go#L1821,但它给出的值与cci函数完全不同。

以下是如何使用该库的伪代码:

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

该库给出的数据如下:

时间戳:2021-05-22 18:59:27.675,交易对:BTCUSDT,间隔:5分钟,开盘价:38193.78000000,收盘价:38122.16000000,最高价:38283.55000000,最低价:38067.92000000,开始时间:2021-05-22 18:55:00.000,结束时间:2021-05-22 18:59:59.999,Sma:38091.41020000,Cci0:-16.63898084,Cci1:-53.92565811,

而TradingView上当前的cci值为:cci0为-136,cci1为-49。

我错过了什么?

附注:cci0表示当前蜡烛的cci值,cci1表示前一个蜡烛的cci值。

英文:

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

得分: 3

PineScript在查找函数时有非常好的参考文档,通常甚至提供了重新创建该函数的Pine代码。

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

虽然没有提供cci的代码,但提供了逐步解释。以下是我使用Pine按照参考文档中的步骤重新创建cci函数的方法:

// 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) =>
    // CCI(商品通道指数)的计算方法如下:
    // 1. 计算商品的典型价格与其简单移动平均值之间的差异,
    // 2. 除以典型价格的平均绝对偏差,
    // 3. 将指数按照0.015的倒数因子进行缩放,以提供更易读的数值

    // 1. 差异
    ma = sma(src,p)
    diff = src - ma
    
    // 2. 平均绝对偏差
    s = 0.0
    for i = 0 to p - 1
        s := s + abs(src[i] - ma)
    mad = s / p
    
    // 3. 缩放
    mcci = diff/mad / 0.015
    mcci
    
plot(cci(close, 100))
plot(cci_breakdown(close,100))

我不知道"mean absolute deviation"是什么意思,但至少在他们的实现中,它似乎是计算范围内每个值与均值的差异,但不会随着回溯而改变均值。

我不了解Go语言,但这就是逻辑。

英文:

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.

huangapple
  • 本文由 发表于 2021年5月23日 01:02:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/67652118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定