Cast constant value from series

Viewed 561

I'm completely new to this forum and tradingview/pinescript. I am trying to script an indicator, and part of the script requires assigning a constant value to a variable from a series.

From a moving average of the volume (50day average volume), I want to set the constant variable 'reference_Vol' to the value of the moving average from yesterday's price bar only and no other days in the past before yesterday, but the variable gets overwritten after each older bar as 'volume' is a series, so effectively the variable is not constant but dynamically changes over the history of the price bars.

SMA_50d = sma(volume,50)
reference_Vol =SMA_50d[1]

I have tried setting the value as a literal (literal int(x)), messing around with the valuewhen() function, but I have just not enough scripting experience of what I am doing. I have a feeling there is an easy solution to this, and I could really use some help.

Thanks a bunch!

1 Answers

This should do what you intended

//@version=5
indicator("My Script", overlay=false)

var float   reference_Vol = na
new_day = ta.change(dayofmonth)

SMA_50d = ta.sma(volume,50)

if new_day
    reference_Vol := SMA_50d[1]

plot(reference_Vol, style=plot.style_linebr, color=new_day ? na : color.blue)
Related