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

RE: [amibroker] Re: Watchlist sorting



PureBytes Links

Trading Reference Links

>From the Help File Amibroker 4.60\Amibroker Formula Language\AFL Reference
Manual:

 
USER-DEFINABLE FUNCTIONS, PROCEDURES, LOCAL/GLOBAL VARIABLES 

Here is a sample code showing user-defined function:

// the following function is 2nd order smoother 

function IIR2( input, f0, f1, f2 ) 
{ 
    result[ 0 ] = input[ 0 ]; 
    result[ 1 ] = input[ 1 ]; 

    for( i = 2; i < BarCount; i++ ) 
    { 
       result[ i ] = f0 * input[ i ] + 
                     f1 * result[ i - 1 ] + 
                     f2 * result[ i - 2 ]; 
    } 

   return result; 
} 

Plot( Close, "Price", colorBlack, styleCandle ); 
Plot( IIR2( Close, 0.2, 1.4, -0.6 ), "function example", colorRed ); 


In this code IIR2 is a user-defined function. input, f0, f1, f2 are formal
parameters of the functions.
At the time of function call the values of arguments are passed in these
variables. Formal parameters behave like local variables.
Later we have result and i which are local variables. Local variables are
visible inside function only. If any other function uses the same variable
name they won't interfere between each other.

Due to the fact that AFL does not require to declare variables the decision
whenever given variable is treated as local or global is taken depends on
where it is FIRST USED.

If given identifier appears first INSIDE function definition - then it is
treated as LOCAL variable.
If given identifier appears first OUTSIDE function definition - then it is
treated as GLOBAL variable.

This default behaviour can be however overriden using global and local
keywords (introduced in 4.36) - see example 2.

Example (commentary):

k = 4; // this is GLOBAL variable 

function f( x ) 
{ 
   z = 3; // this is LOCAL variable 
   return z * x * k; // 'k' here references global variable k (first used
above outside function) 
} 

z = 5; // this is GLOBAL variable with the same name as local variable in
function f 

"The value of z before function call :" + WriteVal( z ); 

// Now even if we call function 
// the value of our global variable z 
// is not affected by function call because 
// global variable z and local variable z are separate and 
// arguments are passed by value (not by reference) 

"The result of f( z ) = " + WriteVal( f( z ) ); 

"The value of z after function call is unchanged : " + WriteVal( z );

Example 2: Using local and global keywords to override default visibility
rules:

VariableA = 5; // implict global variable 

function Test() 
{ 
   local VariableA;  // explicit local variable with the same identifier as
global 
   global VariableB; // explicit global variable not defined earlier 
                     // may be used to return more than one value from the
function 

   VariableA = 99; 
   VariableB = 333; 
} 

VariableB = 1; // global variable 

"Before function call"; 
"VariableA = " + VariableA; 
"VariableB = " + VariableB; 

Test(); 

"After function call"; 
"VariableA = " + VariableA + " (not affected by function call )"; 
"VariableB = " + VariableB + " (affected by the function call )"

At the end of the function we can see 'return' statement that is used to
return the result to the caller. Note that currently return statement must
be placed at the very end of the function.

It is also possible to write a procedure (a function that returns nothing
(void))

procedure SinePlotter( Freq, ColorIndex ) 
{ 
   pname = "Line"+WriteVal(ColorIndex,1.0); 
   array = sin( Cum( Freq * 0.01 ) ); 
   Plot( array, pname , colorRed + ColorIndex, styleThick ); 
} 

for( n = 1; n < 10; n++ ) 
{ 
    SinePlotter( n/2+Cum(0.01), n ); 
}

Note that although there are two separate keywords 'function' and
'procedure' AmiBroker currently treats them the same (they both accept
return values but not require them), but in the future the rules maight get
enforced to use
return statement ONLY in conjunction with function keyword. So it is advised
to use function keyword in case when your function returns any value and
procedure keyword otherwise.

Note also that recursion (having a function call itself from within itself)
is NOT supported as for now.



More information


Please read also Understanding
<mk:@MSITStore:C:\Program%20Files\Amibroker\Broker.chm::/h_understandafl.htm
l> how AFL works article to learn more. 

 

 



> -----Original Message-----
> From: Ken Close [mailto:closeks@xxxxxxxx]
> Sent: Saturday, August 14, 2004 5:31 PM
> To: AmiBroker List
> Subject: [amibroker] Function Help
>
> This seems embarrassing to ask, but where can one find help
> on writing custom functions (syntax, rules, etc).  Seems such
> a basic question but finding it in the help search is
> impossible because "function" is used everywhere for almost
> everything.  "Custom function", "user function", "user
> written function" --- none of these help.  Scanning the TAC
> doesn't help.  So, come here and hope someone will point the
> blind in the right direction.
>
> Thanks,
>
> Ken
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.735 / Virus Database: 489 - Release Date: 8/6/2004
> 
>
>
>
> ------------------------ Yahoo! Groups Sponsor
> --------------------~--> Make a clean sweep of pop-up ads.
> Yahoo! Companion Toolbar.
> Now with Pop-Up Blocker. Get it for free!
> http://us.click.yahoo.com/L5YrjA/eSIIAA/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
>
>
>
> 
> 



[Non-text portions of this message have been removed]



------------------------ 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/