Pine script with two indicators one overlaid on the chart and another on its own

indicatormoving-averagepine-scriptstochastic

I am trying to write a pine script with two indicators one overlaid on the chart (EMA) and another on its own?(Stoch) I cannot seem to find any info on how to separate these (Visually) but keep them within 1 pine script, ie to be able to take trading decisions based on these.

Best Answer

The earlier answer from Luc is right, unfortunately. Each script can either create plots that are overlaid on the default price chart, or shown in a different pane, but not both. But there is a workaround.

Suppose you've made some non-trivial calculation in your script and you'd like to put it in different pane. E.g. the next code:

//@version=4
study(title="Stochastic", shorttitle="Stoch", format=format.price, precision=2)
periodK = input(14, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(3, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
plot(k, title="%K", color=color.blue)
plot(d, title="%D", color=color.orange)
h0 = hline(80)
h1 = hline(20)
fill(h0, h1, color=color.purple, transp=75)

// This next plot would work best in a separate pane
someNonTrivialCalculatedSeries = close
plot(ema(someNonTrivialCalculatedSeries, 25), title="Exporting Plot")

Because they have different scale, one of them most likely will break another indicator's scale. So you'd like show Stoch in different pine, whereas ema() should be overlayed with the main chart. For that you should make the next steps:

  1. Turn off in the study's the extra plot to return scale to normal: enter image description here

  2. Apply to the chart the next script:

    //@version=4
    study("NonOverlayIndicator", overlay=true)
    src = input(defval=close, type=input.source)
    plot(src)
    
  3. Choose in the second's script inputs source required plot from the first script: enter image description here

And voilĂ  - you got the plots in different pines: enter image description here

But if you want split the plots because you have retrictions on amount of studies you allowed to apply (e.g. 3 for free-account) - that won't help you.

Related Topic