# Count past closes below today's close

I would like to count the number of closes below today’s close using a variable look back window. I have coded what I want (below) but it is hard coded to a 7 bar look back. I would like to re-code it to be more flexible (variable look back) but can’t figure out how to do it. Does anyone have a solution?

``````c1 = Ref( C, -1 ) < C;
c2 = Ref( C, -2 ) < C;
c3 = Ref( C, -3 ) < C;
c4 = Ref( C, -4 ) < C;
c5 = Ref( C, -5 ) < C;
c6 = Ref( C, -6 ) < C;
c7 = Ref( C, -7 ) < C;

sumBarsLo = c1 + c2 + c3 + c4 + c5 + c6 + c7;
Plot( sumBarsLo, "Lo", colorRed, styleLine );``````
1 Like

One solution.

``````num = 7;
sumBarsLo = 0;
for( i = 1; i <= num; i++ )
sumBarsLo += Ref( C, -i ) < C;

Plot( sumBarsLo, "Lo", colorRed, styleLine );
``````
5 Likes

@rb250660 this is just a minor variation on fxshrat’s solution. You appear to want to chart this so “parameterize” the first line and you can slide the lookback period and watch your graph change as you change the period.

``````num = Param("num", 7, 2, 50, 1);
sumBarsLo = 0;
for( i = 1; i <= num; i++ )
sumBarsLo += Ref( C, -i ) < C;

Plot( sumBarsLo, "Lo", colorRed, styleLine );
``````
3 Likes

@fxshrat, @portfoliobuilder, thank you very much for your answers. This is doing exactly what I want now. Much appreciated.