Australian (ASX) Stock Market Forum

AFL help - user defined functions

Joined
20 January 2010
Posts
87
Reactions
5
Hi dunno where to post this stuff, but as im reading through Howard Bandy's "Introduction to AmiBroker" I sometimes come across stuff which im confused on. Could someone pls annotate wats goin on each line? (hope this is okay). Especially dont get whats happening with the result, input and i.

From the section on user defined functions:

function IIR2( input, f0, f1, f2)
{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];

for( i = 2; i <BarCount; i++ )
{
result[ i ] = f0 * input[ 1 ] +
f1 * result[ i - 1] +
f2 * result[ i - 2];
}
return result;
}

Plot( Close, "Price", colorBlack, styleCandle );
Plot( IIR2( Close, 0.2, 1.4, -0.6 ), "function example", colorRed );

Cheers!
 
Hi Goponcho --

This is an example of the definition of a user-defined function, then its use in an AFL program.

This part is the function definition
-------------------
function IIR2( input, f0, f1, f2)
{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];

for( i = 2; i <BarCount; i++ )
{
result[ i ] = f0 * input[ 1 ] +
f1 * result[ i - 1] +
f2 * result[ i - 2];
}
return result;
}
---------------

And this part is its use in the program
--------------
IIR2( Close, 0.2, 1.4, -0.6 )
--------------

IIR2 is an "infinite impulse response filter" (hence its name "IIR"). It is similar to an exponential moving average. If you run the program and plot the result, you will see the red line following closely to the closing data.

The code within the function is using "looping" to step through all the bars in the data in order to compute the value of the IIR2 function for each bar.

Input, f0, f1, and f2 are known as formal parameters to the function . They are place-holders for the actual arguments that are passed to the function at the time it is invoked. In this example, the arguments are Close, 0.2, 1.4, and -0.6.

i is known as an index variable to a for loop. It is used to specify which element of the array is being referenced. As the loop is processed, i takes on values that reference all of the data -- from oldest to most recent. The number of elements in the array is BarCount. The [] operator references individual elements in the array.

Result is an array that was used within the function as a local variable to hold the result of the calculation while it was being computed. The statement
Return Result;
specifies that the array Result should be passed back from the function to the calling program and associated with the name IIR2.

--------------------------

The code you posted has a mistake in it. Refer back to the text. The line:
result[ i ] = f0 * input[ 1 ] +
should be:
result[ i ] = f0 * input[ i ] +

----------------------

This example illustrates how looping code is used when it is required. But most of the applications written in AmiBroker's AFL can be accomplished using the builtin functions (such as MA, HHV, and so forth), and do not require looping.

I hope this helps,
Howard
 
Hi Howard,

Thanks for the extensive response. Im piecing it together slowly but not sure of one more thing.

What does this line mean and why is it repeated?

result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];
 
Hi Goponcho --

The [] operator specifies which element of the array that is being referenced. Since the number inside the [] (the index to the array) is different in the two statements, the two statements are different -- not repeated.

--------------------
Result[0] = Input[0];
This line stores the value that is in the zero-th position of array Input into the zero-th position of array Result.

Result[1] = Input[1];
This line stores the value that is in the one-th position of array Input and stores it in the one-th position of array Result.

----------------------

The algorithm that defines the iir function uses the current value and two previous values. The two statements above provide initialization to get the routine started.

In AmiBroker's AFL, as in the C++ language and many others, arrays are "zero based". This means that the index of the first element of all arrays is zero. A 10 element array will have index values that go from 0 through 9. This is why you will see code that processes all the data in a price series (which is always BarCount in length) such as the Close, be of the form:

for (i = 0; i < BarCount; i++)
{
........
xxx = Close;
}

I hope this helps.

Thanks,
Howard
 
Top