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

17,719

Solution 1

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.

Solution 2

It cannot be done. A script runs either in overlay=true mode on the chart, in which case it cannot direct plots elsewhere, or in a separate pane when overlay=false (the default).

When the script is running in a pane, it can change the color of the chart bars using barcolor(), but that's the only way it can modify the chart.

It is possible to rescale signals so that multiple bounded (e.g., 0-100, -1 to +1) signals generated by one script appear one on top of the other, but this is typically impossible in overlay mode, as the vertical scale varies with the bars on the chart. The only way for an overlay script to work with its own scale is when it uses No scale, but this prevents the indicator's plots to plot relative to price, and so the chart's bars.

Solution 3

Nice workaround from Michael. Unfortunately, this only seems to work to pass data for one plot. I would like to pass data for 3 different plots to the stock price graph. If I try this, for 'input.source' I can only select the standard sources: "open, high, low, close ...". I can not select the data from other indicators. If I remove plots 2 and 3, it works as Michael described. Anybody has a workaround for the workaround..? ;-)

Share:
17,719
Brandon Saccone
Author by

Brandon Saccone

Updated on July 30, 2022

Comments

  • Brandon Saccone
    Brandon Saccone almost 2 years

    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.