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