PureBytes Links
Trading Reference Links
|
Tomasz,
thanks for the detailed clarification.
Werner
--- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <amibroker@xxxx>
wrote:
> Hello,
>
> Please note however that IIF is a *function* and it does not work
like
> if-else statement.
> In other words IIF function evaluates ALL parameters and does not
change
> the program flow.
> IIF evaluates all arguments and RETURNS the value that needs to be
assigned
> to some variable.
>
> Quite opposite if-else statement provides control flow and skips
> entirely parts that do not match condition.
>
> See "COMMON misunderstandings" section of
> http://www.amibroker.com/guide/a_language.html
>
> Conditional function IIF()
> The iif() function is used to create conditional assignments. It
contains three parameters as shown in the following example.
>
> dynamicrsi = IIf( Close > MA(C,10), RSI(9), RSI(14) ); The
above "iif" statement reads (in English) as follows: If today's close
is greater than today's 10-day simple moving average of the close,
then assign a 9-day RSI to the dynamicrsi variable, otherwise, assign
a 14-day RSI. The next formula assigns "positive volume" to volresult
variable if the close is greater than the median price.
Otherwise, "negative volume" is assigned.
>
> volresult = IIf( Close > (High+Low)/2, Volume, -Volume ); If you
simply want an expression to be evaluated as either true or false, it
can be done without the use of the iif() function. The following
formula will result in either a 1 (true) or a 0 (false):
>
> result = RSI(14) > 70; The same done with iif() gives the same
results, but the formula is longer.
>
> result = IIf(RSI(14) > 70, 1, 0 ); 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. IIF function is NOT a flow-control
statement. If you need flow control (conditional execution of some
code parts) you should look for if-else conditional statement
described later in this document.
>
> 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 result variable depending
on condition */
>
> if, else Statements
> if( expression )
> statement1
> [else
> statement2]
>
> 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.
>
> Example 1
>
> if ( i > 0 )
> y = x / i;
> else
> {
> x = i;
> y = abs( x );
> }In this example, the statement y = x/i; is executed if i is
greater than 0. If i is less than or equal to 0, i is assigned to x
and abs( x ) is assigned to y. Note that the statement forming the if
clause ends with a semicolon.
>
> When nesting if statements and else clauses, use braces to group
the statements and clauses into compound statements that clarify your
intent. If no braces are present, the compiler resolves ambiguities
by associating each else with the closest if that lacks an else.
>
> Example 2
>
> if ( i > 0 ) /* Without braces */
> if ( j > i )
> x = j;
> else
> x = i;The else clause is associated with the inner if
statement in this example. If i is less than or equal to 0, no value
is assigned to x.
>
> Example 3
>
> if ( i > 0 )
> { /* With braces */
> if ( j > i )
> x = j;
> }
> else
> x = i;The braces surrounding the inner if statement in this
example make the else clause part of the outer if statement. If i is
less than or equal to 0, i is assigned to x.
>
> Common misunderstandings
> "New if-else problem"
>
> Question:
>
> Why I get the syntax error when I write: if( H > Ref(H,-1) )
>
> Answer:
>
> if-else statement changes flow of execution (opposite to IIF
function that evaluates all arguments and works on arrays) and you
can not really write
>
> if ( H >Ref(H,-1) )
>
> because it has no meaning. It would translate to " If high array is
higher than high array shifted one bar" (see tutorial below). Flow
control statement (such as if-else) has to get SINGLE boolean value
to make decision which execution path should be taken. If you write H
(or High) it means ARRAY (entire array).
> if you write H[ i ] - it means i-th element of the array. The
subscript operator [ ] allows you to access individual array elements.
>
> Instead you should write:
>
> for( i = 1; i < BarCount; i++ )
> {
> if ( High[ i ] > High[ i - 1 ] )
> {
> x[ i ] = High[ i ];
> }
> else
> {
> x[ i ] = Low[ i ];
> }
> }
>
>
> this will translate to correct one "for EVERY BAR 'i' assign i-th
element of high array to the i-th element of x array if i-th element
of high array is higher than the previous element, otherwise assign i-
th of low array to the i-th element of x array". The rule is: new if-
else and while statements need single boolean value (not array) to
decide which execution path should be taken. If you want to use them
with arrays you have to iterate through bars using for loop (as shown
above).
>
> On the other hand this can be implemented in single line using old-
style array operations and IIF function:
>
> x = IIf( High > Ref( High, -1 ), High, Low );
>
> This works because IIF operates on ARRAYS as described in the
tutorial.
>
> As you can see in many cases old-style AFL provides much more
compact form. I always tried to explain this advantage of AFL but
only a few realised that. New control statements should be used where
it is better to use them. As I tried to explain during last years in
80% of cases 'old-style' AFL provides the shortest formula. Only
remaining 20% of cases needed script. Those 'script-only' cases now
can be coded in native AFL thanks to new for/while/if-else
statements. And this is correct usage of them - to replace script
parts.
>
>
>
> Best regards,
> Tomasz Janeczko
> amibroker.com
> ----- Original Message -----
> From: "Anthony Faragasso" <ajf1111@xxxx>
> To: <amibroker@xxxxxxxxxxxxxxx>
> Sent: Wednesday, August 04, 2004 2:51 PM
> Subject: Re: [amibroker] IIF statement
>
>
> > iif(x, do action A, iif(y, do action B , iif(z, do action C, do
action D)));
> >
> >
> >
> >
> > Check AmiBroker web page at:
> > http://www.amibroker.com/
> >
> > Check group FAQ at:
http://groups.yahoo.com/group/amibroker/files/groupfaq.html
> > Yahoo! Groups Links
> >
> >
> >
> >
> >
> >
------------------------ Yahoo! Groups Sponsor --------------------~-->
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/GHeqlB/TM
--------------------------------------------------------------------~->
Check AmiBroker web page at:
http://www.amibroker.com/
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|