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
  • What is not plotting? Commented Jul 3, 2023 at 7:58
  • i commented as not plotted--gainlossspread Commented Jul 3, 2023 at 8:58

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
Gratse, i changed plot style now only one is ploted
i used ta.valuewhen for gainlossspread calculation; i added close value as scaling
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

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.