Strategy · Pine Script v5 · Own pane

Bollinger Bands Strategy With StopLoss

by hasnocool

This strategy calculates standard Bollinger Bands using a Simple Moving Average and standard deviation multiplier.

View on GitHub

GitHub stars106on the repository
Pine versionv5runs as is
Lines of code35
Settings4inputs
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

35 lines of Pine Script v5 from Bollinger Bands Strategy with StopLoss.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Bollinger Bands Strategy with StopLoss.pine
//@version=5
strategy(title="Bollinger Bands Strategy With StopLoss", overlay=false, shorttitle="BBS", pyramiding=0, currency=currency.USD, commission_type=strategy.commission.percent, commission_value=0.03, initial_capital=1000)
source = input(close, "Source")
length = input.int(20, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50, step=0.001)
stopLossFactor = input.float(1, "Stop Loss Percent", maxval = 100, minval = 0, step=0.1)
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev
var bool currentIsLong = na
var bool nextExpectedIsLong = true
buyEntry = ta.crossover(source, lower)
sellEntry = ta.crossunder(source, upper)
if (buyEntry and nextExpectedIsLong == true)
    strategy.entry("BBandLE", strategy.long, comment="BBandLE")
    nextExpectedIsLong := false
else
    strategy.cancel("BBandLE")
if (sellEntry and nextExpectedIsLong == false)
    strategy.entry("BBandSE", strategy.short, comment="BBandSE")
    nextExpectedIsLong := true
else
    strategy.cancel("BBandSE")
strategy.close("BBandLE", close < strategy.opentrades.entry_price(0) * (1 - (stopLossFactor / 100)))
strategy.close("BBandSE", close > strategy.opentrades.entry_price(0) * (1 + (stopLossFactor / 100)))
plot(source, "close", color.blue)
plot(lower, "lower", color.red)
plot(upper, "upper", color.red)
plot(strategy.opentrades.entry_price(0), "lastTradePrice", color.green)
plot(strategy.opentrades.entry_price(0) * (1 - (stopLossFactor / 100)), "stopLossLow", color.black)
plot(strategy.opentrades.entry_price(0) * (1 + (stopLossFactor / 100)), "stopLossHigh", color.black)
plotchar(strategy.position_size > 0, "OpenLongStatus", char="-", size=size.tiny, location=location.bottom, color=color.green)
plotchar(strategy.position_size < 0, "",char="-", size=size.tiny, location=location.bottom, color=color.red)
Expand (71 lines)

Overview

Bollinger Bands Strategy With StopLoss enters alternating long positions when price crosses above the lower band and short positions when price crosses below the upper band. Open trades are closed if price exceeds a user-defined percentage stop loss relative to the entry price. Adjustable settings include source, band length, multiplier, and stop loss percentage. The strategy plots close prices, upper and lower bands, trade entry prices, and stop loss levels in a separate pane.

Details

Published by

hasnocool on GitHub

Type

Strategy, Pine Script v5

Draws

Separate pane below the chart

Category

Volatility & bands, Mean reversion

Markets

Crypto

Timeframes

M1-M5, M15-H1

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
Sourcesourceclosesource
lengthint20length
multfloat2.0mult
Stop Loss Percentfloat1stopLossFactor
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 strategy combines Larry Williams' Vix Fix synthetic volatility indicator with Bollinger Bands. It calculates standard and inverted Williams Vix Fix metrics, displaying them as histogram columns with standard deviation bands in a separate pane. Long entrie

Strategy v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Williams Vix Fix Strategy [Alorse]

Greeffer BB Strategy

by SammyEnigma
18 stars

This strategy fades Bollinger Band extremes using the close. It enters short when price crosses above the upper band and the 14-period RSI is above 70, and enters long when price crosses below the lower band and RSI is below 30. It closes half of an open short

Strategy v4 Volatility & bandsOscillators & momentum
Updated 6 years ago · None stated GitHub repository of Greeffer BB Strategy

VT-Lite

by algoboy-kevin
4 stars

VT-Lite is a Pine Script v5 mean-reversion strategy that draws in a separate pane. It sizes equity-percent entries from a short-term EMA and ATR band distance, with a volatility EMA threshold and a choice of close, low, or ATR-band source. It can add reentries

Strategy v5 Mean reversionVolatility & bands
Updated 2 years ago · None stated GitHub repository of VT-Lite

Alpy Vix

by avatar-lavventura
2 stars

This strategy plots the Williams Vix Fix indicator as a histogram in a separate pane to detect potential trend reversals. It calculates the Vix Fix value using a lookback period and standard deviation bands derived from Bollinger Bands on the Vix Fix. Multiple

Strategy v4 Oscillators & momentumVolatility & bands Repaints
Updated 7 months ago · None stated GitHub repository of Alpy Vix

CTC BB Hedge Strategy

by wynnforthework
2 stars

CTC BB Hedge Strategy calculates the Pearson correlation coefficient between the close prices of two symbols to determine market alignment. It applies Bollinger Bands to the primary symbol for mean-reversion signals while optionally filtering with a moving ave

Strategy v5 Volatility & bandsMean reversion Repaints
Updated 1 year ago · None stated GitHub repository of CTC BB Hedge 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