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:
- 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.
- 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.
- 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.
- 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.
i tried this but i don’t get any alerts !!!
I’m not sure why is this happening to you. If you’re using the code 1 by 1 it should work.
i tried this but i don’t get any alerts !!!
I’m not sure why is this happening to you. If you’re using the code 1 by 1 it should work.
Please Show us how to set up autoview alerts!
I haven’t use Autoview for 2 years now, but I will try to take a look at it soon.
Please Show us how to set up autoview alerts!
I haven’t use Autoview for 2 years now, but I will try to take a look at it soon.
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?
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.
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!
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!
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.
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?
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.
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!
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!
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.
I wonder if the solution is by simply setting the 3Commas bot so that it only launches 1 deal per pair and no safety orders. In this way, even if the open order is triggered multiple times, the bot will only place the first one. The same with the close alert/order. Just my uneducated thought.
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 🙂
You have to surround it with quotes, like: “{your_message}”
Thanks I tried that but no dice
line 30: Mismatched input ‘message_type’ expecting ‘)’.
Send me the entire code, I will take a look. Hard to understand what’s going on only based on errors.
Was there a solution to this as I’m having the same problem?
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 🙂
You have to surround it with quotes, like: “{your_message}”
Thanks I tried that but no dice
line 30: Mismatched input ‘message_type’ expecting ‘)’.
Send me the entire code, I will take a look. Hard to understand what’s going on only based on errors.
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
Check the version of your PineScript, it should be v4 only.
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
Check the version of your PineScript, it should be v4 only.
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.
Try to use single quotes for your inputs and delete all “new lines” so it will be only 1 line.
I tried with single quotations
and excluding the “” with \
and with \n
It would be a way easier for us rookies if you would just edit your code here.
And show us the exact code for message_long_entry input.
Thanks
Great stuff Quantnomad! To anyone who has additional questions this video of yours on youtube really helped – https://www.youtube.com/watch?v=jRhbOoUpo04
Thank you!
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
Hi everyone,
can you tell me, in case of using the same CONDITION for entry long and exit short,
why the alert is triggered only for the first strategy.* call (entry long) and do not close the existing short position at the same CONDITION ?
strategy.entry(id=”EntryLong”, long = true, when = CONDITION, alert_message = message_long_entry)
strategy.close(id=”EntryShort”, when = CONDITION, alert_message = message_short_exit)
It seems, the temporary solution is like this:
1) make 2 inputs (for trade only longs or only shorts, or the both):
long_trade_alert = input(true, “Trade longs”)
short_trade_alert = input(true, “Trade shorts”)
2) separate the longs/shorts orders:
strategy.entry(…, when = CONDITION and long_trade_alert, …)
strategy.entry(…, when = CONDITION and short_trade_alert, …)
strategy.close(…, when = CONDITION and long_trade_alert, … )
strategy.close(…, when =CONDITION and short_trade_alert, …)
3) create 2 alerts: one for longs pairs and another for shorts pairs.
I did as Aaz two alert one for long and short
If u got error sending message just change input to single quote
long_trade_alert = input(‘Trade longs’)
short_trade_alert = input(‘Trade shorts’)
Hi! Please, tell me how can I input any variable to message. I want to calculate quantity of lot and put it into message
Hi! Please, tell me how can I input any variable to message. I want to calculate quantity of lot and put it into message
I’m not getting any alerts either. This can’t work without a webhook can it? No point entering a 3Commas bot ID without a corresponding webhook. Where do you put the webhook?
My apologies I looked at the video and saw how to create the alert with the included weekhook.
However whilst your code creates the alert it doesn’t start or close any deals on 3Commas.
I’m not getting any alerts either. This can’t work without a webhook can it? No point entering a 3Commas bot ID without a corresponding webhook. Where do you put the webhook?
Hi community,
does anyone know how to link the 2 alerts mentioned by AZZ and YOUNGKOI to manage Longs and Shorts at the same condition (entry long = exit short / exit long = entry short).
{{strategy.order.alert_message}} will consider all alert_message.
Thanks!
Perfect! Thank you!
Thanks a lot for your extensive explanation and the video! I’m following everything as described, and it seems to work but unfortunately the alerts are not parsed through to 3commas… Anyone encountering the same issue?
It’s a pretty old script, maybe Pine Script changed something. I’ll try to take a look and publish better version.
Informative article, eactly what I waas looking for.
Besides strategy.order.alert_message, my algo provider also requires Authorization token. How to combine both?
Hi Kumar,
You can use dynamic variable as alert_message and paste your token or anything else.