0

I'm trying to plot some data in pine script, some data failed to plot; here is the script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © subikshababu

//@version=5
indicator("Gain Loss Spread", shorttitle = "GLS")
Up=close>open
Down=close<open
int datapoints=input.int(defval = 3,title="total datapoints to check",minval = 1,maxval = 512)

meangain=Up? ta.sma((close[datapoints]+ta.change(close,datapoints)),datapoints):na
meanloss=Down?ta.sma((close[datapoints]-ta.change(close,datapoints)),datapoints):na

meanpopulation=ta.sma(close,datapoints)

gainlossspread=(meangain-meanloss)+close // to be plot

//RS=meangain/meanloss
//RSI=100-(100/1-RS)// to be plot

plot(meangain,color = color.green)
plot(meanloss,color = color.red)
plot(gainlossspread,color = color.blue,style = plot.style_circles) //not ploted?
plot(meanpopulation,color = color.aqua,linewidth = 2)
//plot(RSI) // not plotted

the script plots this

2 Answers2

0

meangain will only have a valid number if Up is true.
meanloss will only have a valid number if Down is true.

Up and Down are the opposites of each other so they cannot be true at the same time.

You calculate gainlossspread as gainlossspread=(meangain-meanloss)+close. Since either meangain or meanloss is na on a given bar, the result also becomes na. THat's why it is not plotting anything.

enter image description here

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • But the script is plotting mean gain and mean loss for every bar; i will add a snap shot of the indicator to my question; i'm confused – Babu Ramasamy Jul 04 '23 at 04:45
  • No, it does not. See my edit. I have also added a screenshot. You see a continuous line but that is because Tradingview connects two dots together. But in between, you have `na` values. Just move your mouse over the chart and observe the plot values. – vitruvius Jul 04 '23 at 06:43
  • Gratse, i changed plot style now only one is ploted – Babu Ramasamy Jul 04 '23 at 11:33
  • i used ta.valuewhen for gainlossspread calculation; i added close value as scaling – Babu Ramasamy Jul 04 '23 at 12:10
0

Here is what i did finally and after that i added rsi-that i will post as another answer

// This source code is subject to the terms of the Mozilla Public License 2.0 at 
  https://mozilla.org/MPL/2.0/
// © subikshababu

//@version=5
indicator("Gain Loss Spread", shorttitle = "GLS",overlay = true)
Up=close>open
Down=close<open
int datapoints=input.int(defval = 3,title="total datapoints to check",minval = 1,maxval = 512)

meangain=Up? ta.sma((close[datapoints]+ta.change(close,datapoints)),datapoints):na
meanloss=Down?ta.sma((close[datapoints]-ta.change(close,datapoints)),datapoints):na

meanpopulation=ta.sma(close,datapoints)
//bolinger band
meanstdev=ta.stdev(meanpopulation,datapoints,false)
hiband=meanpopulation+(meanstdev*3.619)
lowband=meanpopulation-(meanstdev*3.619)

gainlossspread=(ta.valuewhen(Up,meangain,1)-ta.valuewhen(Down,meanloss,1))+close // to be plot

plot(meangain,color = color.aqua,style = plot.style_circles,linewidth = 3)
plot(meanloss,color = color.fuchsia,style = plot.style_cross,linewidth = 3)
plot(gainlossspread,color = color.blue,linewidth = 2) //not ploted?
plot(meanpopulation,color = color.aqua)
plot(hiband,color = color.black)
plot(lowband,color = color.black)

//end
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jul 27 '23 at 00:27