[Python] Pivot function results differ from TradingView

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using this code to calculate pivot points.

    def pivots_low(osc, LBR, LBL):
    pivots = []
    for i in range(len(osc) - LBR):
    pivots.append(0)
    pivot = True
    if i > LBL:
    for j in range(1, LBR + 1):
    if osc >= osc[i + j]:
    pivot = False
    for j in range(1, LBL + 1):
    if osc > osc[i - j]:
    pivot = False
    if pivot is True:
    pivots[len(pivots) - 1] = osc
    for i in range(LBR):
    pivots.append(0)
    return pivots


    This returns an array with 0's where there's no pivots and the value of the pivot if there is one.

    When Comparing the results to TradingView (downloaded csv with pivot points), the only time it matches exactly is when lookback left and right are both 5. Otherwise it deviates in the number of total pivots and the location of some.

    But using this code to calculate pivot highs:

    def pivots_high(osc, LBR, LBL):
    pivots = []
    for i in range(len(osc)-LBR):
    pivots.append(0)
    pivot = True
    if i > LBL:
    for j in range(1,LBL + 1):
    if osc < osc[i-j]:
    pivot = False
    for j in range(1,LBR + 1):
    if osc <= osc[i+j]:
    pivot = False
    if pivot is True:
    pivots[len(pivots)-1] = osc
    for i in range(LBR):
    pivots.append(0)
    return pivots


    the results are perfect regardless of lookback values. But the code is almost exactly the same besides comparison.

    What is going wrong here? This is day 3 of having this problem and I just cant fix it

    To Reproduce:

    Load Data:

    Full_Data = pd.read_csv(file)


    use this simple function to check matches between calculated pivots and TradingView pivots.

    def match_pivs(data, pivs_h, pivs_l): //Data is a DataFrame loaded from tradingview csv
    global lblh
    global lbrh
    global lbll
    global lbrl
    start = lbrh
    if lbrl > lbrh:
    start = lbrl
    match_h = 0
    tot_hd = 0
    tot_hp = 0
    match_l = 0
    tot_ld = 0
    tot_lp = 0
    for i in range(start, len(data)):
    if data['PivHigh'] != 0 and pivs_h[i-lbrh] != 0:
    match_h += 1
    if data['PivLow'] != 0 and pivs_l[i-lbrl] != 0:
    match_l += 1
    if data['PivHigh'] != 0:
    tot_hd += 1
    if data['PivLow'] != 0:
    tot_ld += 1
    if pivs_h != 0:
    tot_hp += 1
    if pivs_l != 0:
    tot_lp += 1
    print('PivsLow ' + str(tot_lp))
    print('DataLows ' + str(tot_ld))
    print('MatchesL ' + str(match_l))
    print('PivsHigh ' + str(tot_hp))
    print('DataHighs ' + str(tot_hd))
    print('MatchesH ' + str(match_h))


    and to get csv from TradingView:

    //@version=5
    indicator("Data Script", overlay=true, max_labels_count=500)

    leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle)
    rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle)
    leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle)
    rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle)
    ph = ta.pivothigh(leftLenH, rightLenH)
    pl = ta.pivotlow(leftLenL, rightLenL)
    if not na(ph)
    plth := ph
    else
    plth := 0.0
    if not na(pl)
    pltl := pl
    else
    pltl := 0.0
    plot(plth, 'PivHigh')
    plot(pltl, 'PivLow')


    then just download csv with this script loaded.

    Run program with these three lines:

    pl = pivots_low(Full_Data['low'], lbll, lbrl)
    ph = pivots_high(Full_Data['high'], lbrh, lblh)
    match_pivs(Full_Data, ph, pl)

    Continue reading...

Compartilhe esta Página