0bps

Strategy vault / Trend following

Index Trading Strategy Based on Bollinger Bands

The strategy is named "Quant Trading Strategy Based on Bollinger Bands". It is an index and stock trading strategy based on improved Bollinger Bands channel. By adjusting Bollinger Bands parameters, it realizes…

Author
ChaoZhang
Complexity
Intermediate · 57 lines
Parameters
12
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

Overview

The strategy is named "Quant Trading Strategy Based on Bollinger Bands". It is an index and stock trading strategy based on improved Bollinger Bands channel. By adjusting Bollinger Bands parameters, it realizes optimization for both long and short positions to profit in both uptrend and downtrend markets.

Trading Logic

The core logic of this strategy is based on Bollinger Bands channel, which consists of middle line, upper band and lower band. The middle line is the moving average of closing price for n days. The upper and lower bands are deviations above and below the middle line. When price approaches the upper band, it indicates the market may be overheated and there can be short opportunities. When price approaches the lower band, it indicates the market may be undervalued and there can be long opportunities.

This strategy uses two Bollinger Bands. Bollinger Band 1 is suitable for long trades and Bollinger Band 2 is suitable for short trades. The parameters of Bollinger Band 1 are optimized with length of 25 and deviation of 2.9 times. The parameters of Bollinger Band 2 are optimized with length of 36 and deviation of 3.2 times. When closing price crosses above the lower band of Bollinger Band 1, it will generate long signal. When closing price crosses below the upper band of Bollinger Band 2, it will generate short signal.

Advantage Analysis

Compared with traditional Bollinger Bands strategies, this strategy has the following advantages:

  1. It realizes dual-directional trading for both long and short sides, which can seize trading opportunities in different market stages.

  2. The parameters are optimized. The two sets of Bollinger Bands parameters are elaborately tested to effectively generate trading signals.

  3. The risk is controllable. The moving stop loss method can effectively control the risk of one side.

Risk Analysis

There are also some potential risks for this strategy:

  1. Invalidity risk of Bollinger Bands. Bollinger Bands may become invalid during extreme market fluctuation.

  2. Risk of stop loss being hit. Moving stop loss could be hit to expand losses. We can properly widen the stop loss or timely stop out to avoid this risk.

  3. High trading frequency risk. Overly sensitive parameters could lead to frequent trading and increased trading costs.

Optimization Directions

There is still room for further optimization of this strategy:

  1. Combine other indicators to filter signals and avoid wrong trades when Bollinger Bands fails. Such as K-line patterns, trading volume etc.

  2. Dynamically adjust parameters to fit the market characteristics of different periods. For example, use adaptive Bollinger Bands.

  3. Optimize the stop loss methods by using trailing stop loss or exponential moving stop loss to effectively control risks.

  4. Combine machine learning algorithms to automatically optimize the parameters.

Summary

In summary, this strategy overall optimizes dual-directional trading for both long and short sides based on double Bollinger Bands channel and parameters optimization. Compared with traditional Bollinger Bands strategies, it has the advantages of dual-directional trading and risk control. It is suitable to seize opportunities in different market stages and has certain practical value. But risks like failure of Bollinger Bands and stop loss being hit still exist. Further optimization and verification are needed before productization.

Parameters

NameDefaultWhat it controls
v_input_125Length BB long
v_input_22.9MULT BB long
v_input_336Length BB short
v_input_43.2MULT BB short
v_input_5truelongEntry
v_input_6trueshortEntry
v_input_7100risk
v_input_8trueleverage
v_input_90.065Take profit % for long
v_input_100.04Stop loss % for long
v_input_110.025Take profit % for short
v_input_120.04Stop loss % for short

Source Pine Script

/*backtest
start: 2022-12-01 00:00:00
end: 2023-12-07 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=4
strategy("BB NDX strategy", overlay = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, calc_on_every_tick = true, commission_type = strategy.commission.percent, commission_value = 0.01)

source = close
length = input(25, minval=1, title="Length BB long")
mult = input(2.9, minval=0.001, maxval=50, step=0.1, title="MULT BB long")

length2 = input(36, minval=1, title="Length BB short")
mult2 = input(3.2, minval=0.001, maxval=50, step=0.1, title="MULT BB short")


basis = sma(source, length)
dev = mult * stdev(source, length)
dev2 = mult2 * stdev(source, length2)

upper = basis + dev2
lower = basis - dev

buyEntry = crossover(source, lower)
sellEntry = crossunder(source, upper)

longEntry=input(true)
shortEntry=input(true)

g(v, p) => round(v * (pow(10, p))) / pow(10, p)
risk     = input(100)
leverage = input(1.0, step = 0.5)
c = g((strategy.equity * leverage / open) * (risk / 100), 4)


tplong=input(0.065, step=0.005, title="Take profit % for long")
sllong=input(0.04, step=0.005, title="Stop loss % for long")
tpshort=input(0.025, step=0.005, title="Take profit % for short")
slshort=input(0.04, step=0.005, title="Stop loss % for short")

if(longEntry)
    strategy.entry("long",1,c,when=buyEntry)
    strategy.exit("short_tp/sl", "long", profit=close * tplong / syminfo.mintick, loss=close * sllong / syminfo.mintick, comment='LONG EXIT',  alert_message = 'closeshort')
    strategy.close("long",when=sellEntry)
if(shortEntry)
    strategy.entry("short",0,c,when=sellEntry)
    strategy.exit("short_tp/sl", "short", profit=close * tpshort / syminfo.mintick, loss=close * slshort / syminfo.mintick, comment='SHORT EXIT',  alert_message = 'closeshort')
    strategy.close("short",when=buyEntry)


Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-08. 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/Index-Trading-Strategy-Based-on-Bollinger-Bands.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