pine-scriptpine-script-v5technical-indicator

Different oscillator indicators in the same pine script but plot them separately


I wrote different oscillator indicators (such as RSI, SRSI, etc.) in the same pine script so that all can be plotted by loading a single script. However, all the indicators are being plotted together. I like to plot them separately. Is there any way to do this?

Below is a sample code. It plots them together.

indicator("RSI and SRSI", overlay=false)

// Input parameters
length = input.int(14, minval=1, title="RSI Length")
srsi_k = input.int(14, title="SRSI %K Length")
srsi_d = input.int(3, title="SRSI %D Smoothing")

// Calculate RSI
rsi = ta.rsi(close, length)

// Calculate SRSI
srsi = ta.stoch(rsi, rsi, rsi, srsi_k)
srsi_d_value = ta.sma(srsi, srsi_d)

// Plot RSI
plot(rsi, color=color.blue, title="RSI")

// Plot SRSI %K
plot(srsi, color=color.red, title="SRSI %K")

// Plot SRSI %D
plot(srsi_d_value, color=color.green, title="SRSI %D")

Solution

  • There is currently no way to plot two oscillators in different panes with a single Pine Script code.

    However, since both RSI and Stochastic RSI are bounded oscillators (i.e., they range from 0 to 100), you can change the scale of one of them to do this.

    Below, I added 100 to the Stochastic RSI and plotted a solid horizontal line using hline() to separate them. I also added over-bought and over-sold levels: enter image description here

    The code:

    // @version=5
    indicator("RSI and SRSI", overlay=false)
    
    // Input parameters
    length = input.int(14, minval=1, title="RSI Length")
    srsi_k = input.int(14, title="SRSI %K Length")
    srsi_d = input.int(3, title="SRSI %D Smoothing")
    
    // Calculate RSI
    rsi = ta.rsi(close, length)
    
    // Calculate SRSI
    srsi = ta.stoch(rsi, rsi, rsi, srsi_k)
    srsi_d_value = ta.sma(srsi, srsi_d)
    
    // Plot RSI
    plot(rsi, color=color.blue, title="RSI")
    
    // Plot RSI OB/OS Levels
    rsi_h0 = hline(70, "RSI Upper Band", color=#787B86)
    hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
    rsi_h1 = hline(30, "RSI Lower Band", color=#787B86)
    fill(rsi_h0, rsi_h1, color=color.rgb(33, 150, 243, 90), title="RSI Background")
    
    // Plot a solid separator line between the two oscillators
    hline(100, "Separator", color.white, hline.style_solid)
    
    // Plot SRSI %K
    plot(srsi+100, color=color.red, title="SRSI %K")
    
    // Plot SRSI %D
    plot(srsi_d_value+100, color=color.green, title="SRSI %D")
    
    // Plot SRSI OB/OS Levels
    srsi_h0 = hline(180, "SRSI Upper Band", color=#787B86)
    hline(150, "SRSI Middle Band", color=color.new(#787B86, 50))
    srsi_h1 = hline(120, "SRSI Lower Band", color=#787B86)
    fill(srsi_h0, srsi_h1, color=color.rgb(33, 150, 243, 90), title="SRSI Background")