@shajan, glad to see you got your "verified badge".
As a New Forum Member, I am making the assumption that you are NEW to AB/AFL. Nothing here meant to offend.
Now looking at your code... in general "LOOPS" are bad (Not really, but generally not needed in AFL).
Let's start with a couple of Hints...
Hint 1:
Think of coding the AFL like you would in a Spreadsheet. Assume you can only look above the current cell, and to the left of the current cell (and above and too the left).
For Example. Say you are at Cell E5. You can reference any of the rows 1-5, and any Column A-E.
So if Row 1-4 contains the OHLC values, you can make row 5 contain the Average of all of them by calcuating. In Excel E5 = (A5+B5+C5+D5)/4. In AFL
myAvg = (O+H+L+C)/4; //
The AFL is an array calculation that does the work for ALL elements in the row.
Hope this helps.
Hint 2:
Using an Exploration is a great way to see what data values you are actually working with.
You will probably have to do some reading on this one...
Now on to your issue....
Let's start with a down close. Do you mean Down from Open or Down from Yesterday. You can easily change the code to what ever definition you want to use. But here is my starting point...
// 3 Consecutive red close
isdownclose = C<Ref(C,-1);
numdownclosein3 = Sum(isdownclose, 3);
Filter = 1;
AddColumn(isdownclose, "Down Close");
AddColumn(numdownclosein3, "# Down");
This code will create a (very) basic Exploration, where you can see the results.
Next step is ... learning about BarsSince.
As you can see I want you to do the work to learn. I hated it when I just wanted an answer, but I found that I learned more, and retained it better, since I did the work... Hope it is the same for you.