Strategy · Pine Script v5 · On the chart

STRG-BBForce

by harryguiacorn· code credited to LutherCoat

STRG-BBForce is a Bollinger Bands trend-following strategy.

View on GitHub

GitHub stars219on the repository
Pine versionv5runs as is
Lines of code59
Settings6inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago8 May 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

59 lines of Pine Script v5 from STRG-BBForce.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

STRG-BBForce.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © LutherCoat

//@version=5
strategy("STRG-BBForce", initial_capital = 10000, overlay=true, margin_long=0, margin_short=0)
//--------------- Built in BB Modified ---------------
length = input.int(26, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
// fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
//--------------- Built in BB Modified ---------------
var existingCondition = 0
condition1 = if upper[0] < upper[1] and lower[0] < lower[1] and basis[0] < basis[1]
    -1
condition2 = if upper[0] > upper[1] and lower[0] > lower[1] and basis[0] > basis[1]
    1
conditionFull = condition1?-1:condition2?1:na

conditionMini = 0
if conditionFull != existingCondition
    conditionMini := conditionFull
    existingCondition := conditionFull
    existingCondition
    
// set conditionFull to show partial bands only when 3 elements point downwards
// p1 = plot(upper, "Upper", color=color.new(color.blue, conditionFull==-1?0:conditionFull==1?0:100), offset = offset, style = plot.style_stepline, linewidth = 4)
// plot(basis, "Basis", color=color.new(color.orange, conditionFull==-1?0:conditionFull==1?0:100), offset = offset, style = plot.style_stepline, linewidth = 4)
// p2 = plot(lower, "Lower", color=color.new(color.blue, conditionFull==-1?0:conditionFull==1?0:100), offset = offset, style = plot.style_stepline, linewidth = 4)
// plotarrow(conditionFull, 'Force', colorup=color.new(color.black, 0), colordown=color.new(color.black, 0), maxheight = 15)

p1 = plot(upper, "Upper", color=color.new(color.blue, 0), offset = offset, style = plot.style_stepline, linewidth = 4,display = display.all)
plot(basis, "Basis", color=color.new(color.orange, 0), offset = offset, style = plot.style_stepline, linewidth = 4,display = display.none)
p2 = plot(lower, "Lower", color=color.new(color.blue, 0), offset = offset, style = plot.style_stepline, linewidth = 4,display = display.all)

plotarrow(conditionFull, 'Force Full', colorup=color.new(color.fuchsia, 0), colordown=color.new(color.fuchsia, 0), maxheight = 15,display = display.none)
plotarrow(conditionMini, 'Force Mini', colorup=color.new(color.yellow, 0), colordown=color.new(color.yellow, 0), maxheight = 15)
contracts = strategy.initial_capital / close // strategy.equity / close
longCondition = conditionMini==1?true:false
fromDate = input.int(2018, 'Look back since')
partialProfitPerc = input.int(50,'Partial Profit %')
if (longCondition and year >= fromDate)
    strategy.close_all("CLOSE ALL")
    strategy.entry("L", strategy.long,qty = contracts)
strategy.exit("PARTIAL%", "L", qty_percent = partialProfitPerc, limit = upper, stop = lower)
strategy.exit("Stop L","L", qty = math.abs(strategy.position_size), stop = lower) // position size can be negative and positive, it needs to be positive in this case

shortCondition = conditionMini==-1?true:false
if (shortCondition and year >= fromDate)
    strategy.close_all("CLOSE ALL")
    strategy.entry("S", strategy.short,qty = contracts)
strategy.exit("PARTIAL%", "S", qty_percent = partialProfitPerc, limit = lower, stop = upper)
strategy.exit("Stop S","S", qty = math.abs(strategy.position_size), stop = upper)

// plotchar(strategy.position_size, char='❄') //allows for checking position size in data window

Overview

STRG-BBForce calculates standard Bollinger Bands and tracks directional alignment across the upper, lower, and basis lines. When all three lines slope upward, it opens a long position and closes short positions. When all three lines slope downward, it opens a short position and closes long positions. It features partial profit-taking using the outer Bollinger Bands as price targets and sets stop losses at the opposing band. Input settings allow adjustments for band length, standard deviation multiplier, backtest start year, and partial profit percentage.

Details

Published by

harryguiacorn on GitHub

Code credit

LutherCoat, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Volatility & bands, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

bollingerbollinger-bandbollinger-bandsichimokuichimoku-cloudichimoku-indicatorpinepinescriptpinescript-guidespinescript-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
lengthint26length
Sourcesourceclosesrc
StdDevfloat2.0mult
Offsetint0offset
Look back sinceint2018fromDate
Partial Profit %int50partialProfitPerc
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

This mean-reversion strategy plots Bollinger Bands and a long-term moving average directly on the price chart. It enters trades when price candles penetrate the outer Bollinger Bands beyond a user-defined body percentage, anticipating a price reversal. Entries

Strategy v5 Mean reversionVolatility & bands
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner PRO [Alorse]
257 stars

This overlay strategy targets one-minute ES futures and plots volume-weighted moving-average bands from the open, scaled by a standard-deviation multiple, plus a slower band from ten-minute opens. A higher-timeframe exponential moving average can filter trades

Strategy v6 Moving averagesVolatility & bands
Updated 6 weeks ago · None stated GitHub repository of ES - Futures 1M No Repaint
106 stars

This trading strategy combines three Exponential Moving Averages (8, 14, and 50 periods by default) with Stochastic RSI and Average True Range (ATR). It enters a long position when EMA 1 is above EMA 2, EMA 2 is above EMA 3, and the Stochastic RSI %K line cros

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3 x EMA + Stochastic RSI + ATR

Bollinger Bands Strategy

by cklose2000
105 stars

This strategy generates buy and sell signals based on Bollinger Band crossovers combined with a simple moving average trend filter. It enters a long position when price crosses above the lower band while trading above the SMA filter line, and enters a short po

Strategy v5 Volatility & bandsMoving averages
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Strategy
104 stars

This overlay strategy combines a Chandelier Exit with a zero-lag least squares moving average. Although the header mentions Heikin Ashi candles, the calculations use ordinary closes. An ATR trailing stop sets long and short levels, and direction flips when pri

Strategy v5 TrendMoving averages
Updated 3 years ago · MIT GitHub repository of Chandelier Exit & ZLSMA Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers