Strategy · Pine Script v5 · On the chart

Greeffer's Bolinger Band Hedge Strategy

by modster· code credited to Greeffer

This strategy combines Bollinger Bands and Bollinger Band Width to trigger breakout trades during periods of low volatility.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code119
Settings3inputs
LicenceCC0-1.0
Last update4 years ago16 Mar 2022

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

119 lines of Pine Script v5 from leobot/bbHedge.pine, published under CC0-1.0. Keep the author's credit and licence lines when you reuse it.

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

//@version=5

// S T U D Y
// indicator(shorttitle="bbWidth Hedge Indicator", title="Bollinger Bands Width Indicator", precision=6, overlay=false)


// S T R A T E G Y 
strategy(
     "Greeffer's Bolinger Band Hedge Strategy",
     "bbWidth Hedge Strategy",
     overlay=true, 
     precision=6,
     pyramiding=2,
     default_qty_type=strategy.percent_of_equity,
     default_qty_value=200,
     initial_capital=5000,
     commission_type=strategy.commission.percent,
     commission_value=0.01,
     margin_long=1./20*50, 
     margin_short=1./20*50
     )


length = input.int(40, minval=1)
src = input(close, title="Source")
mult = input.float(6.0, minval=0.001, maxval=50, title="StdDev")
entryCondition = false
longExitCondition = false
shortExitCondition = false


// B O L L I N G E R   B A N D S
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev


// B B W I D T H
bbWidth = ta.bbw(src, length, mult)


// SMA
bbWidthSMA = ta.sma(bbWidth, 100)


// D O N C H I A N   C H A N N E L S
donUpper = ta.highest(length)
donLower = ta.lowest(length)


// P L O T S
plot(bbWidthSMA, "bbWidthSMA", color=color.yellow)
// plot(basis, "Basis", color=#FF6D00)
// p1 = plot(upper, "Upper", color=color.rgb(150, 33, 243, 25))
// p2 = plot(lower, "Lower", color=color.rgb(33, 150, 243, 25))
// fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
plot(bbWidth, "bbWidth",  color=color.rgb(150, 33, 243, 20))
//plot(ta.highest(100), color=color.yellow)
//plot(ta.lowest(100), color=color.yellow)


// C O N D I T I O N S
// Entries:
var longEntryCondition = false
var shortEntryCondition = false

// Long:
if ta.crossover(close, upper) and bbWidth<bbWidthSMA
    longEntryCondition := true
else
    longEntryCondition := false

// Short:
if ta.crossunder(close, lower) and bbWidth<bbWidthSMA
    shortEntryCondition := true
else
    shortEntryCondition := false

// Exit:
// Long
if ta.crossunder(bbWidth, bbWidthSMA)
    longExitCondition:=true
    shortExitCondition:=true
// else
//     longExitCondition:=false

// // Short
// if ta.crossover(close, upper)
//     shortExitCondition:=true
// else
//     shortExitCondition:=false


// E N T R I E S
var longPosition = false  //<-------------------------------------------maybe plot this somehow instead?
var shortPosition = false //<-------------------------------------------maybe plot this somehow instead?

// Long:
if (longEntryCondition)
    strategy.entry("long", strategy.long)
    //longPosition:=true

// Short:
if (shortEntryCondition)
    strategy.entry("short", strategy.short)
    //shortPosition:=true

// Exits:
if (longExitCondition)
    strategy.close("long", when=shortExitCondition, comment="exitLong", qty_percent=100)//, trail_price=donUpper, trail_offset=10000)
    // strategy.exit("exitLong", from_entry="long", qty_percent=100)//, trail_price=donUpper, trail_offset=10000)

if (shortExitCondition)
    strategy.close("short", when=shortExitCondition, comment="exitShort", qty_percent=100)//, trail_price=donUpper, trail_offset=10000)
    // // strategy.exit("exitShort", from_entry="short", qty_percent=100)//, trail_price=donUpper, trail_offset=10000)

Overview

Greeffer's Bolinger Band Hedge Strategy opens long positions when price crosses above the upper band while band width is below its 100-period moving average, and opens short positions when price breaks below the lower band under the same condition. Trades are closed when band width drops back below its moving average. User settings control the length, source, and standard deviation parameters. It overlay plots on the price chart without explicit stop-loss orders.

Details

Published by

modster on GitHub

Code credit

Greeffer, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Volatility & bands, Breakout, Moving averages

Markets

Crypto

Timeframes

Any timeframe

Risk Notes

  • The strategy can add several entries in the same direction
  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
lengthint40length
Sourcesourceclosesrc
StdDevfloat6.0mult
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

106 stars

This strategy runs on the price chart and enters long when a fast simple moving average crosses above a slow one, and short on the opposite cross, while closing the other side. Entries also need ATR above a filter, a pullback check, a minimum candle-body chang

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 9:22 5 MIN 15 MIN BANKNIFTY

均线突破交易系统

by tangly1024
76 stars

This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the

Strategy v5 TrendMoving averages
Updated 4 months ago · None stated GitHub repository of 均线突破交易系统
16 stars

This strategy plots Donchian channels, trailing stops, and profit targets directly on the price chart. It enters breakout trades using a 55-bar Donchian channel and sizes positions according to an ATR-based risk percentage per unit. The system adds units as pr

Strategy v6 TrendBreakout
Updated 11 months ago · None stated GitHub repository of Seykota Alt 10: Profit Targets
4 stars

This strategy executes trend-following trades based on Bollinger Band breakouts confirmed by a long-term moving average filter. A long position opens when the price crosses above the upper Bollinger Band, provided the band spread meets a minimum threshold and

Strategy v5 Volatility & bandsTrend
Updated 2 years ago · None stated GitHub repository of Bollinger Trend - Strategy V1
4 stars

The Keltner Channel Strategy is a breakout system plotted directly on the main price chart. It calculates upper and lower channel bands around an exponential or simple moving average using ATR, True Range, or bar range multiplied by a specified factor. A long

Strategy v5 TrendVolatility & bands
Updated 5 months ago · None stated GitHub repository of Keltner Channel 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