Strategy · Pine Script v5 · On the chart

Boll MACD RSI Strategy

by zhuzp98· code credited to rhapsodyz

A strategy template that sets up Bollinger Bands, MACD and an RSI with its own Bollinger band, each with adjustable lengths, and uses a share of equity for sizing.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code116
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago17 Jan 2025

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

116 lines of Pine Script v5 from PineScript/Stg_Boll_MACD_SMI_RSI.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

Stg_Boll_MACD_SMI_RSI.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rhapsodyz

//@version=5
strategy(title = "Strategy", 
     overlay = true, 
     pyramiding = 0, 
     initial_capital = 100000,
     default_qty_type= strategy.percent_of_equity, 
     default_qty_value = 100, 
     commission_type = strategy.commission.percent, 
     commission_value = 0.04, 
     slippage = 2)

// #######################
// === General Numbers ===
// #######################

// === Equity Usage ===
// 只使用90%的账面资金交易,保留10%作为紧急流动性来源
equityUsage = input(0.9, title="Equity Usage Percentage")
_qty = (strategy.equity * equityUsage) / close

// === Input A Bollinger Bands ===
// 布林带使用21周期,默认使用2倍stdDev
bollLength      = input.int(21, minval=1, title = "Boll Length",group="Boll Settings")
bollMult        = input.float(2.0, minval=0.001, maxval=50, title="Boll StdDev", group="Boll Settings")

// === Input B MACD ===
// MACD使用经典数值12/26/9
fastLength      = input.int(12, title="Fast Length", group="MACD Settings")
slowLength      = input.int(26, title="Slow Length", group="MACD Settings")
signalLength    = input.int(9, title="Signal Length", group="MACD Settings")

// === Input C RSI ===
// RSI使用默认14周期,使用布林带作为移动均线及附加指标
rsiLength       = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSource       = input.source(hlc3, "RSI Source", group="RSI Settings")
rsiMALength     = input.int(14, title="RSI MA Length", group="RSI Settings")
rsiBollMult     = input.float(2.0, minval=0.001, maxval=50, title="RSI BB StdDev", group="RSI Settings")

// === Input D TD ===

// ##################
// === Indicators ===
// ##################

// === Indicator A Bollinger Bands ===
bollBasis = ta.ema(hlc3, bollLength)
bollDev = bollMult * ta.stdev(hlc3, bollLength)
bollUpper = bollBasis + bollDev
bollLower = bollBasis - bollDev

// === Indicator B MACD ===
[macdCore, macdSignal, macdHist] = ta.macd(hlc3, fastLength, slowLength, signalLength)
macdHistDiff = macdHist - macdHist[1]

// === Indicator C RSI ===
up = ta.rma(math.max(ta.change(rsiSource), 0), rsiLength)
down = ta.rma(-math.min(ta.change(rsiSource), 0), rsiLength)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ta.sma(hlc3, rsiMALength)

rsiBollUpper = rsiMA + ta.stdev(rsi, rsiMALength) * rsiBollMult
rsiBollLower = rsiMA - ta.stdev(rsi, rsiMALength) * rsiBollMult

// ##################
// === Conditions ===
// ##################

// === Condition A Bollinger Bands ===


// === Condition B MACD ===


// === Condition C RSI ===
rsiLong = RSI < 50 and RSI < rsiMA

// #######################
// === Risk Management ===
// #######################

// === Stop-loss ===
stopLoss   = input(3, "Stop Loss")
var float stopLossPrice = na

// === Take-profit ===
takeProfit = input(30, "Take Profit")
var float takeProfitPrice = na

// #######################
// === Order Execution ===
// #######################

// === Execute long entry ===
if enterLong
    stopLossPrice   := close * (1 - stopLoss/100)
    takeProfitPrice := close * (1 + takeProfit/100)
    strategy.entry("Long", strategy.long, qty = _qty)
    strategy.exit("exit", "Long", stop = stopLossPrice, limit = takeProfitPrice)

// === Execute long exit ===
if exitLong
    strategy.close("Long", qty_percent=100)

// === Execute short entry ===
if enterShort
    stopLossPrice   := close * (1 + stopLoss/100)
    takeProfitPrice := close * (1 - takeProfit/100)
    strategy.entry("Short", strategy.short, qty = _qty)
    strategy.exit("Exit", "Short", stop = stopLossPrice, limit = takeProfitPrice)

// === Execute short exit ===
if exitShort
    strategy.close("Short", qty_percent = 100)

Overview

Orders carry a percentage stop loss and take profit, defaulting to 3 and 30. The condition section is mostly empty and the entry and exit signals are not fully defined in the visible code, so its trading rules are incomplete as written.

Details

Published by

zhuzp98 on GitHub

Code credit

rhapsodyz, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Oscillators & momentum, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

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.

Settings

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

SettingTypeDefaultVariable
Equity Usage Percentagefloat0.9equityUsage
Boll Lengthint21bollLength
Boll StdDevfloat2.0bollMult
Fast Lengthint12fastLength
Slow Lengthint26slowLength
Signal Lengthint9signalLength
RSI Lengthint14rsiLength
RSI Sourcesourcehlc3rsiSource
RSI MA Lengthint14rsiMALength
RSI BB StdDevfloat2.0rsiBollMult
Stop Lossint3stopLoss
Take Profitint30takeProfit
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

This strategy identifies trend-following entries using a triple exponential moving average alignment and ATR-based exits on the main price chart. Long signals trigger when the fast EMA is above the medium EMA and medium is above slow, accompanied by a close ab

Strategy v4 Moving averagesVolatility & bands Repaints
Updated 2 years ago · GPL-3.0 GitHub repository of 3 EMA + Stochastic RSI + ATR
105 stars

This overlay strategy looks for gold scalps from break of structure and change of character. It enters long when a bullish break, a bullish character change, and an optional volume filter align, and short on the opposite set. Optional RSI levels can block trad

Strategy v6 SMC / ICT / price actionOscillators & momentum Repaints
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. 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

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy

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

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