Search code examples
pine-scriptpine-script-v5

Pine Script failed to plot some data


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


Solution

  • 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