0bps

Strategy vault / Breakout

BB Breakout Strategy

This strategy is based on the Bollinger Bands indicator and generates trading signals when the price breaks through the upper or lower bands. It goes long when the price breaks above the upper band and goes short…

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

IMG

Overview

This strategy is based on the Bollinger Bands indicator and generates trading signals when the price breaks through the upper or lower bands. It goes long when the price breaks above the upper band and goes short when the price breaks below the lower band. Additionally, if holding a long position, it closes the position when the price falls below the lower band; if holding a short position, it closes the position when the price breaks above the upper band. The strategy aims to capture market volatility, entering trades when price fluctuations intensify and exiting in a timely manner when prices reverse.

Strategy Principle

  1. Calculate the moving average of a specified period as the middle band of the Bollinger Bands. Various types of moving averages can be chosen, such as SMA, EMA, SMMA, WMA, and VWMA.
  2. Calculate the upper and lower bands by adding and subtracting a certain multiple of the standard deviation from the middle band.
  3. Generate a long signal when the price breaks above the upper band, and a short signal when it breaks below the lower band.
  4. If holding a long position, close the position when the price falls below the lower band; if holding a short position, close the position when the price breaks above the upper band.

Advantage Analysis

  1. Bollinger Bands can effectively quantify market volatility and provide clear trading signals when price fluctuations intensify.
  2. The strategy also includes stop-loss conditions, which can effectively control risks.
  3. The strategy parameters are adjustable and can be optimized for different instruments and time frames, providing a certain degree of adaptability and flexibility.

Risk Analysis

  1. In a choppy market, frequent price breakthroughs of the upper and lower Bollinger Bands may lead to excessive trading signals, thereby increasing transaction costs.
  2. Bollinger Bands have a certain lag, and trading signals may be delayed when the market changes rapidly.
  3. Improper selection of Bollinger Band parameters may result in poor strategy performance, requiring optimization based on different instruments and time frames.

Optimization Directions

  1. Consider introducing trend indicators or price behavior pattern recognition methods to further confirm trading signals and reduce losing trades caused by false breakthroughs.
  2. Optimize stop-loss conditions, such as setting dynamic stop-losses based on indicators like ATR or introducing trailing stop-losses to further control risks.
  3. Optimize strategy parameters using methods such as genetic algorithms or grid search to find the optimal parameter combination.

Summary

The BB Breakout Strategy is a trading strategy based on the Bollinger Bands indicator, seeking trading opportunities when prices break through the upper or lower bands. The strategy's advantages are clear signals and easy implementation, with certain risk control measures in place. However, the strategy also has some limitations, such as potentially high trading frequency and signal lag. Therefore, in practical applications, improvements can be considered in areas such as signal confirmation, stop-loss optimization, and parameter optimization to enhance the strategy's stability and profitability.

Source Pine Script

/*backtest
start: 2023-06-08 00:00:00
end: 2024-06-13 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("BB Strategy", overlay=true)

// Input parameters
length = input.int(20, minval=1, title="Length")
maType = input.string("SMA", "Basis MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
offset = input.int(0, "Offset", minval=-500, maxval=500, title="Offset")

// Moving average function
ma(source, length, _type) =>
    switch _type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

// Calculate Bollinger Bands
basis = ma(src, length, maType)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev

// Plot Bollinger Bands
plot(basis, "Basis", color=color.blue, offset=offset)
p1 = plot(upper, "Upper", color=color.red, offset=offset)
p2 = plot(lower, "Lower", color=color.green, offset=offset)
fill(p1, p2, title="Background", color=color.rgb(33, 150, 243, 95))

// Strategy logic
longCondition = ta.crossover(close, upper)
shortCondition = ta.crossunder(close, lower)

// Strategy entries and exits
if (longCondition)
    strategy.entry("Long", strategy.long)
if (shortCondition)
    strategy.entry("Short", strategy.short)
if (shortCondition and strategy.position_size > 0)
    strategy.close("Long")
if (longCondition and strategy.position_size < 0)
    strategy.close("Short")

Published by ChaoZhang on the FMZ strategy square · last modified 2024-06-14. 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/BB-Breakout-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