Strategy · Pine Script v4 · On the chart

Bollinger Bands Strategy with Intraday Intensity Index

by hasnocool

This strategy plots Bollinger Bands on the price chart using an EMA or SMA basis and a matching standard-deviation calculation.

View on GitHub

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code68
Settings9inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

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

68 lines of Pine Script v4 from Bollinger Bands Strategy with Intraday Intensity Index.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Bollinger Bands Strategy with Intraday Intensity Index.pine
//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Bollinger Bands Strategy with Intraday Intensity Index
// by SparkyFlary
//For Educational Purposes
//Results can differ on different markets and can fail at any time. Profit is not guaranteed.
strategy(title="Bollinger Bands Strategy with Intraday Intensity Index", shorttitle="Bollinger Bands Strategy", overlay=true)
BBlength = input(20, title="Bollinger Bands length")
BBtype = input("EMA", title="Bollinger Bands and Standard deviation type", type=input.string, options=["EMA", "SMA"])
BBprice = input(close, title="source")
timeStop = input(20, title="Time-based stop length")
BBmult = input(2.0, title="Bollinger Bands Standard Deviation")
withIII = input(false, title="with Intraday Intensity Index?")
IIIlength = input(21, title="Intraday Intensity Index length")
positionMode = input("Both long and short", options=["Both long and short", "Long only", "Short only"])
earlyExit = input(false)
//function for choosing moving averages
f_ma(type_, src_, len_) =>
    float result = 0
    if type_ == "SMA"
        result := sma(src_, len_)
    if type_ == "EMA"
        result := ema(src_, len_)
    result
//Intraday Intensity Index
k1 = (2 * close - high - low) * volume
k2 = high != low ? high - low : 1
i = k1 / k2
iSum = sum(i, IIIlength)
//standard deviation with exponential option
StdDev_(type_, src_, len_) =>
    mean = 0.0
    sumSqr = 0.0
    variance = 0.0
    if len_ > 0
        mean := f_ma( type_, src_, len_ )
        sumSqr := 0
        for counter = 0 to len_ - 1
            sumSqr := sumSqr + pow( src_[counter] - mean, 2 )
        variance := sumSqr / len_
        stdDev = sqrt( variance )
    else
        stdDev = -1
//Bollinger Bands
BBdev = BBmult * StdDev_(BBtype, BBprice, BBlength)
BBbasis = f_ma(BBtype, BBprice, BBlength)
BBupper = BBbasis + BBdev
BBlower = BBbasis - BBdev
plot(BBupper, title="Bollinger Bands Upper Line")
plot(BBlower, title="Bollinger Bands Lower Line")
plot(BBbasis, title="Bollinger Bands Mid line", color=color.maroon)
//Strategy
buy = close[1]<BBlower[1] and close>BBlower and (withIII ? iSum>0 : 1)
sell = close>BBbasis or (earlyExit?strategy.openprofit>0:0) or barssince(buy)>timeStop
short = close[1]>BBupper[1] and close<BBupper and (withIII ? iSum<0 : 1)
cover = close<BBbasis or (earlyExit?strategy.openprofit>0:0) or barssince(short)>timeStop
if positionMode=="Long only"
    strategy.entry(id="enter long", long=true, when=buy)
    strategy.close(id="enter long", comment="exit long", when=sell)
else if positionMode=="Short only"
    strategy.entry(id="enter short", long=false, when=short)
    strategy.close(id="enter short", comment="exit short", when=cover)
else
    strategy.entry(id="enter long", long=true, when=buy)
    strategy.close(id="enter long", comment="exit long", when=sell)
    strategy.entry(id="enter short", long=false, when=short)
    strategy.close(id="enter short", comment="exit short", when=cover)
Expand (78 lines)

Overview

Bollinger Bands Strategy with Intraday Intensity Index enters long when price crosses back above the lower band and short when price crosses back below the upper band. An optional Intraday Intensity Index, built from price range and volume, can require positive or negative intensity before entry. Exits close a long above the midline or a short below it, after a set number of bars, or immediately when open profit is positive if early exit is enabled. Direction can be long only, short only, or both. There is no price stop loss.

Details

Published by

hasnocool on GitHub

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Volatility & bands, Moving averages, Volume, Mean reversion

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
Bollinger Bands lengthint20BBlength
Bollinger Bands and Standard deviation typestringEMABBtype
sourcesourcecloseBBprice
Time-based stop lengthint20timeStop
Bollinger Bands Standard Deviationfloat2.0BBmult
with Intraday Intensity Index?boolfalsewithIII
Intraday Intensity Index lengthint21IIIlength
positionModestringBoth long and shortpositionMode
earlyExitboolfalseearlyExit
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

257 stars

This overlay strategy is built for ES futures on the one-minute chart. It plots a volume-weighted moving average of price opens and standard-deviation bands at Fibonacci ratios, plus a second longer volume-weighted average from a higher-timeframe open. Call an

Strategy v5 Volatility & bandsMoving averages
Updated 6 weeks ago · None stated GitHub repository of ES Futures 1M No Repaint
218 stars

The Mean Reversion Strategy Template calculates a baseline mean using SMA, EMA, or VWAP alongside upper and lower deviation bands based on standard deviation, ATR, or percentage offsets. It generates long and short entries when price touches, bounces from, or

Strategy v6 Mean reversionMoving averages
Updated 3 months ago · None stated GitHub repository of Mean Reversion Strategy Template

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

Bollinger Bands Strategy

by TWODS-CAPITAL
37 stars

This strategy executes trades based on price interactions with standard Bollinger Bands plotted on the price chart. It calculates a simple moving average basis line along with upper and lower standard deviation bands. Long entries are triggered when price cros

Strategy v5 Volatility & bandsMean reversion
Updated 3 years ago · MIT GitHub repository of Bollinger Bands Strategy

CombinedBinHAndClucV8

by viktors1982
36 stars

This strategy trades on the price chart using multiple indicators. It enters long positions when price dips meet conditions on Bollinger Bands 40 and 20, RSI, MFI, volume and other thresholds. It exits at custom ROI profit levels based on RSI or after timeout

Strategy v4 Oscillators & momentumMoving averages Repaints
Updated 2 years ago · None stated GitHub repository of CombinedBinHAndClucV8

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