TradingView Alerts to MT4 MT5 - Forex, indices, commodities
Jan 25, 2021

The TradingView Alerts to MT4 MT5 - Forex, indices, commodities indicator demonstrates how to automate strategy execution by utilizing the alert() function to bridge Pine Script strategies with external trading platforms.
Usage
This script serves as a practical example for traders looking to automate their TradingView strategies on MetaTrader 4 or 5. It uses a Stochastic-based logic to trigger trades, but the primary utility lies in its automated alert message construction.
To use this tool:
- Apply the script to a chart (backtested on EURUSD 6h).
- The script identifies entry signals based on Stochastic crossovers.
- Upon a signal, it calculates dynamic parameters such as stop-loss prices based on recent pivot points.
- It generates a formatted alert message containing the trade direction, stop loss, trade ID, and take profit levels.
- Users can set up a TradingView alert using the "Alert() function" condition to pass these messages to a bridge like TradingConnector.
The logic includes support for full entries, partial profit-taking (50% exit), and dynamic stop-loss management.
Details
The execution model focuses on bridging the gap between backtesting and live automation. By using the alert() function directly within a strategy, the script eliminates the need to maintain separate "study" versions of a strategy. This ensures that the results seen in the Strategy Tester—which include commission and specific entry/exit logic—are accurately reflected in the automated execution.
Key implementation details:
- Non-Repainting: The logic is designed to execute on bar close to ensure signal stability.
- Dynamic Variables: Unlike static alerts, this script embeds real-time data (like the
stoplossLongprice) into the alert string. - Partial Exits: The script tracks bars since entry to manage partial take-profit levels, sending specific "closepart" commands.
Settings
- Take Profit Level: Sets the target distance in ticks/pips for the final exit.
- Take Partial Profit Level: Sets the target distance for closing 50% of the position.
- K: The period for the %K line of the Stochastic oscillator.
- D: The period for the %D moving average of the Stochastic.
- Smooth: The smoothing factor applied to the Stochastic %K.
FAQ
How do I automate this script?
You can automate it by creating a TradingView alert and selecting the script as the condition. Ensure the "Alert() function calls" option is active in the alert settings.
Can I use this for other markets?
Yes, while backtested on Forex, the alert syntax and logic can be adapted for indices, commodities, stocks, and crypto.
How can I get access to more advanced tools?
You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.
Free indicator
Get free access to this indicator on the platforms below.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.