如何从Golang的Trading视图中获得相同的cci值?

ctehm74n  于 2023-04-27  发布在  Go
关注(0)|答案(1)|浏览(103)

我试图从golang中的pine脚本cci()函数中复制值。我找到了这个库https://github.com/markcheno/go-talib/blob/master/talib.go#L1821,但它给出的值与cci函数完全不同
伪代码如何使用库

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

lib提供了以下数据

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,

而TradingView上的当前cci值为:cci0 --136,cci1 - -49
有没有人能告诉我我错过了什么?
谢谢你
P.S. cci 0-当前蜡烛cci,cci 1-上一个蜡烛cci

laawzig2

laawzig21#

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) =>
    // 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))

我不知道平均绝对偏差是什么意思,但至少在他们的实现中,它似乎是从范围内的每个值的平均值的差异,但不改变平均值,因为你回去。
我不知道围棋,但这就是逻辑。

相关问题