Welcome Features News Download Registration Support FAQ Wish list Links
Advanced stock charting and analysis program

A-Z Index | Category Index |

IIF
- immediate IF function

Trading system toolbox


SYNTAX iif( EXPRESSION, TRUE_PART, FALSE_PART )
RETURNS ARRAY
FUNCTION "Immediate-IF" - a conditional function that returns the value of the second parameter (TRUE_PART) if the conditional expression defined by the first parameter (EXPRESSION) is true; otherwise, the value of third parameter is returned (FALSE_PART). Please note that IIF is a function - so the result of evaluation is returned by that function and should be assigned to some variable. IIf always evaluates both TRUE_PART and FALSE_PART, even though it returns only one of them. Because of this, you should watch for undesirable side effects. The following example shows one common error made with IIF function: IIF( condition, result = 7, result = 9 ); // THIS IS WRONG Correct usage is: result = IIF( condition, 7, 9 ); /* 7 or 9 is *returned* and assigned to a variable depending on condition */
EXAMPLE The formula result = iif( macd()<signal(), Volume, -Volume) will assign positive volume values to the result variable on days when macd was below its signal line, and negative volume values on theother days.    
SEE ALSO More details are given in AFL reference manual (earlier in this chapter)

Comments:

Tomasz Janeczko
tj --at-- amibroker.com
2003-06-16 03:04:48
IIF can be re-implemented using new if-else flow control statements. The code below shows this and explains what IIF in fact does internally.

function _IIF( ConditionArray, TrueArray, FalseArray )
{
for( i = 0; i < BarCount; i++ )
{
if( ConditionArray[ i ] )
{
result[ i ] = TrueArray[ i ];
}
else
{
result[ i ] = FalseArray[ i ];
}
}
}
Tomasz Janeczko
tj --at-- amibroker.com
2003-07-28 09:24:10
If you want to operate on STRINGS use WriteIF function:

result = WriteIF( condition, "Text 1", "Text 2" );

(note that this function returns single string, depending on 'selected value' of condition).

References:

The IIF function is used in the following formulas in AFL on-line library:


Unreviewed user comments:

Rolly
rollyzhang [at] hotmail.com
2007-09-18 03:29:28
By definition, the return is an array of either the TRUE_PART or the FALSE_PART. However, in many examples, the two parts can be mix of array and/or constant such as 1, 2, 3, ... or V, C, ...

It is confusing. The sample _IIF() doesn't show the true logic either.

Please explain.
Tomasz Janeczko
tj at amibroker dot com
2007-11-12 14:04:11
It does not matter if part is array or constant as AmiBroker automatically converts it (upsizes constant to array when necessary). And the sample _IIF shows exactly how it works.

 


About | Privacy | Terms of Use | Contact information
Copyright © 2003 AMIBROKER.COM