Creating alerts for strategy with Stop Loss and Profit Target in TradingView

//@version=4
study("Strategy with SL/PT", overlay = true)

pos = 0

long_entry = nz(pos[1]) == 0 and crossover(sma(close, 10), sma(close, 30))

sl_level = 0.0
sl_level := long_entry[1] ? open * 0.99 : nz(sl_level[1])

pt_level = 0.0
pt_level := long_entry[1] ? open * 1.02 : nz(pt_level[1])

sl_exit =  nz(pos[1]) == 1 and crossunder(low, sl_level)
pt_exit = nz(pos[1]) == 1 and crossover(high, pt_level)

pos := long_entry ? 1 :
       nz(pos[1] == 1) and (sl_exit or pt_exit) ? 0 : 
       nz(pos[1])

plot(sl_level, color = color.red)
plot(pt_level, color = color.green)

plotshape(long_entry, text = "Long")
plotshape(sl_exit, text = "SL")
plotshape(pt_exit, text = "PT")

//if (long_entry)
//    strategy.entry("Long", true)
//    strategy.exit("Long", loss = close / 100 / syminfo.mintick, profit = 2 * close / 100 / syminfo.mintick)

alertcondition(long_entry, "Long", "Long")
alertcondition(sl_exit, "PT", "PT")
alertcondition(pt_exit, "SL", "SL")

Leave a Reply

Your email address will not be published. Required fields are marked *