Search code examples
pine-scriptpine-script-v5

Tradingview alert: trigger in the last 30 minutes if condition is met


I want ot create an alert which shall trigger in the last 30 min of the day (EDT) if a certain condition is met: if the close of the actual 5min candle - the actual day's low / (day's High - day's low) is larger than 0.7, i.e.: (close - Low)/(High - Low) > 0.7.

Here's the code, which does not trigger:

//@version=5
indicator("ClosingAlert")

High = request.security(syminfo.tickerid, "D", high)
Low = request.security(syminfo.tickerid, "D", low)
Open = request.security(syminfo.tickerid, "D", open)
HL = High - Low
InSession(sess) => not na(time("5", sess, "America/New_York")[1])
sellClose = ((close - Low)/HL > 0.7) and InSession("1530-1600") 

alertcondition(condition=sellClose, message="Sell Signal")

What am I missing? Are the different time fames wrongly coded?


Solution

  • Ok, I don't fully understand why it does not trigger but I found a workaround: I skipped the InSession row but calculated the close in a 5 min frame and modified the sellClose condition:

    //@version=5
    indicator("ClosingAlert")
    High = request.security(syminfo.tickerid, "D", high)
    Low = request.security(syminfo.tickerid, "D", low)
    Open = request.security(syminfo.tickerid, "D", open)
    Close = request.security(syminfo.tickerid, "5", close)
    HL = High - Low
    sellClose = ((Close - Low)/HL > 0.9) and (time_close - timenow) < 1800000
    alertcondition(condition=sellClose, message="Sell Signal")
    

    Since the time is reported in ms, 1800000 corresponds to 30min between the closing time and the actual time.