pivothigh() 和 pivotlow() 函数如何在 Tradingview Pinescript 上工作?

Ins*_*nso 3 tradingview-api pine-script

我正在尝试将脚本重写为 Python,但我无法弄清楚 pivothigh() 和 pivotlow() 函数是如何工作的,而且我找不到源代码,我知道如何计算枢轴点,但是 leftbars 和rightbars 在这两个函数中意味着什么?请帮忙。

小智 14

我也需要更好地了解pivothigh()和函数内部如何工作,因此我努力为自己编写了一个 Pine Script 版本(使用 Pine Script 版本 5),并与和pivotlow()函数并排测试了它,看起来好好工作。也许这也会对你有帮助。ta.pivotlow()ta.pivothigh()

my_pivothigh(float _series = high, int _leftBars, int _rightBars) =>
    float _pivotHigh = na
    int _pivotRange = ( _leftBars + _rightBars )
    float _leftEdgeValue = nz(_series[_pivotRange], na)
    if not na(_series) and _leftBars > 0 and _rightBars > 0 and not na(_leftEdgeValue)
        float _possiblePivotHigh = _series[_rightBars]
        float[] _arrayOfSeriesValues = array.new_float(0)
        for _barIndex = _pivotRange to 0
            array.push(_arrayOfSeriesValues, _series[_barIndex])
        //end for
        int _pivotHighRightBars = array.size(_arrayOfSeriesValues) - array.lastindexof(_arrayOfSeriesValues, array.max(_arrayOfSeriesValues)) - 1
        _pivotHigh := ( _pivotHighRightBars == _rightBars ) ? _possiblePivotHigh : na
    //end if
    _pivotHigh
Run Code Online (Sandbox Code Playgroud)
my_pivotlow(float _series = low, int _leftBars, int _rightBars) =>
    float _pivotLow = na
    int _pivotRange = ( _leftBars + _rightBars )
    float _leftEdgeValue = nz(_series[_pivotRange], na)
    if not na(_series) and _leftBars > 0 and _rightBars > 0 and not na(_leftEdgeValue)
        float _possiblePivotLow = _series[_rightBars]
        float[] _arrayOfSeriesValues = array.new_float(0)
        for _barIndex = _pivotRange to 0
            array.push(_arrayOfSeriesValues, _series[_barIndex])
        //end for
        int _pivotLowRightBars = array.size(_arrayOfSeriesValues) - array.lastindexof(_arrayOfSeriesValues, array.min(_arrayOfSeriesValues)) - 1
        _pivotLow := ( _pivotLowRightBars == _rightBars ) ? _possiblePivotLow : na
    //end if
    _pivotLow
Run Code Online (Sandbox Code Playgroud)


小智 8

我知道这是一篇旧帖子,但我做了一个非常简单的 python 实现,任何人都可以在其上构建,它与 Pine 脚本 ta.pivot 函数执行相同的操作。

代码:

def pivots_high(data, LBR, LBL):
    pivots = []
    for i in range(len(data)-LBR):
        pivots.append(0)
        pivot = True
        if i > LBL:
            for j in range(LBL + 1):
                if data[i - j] > data[I]:  # do if data[i - j] < data[i] for pivot low
                    pivot = False
            for j in range(LBR + 1):
                if data[i + j] > data[I]:  # do if data[i + j] < data[i] for pivot low
                    pivot = False
        if pivot is True:
            pivots[len(pivots)-1] = data[i]
    for p in range(LBR):
        pivots.append(0)  # This is so the pivots length matches your data length
    return pivots  # The Pivots will be any value that is not 0 and it will be where the lowest/highest value is
Run Code Online (Sandbox Code Playgroud)

回溯变量只是意味着,如果您采用一个价格点并向左看 n(回溯左)根蜡烛和向右 n(回溯右)蜡烛,并且仍然是最低/最高,那么这就是枢轴

更新

我在使用大型数据集上的不同数字组合时遇到了一些问题,因此我必须完全更改它以始终与交易视图匹配。

新代码:

def checkhl(data_back, data_forward, hl):
    if hl == 'high' or hl == 'High':
        ref = data_back[len(data_back)-1]
        for i in range(len(data_back)-1):
            if ref < data_back[i]:
                return 0
        for i in range(len(data_forward)):
            if ref <= data_forward[i]:
                return 0
        return 1
    if hl == 'low' or hl == 'Low':
        ref = data_back[len(data_back)-1]
        for i in range(len(data_back)-1):
            if ref > data_back[i]:
                return 0
        for i in range(len(data_forward)):
            if ref >= data_forward[i]:
                return 0
        return 1


def pivot(osc, LBL, LBR, highlow)
    left = []
    right = []
    for i in range(len(osc)):
        pivots.append(0.0)
        if i < LBL + 1:
            left.append(osc[i])
        if i > LBL:
            right.append(osc[i])
        if i > LBL + LBR:
            left.append(right[0])
            left.pop(0)
            right.pop(0)
            if checkhl(left, right, highlow):
                pivots[i - LBR] = osc[i - LBR]
    return pivots
Run Code Online (Sandbox Code Playgroud)

然后就做:

pivots_low = pivot(data, lbl, lbr, 'low')
pivots_high = pivot(data, lbl, lbr, 'high')
Run Code Online (Sandbox Code Playgroud)

输出是其实际位置的枢轴,否则为 0.0s。


e2e*_*2e4 7

Leftbars 和 rightbars 是pivot函数在搜索枢轴时查看的条数。例如: pivothigh(10,10)将搜索在左侧 10 根柱线(过去数据)和右侧 10 根柱线(未来数据)内未超过的高价。请注意,如果右侧的柱线少于 10 个,则该函数将无法确定枢轴点。

  • 正确,滞后 = rightbars+1,模式 = leftbars+rightbars+1 (3认同)

小智 5

您可以使用 pandas 构建类似的东西:

pivots = high_column.shift(-len_right, fill_value=0).rolling(len_left).max()
Run Code Online (Sandbox Code Playgroud)

对于“高”枢轴 pd.Series 'high_column' 和:

pivots = low_column.shift(-len_right, fill_value=0).rolling(len_left).min()
Run Code Online (Sandbox Code Playgroud)

对于“低点”。它避免使用循环,并且是一个快速矢量化函数。