0bps

Strategy vault / Breakout

Dynamic MACD Optimization Trading Strategy

This strategy optimizes the classic MACD indicator in multiple ways to generate more accurate and reliable trading signals and achieve stricter risk control. The main optimizations include: 1introducing RSI indicator…

Author
ChaoZhang
Complexity
Intermediate · 45 lines
Parameters
7
Backtest bar
1h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

This strategy optimizes the classic MACD indicator in multiple ways to generate more accurate and reliable trading signals and achieve stricter risk control. The main optimizations include: 1introducing RSI indicator to avoid overbuying/overselling; 2adding volume confirmation; 3setting stop loss and take profit; 4optimizing parameter combination.

Strategy Principle

The basic principle still uses the MACD golden cross for long and death cross for short. The main optimizations are reflected in:

  1. Introducing the RSI indicator to avoid generating false signals when the market is overestimated or underestimated. RSI can effectively reflect the buying/selling pressure in the market.

  2. Adding volume judgment, signals are only generated when the trading volume increases, avoiding invalid breakouts. The enlargement of trading volume can confirm the strength of the trend.

  3. Setting stop loss and take profit mechanisms that can dynamically track market fluctuations and control risks within bearable ranges. Stop loss can effectively limit per trade loss; take profit locks in profits and avoids profit retracement.

  4. Optimizing MACD parameter combination to obtain better parameter portfolio and generate more precise trading signals.

Advantage Analysis

This multi-optimized MACD strategy has the following significant advantages:

  1. Greatly increased signal reliability and accuracy by reducing false signals.

  2. The strict stop loss and take profit mechanism controls trading risks and locks in profits to the maximum extent.

  3. MACD parameters are optimized and more suitable for different products and time frames.

  4. Signals generated from multiple indicator combinations have higher robustness and adaptability to broader market environments.

  5. Overall capital efficiency and risk reward ratio are greatly improved.

Risk Analysis

Some risks of this strategy also need to be prevented:

  1. The optimized parameters may not be 100% suitable for all products and periods, requiring situational adjustments.

  2. The frequency of signal generation will be reduced, resulting in certain missed trade risks.

  3. Conflicting signals may appear from multiple indicators under extreme market conditions, requiring manual judgment.

  4. Automatic stop loss may stop out prematurely in fast gap scenarios, posing some risk to profits.

The counter measures are mainly manual monitoring and judgment, adjusting parameters according to market conditions when necessary, and controlling position sizing.

Optimization Directions

The strategy can be further optimized in the following aspects:

  1. Test more indicator combinations such as Bollinger Bands, KD to form a group judgment.

  2. Apply machine learning algorithms to automatically optimize parameters for higher intelligence.

  3. Introduce stricter money management strategies such as fixed fractional, Kelly formula etc.

  4. Develop automatic take profit strategies to adjust take profit points based on trends and volatility.

  5. Apply cutting edge algorithms like deep learning for more accurate predictions.

Conclusion

By multi-dimensional optimization of the original MACD indicator, this strategy solves the problems of MACD's tendency to generate false signals and inadequate risk control. The application of multiple indicators combined with stop loss and take profit makes the signals more accurate and reliable, and the risk control is also more strict. This strategy deserves further development and application, and is a paradigm of MACD indicator enhancement.

Parameters

NameDefaultWhat it controls
v_input_116Fast line period
v_input_234Slow line period
v_input_310Signal line smoothing
v_input_419RSItimeframe
v_input_513Average trading volume period
v_input_float_110.5Stop Loss Percentage
v_input_float_20.3Take profit percentage

Source Pine Script

/*backtest
start: 2023-12-01 00:00:00
end: 2023-12-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Optimized MACD trading strategy ", overlay=true)

// Input Parameters
fastLength = input(16, "Fast line period")
slowLength = input(34, "Slow line period")
signalSmoothing = input(10, "Signal line smoothing")
rsiPeriod = input(19, "RSItimeframe")
overboughtRsi = 70
oversoldRsi = 30
volumeAvgPeriod = input(13, "Average trading volume period")
stopLossPerc = input.float(10.5, "Stop Loss Percentage", step=0.1)
takeProfitPerc = input.float(0.3, "Take profit percentage", step=0.1)

// Calculate indicator
[macdLine, signalLine, _] = ta.macd(close, fastLength, slowLength, signalSmoothing)
rsi = ta.rsi(close, rsiPeriod)
volumeAvg = ta.sma(volume, volumeAvgPeriod)

// Trading signals
longCondition = ta.crossover(macdLine, signalLine) and macdLine > 0 and rsi < overboughtRsi and volume > volumeAvg
shortCondition = ta.crossunder(macdLine, signalLine) and macdLine < 0 and rsi > oversoldRsi and volume > volumeAvg

// Stop loss and take profit
longStopLossPrice = close * (1 - stopLossPerc / 100)
longTakeProfitPrice = close * (1 + takeProfitPerc / 100)
shortStopLossPrice = close * (1 + stopLossPerc / 100)
shortTakeProfitPrice = close * (1 - takeProfitPerc / 100)

// Execute Trade
if longCondition
    strategy.entry("buy", strategy.long)
    strategy.exit("Buy stop-loss/take-profit", "buy", stop=longStopLossPrice, limit=longTakeProfitPrice)

if shortCondition
    strategy.entry("sell", strategy.short)
    strategy.exit("Sell stop-loss/take-profit", "sell", stop=shortStopLossPrice, limit=shortTakeProfitPrice)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-23. 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/Dynamic-MACD-Optimization-Trading-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