0bps

Strategy vault / Breakout

MACD Dynamic Oscillation Cross Prediction Strategy

This strategy bases trading decisions on the dynamic characteristics of the MACD (Moving Average Convergence Divergence) indicator. The core approach focuses on observing changes in the MACD histogram to predict…

Author
ChaoZhang
Complexity
Starter · 58 lines
Parameters
0
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

This strategy bases trading decisions on the dynamic characteristics of the MACD (Moving Average Convergence Divergence) indicator. The core approach focuses on observing changes in the MACD histogram to predict potential golden and death crosses, allowing for early position establishment. The strategy goes beyond traditional MACD crossover signals by emphasizing the dynamic characteristics of the histogram to obtain better entry timing.

Strategy Principles

The strategy employs a modified MACD indicator system, incorporating the difference between fast (EMA12) and slow (EMA26) moving averages, along with a 2-period signal line. The core trading logic is based on several key points:

  1. Calculating histogram rate of change (hist_change) to judge trend dynamics
  2. Anticipating golden cross signals by entering long positions when the histogram is negative and shows an upward trend for three consecutive periods
  3. Anticipating death cross signals by closing positions when the histogram is positive and shows a downward trend for three consecutive periods
  4. Implementing a time filtering mechanism to trade only within specified time ranges

Strategy Advantages

  1. Strong Signal Prediction: Anticipates potential crossover signals by observing histogram dynamics, improving entry timing
  2. Reasonable Risk Control: Incorporates 0.1% commission and 3-point slippage, reflecting realistic trading conditions
  3. Flexible Capital Management: Uses percentage-based position sizing relative to account equity for effective risk control
  4. Excellent Visualization: Uses color-coded histograms and arrow markers for trade signals, facilitating analysis

Strategy Risks

  1. False Breakout Risk: Frequent false signals may occur in ranging markets
  2. Lag Risk: Despite predictive mechanisms, MACD retains some inherent lag
  3. Market Environment Dependency: Strategy performs better in trending markets, potentially underperforming in ranging conditions
  4. Parameter Sensitivity: Strategy performance is highly dependent on fast and slow line period settings

Optimization Directions

  1. Market Environment Filtering: Add trend identification indicators to adjust trading parameters based on market conditions
  2. Position Management Enhancement: Implement dynamic position sizing based on signal strength
  3. Stop Loss Implementation: Add trailing or fixed stop losses to control drawdown
  4. Signal Confirmation Enhancement: Incorporate additional technical indicators for cross-validation
  5. Parameter Optimization: Implement adaptive parameters that adjust based on market conditions

Summary

This strategy innovatively utilizes the dynamic characteristics of the MACD histogram to improve upon traditional MACD trading systems. The predictive mechanism provides earlier entry signals, while strict trading conditions and risk control measures ensure strategy stability. With further optimization and refinement, this strategy shows promise for improved performance in actual trading conditions.

Source Pine Script

/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy(title="Demo GPT - Moving Average Convergence Divergence", shorttitle="MACD", commission_type=strategy.commission.percent, commission_value=0.1, slippage=3, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval=1, maxval=50, defval=2)  // Set smoothing line to 2
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

// Date inputs
start_date = input(title="Start Date", defval=timestamp("2018-01-01T00:00:00"))
end_date = input(title="End Date", defval=timestamp("2069-12-31T23:59:59"))

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

// Strategy logic
isInDateRange = true

// Calculate the rate of change of the histogram
hist_change = hist - hist[1]

// Anticipate a bullish crossover: histogram is negative, increasing, and approaching zero
anticipate_long = isInDateRange and hist < 0 and hist_change > 0 and hist > hist[1] and hist > hist[2]

// Anticipate an exit (bearish crossover): histogram is positive, decreasing, and approaching zero
anticipate_exit = isInDateRange and hist > 0 and hist_change < 0 and hist < hist[1] and hist < hist[2]

if anticipate_long
    strategy.entry("Long", strategy.long)

if anticipate_exit
    strategy.close("Long")

// Plotting
hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(hist, title="Histogram", style=plot.style_columns, color=(hist >= 0 ? (hist > hist[1] ? #26A69A : #B2DFDB) : (hist < hist[1] ? #FF5252 : #FFCDD2)))
plot(macd, title="MACD", color=#2962FF)
plot(signal, title="Signal", color=#FF6D00)

// Plotting arrows when anticipating the crossover
plotshape(anticipate_long, title="Long +1", location=location.belowbar, color=color.green, style=shape.arrowup, size=size.tiny, text="Long +1")
plotshape(anticipate_exit, title="Short -1", location=location.abovebar, color=color.red, style=shape.arrowdown, size=size.tiny, text="Short -1")

Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-27. 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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/MACD-Dynamic-Oscillation-Cross-Prediction-Strategy.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open