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.

19 thoughts on “Automating long/short TradingView strategies with only 1 alert”

  1. Hello QuantNomad, tks for your idea. It works great! Previously I tried pasting the 3commas msg directly into the script under alert_message but it prompted error message. I wonder why.

    Also, I tried adding trailing stop loss under strategy.exit and I wanted to trigger the msg once the price trigger my stop loss level, not when bar close, like trigger “once per bar” when creating Alert. Do you know how to do that? Do you think adding “barstate.isrealtime” in the strategy exit script will work?

    1. Hi. Unfortunately, my solution works only for market orders. I’m thinking about a solution for stop/limit orders, but I’m not sure it’s possible to make a robust solution with TRadingView.

      1. what I want to do is in fact a market order, meaning when live price data hit the stop loss level in tradingview, the close at market msg will be triggered to 3commas. I can do it by setting in Alert, selecting the “once per bar”. Guess I will continue to use Alert for my ATR stop loss for the time being. The rest of the messages like open or close trades I can use your method. Many tks QuantNomad!

      2. Hi QuantNomad, I found the solution. I didn’t include the barstate in the strategy exit script and the message fire instantly once the live price hit my stop loss level, without waiting for bar closure. Awesome!

        1. Hi Khong,
          Be careful with it. Depend on your implementation it might work or not for you.
          Without barstate check, you might receive quite a lot of alerts inside one bar. It might work for the exit, but for entries with stop/limit orders, it might be trickier.
          I just don’t have time to test it myself for these orders. Let me know if that works in the end for you.

  2. Is there any way to hardcode the 3commas script into the strategy instead of pasting it into the Input?

    Every time I try to do this I get compile errors like this:
    line 27: no viable alternative at character ‘{‘

    I tried commenting it out but even with line breaks I couldn’t get it to work

    Thanks for any advice you can give 🙂

  3. I love this concept! However I am not able to get it to work. When I try to Add to Chart, I get the following error message. Any help on how to resolve this would be greatly appreciated.

    Add to Chart operation failed, reason: line 41: Unknown argument `alert_message` of type string;
    line 41: Cannot call `strategy.entry` with arguments (literal string, const bool, when=series[bool], alert_message=string); available overloads: strategy.entry(const string, series[bool], series, series, series, string, const string, string, series[bool]) => void;
    line 42: Unknown argument `alert_message` of type string;
    line 42: Cannot call `strategy.entry` with arguments (literal string, const bool, when=series[bool], alert_message=string); available overloads: strategy.entry(const string, series[bool], series, series, series, string, const string, string, series[bool]) => void;
    line 44: Cannot call `strategy.close` with arguments (literal string, when=series[bool], alert_message=string); available overloads: strategy.close(const string, series[bool]) => void;
    line 45: Cannot call `strategy.close` with arguments (literal string, when=series[bool], alert_message=string); available overloads: strategy.close(const string, series[bool]) => void

  4. Hey Quantnomad,

    Thanks for the above. Super idea!
    I am getting the same problem as NEIL.
    Pine editor has problems when the entry and exit messages are replaced with 3Commas bot messages.

    Examples:
    1.

    message_long_entry = input(“{ “message_type”: “bot”, “bot_id”: 2890682, “email_token”: “b0ee4bf0-5748-4765-ac91-33dca204ca6f”, “delay_seconds”: 0, “action”: “start_bot_and_start_deal”}”)

    Return from Pine editor: line 10: Mismatched input ‘message_type’ expecting ‘)’.

    2.
    message_long_entry = input(
    “{ “message_type”: “bot”, “bot_id”: 2890682, “email_token”: “b0ee4bf0-5748-4765-ac91-33dca204ca6f”, “delay_seconds”: 0, “action”: “start_bot_and_start_deal”}”)

    Return from Pine editor: line 10: Mismatched input ‘message_type’ expecting ‘)’.

    3.
    message_long_entry = input(”
    { “message_type”: “bot”, “bot_id”: 2890682, “email_token”: “b0ee4bf0-5748-4765-ac91-33dca204ca6f”, “delay_seconds”: 0, “action”: “start_bot_and_start_deal”}
    “)

    Return from Pine Editor: line 10: mismatched character ‘\n’ expecting ‘”‘
    Note: If message_long_entry = input(” is on line 10, any other changes to the message produce the same return from Pine Editor.

    4.
    message_long_entry = input(“test”)
    Return from Pine editor: Processing script…
    Script ‘Test’ has been saved

    Thanks for your revert.

  5. I don’t think you need barstate.isconfirmed condition, strategies by default only execute on confirmed bars, unless you set calc_on_every_tick in strategy call

Leave a Reply

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