All indicators

Moving Stop-Loss mechanism + alerts to MT4/MT5

Jul 7, 2021

Static chart image
SignalsOscillatorsPivot Based (Retrospective)Trailing-Stop

The Moving Stop-Loss mechanism + alerts to MT4/MT5 indicator provides an educational framework for implementing a dynamic stop-loss system based on pivot points, specifically designed to bridge the gap between TradingView alerts and MetaTrader execution.

Usage

The script is used to visualize and automate the movement of a stop-loss level as a trade progresses. It utilizes Stochastic crossovers (K and D lines) for entry signals and subsequently manages the risk through trailing stop-loss adjustments.

Key usage points include:

  • Dynamic Adjustments: When a new pivot high or low is detected while a trade is active, the script updates the stop-loss variable to the price of that pivot.
  • Visual Confirmation: Green and red labels ("SL move") appear on the chart to indicate exactly when and where the stop-loss has been adjusted.
  • External Execution: The script generates formatted alert messages (e.g., slmod slprice=...) intended for use with third-party connectors to execute modifications on MT4 or MT5 platforms.

Details

The core logic revolves around mutable variables stoplossLong and stoplossShort. Upon a valid entry signal, the initial stop-loss is set to the most recent pivot level or the current bar's extreme.

As the price moves and the strategy.position_size remains active, the ta.pivothigh() and ta.pivotlow() functions monitor for new structural points. If a new pivot is formed that would tighten the risk, the script triggers an update. This logic is integrated directly into the strategy.exit() function using the stop parameter to ensure the backtester reflects the modified exit price.

Settings

  • Take Profit Level: Sets the distance in ticks/pips for the profit target.
  • Enable Stoploss Modification Mechanism: A toggle to turn the dynamic trailing logic on or off. When disabled, the stop-loss remains at its initial entry level.

FAQ

How do I access Moving Stop-Loss mechanism + alerts to MT4/MT5?

You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Can this script be used for live trading?

While the script is designed for educational purposes to demonstrate stop-loss modification coding, it can be adapted for live trading by connecting the alert outputs to a compatible execution bridge.

Why do I need a trade ID in the alerts?

When sending updates to MetaTrader, the tradeid variable ensures that the platform knows exactly which ticket or position to modify when multiple trades might be active.

Free indicator

Get free access to this indicator on the platforms below.

TradingView
NinjaTrader
MetaTrader 4/5

Unlock the entire LuxAlgo Library

Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.