pine-scriptalert

Assistance required with specific alert conditions on a complex pine script


i have attempted to add alerts to the following Pivot point script but i can't get it to function correct.

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator("Example Pivot high/low",overlay = true, max_bars_back=5000)


if not na(pvtHighTemp)
    if pvtHighTemp > pvtHigh1Temp
        label.delete(tempHigh[1])
        tempHigh := label.new(bar_index[pvtLengthTemp], pvtHighTemp, '* ' + str.tostring(pvtHighTemp, format.mintick), xloc.bar_index, yloc.price, #284aa9, label.style_label_down, color.white, size.small)
    pvtHigh1Temp := pvtHighTemp
    
if high > pvtHigh1Temp
    label.delete(tempHigh[1])

if not na(pvtLowTemp)
    if pvtLowTemp < pvtLow1Temp
        label.delete(tempLow[1])
        tempLow := label.new(bar_index[pvtLengthTemp], pvtLowTemp, '* ' + str.tostring(pvtLowTemp, format.mintick), xloc.bar_index, yloc.price, #284aa9, label.style_label_up, color.white, size.small)
    pvtLow1Temp  := pvtLowTemp

if low < pvtLow1Temp
    label.delete(tempLow[1])

i Tried the following:

alertcondition(not na(tempHigh), title="Pivot TOP Alert", message="New Pivot TOP Signal")
alertcondition(not na(tempLow), title="Pivot BOTTOM Alert", message="New Pivot BOTTOM Signal")

The Result:

it works but if there is already a temporary pivot high/low then the alert triggers instantaneously once activated.

What i am expecting:

It needs to trigger the alerts with every new temporary pivot high/low regardless of any existing temporary pivots. For example lets say there is already a temp pivot high and i activate the top pivot alert then it must only trigger with the next new temp pivot high AND NOT trigger instantaneously because of the already existing temp pivot high.

P.S. i am not a wizard or a professional coder so i am finding this very difficult. Any assistance would be greatly appreciated


Solution

  • When you want to add alerts to such scripts, it is a good idea to check where those labels are created. So, you can start by searching label.new in the code.

    Since you want to use temporary high and lows, you want to receive an alert whenever tempHigh and tempLow labels are created. Just add a flag in that if block and use it later to trigger your code.

    is_new_temp_high = false
    is_new_temp_low = false
    
    if not na(pvtHighTemp)
        if pvtHighTemp > pvtHigh1Temp
            is_new_temp_high := true
            label.delete(tempHigh[1])
            tempHigh := label.new(bar_index[pvtLengthTemp], pvtHighTemp, '* ' + str.tostring(pvtHighTemp, format.mintick), xloc.bar_index, yloc.price, #284aa9, label.style_label_down, color.white, size.small)
        pvtHigh1Temp := pvtHighTemp
    
    if not na(pvtLowTemp)
        if pvtLowTemp < pvtLow1Temp
            is_new_temp_low := true
            label.delete(tempLow[1])
            tempLow := label.new(bar_index[pvtLengthTemp], pvtLowTemp, '* ' + str.tostring(pvtLowTemp, format.mintick), xloc.bar_index, yloc.price, #284aa9, label.style_label_up, color.white, size.small)
        pvtLow1Temp  := pvtLowTemp
    
    plotshape(is_new_temp_high, title="Pivot TOP Alert", color=color.green)
    plotshape(is_new_temp_low, title="Pivot BOTTOM Alert", color=color.red)
    
    alertcondition(is_new_temp_high, title="Pivot TOP Alert", message="New Pivot TOP Signal")
    alertcondition(is_new_temp_low, title="Pivot BOTTOM Alert", message="New Pivot BOTTOM Signal")
    

    I have added two plotshape()s so you can see exactly where your alerts would be triggered. Since this is a repainting script, they will not be triggered exactly on the pivot bars.

    enter image description here