All indicators

Trailing Stop Loss

May 3, 2021

Static chart image
SignalsMoving AveragesTrailing-Stop

The Trailing Stop Loss tool provides a dynamic risk management mechanism that tracks price movements to lock in profits while protecting against significant reversals.

Usage

The Usage section describes how the script can be used, particularly in trending markets where protecting unrealized gains is a priority.

  • Trend Following: The strategy uses a Fast and Slow Simple Moving Average (SMA) crossover to identify entry points. When the Fast SMA crosses above the Slow SMA, a long position is initiated.
  • Dynamic Protection: Once a position is active, the trailing stop loss follows the price upward. If the price drops by the user-defined percentage from its peak (when trailing is enabled), the position is automatically closed.
  • Time Filtering: Users can restrict the strategy to operate only within specific date ranges using the built-in "From" and "To" filters, which is useful for backtesting specific market regimes.

Details

This script implements a trailing stop loss logic that differs from a static stop loss by adjusting its level as the market moves in a favorable direction.

  • Trailing Mechanism: When "Enable Trailing" is active, the stop loss level is calculated based on the highest price reached since the entry. If the market continues to make new highs, the stop loss price rises accordingly.
  • Non-Repainting Logic: The stop loss price is designed to only move in the direction of the trade (upwards for longs) and never decreases, ensuring that gains are preserved.
  • Visual Feedback: The script plots the Fast SMA, Slow SMA, and the current active Trailing Stop Loss level directly on the chart. Labels are generated to indicate "Open" and "Close" signals for clear trade identification.

Settings

Filters

  • From: Enables and sets the starting date and time for the strategy execution.
  • To: Enables and sets the ending date and time for the strategy execution.
  • Timezones Ref->Chart: Adjusts the reference timezone for the date filters to match the user's specific exchange or chart settings.

Strategy

  • Fast SMA Length: Sets the lookback period for the shorter-term Moving Average.
  • Slow SMA Length: Sets the lookback period for the longer-term Moving Average.

Stop Loss

  • Long Stop Loss %: Determines the percentage distance from the peak price at which the stop loss will be triggered.
  • Enable Trailing: Toggles between a static stop loss (based on entry price) and a dynamic trailing stop loss (based on the highest price reached during the trade).

FAQ

How do I adjust the sensitivity of the exit?

You can adjust the "Long Stop Loss %" in the settings. A smaller percentage creates a tighter trailing stop that exits quickly on pullbacks, while a larger percentage allows for more market volatility before closing the position.

Can this script be used for short positions?

This specific implementation is optimized for long positions using SMA crossovers. However, the logic of trailing a price peak can be inverted for short-selling strategies.

How do I access the Trailing Stop Loss script?

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.

TradingView
NinjaTrader
MetaTrader 4/5

Unlock the entire LuxAlgo Library

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