Strategy · Pine Script v5 · On the chart

Bollinger Bands and RSI Strategy

by geraked

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return.

View on GitHub

GitHub stars104on the repository
Pine versionv5runs as is
Lines of code58
Settings6inputs
LicenceMIT
Last update3 years ago29 Oct 2023

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

58 lines of Pine Script v5 from strategies/BBRSI.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

BBRSI.pine
// Implementation of a strategy using Bollinger Bands and RSI
// The strategy has been explained in this video: https://youtu.be/pCmJ8wsAS_w
// © 2023 Geraked, Rabist
// Licensed under MIT

// Backtesting result:
//      Symbol:             EURUSD
//      Timeframe:          5 MIN
//      Range:              2023-06-18 21:00 — 2023-07-14 11:05
//      Percent Profitable: 75.0%
//      Total Trades:       88
//      Profit Factor:      2.120

//@version=5
strategy("Bollinger Bands and RSI Strategy", "BBRSI", overlay=true)

bbLength = input.int(20, "BB Length", minval=1)
bbMult = input.float(2.0, "BB StdDev", minval=0.001, maxval=50)
rsiLength = input.int(3, "RSI Length", minval=1)
slCoef = input.float(1.5, "SL Coef")
tpCoef = input.float(1.5, "TP Coef")
closeBbBasis = input.bool(true, "Close on BB Basis")

bbBasis = ta.sma(close, bbLength)
dev = bbMult * ta.stdev(close, bbLength)
bbUpper = bbBasis + dev
bbLower = bbBasis - dev

up = ta.rma(math.max(ta.change(close), 0), rsiLength)
down = ta.rma(-math.min(ta.change(close), 0), rsiLength)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiUpper = 70
rsiBasis = 50
rsiLower = 30

longSignal = rsi[1] < rsiLower and close[1] < bbLower and rsi > rsiLower and close > bbLower and rsi < rsiBasis and close < bbBasis
shortSignal = rsi[1] > rsiUpper and close[1] > bbUpper and rsi < rsiUpper and close < bbUpper and rsi > rsiBasis and close > bbBasis

longSL = bbLower - slCoef * (bbBasis - bbLower)
shortSL = bbUpper + slCoef * (bbUpper - bbBasis)

if closeBbBasis
    if (ta.crossover(close, bbBasis))
        strategy.close(id="long", comment="Close longs")
    if (ta.crossunder(close, bbBasis))
        strategy.close(id="short", comment="Close shorts")

if longSignal
    strategy.entry("long", strategy.long)
    inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
    longTP = inn + tpCoef * (inn - longSL)
    strategy.exit("exit", "long", stop=longSL, limit=longTP, comment_loss="SL long", comment_profit="TP long")

if shortSignal
    strategy.entry("short", strategy.short)
    inn = strategy.opentrades.entry_price(strategy.opentrades - 1)
    shortTp = inn - tpCoef * (shortSL - inn)
    strategy.exit("exit", "short", stop=shortSL, limit=shortTp, comment_loss="SL short", comment_profit="TP short")

Overview

A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at the upper band and 70. Stops sit beyond the band by a coefficient of band width, and take profit scales that stop distance from the entry. When enabled, an open trade also closes if price crosses the band basis. It draws on the price chart.

Details

Published by

geraked on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Oscillators & momentum, Volatility & bands, Mean reversion

Markets

Forex

Timeframes

M1-M5

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

backtesting-trading-strategiesexpert-advisorexpert-advisorsforex-botgerakedmetatraderpinescriptrabiststock-markettrading-algorithms

Settings

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

SettingTypeDefaultVariable
BB Lengthint20bbLength
BB StdDevfloat2.0bbMult
RSI Lengthint3rsiLength
SL Coeffloat1.5slCoef
TP Coeffloat1.5tpCoef
Close on BB BasisbooltruecloseBbBasis
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

330 stars

Alert BB + RSI [Alorse] is a multi-symbol mean-reversion strategy designed for cryptocurrency trading. It evaluates Bollinger Bands and the Relative Strength Index (RSI) across up to 20 user-defined ticker symbols simultaneously. The strategy enters long posit

Strategy v4 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Alert BB + RSI [Alorse]
106 stars

A multi-timeframe Dollar Cost Averaging (DCA) trading strategy that combines signals from three RSI indicators, three CCI indicators, and Bollinger Bands. It places up to five weighted scale-in safety orders to lower the average entry price. The script offers

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of 3RSI 3CCI BB 5orders DCA Strategy+

Greeffer RSI Strategy

by SammyEnigma
18 stars

Greeffer RSI Strategy is a trading strategy that generates buy and sell entries based on Bollinger Bands and RSI levels. It places stop orders at upper and lower Bollinger Bands on band crossovers, and adds positions when RSI reaches extreme levels. Users can

Strategy v4 Oscillators & momentumVolatility & bands
Updated 6 years ago · None stated GitHub repository of Greeffer RSI Strategy
13 stars

This strategy overlays the price chart to trade DAX futures during European market hours. It calculates exponential and simple moving averages, RSI, MACD, Bollinger Bands, ATR, and realized volatility to simulate VIX levels. Trades execute when session time fi

Strategy v5 Sessions & timeOscillators & momentum
Updated 8 months ago · Custom licence GitHub repository of ClaudeHedge - DAX Session Strategy

Calculates a combination of the Relative Strength Index, MACD histogram, and Bollinger Bands contraction to identify long trade entries. The strategy signals a long position on a bullish bar when the MACD histogram turns upward while negative, RSI remains belo

Strategy v4 Oscillators & momentumVolatility & bands
Updated 3 years ago · Apache-2.0 GitHub repository of RSI + MACD + BB Long Strategy (Not Good)

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