Code not plotting desired outcome

This is my code for counting up and down bars. It works in TradingView but not in AB, it just plots zero for both. Can someone please check it and let me know what mistake I have made. It's probably something simple in the conversion from Pine to AFl but I can't see it. Thanks for any help.

_SECTION_BEGIN("BarCount");
UpBarCount   = 0;
DnBarCount   = 0;
InBarH = -99999;
InBarL = 99999;
for (i = 1; i<BarCount-1; i++)
   if ((High[i] <= High[i+1] AND Low[i] >= Low[i+1]) OR (High[i] <= InBarH AND Low[i] >= InBarL))
//inside bar
        {
         InBarH = Max(High[i],InBarH);
         InBarL = Min(Low[i],InBarL);
        }     
    if (InBarH >-99999 OR InBarL < 99999)
        if (High[i] > InBarH AND Low[i] < InBarL)//Outside bar
           {
            UpBarCount  += 1;
            DnBarCount  += 1;
            InBarH = High[i];
            InBarL = Low[i];            }
        else if (High[i] > InBarH AND Low[i] >= InBarL)//Up bar
           {
            UpBarCount  += 1;
            DnBarCount  = 0;
            InBarH = High[i];
            InBarL = Low[i];
           }
        else if (Low[i] < InBarL AND High[i] <= InBarH)//Down bar
           {
            UpBarCount  = 0;
            DnBarCount  += 1;
            InBarH = High[i];
            InBarL = Low[i];
           }

Plot(UpBarCount, "UpBarCount", colorGreen, styleLine);
Plot(DnBarCount,   "DnBarCount",   colorRed, styleLine);

_SECTION_END();
UpBarCount   = 0;
DnBarCount   = 0;
aUpBarCount   = 0;
aDnBarCount   = 0;
InBarH = 0;
InBarL = 0;
for (i = 1; i<BarCount-1; i++) {
   if ((High[i] <= High[i+1] AND Low[i] >= Low[i+1]) OR (High[i] <= InBarH AND Low[i] >= InBarL))
//inside bar
	{
	 InBarH = Max(High[i],InBarH);
	 InBarL = Min(Low[i],InBarL);
	}     
	if (InBarH > 0 AND High[i] > InBarH AND Low[i] < InBarL) //Outside bar
	{
		UpBarCount  += 1;
		DnBarCount  += 1;
		InBarH = High[i];
		InBarL = Low[i];            
	}
	else if (InBarH > 0 AND High[i] > InBarH AND Low[i] >= InBarL)//Up bar
	{
		UpBarCount  += 1;
		DnBarCount  = 0;
		InBarH = High[i];
		InBarL = Low[i];
	}
	else if (Low[i] < InBarL AND High[i] <= InBarH)//Down bar
	{
		UpBarCount  = 0;
		DnBarCount  += 1;
		InBarH = High[i];
		InBarL = Low[i];
	}

	aUpBarCount[i] = UpBarCount;
	aDnBarCount[i] = DnBarCount;
}
Plot(aUpBarCount, "UpBarCount", colorGreen, styleLine);
Plot(aDnBarCount,   "DnBarCount",   colorRed, styleLine);

2 Likes

@awilson I see I had no curly braces in the for loop and the count has to be put into an array. It is still not registering inside and outside bars properly, but you have given me enough to work with.
Thank you.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.