pine-scriptpine-script-v5rsi

How can I combine two indicators into one with Bollinger Band and RSI?


I'm trying to combine two indicators - Bollinger Bands with RSI in version5.

When I merge these two, the RSI didn't show in the bottom of the chart. How can I fix this?

I want BB on the chart candles as default and RSI on the bottom of the chart like as default.

I'm not a programmer. Know very little about coding, trying to learn though! Used ChatGPT also didn't work.

Here is the before and after merging the scripts screenshot.

Here is the code.

//@version=5
indicator(shorttitle="BB", title="Bollinger Bands", overlay=true, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
maType = "SMA"
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")

ma(source, length, _type) =>
    switch _type
        "SMA" => ta.sma(source, length)

basis = ma(close, length, maType)
dev = mult * ta.stdev(close, length)
upper = basis + dev
lower = basis - dev
plot(basis, "Basis", color=#2962FF, offset = 0)
p1 = plot(upper, "Upper", color=#F23645, offset = 0)
p2 = plot(lower, "Lower", color=#089981, offset = 0)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

/// RSI SETTINGS

// Inputs
rsiLengthInput = input.int(13, minval=1, title="Length")
rsiSourceInput = close
maLengthInput = 14
bbMultInput = 2.0

// RSI Calculation
up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maType)
isBB = maType == "Bollinger Bands"

// Plotting RSI
rsiPlot = plot(rsi, title='RSI', color=color.new(#7E57C2, 0))

// Adding horizontal lines and fill
rsiUpperBand = hline(70, "Upper Band", color=#787B86)
midline = hline(50, "Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Fill")


Solution

  • To show RSI, indicator must be overlay false

    Then, to show Bollinger Bands, plot must be overlay_true

    //@version=5
    indicator(shorttitle="BB", title="Bollinger Bands", overlay=false, timeframe="", timeframe_gaps=true)
    length = input.int(20, minval=1)
    maType = "SMA"
    mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
    
    ma(source, length, _type) =>
        switch _type
            "SMA" => ta.sma(source, length)
    
    basis = ma(close, length, maType)
    dev = mult * ta.stdev(close, length)
    upper = basis + dev
    lower = basis - dev
    plot(basis, "Basis", color=#2962FF, offset = 0, force_overlay  = true)
    p1 = plot(upper, "Upper", color=#F23645, offset = 0, force_overlay  = true)
    p2 = plot(lower, "Lower", color=#089981, offset = 0, force_overlay  = true)
    fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
    
    /// RSI SETTINGS
    
    // Inputs
    rsiLengthInput = input.int(13, minval=1, title="Length")
    rsiSourceInput = close
    maLengthInput = 14
    bbMultInput = 2.0
    
    // RSI Calculation
    up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
    down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
    rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
    rsiMA = ma(rsi, maLengthInput, maType)
    isBB = maType == "Bollinger Bands"
    
    // Plotting RSI
    rsiPlot = plot(rsi, title='RSI', color=color.new(#7E57C2, 0))
    
    // Adding horizontal lines and fill
    rsiUpperBand = hline(70, "Upper Band", color=#787B86)
    midline = hline(50, "Middle Band", color=color.new(#787B86, 50))
    rsiLowerBand = hline(30, "Lower Band", color=#787B86)
    fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Fill")
    

    rsi+bb