Strategy vault / Trend following
MACD Crossover Trading Strategy
The MACD crossover trading strategy is a trend-following strategy. It uses the crossover of fast and slow moving average lines as buy and sell signals. When the fast moving average line crosses above the slow moving…

Overview
The MACD crossover trading strategy is a trend-following strategy. It uses the crossover of fast and slow moving average lines as buy and sell signals. When the fast moving average line crosses above the slow moving average line, a buy signal is generated. When the fast moving average line crosses below the slow moving average line, a sell signal is generated.
Strategy Principle
This strategy is based on the MACD indicator. The MACD indicator is the difference between two moving average lines with different parameters, reflecting the changes in the momentum of prices. Specifically, it is the difference between the fast moving average line (default parameter is 12-day line) and the slow moving average line (default parameter is 26-day line), called the MACD bar. To eliminate oscillations, the MACD indicator also introduces a DEA line or signal line, usually the 9-day weighted moving average of the MACD.
When the MACD bar breaks through the DEA line from bottom up and enters the positive area, it indicates that the short-term average line crosses above the long-term average line, indicating that the price trend turns to upward and a buy signal is generated. When the MACD falls from top to bottom through the DEA line and enters the negative area, it indicates that the short-term average line crosses below the long-term average line and the price trend turns to downward, generating a sell signal.
The strategy uses the crossover of the MACD bar and the DEA line to determine the timing of buying and selling. It buys when the MACD bar crosses above the DEA line and sells when the MACD bar crosses below the DEA line.
Advantage Analysis
The advantages of this strategy include:
- Ability to follow the trend and capture price changes in a timely manner.
- Simple and easy to understand and implement.
- Relatively fixed parameters without frequent adjustment.
- Applicable to different timeframes.
Risk Analysis
This strategy also has some risks:
- May generate multiple false signals or whipsaws in sideways markets.
- Has some lag and may miss the best timing of price changes.
- Parameters are easily overoptimized and actual results may be poor.
To reduce risks, parameters can be adjusted, or combined with other indicators like volume and volatility indicators. In addition, proper stop loss and take profit strategies are also important.
Optimization Directions
This strategy can be optimized in the following aspects:
Parameter optimization to find the optimal parameters while avoiding overoptimization.
Combining with other indicators to form more powerful combination strategies.
Setting proper stop loss and take profit points to effectively control risks.
Adaptive optimization to apply this strategy to different markets and timeframes based on actual conditions.
Conclusion
The MACD crossover trading strategy captures trend changes at a low cost by following price trends. It is simple, practical and easy to implement, making it a suitable starter strategy for beginners. But this strategy also has some flaws. By constantly optimizing and improving, the actual effect of this strategy can be better. It is worth recommending.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 20 | fastLength |
| v_input_2 | 40 | slowlength |
| v_input_3 | 4 | MACDLength |
| v_input_4 | 2011 | From Year |
| v_input_5 | true | From Month |
| v_input_6 | true | From Day |
| v_input_7 | 9999 | To Year |
| v_input_8 | 12 | To Month |
| v_input_9 | 31 | To Day |
Source Pine Script
/*backtest
start: 2022-12-29 00:00:00
end: 2024-01-04 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("MACD Strategy by Forbes",default_qty_type=strategy.percent_of_equity, default_qty_value=100, overlay=false)
fastLength = input(20)
slowlength = input(40)
MACDLength = input(4)
// === INPUT BACKTEST RANGE ===
FromYear = input(defval = 2011, title = "From Year", minval = 2009)
FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
ToYear = input(defval = 9999, title = "To Year", minval = 2009)
ToMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)
ToDay = input(defval = 31, title = "To Day", minval = 1, maxval = 31)
// === FUNCTION EXAMPLE ===
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
window() => true // create function "within window of time"
MACD = ema(close, fastLength) - ema(close, slowlength)
aMACD = ema(MACD, MACDLength)
delta = MACD - aMACD
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
f1 = plot(MACD,color=red)
s1 = plot(aMACD,color=blue)
plotColor = if delta > 0
delta > delta[1] ? lime : green
else
delta < delta[1] ? maroon : red
plot(delta, color=plotColor, style=columns)
if (crossover(delta, 0))
strategy.entry("Buy", true, when=window(), comment="Buy")
if (crossunder(delta, 0))
strategy.close_all(when=window())
//plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-05. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/MACD-Crossover-Trading-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Adaptive ATR and RSI Trend Following Strategy with Trailing Stop LossPine Script · Trend following · 1h
- Advanced Multi Indicator Multi Dimensional Trend Cross Quantitative StrategyPine Script · Trend following · 3h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Crossover EMA Short term Trading StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dual EMA Crossover Oscillation Tracking StrategyPine Script · Trend following · 5m