0bps

Strategy vault / Mean reversion

Dual Moving Average Bollinger Band MACD Trading Strategy

This strategy combines dual moving averages, Bollinger bands and the MACD indicator to set buy and sell conditions for trading the Bank Nifty index on a 5-minute timeframe. It goes long when the MACD line crosses…

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

IMG

Overview

This strategy combines dual moving averages, Bollinger bands and the MACD indicator to set buy and sell conditions for trading the Bank Nifty index on a 5-minute timeframe. It goes long when the MACD line crosses above the signal line and the closing price breaks above the Bollinger band upper line, and goes short when the MACD line crosses below the signal line and the closing price falls below the Bollinger band lower line. By integrating the advantages of multiple indicators, this strategy can identify trends and locate extremum points for efficient trading.

Trading Logic

  1. Set MACD parameters: Fast length 12, Slow length 26, Signal length 9
  2. Calculate MACD value: Fast line - Slow line
  3. Set Bollinger band parameters: Middle band period 20, Standard deviation multiplier 2
  4. Calculate Bollinger band upper and lower lines: Middle band ± Standard deviation * Multiplier
  5. Buy condition: MACD line crosses above signal line (golden cross) and close > Upper band
  6. Sell condition: MACD line crosses below signal line (dead cross) and close < Lower band
  7. Set take profit and stop loss
  8. Enter long position: when buy condition holds
  9. Close long position: take profit or stop loss
  10. Enter short position: when sell condition holds
  11. Close short position: take profit or stop loss

The above summarizes the overall trading logic of this strategy.

Advantage Analysis

This is a very practical trend-following strategy with the advantages below:

  1. MACD identifies trend direction and momentum
  2. Bollinger band determines overbought and oversold zones, complementing MACD
  3. Dual moving averages improve judgment accuracy
  4. Combining multiple indicators improves reliability
  5. Implementing take profit and stop loss manages risks
  6. Adjustable parameters adapt to changing market dynamics

In summary, this strategy leverages the strengths of various indicators for accurate judgments and disciplined execution, making it a reliable and controllable trend trading system.

Risk Analysis

Despite its merits, this strategy has certain risks to note:

  1. Violent market swings may penetrate stops
  2. Multiple parameters combinations increase misjudgment risks
  3. High trading frequency from short-term operations increases costs
  4. Suboptimal parameter tuning fails to capture best entry/exit points

The solutions are:

  1. Strict stop loss controls single trade loss
  2. Optimize parameters to improve judgment accuracy
  3. Adjust timeframe to reduce trade frequency
  4. Backtest to find optimal parameter combinations

Enhancement Opportunities

There is room for improving this strategy:

  1. Utilize machine learning to find optimum parameters
  2. Incorporate adaptive techniques to auto tune parameters
  3. Integrate more indicators e.g. momentum, volatility metrics
  4. Add position sizing module to adjust by capital, risk
  5. Innovate signal rules with custom indicators or formulae

Overall, this strategy has a robust framework. Further refinements via parameter optimization, indicator innovation, adaptive mechanisms etc. can transform it into an even more powerful and consistent system.

Conclusion

This dual moving average Bollinger MACD strategy effectively identifies entry and exit points by combining trend identification and extremum detection. With disciplined execution, configurable risk controls and optimization potential, this is an efficient and consistent trading approach. As continued innovations enhance its capabilities, this strategy provides investors a valuable tool for achieving steady and manageable profits in financial markets.

Parameters

NameDefaultWhat it controls
v_input_112Fast Length
v_input_226Slow Length
v_input_39Signal Length
v_input_420Bollinger Band Length
v_input_52Bollinger Band Multiplier
v_input_660Profit Target (Points)
v_input_730Stop Loss (Points

Source Pine Script

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

//@version=4
strategy("Modified MACD and Bollinger Band Strategy", shorttitle="Mod_MACD_BB", overlay=true)

var bool open_buy_position = na
var bool open_sell_position = na

// MACD settings
fast_length = input(12, title="Fast Length")
slow_length = input(26, title="Slow Length")
signal_length = input(9, title="Signal Length")
src = close
[macdLine, signalLine, _] = macd(src, fast_length, slow_length, signal_length)

// Bollinger Band settings
bb_length = input(20, title="Bollinger Band Length")
bb_mult = input(2, title="Bollinger Band Multiplier")
basis = sma(src, bb_length)
dev = bb_mult * stdev(src, bb_length)
upper_band = basis + dev
lower_band = basis - dev

// Define profit target and stop loss
profit_target = input(60, title="Profit Target (Points)")
stop_loss = input(30, title="Stop Loss (Points")

// Buy condition: MACD crosses up the signal line and close is above upper Bollinger Band
buy_condition = crossover(macdLine, signalLine) and close > upper_band

// Sell condition: MACD crosses below the signal line and close is below the lower Bollinger Band
sell_condition = crossunder(macdLine, signalLine) and close < lower_band

// Check for open positions
if (buy_condition)
    open_buy_position := true
if (sell_condition)
    open_sell_position := true

// Strategy Orders
strategy.entry("Buy", strategy.long, when = buy_condition and not open_sell_position)
strategy.exit("Take Profit/Stop Loss", from_entry = "Buy", limit = close + profit_target, stop = close - stop_loss)

strategy.entry("Sell", strategy.short, when = sell_condition and not open_buy_position)
strategy.exit("Take Profit/Stop Loss", from_entry = "Sell", limit = close - profit_target, stop = close + stop_loss)

// Reset open position status
if (sell_condition)
    open_buy_position := na
if (buy_condition)
    open_sell_position := na

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-29. 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/Dual-Moving-Average-Bollinger-Band-MACD-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