Apply twice the formula causes wrong indexing

Hi there,
I'm having this strange behavior. I'm surely doing something wrong but I can't figure out where is the mistake.

I need a function that should return several values and I need to call that function more times into the script. The original formula is quite long so I've recreated a similar issue with this code:

//	Formula 185.afl
//	Calculate the mean value of the closes that are above their MA(5)
//	the function returns the mean value and the number of the

global mean;
global nValues;

function meanBetween(bar1_ID, bar2_ID)
{
	initial = BarCount - bar1_ID; 	// initial bar index referred to the newest
	final = BarCount - bar2_ID;	// final bar index referred to the newest
	bID = 0;
	for (bID = initial; bID < final; bID ++)
	cumulated = 0;
	nValues = 0;
	mean = 0;
	MA5 = MA(C,5);
	for (bID = initial; bID < final; bID++)
	{
		if (C[bID]> MA5[bID])
		{
			cumulated = cumulated + C[bID];
			nValues ++;
		}
	}
	mean = cumulated / nValues;
}

meanBetween(10, 2);
mean1 = mean;
nVals1 = nValues;
printf("\nCALL1 -- # C above MA5:" + NumToStr(nVals1, 3.0) + "; their mean is "+ NumToStr(mean1, 8.3));

meanBetween(200, 10);
mean2 = mean;
nVals2 = nValues;
printf("\nCALL2 -- # C above MA5:" + NumToStr(nVals2, 3.0) + "; their mean is "+ NumToStr(mean2, 8.3));

THE ISSUE
if I apply that script the first time, it works correctly:
Interpretation OK

BUT the second time that I'm gonna apply it (or I scroll the chart forcing the recalculation etc...) I'm having this error:
Schermata 2022-08-05 alle 19.05.23

I've tried to initialize all the variables at the beginning of the function, so where I'm doing wrong?
Many thanks for any insight

Please read this KB article:

1 Like

Thank you Tomasz, the KB has been very useful for pointing me to the issue.
I would share here how I fixed the issue hoping this could be useful for other users.

The issue was related to a couple of concepts about how AFL works.

FIRST
as correctly pointed into the knowledge base The formula should be written so it is able to execute without errors with BarCount as small as 1 (ONE).

SECOND
AFL is highly optimized (quick AFL).
When running the script for the first time it uses all the bars, but the second time it doesn't (by default settings). This can be clearly seen probing the bar count using the printf function:
Barcount at the second iteration

How I Fixed it
First : I imposed that function can be called only if I barcount is grater than the first bar index.
Second : I forced Amibroker to process al bars (by SetBarRequired function)

Fixed Code

//	Formula 185.afl
//	Calculate the mean value of the closes that are above their MA(5)
//	the function returns the mean value and the number of the

global mean; 
global nValues;

function meanBetween(bar1_ID, bar2_ID)
{
	SetBarsRequired(-2,-2);
	a = BarCount;
	printf("\nNow the value of BARCOUNT is : " + NumToStr(a));
	if (BarCount > bar1_ID)
	{
		initial = BarCount - bar1_ID; 	// initial bar index referred to the newest
		final = BarCount - bar2_ID;		// final bar index referred to the newest
		bID = 0;
		for (bID = initial; bID < final; bID ++)
		cumulated = 0;
		nValues = 0;
		mean = 0;
		MA5 = MA(C,5);
		for (bID = initial; bID < final; bID++)
		{
		
			if (C[bID]> MA5[bID])
			{
				cumulated = cumulated + C[bID];
				nValues ++;
			}
		}
		mean = cumulated / nValues;
	}
}


meanBetween(10, 2);
mean1 = mean;
nVals1 = nValues;
printf("\nCALL1 -- # C above MA5:" + NumToStr(nVals1, 3.0) + "; their mean is "+ NumToStr(mean1, 8.3));

meanBetween(200, 10);
mean2 = mean;
nVals2 = nValues;
printf("\nCALL2 -- # C above MA5:" + NumToStr(nVals2, 3.0) + "; their mean is "+ NumToStr(mean2, 8.3));```

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