Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 859
Brandon Saccone Avatar asked Jan 28 '20 19:01

Brandon Saccone


2 Answers

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.

like image 71
Michel_T. Avatar answered Sep 17 '22 05:09

Michel_T.


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.

like image 22
PineCoders-LucF Avatar answered Sep 21 '22 05:09

PineCoders-LucF