Strategy · Pine Script v5 · On the chart

Bollinger Bands, RSI, and MA Strategy

by hasnocool

This strategy initiates momentum breakout positions when the Relative Strength Index reaches extreme levels and Bollinger Band standard deviation is expanding.

View on GitHub

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

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

Bollinger Bands, RSI, and MA Strategy.pine
//@version=5
strategy("Bollinger Bands, RSI, and MA Strategy", overlay=true)
// Define input variables
b_len = input(20, title="BB Length")
bb_mult = input(2.0, title="BB Standard Deviation")
bb_deviation1 = input(1.0, title="BB Deviation 1")
rsi_len = input(14, title="RSI Length")
overbought = input(70, title="Overbought RSI Level")
oversold = input(30, title="Oversold RSI Level")
ma_len = input(50, title="MA Length")
stop_loss_percent = input(1.0, title="Stop Loss Percentage")
source = input(close, title="Source")
// Calculate Bollinger Bands
bb_upper = ta.sma(source, b_len) + bb_mult * ta.stdev(source, b_len)
bb_lower = ta.sma(source, b_len) - bb_mult * ta.stdev(source, b_len)
bb_upper1 = ta.sma(source, b_len) + bb_deviation1 * ta.stdev(source, b_len)
bb_lower1 = ta.sma(source, b_len) - bb_deviation1 * ta.stdev(source, b_len)
// Calculate RSI
rsi = ta.rsi(source, rsi_len)
// Calculate Moving Average
ma = ta.sma(source, ma_len)
// Determine if Bollinger Bands are contracting
bb_contracting = ta.stdev(source, b_len) < ta.stdev(source, b_len)[1]
// Entry conditions
enterLong = rsi > overbought and not bb_contracting
enterShort = rsi < oversold and not bb_contracting
// Exit conditions
exitLong = close < ma
exitShort = close > ma
// Exit trades and generate alerts
if strategy.position_size > 0 and exitLong
    strategy.close("Long") // Exit the long trade
    alert("Long Exit", alert.freq_once_per_bar_close)
if strategy.position_size < 0 and exitShort
    strategy.close("Short") // Exit the short trade
    alert("Short Exit", alert.freq_once_per_bar_close)
// Strategy orders
if enterLong
    strategy.entry("Long", strategy.long)
if enterShort
    strategy.entry("Short", strategy.short)
if exitLong
    strategy.close("Long")
if exitShort
    strategy.close("Short")
// Plotting Bollinger Bands
plot(bb_upper, color=color.blue, title="BB Upper 2")
plot(bb_lower, color=color.blue, title="BB Lower 2")
plot(bb_upper1, color=color.red, title="BB Upper 1")
plot(bb_lower1, color=color.red, title="BB Lower 1")
// Plotting RSI
plot(rsi, color=color.orange, title="RSI")
// Plotting Moving Average
plot(ma, color=color.green, title="Moving Average")
Expand (67 lines)

Overview

Long trades open when RSI is above the overbought threshold and bands are not contracting, while short trades trigger when RSI is below oversold and bands are expanding. Exits occur when price crosses a simple moving average. Plotted on the main price chart are two sets of Bollinger Bands, the moving average, and the raw RSI oscillator. Although a stop loss parameter exists in the inputs, it is not executed in code.

Details

Published by

hasnocool on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Oscillators & momentum, Moving averages

Markets

Crypto

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
BB Lengthint20b_len
BB Standard Deviationfloat2.0bb_mult
BB Deviation 1float1.0bb_deviation1
RSI Lengthint14rsi_len
Overbought RSI Levelint70overbought
Oversold RSI Levelint30oversold
MA Lengthint50ma_len
Stop Loss Percentagefloat1.0stop_loss_percent
Sourcesourceclosesource
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

This strategy plots MACD line, signal line, Bollinger Bands, and RSI on the price chart. It signals long entries on MACD crossover above signal with RSI below 50 and price below BB basis, plus optional DMI filter. Short entries are available but disabled by de

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of MACD + BB + RSI [Alorse]

Take Profits & Stop Losses

by PythonForForex
64 stars

This educational strategy demonstrates moving average crossovers filtered by RSI momentum, incorporating ATR-based risk management. It calculates a 10-period SMA, a 30-period SMA, a 14-period RSI, and a 14-period ATR. Long entries trigger when the fast SMA cro

Strategy v5 Moving averagesOscillators & momentum
Updated 4 years ago · None stated GitHub repository of Take Profits & Stop Losses

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL

Bollinger bands examples

by tradingstrategy-ai
53 stars

This strategy plots exponential-moving-average Bollinger bands on the price chart and calculates RSI. It opens a long position when close crosses above the upper band while RSI is at or above a threshold, the date filter allows trading, and no position is open

Strategy v5 Volatility & bandsOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Bollinger bands examples

CombinedBinHAndClucV6

by viktors1982
36 stars

CombinedBinHAndClucV6 is a trading strategy based on the popular BinHV45 and ClucMay72018 algorithms. It calculates dual Bollinger Bands, Exponential Moving Averages, Relative Strength Index, Average True Range, and volume metrics across primary and one-hour h

Strategy v4 Volatility & bandsOscillators & momentum Repaints
Updated 2 years ago · None stated GitHub repository of CombinedBinHAndClucV6

BB Swing

by benso87
24 stars

This strategy calculates Bollinger Bands, a configurable moving average slope, Stochastic RSI, ATR, and ADX directly on the price chart. It evaluates trend bias and momentum conditions to generate long and short swing entries using percent-of-equity sizing. Th

Strategy v5 Volatility & bandsOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of BB Swing

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