The if keyword executes statement1 if expression is true (nonzero); if else is present and expression is false (zero), it executes statement2. After executing statement1 or statement2, control passes to the next statement. Expression must be boolean ( True/False) type (so it CANNOT be ARRAY because there would be no way do decide whether to execute statement1 or not, if for example array was: [True,True,False,.....,False,True] )
if( expression )
statement1;
else
statement2;
EXAMPLE
if( Close > Open ) //
WRONG
Color = colorGreen; //statement
1
else
Color = colorRed; //statement
2
Plot(Close,"Colored
Price",Color,styleCandle);
The above example is wrong, as both Open and Close are arrays and such expression as Close > Open is also an ARRAY. The solution depends on the statement. It’s either possible to implement it on bar-by-bar basis, with use of FOR loop:
for(
i = 0; i < BarCount;
i++ )
{
if( Close[
i ] > Open[
i ] ) // CORRECT
Color[ i ] = colorGreen;
else
Color[ i ] = colorRed;
}
Plot( Close, "Colored
Price", Color, styleCandle );
It is also possible in this case to use IIf( ) function:
Color = IIf( Close > Open, colorGreen, colorRed ); //
ALSO CORRECT - working directly on arrays
Plot( Close, "Colored
Price", Color, styleCandle );