0bps

Strategy vault / Breakout

Bollinger Band Moving Average and MACD Combined Trading Strategy

This strategy combines Bollinger Band, moving average and MACD, forming a relatively complete trading system. While judging the market trend, it can also capture some reversal opportunities.

Author
ChaoZhang
Complexity
Intermediate · 93 lines
Parameters
13
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

IMG

Strategy Overview

This strategy combines Bollinger Band, moving average and MACD, forming a relatively complete trading system. While judging the market trend, it can also capture some reversal opportunities.

Strategy Name & Rationale

The strategy is named "Triangle Anchoring Trend Tracking Strategy". The name highlights its use of three technical indicators to determine trend direction and anchor entry points.

The basic trading logic is:

  1. Judge trend direction. Compare Bollinger Mid Band, EMA and MACD zero line to determine if the market is in an uptrend or downtrend phase.

  2. Find entry opportunities. After a trend is identified, the strategy checks if EMA crosses BB Mid Band and if MACD histogram crosses signal line to determine entries.

  3. Set profit target and stop loss. Once entered, fixed target and stop loss levels are preset.

Advantage Analysis

The biggest advantage of this strategy is the simultaneous use of trend, moving average and MACD tools to guide decisions. This allows more accurate judgments of market momentum and also helps capture some reversals.

Firstly, BB Mid Band clearly reflects the current primary trend direction. The role of EMA is to track the progress of trends. Their comparison and combination enables more precise trend identification.

Secondly, BB itself has strong envelope characteristics. The area around the mid band also indicates certain support/resistance levels. Hence EMA crossovers have signal value.

Additionally, the MACD measures the wax and wane of bullish/bearish momentum. Its absolute size represents high or low crowd emotions, also hinting potential reversals.

Finally, the pre-set profit target and stop loss controls risk/reward of individual trades, ensuring overall stability.

Risk Analysis

Despite the use of multiple analytical tools, main risks are:

  1. Improper BB parameters fail to clearly reflect the primary trend.

  2. EMA system signals long but MACD does not clearly turn positive, bearish forces may expand.

  3. Profit target/stop loss range too wide, single trade loss widens.

Main solutions are:

  1. Adjust BB parameters to ensure mid band effectively reflects main trend.

  2. Introduce more technical indicators to judge bull/bear momentum.

  3. Evaluate historical trades and optimize profit target/stop loss.

Optimization Directions

The strategy can be further improved in the following aspects:

  1. Introduce more indicators like KDJ, ATR etc to aid trend judgment and improve accuracy.

  2. Implement more sophisticated stops like trailing stop, breakout stop etc.

  3. Assess performance across different products. Fine tune parameters to suit various market conditions.

  4. Test and tweak strategy based on backtest results over different timeframes and markets.

  5. Incorporate machine learning for automatic parameter optimization and dynamic strategy update.

Conclusion

This strategy leverages BB, MA and MACD together. It has clear trend judgment, certain envelope characteristics and also captures some reversals. With more auxiliary tools for judging entries/exits, it can achieve more reliable performance. Further evaluation and enhancement of this strategy is warranted and expected to produce a robust quantitative tool.

Parameters

NameDefaultWhat it controls
v_input_1150BB Length
v_input_2_close0BB Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_32BB StdDev
v_input_434EMA Length
v_input_5_close0EMA Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_6SMAMethod
v_input_75Length
v_input_89Fast Length
v_input_917Slow Length
v_input_10_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_int_19Signal Smoothing
v_input_string_10Oscillator MA Type: EMA | SMA
v_input_string_20Signal Line MA Type: EMA | SMA

Source Pine Script

/*backtest
start: 2024-01-04 00:00:00
end: 2024-02-03 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("Combined Strategy", overlay=true, shorttitle="Comb Strat", default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// Precio de beneficio y Stop Loss
takeProfitTicks = 87636
stopLossTicks = 53350

// Bollinger Bands + EMA
length_bb = input(150, title="BB Length")
src_bb = input(close, title="BB Source")
mult = input(2.0, title="BB StdDev")
basis = ta.sma(src_bb, length_bb)
dev = mult * ta.stdev(src_bb, length_bb)
upper = basis + dev
lower = basis - dev

len_ema = input(34, title="EMA Length")
src_ema = input(close, title="EMA Source")
out_ema = ta.ema(src_ema, len_ema)

typeMA = input("SMA", title="Method")
smoothingLength = input(5, title="Length")

var float smoothingLine = na
if (typeMA == "SMA")
    smoothingLine := ta.sma(out_ema, smoothingLength)
else if (typeMA == "EMA")
    smoothingLine := ta.ema(out_ema, smoothingLength)

// MACD
fast_length = input(title="Fast Length", defval=9)
slow_length = input(title="Slow Length", defval=17)
src_macd = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval=1, maxval=50, defval=9)
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"])

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

// Condiciones de compra y venta
longCondition = (out_ema > basis) and (macd > signal) and (signal > 0)
shortCondition = (out_ema < basis) and (macd < signal) and (signal < 0)

// Variables de estado
var bool longExecuted = na
var bool shortExecuted = na

// Estrategia
if (longCondition and not longExecuted)
    strategy.entry("Long", strategy.long)
    longExecuted := true
    shortExecuted := na
if (shortCondition and not shortExecuted)
    strategy.entry("Short", strategy.short)
    shortExecuted := true
    longExecuted := na

// Take Profit y Stop Loss para Compras y Ventas Cortas
strategy.exit("Take Profit/Close Long", from_entry="Long", profit=takeProfitTicks, loss=stopLossTicks)
strategy.exit("Take Profit/Close Short", from_entry="Short", profit=takeProfitTicks, loss=stopLossTicks)

// Cierre de posiciones cuando la dirección cambia
if ((out_ema < basis) and (macd < signal))
    strategy.close("Long")
    longExecuted := na
if ((out_ema > basis) and (macd > signal))
    strategy.close("Short")
    shortExecuted := na

// Plots
plot(basis, "BB Basis", color=#FF6D00)
plot(upper, "BB Upper", color=color.new(#2962FF, 0.5))
plot(lower, "BB Lower", color=color.new(#2962FF, 0.5))

plot(smoothingLine, title="Smoothing Line", color=#f37f20, linewidth=2)

hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(hist, title="Histogram", style=plot.style_columns, color=(hist >= 0 ? (hist[1] < hist ? color.green : color.red) : (hist[1] < hist ? color.red : color.green)))
plot(macd, title="MACD", color=color.blue)
plot(signal, title="Signal", color=color.orange)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-04. 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/Bollinger-Band-Moving-Average-and-MACD-Combined-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