[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] help on Set Option marginrequirement AFL



PureBytes Links

Trading Reference Links




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 

<FONT face=Arial 
size=2>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<FONT 
  color=#000000>( condition, result = 7<FONT 
  color=#000000>, result = 9<FONT 
  color=#000000> ); // THIS IS 
WRONG
Correct usage is:

  result = <FONT 
  color=#0000ff>IIf( condition, <FONT 
  color=#ff00ff>7, <FONT 
  color=#ff00ff>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 1if ( 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 <FONT 
face="Courier New">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 2if ( 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 3if ( 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<FONT 
color=#800000>if ( <FONT 
color=#000000>H ><FONT 
color=#0000ff>Ref(<FONT 
color=#000000>H,-<FONT 
color=#ff00ff>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:<FONT 
color=#800000>for( i = <FONT 
color=#ff00ff>1; i < <FONT 
color=#000000>BarCount; i++ ) { 
   if<FONT 
color=#000000> ( High<FONT 
color=#000000>[ i ] > High<FONT 
color=#000000>[ i - 1 ] ) 
   {        x[ i ] = 
High[ i ]; 
   }    <FONT 
color=#800000>else    { 
       x[ i ] = <FONT 
color=#000000>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<FONT 
color=#000000>( High<FONT 
color=#000000> > Ref( 
High, -<FONT 
color=#ff00ff>1 ), <FONT 
color=#000000>High, <FONT 
color=#000000>Low );
This works because IIF operates on ARRAYS as described in the <A 
href="">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 <A 
href="">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 
Janeczkoamibroker.com
----- Original Message ----- 
From: "Anthony Faragasso" <<A 
href=""><FONT face=Arial 
size=2>ajf1111@xxxxxxxx>
To: <<A 
href=""><FONT face=Arial 
size=2>amibroker@xxxxxxxxxxxxxxx<FONT face=Arial 
size=2>>
Sent: Wednesday, August 04, 2004 2:51 
PM
Subject: Re: [amibroker] IIF 
statement
<FONT face=Arial 
size=2>> iif(x, do action A, iif(y, do action B , iif(z, do action C, do 
action D)));> > > > ------------------------ Yahoo! 
Groups Sponsor --------------------~--> > Yahoo! Domains - Claim yours 
for only $14.70> <A 
href=""><FONT face=Arial 
size=2>http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/GHeqlB/TM<FONT 
face=Arial size=2>> 
--------------------------------------------------------------------~-> 
> > Check AmiBroker web page at:> <A 
href=""><FONT face=Arial 
size=2>http://www.amibroker.com/> 
> Check group FAQ at: <A 
href=""><FONT 
face=Arial 
size=2>http://groups.yahoo.com/group/amibroker/files/groupfaq.html<FONT 
face=Arial size=2> > Yahoo! Groups Links> > <*> To 
visit your group on the web, go to:>     <A 
href=""><FONT face=Arial 
size=2>http://groups.yahoo.com/group/amibroker/<FONT face=Arial 
size=2>> > <*> To unsubscribe from this group, send an email 
to:>     <A 
href=""><FONT face=Arial 
size=2>amibroker-unsubscribe@xxxxxxxxxxxxxxx<FONT face=Arial 
size=2>> > <*> Your use of Yahoo! Groups is subject to:> 
    <FONT 
face=Arial size=2>http://docs.yahoo.com/info/terms/<FONT 
face=Arial size=2>>  > > 


Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html








Yahoo! Groups Sponsor


  ADVERTISEMENT 












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 the Yahoo! Terms of Service.