TradingView

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

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")

1 thought on “Creating alerts for strategy with Stop Loss and Profit Target in TradingView”

  1. Hi, I need help to create alert for my tp and sl on my strategy. This code doesn’t work for some reason. Can you help me?

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Pine Script Programming Courses
Pine Script Programming Courses
Learn to build your own TradingView Indicators and Strategies
Sidebar Signup Form
If you want to be the first in this business, subscribe to the latest news