MACD Strategy with trailing ATR stop

Dec 9, 2020

Static chart image
Signals
Moving Averages
Trailing-Stop
Volatility

The MACD Strategy with trailing ATR stop indicator provides a comprehensive trend-following framework that combines EMA/SMA crossovers for trend filtering, MACD for entry timing, and an ATR-based trailing stop for risk management.

Usage

The Usage section describes how the script can be used to identify trend-aligned entries and manage multi-stage exits.

  • Entry Logic: A long entry is triggered when the Trend Ribbon is bullish (EMA is above the SMA) and the MACD line crosses above the signal line.
  • Pyramiding: The strategy allows for up to two simultaneous positions, enabling traders to add to their position if a second valid signal occurs while the first is active.
  • Exit Strategy: The script utilizes a three-tier exit system:
    1. Partial Profit 1: Closes a set percentage of the position (default 10%) when price reaches a specific percentage gain (default 1%).
    2. Partial Profit 2: Closes a second portion (default 50%) when the price reaches a higher target (default 5%).
    3. Trailing Stop: The remaining position is held until the price closes below the dynamic ATR-based trailing stop line.

Details

The script utilizes a "Trend Ribbon" composed of a 34-period Exponential Moving Average (EMA) and a Simple Moving Average (SMA). This filter ensures that MACD long signals are only taken during established uptrends. The MACD calculation is highly customizable, allowing users to switch between EMA and SMA for both the oscillator and the signal line.

For risk management, the strategy calculates a "Floating SL" based on the Average True Range (ATR) multiplied by a user-defined coefficient. This level is established at the time of entry to protect against downside volatility.

Settings

Trend

  • Ribbon Period: Determines the lookback period for both the EMA and SMA used to filter the trend.

MACD

  • Fast Length: The short-term lookback period for the MACD oscillator.
  • Slow Length: The long-term lookback period for the MACD oscillator.
  • Source: The price data used for MACD calculations (default is Close).
  • Signal Smoothing: The lookback period for the MACD signal line.
  • Simple MA (Oscillator): When enabled, uses SMA instead of EMA for the MACD line calculation.
  • Simple MA (Signal Line): When enabled, uses SMA instead of EMA for the signal line calculation.

Take Profit & Stop Loss

  • Long Take Profit 1 %: The percentage gain required to trigger the first partial exit.
  • Long Take Profit 1 Qty: The percentage of the total position to close at the first target.
  • Long Take Profit 2 %: The percentage gain required to trigger the second partial exit.
  • Long Take Profit 2 Qty: The percentage of the total position to close at the second target.
  • SL Multiplier: The value multiplied by the ATR to determine the distance of the stop loss.
  • ATR Period: The lookback period used for the ATR calculation.

Backtest Period

  • Start/Stop Year, Month, Day: Defines the specific time range for historical performance evaluation.

FAQ

How do I adjust the strategy for different assets? Cryptocurrency and equity markets have varying volatility profiles; you should adjust the ATR Multiplier and Take Profit percentages to align with the specific volatility of the asset being traded.

What happens if the second Take Profit is hit before the first? The strategy executes exits based on price levels relative to the average entry price; the orders are placed as limit orders once a position is opened.

How can I access the MACD Strategy with trailing ATR stop? You can get access on the LuxAlgo Library for charting platforms like TradingView, MetaTrader (MT4/MT5), and NinjaTrader for free.

Free access on the following platforms
tradingviewSymbolTradingView
ninjatraderNinjaTrader
metatrader4MetaTrader 4/5
thinkorswimThinkorswim

Unlock Unlimited Access to the LuxAlgo Library

Upgrade your plan to get all indicators, strategies, charts, and full access to Quant, our AI agent.

Trading & investing are risky and many will lose money in connection with trading and investing activities. All content on this site is not intended to, and should not be, construed as financial advice. Decisions to buy, sell, hold or trade in securities, commodities and other investments involve risk and are best made based on the advice of qualified financial professionals. Past performance does not guarantee future results.

Hypothetical or Simulated performance results have certain limitations. Unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, including, but not limited to, lack of liquidity. Simulated trading programs in general are designed with the benefit of hindsight, and are based on historical information. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

Testimonials appearing on this website may not be representative of other clients or customers and is not a guarantee of future performance or success.

As a provider of technical analysis tools and strategies, we do not have access to the personal trading accounts or brokerage statements of our customers. As a result, we have no reason to believe our customers perform better or worse than traders as a whole based on any content, tool, or platform feature we provide.

Charts used on this site are by TradingView in which the majority of our technical indicators are built on. TradingView® is a registered trademark of TradingView, Inc. www.TradingView.com TradingView® has no affiliation with the owner, developer, or provider of the Services described herein.

Market data is provided by CBOE, CME Group, BarChart, Massive, CoinAPI. Select U.S. equities data is provided through Massive. CBOE BZX real-time U.S. equities data is licensed from CBOE and provided through BarChart. Real-time futures data is licensed from CME Group and provided through BarChart. Select cryptocurrency data, including major coins, is provided through CoinAPI. All data is provided “as is” and should be verified independently for trading purposes.

This does not represent our full Disclaimer. Please read our full disclaimer.

© 2026 LuxAlgo Global, LLC.