简体   繁体   中英

PineScript multi frame indicator alert not triggering at closing candle

I am trying to create a simple alert indicator whereas a price input is entered manually as well as timeframe resolution for close candle. The idea is to be notified when a candle in a particular timeframe closes above or below such price input.

Below is my entire code. I can't seem to get it to work despite trying different ideas. Such as not including barstate.isnew or even adding request.security(syminfo.tickerid,reso,barstate.isnew) thinking that may provide data for new bar at timeframe of choice

Any thoughts?

//@version=5
indicator("MTF Candle Cross",overlay=true)

line_input = input.float(title="Enter price at line level",defval=0.00000)
reso = input.timeframe(title="Candle Timeframe",defval="")

if barstate.isnew and request.security(syminfo.tickerid,reso,close) > line_input and request.security(syminfo.tickerid,reso,close[1]) <= line_input
    alert("candle close above line",alert.freq_once_per_bar_close)
    
if barstate.isnew and request.security(syminfo.tickerid,reso,close) < line_input and request.security(syminfo.tickerid,reso,close[1]) >= line_input
    alert("candle close below line",alert.freq_once_per_bar_close)

Set correct line_input value (notice that when creating alert, this value will be constant, changing this value (input.float) in the script that applied to the chart will not affect applied alert script).

//@version=5
indicator("MTF Candle Cross",overlay=true)

// line_input = input.float(title="Enter price at line level", defval = 0.00000)
line_input = close

reso = input.timeframe(title="Candle Timeframe",defval="")

s = request.security(syminfo.tickerid, reso, close)

var debugStr = ""

if barstate.isnew
    if s > line_input and s[1] <= line_input
        // alert("candle close above line",alert.freq_once_per_bar_close)
        debugStr := "candle close above line"    
        
    if s < line_input and s >= line_input
        // alert("candle close below line",alert.freq_once_per_bar_close)
        debugStr := "candle close above line"   
    
plot(line_input)

if barstate.islast 
    label.new(bar_index, high, debugStr)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM