Automating long/short TradingView strategies with only 1 alert

Using new alerts on the strategies in Tradingview you can do pretty interesting stuff in PineScript. In this post, I will show you how you can automate pretty complicated strategies in TradingView using only 1 alerts instead of at least 4 alerts you need using usual alerts. I’m using 3commas to automate strategies, but you can use Autoview or any other tool with the same approach.

Code for the strategy I’m using:

//@version=4
strategy("Bollinger Bands Strategy", overlay=true)

////////////
// Inputs //

length = input(20,  minval=1)
mult   = input(2.0, minval=0.001, maxval=50)

message_long_entry  = input("long entry message")
message_long_exit   = input("long exit message")
message_short_entry = input("short entry message")
message_short_exit  = input("short exit message")

//////////////////
// Calculations //

basis = sma(close, length)
dev   = mult * stdev(close, length)

upper = basis + dev
lower = basis - dev

//////////
// PLOT //

plot(basis, color = color.gray,  linewidth = 2)
lu = plot(upper, color = color.green, linewidth = 2)
ll = plot(lower, color = color.red,   linewidth = 2)

fill(lu, ll, color = color.gray)

/////////////
// Signals //

long  = crossover(close, upper)
short = crossunder(close, lower)

long_exit  = crossunder(close, basis)
short_exit = crossover(close, basis) 

//////////////
// Strategy //

strategy.entry("long",  strategy.long,  when = long  and barstate.isconfirmed, alert_message = message_long_entry)
strategy.entry("short", strategy.short, when = short and barstate.isconfirmed, alert_message = message_short_entry)

strategy.close("long",  when = long_exit  and barstate.isconfirmed, alert_message = message_long_exit)
strategy.close("short", when = short_exit and barstate.isconfirmed, alert_message = message_short_exit)

Code is pretty simple but there are few important things you need to be aware of:

  1. To pass different alert messages in 1 alert I’m using alerts of strategies and not a study. There is no way to build something similar in a study for now.
  2. To create dynamic messages for 1 alert I’m using “alert_message” in all my orders. The content of this parameter can be a variable so it can be different and dynamic for all orders.
  3. To be sure I receive only one alert per bar I’m using an additional parameter of barstate.isconfirmed. This build-in variable will make sure that the order will be executed (and alert sent) only once at the very last price for every bar.
  4. To keep it a bit simpler I added inputs for all 3commas messages. So to change them I don’t need to change my code but just replace these parameters.

As I said I’m using 3commas, so for me messages look like these:

{
"message_type": "bot",
  "bot_id": 1352051,
  "email_token": "83da02b1-ac90-44c4-be6f-467cca698946",
  "delay_seconds": 0
}

It’s possible to use the same approach with Autoview as well, so your messages will look a bit different. Moreover in Autoview you can change quite a lot of things from the message itself, like quantity, price, pair, etc. So it is possible to create much more complicated logic inside pine script and even try to trade multiple coins from 1 strategy.

Creating alerts after that it’s super simple. Create an alert from the strategy and set up it in a usual for the tool you’re using. As a message use only 1 placeholder:

{{strategy.order.alert_message}}

That’s it, when you set up your alert this way you will be able to send all messages to 3commas using only 1 alert.

Leave a Reply

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