I had to modify your code slightly to get it to compile, but it is not plotting anything. The value says NA.
Code:
input Per = 14;
def barnum = BarNumber();
script MyFunc {
input period = 1;
plot myFunc = (fold n = 0 to period with s=0 do s + getValue(Close, n, 0));
}
def ThisBarInPeriod = highestall(barnum) - barnum;
def MyVar = if barnum > (highestall(barnum) - Per)
then
MyFunc(period = ThisBarInPeriod)
else
double.nan;
plot MyPlot = MyVar;
I'm not certain your example is going to do what I need, so I am going to try and further clarify. Assume the chart is loading the data, so our calculations will start on bar #1 (with a period of 3). When bar #1 closes:
Code:
plot myPlot = Sum(Close, 1);
When bar #2 closes, we need to set a value for the plot on bar #2 and also update the plot value on bar #1:
Code:
plot myPlot = Sum(Close, 1);
myPlot[1] = Sum(Close, 2);
When bar #3 closes, we need to set a value for the plot on bar #3 and also update the plot values on bars #1 and #2:
Code:
plot myPlot = Sum(Close, 1);
myPlot[1] = Sum(Close, 2);
myPlot[2] = Sum(Close, 3);
Because we are using a period of 3, the value of myPlot on bar #1 will not be modified again after bar #3 closes.