0

I want to trigger an alert on the script executed below. An alert should be triggered at the end of the up or down movement. Therefore, when the green up line comes to an end, the alert should be triggered on the next candle. It should be achieved that the alert is only triggered when the trend change is found. The evaluation of the end point of the trend can always lead to an extension of the evaluation. I managed to query the last calculation up to the end of the trend, but I didn't manage to only set an alert that triggers on a new candle after the trend change. Is there a solution for this with this script?

Screen of indicator

//@version=5
indicator("Test 41", max_lines_count=30, overlay = false)

// { Inputs
// -----------------------------------------------------------------------------
indiversion     = input.string(              "DRO",   "==== Select Indicator ====", options=["DRO", "DPO", "BOTH"])
src             = input.source(              close,                     "  Source")


detrendS        = input.string(           "On",                         "  Detrend", options = ["On", "Off"], group="DPO", inline="de")

fastLength      = input.int(                  3,                "  Fast  - ", group="DPO", inline="l")
slowLength      = input.int(                 50,                " Slow:", group="DPO", inline="l")

//... ZigZag
zigperiod       = input.int(                 15,                     "  Period", minval=5, group="ZigZag", inline="p")
zigstyle        = input.string(           "Solid",                     "  Line -", options = ["Solid", "Dotted"], group="ZigZag", inline="line")
zigwidth        = input.int(                  4,                  " Width  -- ", options=[1,2,3,4], group="ZigZag", inline="line") 

showdistanceS   = input.string(           "On",                         "  Labels", options = ["On", "Off"], group="ZigZag", inline="switch")
showdetrendedS  = input.string(           "On",                       " Detrended", options = ["On", "Off"], group="ZigZag", inline="switch") 

upcolor         = input(defval = #43f80c, title = "  Colors:  Up", group="ZigZag", inline="color")
downcolor       = input(defval = #f30909, title = " Down", group="ZigZag", inline="color")
txtcol          = input(defval = #787B86, title = " Text", group="ZigZag", inline="color")

//... Switches
showdistance    = (showdistanceS == "On") ? true : false 
showdetrended   = (showdetrendedS == "On") ? true : false  
detrenddata     = (detrendS == "On") ? true : false
// } Inputs

// { Variables
// -----------------------------------------------------------------------------
var max_array_size = 15
var ziggyzags = array.new_float(0)
var zzl = array.new_float(0)
var dir1 = 0
var lastPivot=0
var lastPivotDistance=0
// } Variables

// { Functions
// -----------------------------------------------------------------------------

add_to_zigzag(pointer, value, bindex)=>
    array.unshift(pointer, bindex)
    array.unshift(pointer, value)
    if array.size(pointer) > max_array_size
        array.pop(pointer)
        array.pop(pointer)
    
update_zigzag(pointer, value, bindex, dir)=>
    if array.size(pointer) == 0
        add_to_zigzag(pointer, value, bindex)
    else
        if (dir == 1 and value > array.get(pointer, 0)) or (dir == -1 and value < array.get(pointer, 0))
            array.set(pointer, 0, value)
            array.set(pointer, 1, bindex)
        0.

// } Functions

// { Calculations
// -----------------------------------------------------------------------------

//... DPO - Detrending
ppo = if detrenddata
    100 * (ta.ema(src, fastLength) - ta.ema(src, slowLength)) / ta.ema(src, slowLength)
else
    ppo=src

//... ZigZag
float highs = ta.highestbars(ppo, zigperiod) == 0 ? ppo : na
float lows = ta.lowestbars(ppo, zigperiod) == 0 ? ppo : na

dir1 := highs and na(lows) ? 1 : lows and na(highs) ? -1 : dir1

dir1changed = ta.change(dir1)
if highs or lows
    if dir1changed 
        add_to_zigzag(ziggyzags, dir1 == 1 ? highs : lows, bar_index)
        
    else
        update_zigzag(ziggyzags, dir1 == 1 ? highs : lows, bar_index, dir1)

// } Calculations

// { Plots
// -----------------------------------------------------------------------------

//... Detrended Price Oscillator
plot(indiversion=="DPO" or indiversion=="BOTH" ? ppo : na)

//... Detrended Zig-Zag lines with distance labels
if array.size(ziggyzags) >= 6
    var line zzline1 = na
    var label zzlabel1 = na
    float val = array.get(ziggyzags, 0)
    int point = math.round(array.get(ziggyzags, 1))

    if ta.change(val) or ta.change(point)
        float val1 = array.get(ziggyzags, 2)
        int point1 = math.round(array.get(ziggyzags, 3))
        plabel="?"
        lastPivotDistance:=point-lastPivot
        lastPivot:=point1
        //label.new(bar_index, na, "", color=color.rgb(233, 24, 9), textcolor=color.black, style=label.style_label_down, yloc=yloc.price)
        
        if ta.change(val1) == 0 and ta.change(point1) == 0
            line.delete(zzline1)
            label.delete(zzlabel1)  // 
            //label.new(bar_index, na, "", color=color.green, textcolor=color.black, style=label.style_label_down, yloc=yloc.price)
            
            if (array.size(zzl) >1)
                lastDistance=array.get(zzl,1)
                plabel:= str.tostring(lastDistance+lastPivotDistance)
                
            if (array.size(zzl)>0)
                array.shift(zzl)
            0.
        else
            if (array.size(zzl) >0)
                lastPivotDistance:=point-lastPivot
                if (array.size(zzl) >1)
                    int nw=math.round(array.get(zzl,0))
                    plabel:=str.tostring(lastPivotDistance+nw) 
                0
            else
                if (array.size(zzl)>0)
                    array.shift(zzl)
                0
            0.
            
        if(indiversion=="DRO" or indiversion=="BOTH")
            zzline1 := line.new(x1 = point, x2 = point1, y1 = showdetrended ? dir1 == 1 ? 100 : -100 : val , y2 = showdetrended ? dir1 == 1 ? -100 : 100 : val1, color = dir1 == 1 ? upcolor : downcolor, width = zigwidth, style = zigstyle == "Solid" ? line.style_solid : line.style_dotted)

        if ((indiversion=="DRO" or indiversion=="BOTH") and showdistance)
            zzlabel1 := label.new(x = point, y = showdetrended ? (dir1 == 1 ? 100 : -125) : val,  text = plabel, textcolor = txtcol, style = label.style_none) 
            array.unshift(zzl, lastPivotDistance)

//... zzl hold the lengths of the distance between h-h, l-l / only need the last 3 points
if array.size(zzl) > max_array_size
    array.pop(zzl)
    
    
// } Plots      
Max
  • 19
  • 1

0 Answers0