0bps

Strategy vault / Trend following

Long Term Trading Strategy Based on Bollinger Bands B Indicator

This strategy generates trading signals based on the Bollinger Bands %B indicator. It goes long when the %B value falls below a preset threshold and adopts a dynamic position averaging approach to follow the trend…

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

IMG

Overview

This strategy generates trading signals based on the Bollinger Bands %B indicator. It goes long when the %B value falls below a preset threshold and adopts a dynamic position averaging approach to follow the trend until take profit or stop loss is triggered. The strategy is suitable for identifying pullback opportunities after the support of the Bollinger lower band is broken.

Strategy Logic

  1. Calculate the middle band, upper band and lower band of N-day Bollinger Bands
  2. Calculate %B value: (%B = (Close - LowerBB)/(UpperBB - LowerBB) )
  3. Go long when %B value falls below the threshold (default is 0)
  4. Set take profit based on entry price (default is 105% of entry price) and stop loss (default is 95% of entry price)
  5. Add to the position as long as conditions are met after opening position
  6. The first triggered take profit or stop loss closes the position

Advantage Analysis

The advantages of this strategy are:

  1. %B indicator efficiently identifies pullback points after lower band support
  2. Dynamic position averaging tracks the trend for greater profit
  3. Clear take profit and stop loss conditions facilitate risk control

Risk Analysis

There are also some risks associated with this strategy:

  1. Higher probability of false signals from %B
  2. More frequent stop loss triggers during ranging markets
  3. Aggressive averaging risks uncontrolled loss

Solutions:

  1. Combine with indicators like KD and MACD to confirm signal reliability
  2. Adjust stop loss placement to withstand market volatility
  3. Control averaging pace to avoid risk explosion

Enhancement Opportunities

The strategy can be further optimized in the following areas:

  1. Test different parameter combinations for best results
  2. Optimize averaging logic, e.g. stop adding after certain profit target achieved
  3. Add liquidity filter to prevent errant trading in low liquidity stocks

Summary

Overall this is a relatively robust long-term trading strategy. There is room for improvement in both signal accuracy and parameter tuning. When combined with additional signal filtering and prudent position sizing, this strategy can achieve decent results in trending markets.

Parameters

NameDefaultWhat it controls
v_input_int_120BB Length
v_input_1_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_float_12Deviation
v_input_25Take Profit
v_input_3100Stop Loss
v_input_4timestamp(01 Jan 2023 00:00 +0000)Start Date
v_input_5timestamp(01 Jan 2024 00:00 +0000)End Date
v_input_float_2falsehangi degerin altinda long alsin

Source Pine Script

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

//@version=5
strategy("Bollinger Bands %B Long Strategy", shorttitle="BB %B Long Strategy", overlay=true)

// Girdiler
length = input.int(20, title="BB Length")
src = input(close, title="Source")
dev = input.float(2.0, title="Deviation")
kar_hedefi = input(5, title="Take Profit")
zarar_durumu = input(100, title="Stop Loss")
start_date = input(timestamp("01 Jan 2023 00:00 +0000"), "Start Date")
end_date = input(timestamp("01 Jan 2024 00:00 +0000"), "End Date")
altinda_kalirsa_long = input.float(0, title="hangi degerin altinda long alsin")

// Bollinger Bantları %B göstergesi
basis = ta.sma(src, length)
stdDev = ta.stdev(src, length)
upperBand = basis + dev * stdDev
lowerBand = basis - dev * stdDev
percentB = (src - lowerBand) / (upperBand - lowerBand)

// Alım-Satım Sinyalleri
longCondition = percentB < altinda_kalirsa_long

// Kar/Zarar Hesaplama
takeProfit = strategy.position_avg_price * (1 + kar_hedefi / 100)
stopLoss = strategy.position_avg_price * (1 - zarar_durumu / 100)

// Long (Alım) İşlemi
if (longCondition )
    strategy.entry("Long", strategy.long)
    strategy.exit("Take Profit/Stop Loss", "Long", limit=takeProfit, stop=stopLoss)

// Take Profit Seviyesi Çizgisi
plot(takeProfit, title="Take Profit", color=color.green, linewidth=1, style=plot.style_linebr)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-01. 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/Long-Term-Trading-Strategy-Based-on-Bollinger-Bands-B-Indicator.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