MACD Crossover trend strategy, Long and Short
Mar 21, 2021

The MACD Crossover trend strategy, Long and Short indicator is a comprehensive trend-following tool designed to capture momentum shifts using a combination of MACD signals and moving average filters. It provides a structured approach to entering and exiting trades based on trend alignment and momentum crossovers.
Usage
The strategy identifies potential trade opportunities by filtering MACD crossovers through a long-term EMA.
- Long Entry: Triggered when the MACD line crosses above the signal line while both are above zero, provided the price is trading above the user-defined EMA.
- Short Entry: Triggered when the MACD line crosses below the signal line while both are below zero, provided the price is trading below the user-defined EMA.
Users can choose between two primary exit methodologies via the "Strategy" setting:
- Pullback: This mode uses Donchian-style logic for risk management. It sets a stop loss based on the recent period's high/low and calculates a take profit based on a user-defined Risk/Reward ratio.
- MACD Inverse Cross: This mode closes positions when the MACD and signal lines produce a counter-trend crossover, prioritizing momentum exhaustion over fixed price targets.
Details
The script integrates classic momentum oscillators with trend-filtering components. By requiring the MACD crossover to occur on the specific side of the zero line (above for longs, below for shorts), the strategy aims to enter trades where momentum is already established in the desired direction. The inclusion of a higher-period EMA serves as a regime filter to ensure trades are only taken in alignment with the broader market trend. The exit logic incorporates a "Pullback Period" which utilizes the ta.highest and ta.lowest functions to determine dynamic support and resistance levels for stop-loss placement.
Settings
MACD Inputs
- Fast Length: Sets the period for the shorter EMA used in MACD calculation.
- Slow Length: Sets the period for the longer EMA used in MACD calculation.
- Source: Determines the price data used for calculations (default is Close).
- Signal Smoothing: Sets the smoothing period for the MACD signal line.
- Simple MA (Oscillator): If enabled, uses SMA instead of EMA for the MACD lines.
- Simple MA (Signal Line): If enabled, uses SMA instead of EMA for the signal line.
Indicators & Strategy
- EMA Input: Sets the length for the trend-filtering EMA.
- Pullback Period: Defines the lookback window for identifying price extremes used in the Pullback exit mode.
- Risk/Reward Ratio: Determines the target profit relative to the stop-loss distance.
- Strategy: Toggles between "Pullback" (fixed RR) and "MACD inverse cross" (momentum-based) exit logic.
Backtest Date Range
- From/Thru Day/Month/Year: Defines the specific window of time for which the strategy will calculate and display results on the chart.
FAQ
How do I use the MACD Crossover trend strategy, Long and Short?
You can use it to identify trend-aligned entries by looking for MACD crossovers that occur on the correct side of the zero line while price is confirmed by the EMA filter.
What is the difference between the exit modes?
The "Pullback" mode uses recent price extremes and a fixed risk-to-reward ratio to manage the trade, while the "MACD inverse cross" mode exits as soon as momentum starts to shift in the opposite direction.
How can I access this 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.
Unlock the entire LuxAlgo Library
Every indicator, every strategy, full charts, and complete access to Quant — our AI agent.