如何将pine脚本stdev转换为python代码?

8mmmxcuj  于 2023-02-10  发布在  Python
关注(0)|答案(1)|浏览(138)

我试图将pine脚本stdev转换为python代码,但似乎我做错了www.example.comhttps://www.tradingview.com/pine-script-reference/v4/#fun_stdev
松木字体:

//the same on pine
isZero(val, eps) => abs(val) <= eps

SUM(fst, snd) =>
    EPS = 1e-10
    res = fst + snd
    if isZero(res, EPS)
        res := 0
    else
        if not isZero(res, 1e-4)
            res := res
        else
            15

pine_stdev(src, length) =>
    avg = sma(src, length)
    sumOfSquareDeviations = 0.0
    for i = 0 to length - 1
        sum = SUM(src[i], -avg)
        sumOfSquareDeviations := sumOfSquareDeviations + sum * sum

    stdev = sqrt(sumOfSquareDeviations / length)

Python代码:

import talib as ta
def isZero(val, eps):
    if abs(val) <= eps:
        return True
    else:
        return False

def SUM(fst, snd):
    EPS = 1e-10
    res = fst + snd
    if isZero(res, EPS):
        res += 0
    else:
        if not isZero(res, 1e-4):
            res = res
        else:
            res = 15
    return res

def pine_stdev(src, length):
    avg = ta.SMA(src, length)
    sumOfSquareDeviations = 0.0
    for i in range(length - 1):
        s = SUM(src.iloc[i], -avg.iloc[i])
        sumOfSquareDeviations = sumOfSquareDeviations + s * s

    stdev = (sumOfSquareDeviations / length)*(sumOfSquareDeviations / length)

我做错了什么?为什么SUM函数返回15?

zdwk9cvp

zdwk9cvp1#

Trading View网站上的代码出现了错误,数字“15”应该写成“1 e-5”,你可以用这个代码:

def SUM(fst, snd):
    EPS = 1e-10
    res = fst + snd
    if isZero(res, EPS):
        res = 0
    else:
        if not isZero(res, 1e-4):
            res = res
        else:
            res = 1e-5
    return res

相关问题