Adding alert to a tradingview script

  Kiến thức lập trình

I have a tradingview script that has no alert function, and I want to add one to it, which alerts me when the price touches the upper line of the bullish box

here’s the line I want to get alerts for

//@version=5
indicator("Implied Orderblock Breaker (Zeiierman)",overlay=true)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Tooltips {
t1 = "The 'Period' setting in the indicator determines the number of bars used to identify pivot highs and lows. Increasing this value leads to fewer but more significant pivot points, as it considers a wider range of data. Decreasing it results in more frequent pivot points but with potentially less significance, suitable for short-term analysis."
t2 = "The 'Pivot Surrounding' or 'candles' setting defines the number of candles analyzed around each pivot point to identify bullish or bearish patterns. Increasing this value extends the analysis range, possibly capturing more patterns but including less significant ones. Decreasing the value focuses on a narrower range, potentially highlighting more significant trade setups but missing broader patterns. This setting can be adjusted based on trading style: smaller numbers for short-term trades and larger numbers for broader market analysis."
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Inputs {
prd     = input.int(20, minval=1, title="Period", group="", inline="", tooltip=t1)
candles = input.int(5, minval=1, title="Pivot Surrounding", group="", inline="", tooltip=t2)
Bullish = input.color(color.lime, title="", group="", inline="color", tooltip="")
Bearish = input.color(color.red, title="", group="", inline="color", tooltip="")
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Declaration {
type Box
    array<box> bull
    array<box> bear 

b = bar_index
var bo = Box.new(array.new<box>(),array.new<box>())
var hi = float(na)
var lo = float(na)
var hiLoc = int(na)
var loLoc = int(na)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Pivots {
pvtHi = ta.pivothigh(high,prd,prd)
pvtLo = ta.pivotlow(low,prd,prd)

if not na(pvtHi)
    hi := high[prd]
    hiLoc := b-prd
if not na(pvtLo)
    lo := low[prd]
    loLoc := b-prd
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Logic {
if ta.crossover(close,hi) and lo<=ta.lowest(low,prd)
    for i=loLoc to loLoc+candles
        loc = b-i
        if close[loc]>open[loc] and low[loc-1]>open[loc]
            bo.bull.push(box.new(i,low[loc-1],b,open[loc],
             border_color=Bullish,bgcolor=color.new(Bullish,75),
             text=str.tostring(volume[loc],format.volume),text_color=chart.fg_color))
            line.new(hiLoc,high[b-hiLoc],b,high[b-hiLoc],color=Bullish)
            hi := float(na)
            break
if ta.crossunder(close,lo) and hi>=ta.highest(high,prd)
    for i=hiLoc to hiLoc+candles
        loc = b-i
        if close[loc]<open[loc] and high[loc-1]<open[loc]
            bo.bear.push(box.new(i,high[loc-1],b,open[loc],
             border_color=Bearish,bgcolor=color.new(Bearish,75),
             text=str.tostring(volume[loc],format.volume),text_color=chart.fg_color))
            line.new(loLoc,low[b-loLoc],b,low[b-loLoc],color=Bearish)
            lo := float(na)
            break
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

// ~~ Validation {
BoxCheck(arr,bube)=>
    for [i,element] in arr
        if bube and close<element.get_bottom() or not bube and close>element.get_top()
            arr.remove(i)
        else
            element.set_right(b)
    true
BoxCheck(bo.bull,true)
BoxCheck(bo.bear,false)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}

I tried adding an alert by different ways as alert() alertcondition() but everytime it either doesn’t alert me at all or alerts me only when the lower line of the bullish box is touched or alerts we when pivot high line is touched.

New contributor

Raid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT