Pine script Tradingview - Alerts not working

Discussion in 'App Development' started by DevBru, Aug 25, 2020.

  1. DevBru

    DevBru

    I want to add an alert to and (existing) indicator, unfortunately whatever i try it doesn't seem to give an alert when conditions are met.

    Here is how i wrote it:

    Cross1 = cross(close, day_low)
    Cross2 = cross(close, day_high)

    alertcondition(condition=(Cross1 or Cross2), message='Price Crossed Level')

    I have no coding experience so i tried to create it with information online, but with no succes.

    Any help would be appreciated!
     
  2. ssp729

    ssp729

  3. DevBru

    DevBru

    Thanks, did send them a message but they redirected me to another forum, asked the question there but there are almost no reply's in any of the old questions so i am assuming i will get no response there.

    Still hoping someone here can point me in the right direction, whatever i try i am unable to get it working.

    For those wondering, here is the original code:

    Code:
    study("Premarket High/Low", overlay=true)
    
    ending_hour = input(defval=9, title="Ending Hour", type=input.integer)
    ending_minute = input(defval=30, title="Ending Minute", type=input.integer)
    atr_factor = input(title="ATR Faktor", type=input.float, defval=1)
    aftermarket = input(title="Include Aftermarket", type=input.bool, defval=false)
    // LastOnly = input(title="Last only", type=input.bool, defval=false)
    
    t = (aftermarket == false) ? time("1440", "0000-1530") : time("1440", "1600-0000")  // 1440=60*24 is the number of minutes in a whole day. You may use "0930-1600" as second session parameter
    is_first = na(t[1]) and not na(t) or t[1] < t
    
    day_high = float(na)
    day_low = float(na)
    k = int(na)
    
    if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
        day_high := high
        day_low := low
        day_low
    else
        day_high := day_high[1]
        day_low := day_low[1]
        day_low
    
    if high > day_high and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
        day_high := high
        day_high
    
    if low < day_low and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
        day_low := low
        day_low
    
    // if LastOnly == true
    //     k := -9999
    // else
    //     k := 0
     
    dailyATR = security(syminfo.tickerid, 'D', atr(14))
    
    plot(day_high, style=plot.style_circles, trackprice=true, offset=-9999, color=color.yellow, linewidth=1)
    plot(day_high + dailyATR*atr_factor, style=plot.style_circles, trackprice=true, offset=-9999, color=color.white, linewidth=3)
    plot(day_low, style=plot.style_circles, trackprice=true, offset=-9999, color=color.yellow, linewidth=1)
    plot(day_low - dailyATR*atr_factor, style=plot.style_circles, trackprice=true, offset=-9999, color=color.white, linewidth=3)

    I would like to get an alert every time price crosses the high or low, i would like 1 alert for both high or low since my Tradingview account is limited to 30 alerts, it might be easier to set 1 alert for the high and 1 for the low but then i can only get alerts for 15 instruments, if i come across a busy day that might not be enough.
     
  4. DevBru

    DevBru

    Tried it like this but with no result either, alerts are not being triggered.

    alertcondition(cross(close,day_low), message = 'Price crossed Pre Market level')
    Code:
    alertcondition(cross(close,day_low), message = 'Price crossed Pre Market level')
    
     
  5. userque

    userque

    I don't know pinescript, but I code.

    My guess is that the close will never cross the low or high.

    The close can never be lower than the low ... the low becomes the close. Same with the high.

    There is probably more the one way to solve this.

    If pinescript allows, you can let Cross = cross(close, PREVIOUS day_low). Same with the HIGH.

    Or, you may be able to store the day_low into a variable, and compare the next bar to that variable instead.

    Keep us posted.
     
  6. DevBru

    DevBru

    Thanks.

    The high/low stop calculating at 9:30 and the high/low levels from pre market are plotted on the chart, i would need my alert triggered when those levels are crossed.

    I do understand close could never cross low or high if it was for the current bar, but when the low/high is calculated between a certain time that should work, right?
     
  7. userque

    userque

    I'll post after I research pinescript. May take several minutes.
     
  8. userque

    userque

    Ha! Just realized you posted the indicator code. That helps.

    However, I don't see your alert modifications to the code. Can you post the code after you added your alert code?
     
  9. DevBru

    DevBru

    Sure, i have adjusted the code from above a little bit and tried adding alerts, here are the 2 codes i tried.

    Code:
    study("4C PreMarket High/Low", shorttitle="4C PM_H/L", overlay=true)
    t = time("1440", "0000-0930")
    
    is_first = na(t[1]) and not na(t) or t[1] < t
    ending_hour = input(defval=9, title="Ending Hour", type=input.integer)
    ending_minute = input(defval=30, title="Ending Minute", type=input.integer)
    
    day_high = float(na)
    day_low = float(na)
    Cross1= cross(close,day_low)
    Cross2= cross(close, day_high)
    alertcondition(condition=(Cross1 or Cross2),message='Price crossed Pre Market level')
    
    if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
        day_high := high
        day_low := low
        day_low
    else
        day_high := day_high[1]
        day_low := day_low[1]
        day_low
    
    if high > day_high and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
        day_high := high
        day_high
    
    if low < day_low and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
        day_low := low
        day_low
    
    
    plot(day_high, style=plot.style_line, color=color.yellow, linewidth=1)
    plot(day_low, style=plot.style_line, color=color.orange, linewidth=1)
    
    And this one so far i only added an alert for the daily low since there is no use in adding the second one if the first one isn't working:

    Code:
    study("4C PreMarket High/Low", shorttitle="4C PM_H/L", overlay=true)
    t = time("1440", "0000-0930")
    
    is_first = na(t[1]) and not na(t) or t[1] < t
    ending_hour = input(defval=9, title="Ending Hour", type=input.integer)
    ending_minute = input(defval=30, title="Ending Minute", type=input.integer)
    
    day_high = float(na)
    day_low = float(na)
    
    alertcondition(cross(close,day_low), message = 'Price crossed Pre Market level')
    
    if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
        day_high := high
        day_low := low
        day_low
    else
        day_high := day_high[1]
        day_low := day_low[1]
        day_low
    
    if high > day_high and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
        day_high := high
        day_high
    
    if low < day_low and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
        day_low := low
        day_low
    
    
    plot(day_high, style=plot.style_line, color=color.yellow, linewidth=1)
    plot(day_low, style=plot.style_line, color=color.orange, linewidth=1)
     
  10. userque

    userque

    Ok, looks like your code is placed before the day_low and day_high's are actually calculated.
    Try putting it above the plot code, and below the if-then code. Then let me know how that goes.
     
    #10     Aug 28, 2020
    DevBru likes this.