How to force strategies fire exit alerts not reversalsPineScript has gone a long way, from very simple and little-capable scripting language to a robust coding platform with reliable execution endpoints. However, this one small intuitivity glitch is still there and is likely to stay, because it is traditionally justified and quite intuitive for significant group of traders. I'm sharing this workaround in response to frequent inquiries about it.
What's the glitch? When setting alerts on strategies to be synchronized with TradingView's Strategy Tester events, using simple alert messages such as "buy" or "sell" based on entry direction seems straightforward by inserting {{strategy.order.action}} into the Create Alert's "Message" field. Because "buy" or "sell" are exactly the strings produced by {{strategy.order.action}} placeholder. However, complications arise when attempting to EXIT positions without reversing, whether triggered by price levels like Stop Loss or Take Profit, or logical conditions to close trades. Those bricks fall apart, because on such events {{strategy.order.action}} sends the same "sell" for exiting buy positions and "buy" for exiting sell positions, instead of something more differentiating like "closebuy" or "closesell". As a result reversal trades are opened, instead of simply closing the open ones.
This convention harkens back to traditional stock market practices, where traders either bought shares to enter positions or sold them to exit. However, modern trading encompasses diverse instruments like CFDs, indices, and Forex, alongside advanced features such as Stop Loss, reshaping the landscape. Despite these advancements, the traditional nomenclature persists.
And is poised to stay on TradingView as well, so we need a workaround to get a simple strategy going. Luckily it is here and is called alert_message . It is a parameter, which needs to be added into each strategy.entry() / strategy.exit() / strategy.close() function call - each call, which causes Strategy Tester to produce entry or exit orders. As in this example script:
line 12: strategy.entry(... alert_message ="buy")
line 14: strategy.entry(... alert_message ="sell")
line 19: strategy.exit(... alert_message ="closebuy")
line 20: strategy.exit(... alert_message ="closesell")
line 24: strategy.close(... alert_message ="closebuy")
line 26: strategy.close(... alert_message ="closesell")
These alert messages are compatible with the Alerts Syntax of TradingConnector - a tool facilitating auto-execution of TradingView alerts in MetaTrader 4 or 5. Yes, simple alert messages like "buy" / "sell" / "closebuy" / "closesell" suffice to carry the execution of simple strategy, without complex JSON files with multiple ids and such. Other parameters can be added (actually plenty), but they are only option and that's not a part of this story :)
Last thing left to do is to replace "Message" in Create Alert popup with {{strategy.order.alert_message}} . This placeholder transmits the string defined in the PineScript alert_message= parameter, as outlined in this publication. With this workaround, executing closing alerts becomes seamless within PineScript strategies on TradingView.
Disclaimer: this content is purely educational, especially please don't pay attention to backtest results on any timeframe/ticker.
Metatrader
Moving Stop-Loss mechanism + alerts to MT4/MT5"How to code moving stop-loss mechanism", is one of the most often repeating questions in private messages I receive, so just to focus on this mechanism, I made a spin-off from my previous script: TradingView-Alerts-to-MT4-MT5-dynamic-variables-NON-REPAINTING .
The logic here moves the stop-loss each time a trade is running and a new pivot high/low is detected. When such event occurs (UpdateLongStopLoss or UpdateShortStopLoss), stoploss_long or stoploss_short mutable variable is modified. And it needs to be coded inside strategy.exit() line as "stop=stoploss_long" or "stop=stoploss_short". Entries are pretty straightforward - on Stoch crosses.
Last lines of the script show how to wrap information about such updates and send send alerts to MetaTrader via TradingConnector for execution in Forex/indices/commodities/crypto markets via MetaTrader. Please note that "tradeid=" variable must be passed with each alert, to let MetaTrader know which trade to modify. SLMOD, TPMOD are recently added commands, along with BE (as in "move stop-loss to breakeven" - but that's another topic).
Please disregard strategy backtest results, as this script is for coding education purposes only. However, it seems with the stop-loss mechanism enabled, the results are even better, than in original version of the script :)