Automate Tradingview Strategies New Method
Watch the Video
Click here to watch the full video on YouTube
Code Snippets
Code Shared In Video to set up tradingview alerts.
longstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
shortstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
long_message = input.text_area(title = 'Long Alert Message', defval = 'Long ', confirm = false, group = 'Alert Messages', tooltip = longstring)
short_message = input.text_area(title = 'Short Alert Message', defval = 'short ', confirm = false, group = 'Alert Messages', tooltip = shortstring)
if your_long_condition
strategy.entry('LONG', strategy.long, alert_message = long_message)
if your_short_condition
strategy.entry('SHORT', strategy.short, alert_message = short_message)
For Strategy alert message.
{{strategy.order.alert_message}}
Full Strategy Code
//@version=6
strategy('Strategy Alert Example', overlay = true)
ma(source, length, type) =>
switch type
'SMA' => ta.sma(source, length)
'Bollinger Bands' => ta.sma(source, length)
'EMA' => ta.ema(source, length)
'SMMA (RMA)' => ta.rma(source, length)
'WMA' => ta.wma(source, length)
'VWMA' => ta.vwma(source, length)
rsiLengthInput = input.int(14, minval = 1, title = 'RSI Length', group = 'RSI Settings')
rsiSourceInput = input.source(close, 'Source', group = 'RSI Settings')
maTypeInput = input.string('SMA', title = 'MA Type', options = ['SMA', 'Bollinger Bands', 'EMA', 'SMMA (RMA)', 'WMA', 'VWMA'], group = 'MA Settings')
maLengthInput = input.int(14, title = 'MA Length', group = 'MA Settings')
bbMultInput = input.float(2.0, minval = 0.001, maxval = 50, title = 'BB StdDev', group = 'MA Settings')
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
rsiMA = ma(rsi, maLengthInput, maTypeInput)
long = ta.crossover(rsi, 70)
short = ta.crossunder(rsi, 30)
longstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
shortstring = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
long_message = input.text_area(title = 'Long Alert Message', defval = 'Long ', confirm = false, group = 'Alert Messages', tooltip = longstring)
short_message = input.text_area(title = 'Short Alert Message', defval = 'short ', confirm = false, group = 'Alert Messages', tooltip = shortstring)
if long
strategy.entry('LONG', strategy.long, alert_message = long_message)
if short
strategy.entry('SHORT', strategy.short, alert_message = short_message)
// Set Inputs for Take Profit and Stop Loss
stopPer = input(1.0, title = 'Stop Loss %') / 100
takePer = input(1.0, title = 'Take Profit %') / 100
// Determine where you've entered and in what direction
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
shortTake = strategy.position_avg_price * (1 - takePer)
longTake = strategy.position_avg_price * (1 + takePer)
long_x_string = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
short_x_string = 'Input your custom alert message here. \n and put {{strategy.order.alert_message}} in Message box.'
long_x_message = input.text_area(title = 'Long Exit Alert Message', defval = 'Long Exit', confirm = false, group = 'Alert Messages', tooltip = long_x_string)
short_x_message = input.text_area(title = 'Short Exit Alert Message', defval = 'Short Exit', confirm = false, group = 'Alert Messages', tooltip = short_x_string)
if strategy.position_size > 0
strategy.exit(id = 'Close Long', stop = longStop, limit = longTake, alert_message = long_x_message)
if strategy.position_size < 0
strategy.exit(id = 'Close Short', stop = shortStop, limit = shortTake, alert_message = short_x_message)
//PLOT FIXED SLTP LINE
plot(strategy.position_size > 0 ? longStop : na, style = plot.style_linebr, color = color.new(color.red, 0), linewidth = 1, title = 'Long Fixed SL')
plot(strategy.position_size < 0 ? shortStop : na, style = plot.style_linebr, color = color.new(color.red, 0), linewidth = 1, title = 'Short Fixed SL')
plot(strategy.position_size > 0 ? longTake : na, style = plot.style_linebr, color = color.new(color.green, 0), linewidth = 1, title = 'Long Take Profit')
plot(strategy.position_size < 0 ? shortTake : na, style = plot.style_linebr, color = color.new(color.green, 0), linewidth = 1, title = 'Short Take Profit')
