How do I stop a for loop on the last bar of the array?

Hi,
Im wriing code to detect a chart pattern (pivot high) and I want to color the bars of that make up the pattern when they occur on the chart.

I am getting one problem and that is in this line

PIVHIGH[i+1] = 1;

I get the following error :
"Array subscript out of range....You atttempted to access non-existing 200-th element of array"

I understand why Im getting the error. I don't know how to code it so that it will stop at the last bar in the array?

My entire code is below

As always, thanks

_SECTION_BEGIN("PIVOT HIGH");
//CS high is higher than the CS before and after it.
//CS low is higher than the CS before and after it.
//Color the pivot high candles with a solid color

BI = BarIndex();
PIVHIGHCOL = Null;

PIVHIGH = IIf((H > Ref(H,-1) AND H > Ref(H,1) AND L >Ref(L,-1) AND L > Ref(L,1)),1,0);

for(i = 0; i < BarCount; i++)
{
	if(PIVHIGH[i] ==1)
	{
	PIVHIGH[i-1] = 1;
	PIVHIGH[i+1] = 1;
	}
}

PIVHIGHCOL = iif(PIVHIGH ==1,colorGreen,Null);

Plot(Close,_DEFAULT_NAME(),PIVHIGHCOL,styleCandle);
_SECTION_BEGIN("PIVOT HIGH");
//CS high is higher than the CS before and after it.
//CS low is higher than the CS before and after it.
//Color the pivot high candles with a solid color

PIVHIGH = H > Ref(H,-1) AND H > Ref(H,1) AND L >Ref(L,-1) AND L > Ref(L,1);
PreviousPH = Ref(PIVHIGH, 1);
AfterPH = Ref(PIVHIGH, -1);

PIVHIGHCOL = iif(PIVHIGH  or PreviousPH or AfterPH, colorGreen, colorDefault);

Plot(Close,_DEFAULT_NAME(),PIVHIGHCOL,styleCandle);
2 Likes

How do I stop a for loop on the last bar of the array?

for(i = 0; i < BarCount - 1; i++)
1 Like

Thank you Sir. It worked. So simple! :thinking:

I've spent alot of time learning what AFL can do, now I need to learn how to translate ideas into code language.

Cheers

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