Pine script Tradingview - Alerts not working

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

  1. DevBru

    DevBru

    Great, thanks man! this one is working now:

    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)
    
    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
    
    alertcondition(cross(close,day_high), message = 'Price crossed Pre Market level')
    
    plot(day_high, style=plot.style_line, color=color.yellow, linewidth=1)
    plot(day_low, style=plot.style_line, color=color.orange, linewidth=1)
    Now i need to find a way so i get 1 alert for both the high and the low, probably something like this?

    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)
    
    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
    
    alertcondition(cross(close,day_high)or(cross(close,day_low)), message = 'Price crossed Pre Market level')
    
    plot(day_high, style=plot.style_line, color=color.yellow, linewidth=1)
    plot(day_low, style=plot.style_line, color=color.orange, linewidth=1)
     
    #11     Aug 28, 2020
  2. userque

    userque

    You have some extra parentheses, but it should work; if it doesn't, we'll fix it.
     
    #12     Aug 28, 2020
    DevBru likes this.
  3. DevBru

    DevBru

    It seems to be working but further testing will need to be done next week.

    Thanks for the help!
     
    #13     Aug 28, 2020
    userque likes this.
  4. userque

    userque

    You're welcome and good luck!
     
    #14     Aug 28, 2020
    DevBru likes this.
  5. DevBru

    DevBru

    It seems like it working fine!

    One more thing i would like to do if at all possible, i would like to have a different message for when the high or low is crossed.

    Not sure if this is possible since i am using 1 alert for both the high and low.

    I am thinking something like this?

    Code:
    alertcondition(cross(close,day_high), message= 'Price crossed Pre Market high!')or(cross(close,day_low), message= 'Price crossed Pre Market low!')
    However i get this error: line31: Syntax error at input 'or'.
     
    #15     Aug 30, 2020
  6. userque

    userque

    Glad you got it working. Your thinking is correct, but you should separate the two tasks.

    Create one task for crossing the high; another task (line) for the low. If I recall correctly, this is how you tried doing it in the beginning.

    Try using these two lines, instead of the one line. Let me know how it works, as I'm relying on your above syntax and haven't tested this myself.

    Code:
    alertcondition(cross(close,day_high), message= 'Price crossed Pre Market high!')
    alertcondition(cross(close,day_low), message= 'Price crossed Pre Market low!')
    Note: You could probably do this on one line using IF-THEN; but it's probably better to keep them separate at this point.
     
    #16     Aug 30, 2020
  7. DevBru

    DevBru

    Thanks, unfortunately this way i have to set 2 separate alerts, one for the low cross and one for the high cross.

    I would prefer to only have 1 alert for both the low cross and high cross, since my number of alerts is limited. It isn't that important so for now i will probably stick with the code i have successfully working.
     
    #17     Aug 30, 2020
  8. userque

    userque

    We can try using IF-Then statements if you like?
     
    #18     Aug 30, 2020
  9. Not to hijack the thread, but until I started with Alpaca and TradingView, I had never heard of Pine. There doesn't seem to be a lot of documentation. Anybody know where I could find the docs, and is there some reason why I would want to use Pine instead of a Python script? Hope I am not coming across like a real dumdum here... wouldn't be the first time.
     
    #19     Aug 30, 2020
  10. userque

    userque

    Manual: https://www.tradingview.com/pine-script-docs/en/v4/Introduction.html

    You can do a search for tutorials as well.

    Pine script is specific to tradingview. Tradingview provides the whole trading/charting/testing framework; so it'll be quicker and easier to code. It only works in tradingview, for only that purpose.

    Python is a general purpose language; it'll be more powerful than pine script, but it'll be slower and more difficult to code. It doesn't work with tradingview. Python can be used to code most any type of computer application.
     
    #20     Aug 31, 2020
    GrowleyMonster likes this.