TradingView
Peter_O
Jul 20, 2021 2:06 PM

Alert(), alertcondition() or strategy alerts? 

Euro Fx/U.S. DollarFXCM

Description

Variety of possibilities offered by PineScript, especially thanks to recent additions, created some confusion. Especially one question repeats quite often - which method to use to trigger alerts?

I'm posting this to clarify and give some syntax examples. I'll discuss these 3 methods in chronological order, meaning - in the order they were introduced to PineScript.

ALERTCONDITION() - it is a function call, which can be used only in study-type script. Since years ago, you could create 2 types of a script: strategy and study. First one enables creating a backtest of a strategy. Second was to develop scripts which didn't require backtesting and could trigger alerts. alertcondition() calls in strategy-type scripts were rejected by Pine compiler. On the other hand compiling study-type scripts rejected all strategy...() calls. That created difficulties, because once you had a nice and backtested strategy, you had to rip it off from all strategy...() function calls to convert your script to study-type so you could produce alerts. Maintenance of two versions of each script was necessary and it was painful.

"STRATEGY ALERTS" were introduced because of alertcondition() pains. To create strategy alert, you need to click "Add alert" button inside Strategy Tester (backtester) and only there. Alerts set-up this way are bound with the backtester - whenever backtester triggers an order, which is visible on the chart, alert is also fired. And you can customize alert message using some placeholders like {{strategy.order.contracts}} or {{ticker}}.

ALERT() was added last. This is an alerts-triggering function call, which can be run from strategy-type script. Finally it is doable! You can connect it to any event coded in PineScript and generate any alert message you want, thanks to concatenation of strings and wrapping variables into tostring() function.

Out of these three alertcondition() is obviously archaic and probably will be discontinued. There is a chance this makes strategy/study distinction not making sense anymore, so I wouldn't be surprised if "studies" are deprecated at some point.

But what are the differences between "Strategy alerts" and alert()? "Strategy alerts" seem easier to set-up with just a few clicks and probably easier to understand and verify, because they go in sync with the backtester and on-chart trade markers. It is especially important to understand how they work if you're building strategy based on pending orders (stop and limit) - events in your code might trigger placing pending order, but alert will be triggered only (and when) such order is executed.

But "Strategy Alerts" have some limitations - not every variable you'd like to include in alert message is available from PineScript. And maybe you don't need the alert fired when the trade hit a stop-loss or take-profit, because you have already forwarded info about closing conditions in entry alert to your broker/exchange.

Alert() was added to PineScript to fill all these gaps. Is allows concatenating any alert message you want, with any variable you want inside it and you can attach alert() function at any event in your PineScript code. For example - when placing orders, crossing variables, exiting trades, but not explicitly at pending orders execution.

The Verdict

"Strategy Alerts" might seem a better fit - easier to set-up and verify, flexible and they fire only when a trade really happens, not producing unnecessary mess when each pending order is placed. But these advantages are illusionary, because they don't give you the full-control which is needed when trading with real money. Especially when using pending orders. If an alert is fired when price actually hit a stop-order or limit-order level, and even if you are executing such alert within 1 second thanks to a tool like TradingConnector, you might already be late and you are making entry at a market price. Slippage will play a great role here. You need to send ordering alert when logical conditions are met - then it will be executed at the price you want. Even if you need to cancel all the pending orders which were not executed. Because of that I strongly recommend sticking to ALERT() when building your alerts system.

Below is an example strategy, showing syntax to manage placing the orders and cancelling them. Yes, this is another spin-off from my TradingView Alerts to MT4 MT5 . As usual, please don't pay attention to backtest results, as this is educational script only. tradingview.com/script/9MJO3AgE-TradingView-Alerts-to-MT4-MT5-dynamic-variables-NON-REPAINTING/

P.S. For the last time - farewell alertcondition(). You served us well.
Comments
adolgov
I'd like to add some arguments `pro` for strategy alerts.
If you needs signals for open a position and close a position and you strategy has complicated logic for entry (by stop or limit orders for example) or exit (by trailing or stop loss for example) or complex `pyramiding` and/or `risk` rules then it is too hard to detect open and close position events and call `alert` function appropriatelly. In this case the `strategy alerts` has huge advantages compare to `alert` function.
More over, if you want to get custom (dynamic) alert messages for each order generated by appropiate `strategy.entry` or `strategy.exit` commands you can use `alert_message` parameter for make custom alert messages. In this case in the Create Alert dlg just print `{{strategy.alert_message}}`. When an order will be filled, alert will have text what you have pass into `alert_message` parameter when call `strategy.entry` or `strategy.exit`.
adolgov
My general recommendation is: If you already have working strategy and want to automate it, my general recommendation is: just try to create the `strategy alert` for it and look how it works. If something will work bad then try to use `alert()` feature or something else.
adolgov
Correction: `{{strategy.order.alert_message}}` is correct placeholder name
MahmudAyaz
@adolgov, one question, you have mentioned that dynamic alert will work with strategy.entry and strategy.exit... will it work with strategy.close as well?
adolgov
adel20
@adolgov,
Hello sir
The alert does not work with the strategy accurately
Gives alerts
outside of entry and exit points on chart
adolgov
@adel20, The strategy repaints?
shkar80
My strategy places 2 orders in a time for buy and sell using alert() function. I've noticed that alerts are not properly ordered. As I understand that is because TV can not guarantee right order because it is plays async. Does any one know how to avoid it?
syrinxflunki
phenomenal work, thanks you x
Peter_O
@syrinxflunki, You are welcome :)
More