| 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
*/
When working on arrays, Iif function evaluates all bars, condition is checked on each bar and returned value for given bar is choosen appropriately on bar by bar basis. The following code shows how array operation of Iif is implemented internally. Take a look also at Understanding AFL chapter of the manual.
function IIF_AFL(
condition, inputA, inputB )
{
result = Null;
for(
bar = 0; bar < BarCount;
bar++ )
{
if( condition[ bar
] )
result[ bar ] = inputA[ bar ];
else
result[ bar ] = inputB[ bar ];
}
return result;
} |
| EXAMPLE |
// The formula below
// 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.
result = IIf( MACD() < Signal(), Volume,
-Volume );
// The formula below
// will assign colorRed to the dynamic_color variable
on days when
// Close < Open and color Green otherwise
dynamic_color = IIf( Close < Open, colorRed, colorGreen );
Plot( Volume, "Color
volume", dynamic_color, styleHistogram | styleThick );
|