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

[amibroker] Re: AmiBroker 4.34.0 BETA released - now with user-definable functions with arguments and local variables



PureBytes Links

Trading Reference Links

Tomasz,

This looks interesting.

A couple of questions tho ...

1.  Do the functions have to be inline in whatever code is calling 
them or can they be seperately saved some how and referenced elsewhere

2.  Can arrays be passed as input and returned or only variables ?

3.  Can one use array oriented statements inside the functions or are 
functions assumed to require looping.

Thanks in advance,

Fred

--- In amibroker@xxxxxxxxxxxxxxx, "Tomasz Janeczko" <tj@xxxx> wrote:
>  
> Hello,
> 
> A new beta version (4.34.0) of AmiBroker has just been released.
> 
> It is available for registered users only from the members area at:
> http://www.amibroker.com/members/bin/ab4340beta.exe
> 
> If you forgot your user name / password to the members area
> you can use automatic reminder service at: 
http://www.amibroker.com/login.html
> 
> The highlight of this release are user definable functions / 
procedures
> allowing passing parameters and supporting local variables.
> 
> Please read "HELP ON NEW FEATURES" section below for the details.
> The description of remaining additions is included in the CHANGE LOG
> later in this document.
> 
> Best regards,
> Tomasz Janeczko
> amibroker.com
> 
> AmiBroker 4.34.0 Beta Read Me
> April 30, 2003 13:42 
> 
> THIS IS AN EARLY BETA VERSION OF THE SOFTWARE. EXPECT BUGS !!!
> 
> AGAIN: THIS IS AN EARLY BETA VERSION OF THE SOFTWARE. EXPECT 
BUGS !!!
> 
> 
> Backup your data files and entire AmiBroker folder first!
> 
> INSTALLATION INSTRUCTIONS
> 
> IMPORTANT: This archive is update-only. You have to install full 
version 4.30 first. 
> 
> Just run the installer and follow the instructions. 
> 
> Then run AmiBroker. You should see "AmiBroker 4.34.0 beta" written 
in the About box.
> 
> HELP ON NEW FEATURES
> USER-DEFINABLE FUNCTIONS, PROCEDURES, LOCAL/GLOBAL VARIABLES (4.34 
or higher)
> 
> 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 depending 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.
> 
> 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 );
> 
> 
> 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.
> 
> 
> FLOW CONTROL AND LOOPING (4.31 or higher)
> 
> while Statement
> The while statement lets you repeat a statement until a specified 
expression becomes false. 
> 
> Syntax
> 
> while ( expression ) statement
> 
> The expression must have arithmetic (numeric/boolean) type. 
Execution proceeds as follows: 
> 
>   1.. The expression is evaluated.
> 
> 
>   2.. If expression is initially false, the body of the while 
statement is never executed, and control passes from the while 
statement to the next statement in the program. 
>   If expression is true (nonzero), the body of the statement is 
executed and the process is repeated beginning at step 1.
> 
> This is an example of the while statement: 
> 
> i = 10;while( i < 20 ){  Plot( MA( Close, i ), "MA" + WriteVal( i, 
0 ), colorBlack + i );  i = i + 1;}The example plots 10, 11, 12, 13, 
14, 15, 16, 17, 18, 19 - bar moving averages.
> 
>   
> for Statement
> 
> The for statement lets you repeat a statement or compound statement 
a specified number of times. The body of a for statement is executed 
zero or more times until an optional condition becomes false.  
> 
> Syntax
> 
> for ( init-expression ; cond-expression ; loop-expression ) 
statement 
> 
> Execution of a for statement proceeds as follows: 
> 
>   1.. The init-expression, is evaluated. This specifies the 
initialization for the loop. There is no restriction on the type of 
init-expression.
> 
> 
>   2.. The cond-expression,  is evaluated. This expression must have 
arithmetic type. It is evaluated before each iteration. Three results 
are possible: 
>     a.. If cond-expression is true (nonzero), statement is 
executed; then loop-expression, if any, is evaluated. The loop-
expression is evaluated after each iteration. There is no restriction 
on its type. Side effects will execute in order. The process then 
begins again with the evaluation of cond-expression.
> 
> 
>     b.. If cond-expression is false (0), execution of the for 
statement terminates and control passes to the next statement in the 
program. 
> This example illustrates the for statement: 
> 
>  myema[ 0 ] = Close[ 0 ];for( i = 1; i < BarCount; i++ ){   myema[ 
i ] = 0.1 * Close[ i ] + 0.9 * myema[ i - 1 ];}This example iterates 
all bars of close array to calculate exponential moving averageif, 
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. 
> 
> CHANGE LOG
> CHANGES FOR VERSION 4.34.0 (as compared to 4.33.0)
> 
>   a.. user-definable functions and procedures with parameters and 
local variables 
> 
>   b.. 'A' is NO LONGER predefined symbol. I decided to remove it 
because people tend to use A as user-variable forgetting the fact 
that it was build-in array holding typical price (H+L+C)/3. Use 'Avg' 
instead.
> 
>   c.. indicator list column width increased in Indicator Builder 
>   d.. DayOfYear - returns the calendar day number counting from 
beginning of the year January 1st is 1. Maximum number returned is 
366 
> CHANGES FOR VERSION 4.33.0 (as compared to 4.32.2)
> 
>   a.. Database purify tool implemented (available via Tools-
>Database Purify)
>   allows to detect missing/extra quotes, possible splits, invalid 
OHLC relationship
> 
>   Apply to/range settings similar to AA window. You can also right 
click over result list to add symbols to watch list and copy the list 
to the clipboard (and paste it later to any other program for futher 
use)
> 
> 
>   b.. further improvements to AFL garbage collector, now looping 
regardless of loop count requires the same amount of memory as just 
single pass of the code (no growing allocated memory during loops).
>   This enormously lowered memory consumption for some formulas and 
increased the speed of some loops 3..4 times.
> 
> 
>   c.. added variable period support to the following functions:
>   LinRegSlope,
>   LinearReg,
>   LinRegIntercept,
>   StdErr,
>   TSF 
>   Sample code:
> 
>   Plot( Close, "Test", colorBlack );
> 
>   range = 15 * MA( ATR( 15 ), 50 ) / ATR( 15 ); 
> 
>   //Plot( range, "range", colorBlue, styleOwnScale );
>   Plot( LinearReg( Close, range ), "Test", colorRed );
> 
>   d.. fixed sometimes incorrect output of variable-period version 
of LLV/HHV
> 
>   e.. fixed crash occuring when bad arguments were passed to the 
function (bug introduced in 4.32.x).
> 
> CHANGES FOR VERSION 4.32.2 (as compared to 4.32.1)
> 
>   a.. second bug in experimental garbage collector fixed. 
> CHANGES FOR VERSION 4.32.1 (as compared to 4.32.0)
> 
>   a.. garbage collector was releasing memory too soon in some 
cases, now fixed. 
> CHANGES FOR VERSION 4.32.0 (as compared to 4.31.1)
> 
>   a.. added type check in IF/ELSE statements 
>   b.. added type check in array element assignment 
>   c.. error messages now numbered and display changed slightly 
>   d.. you can break running loop by pressing Shift+BREAK (Pause) 
key combination 
>   e.. calling COM objects works again (was broken in 4.31.x) 
>   f.. changed slightly the way TAB works in editor, if TAB is 
pressed any selection is deselected to avoid accidential deletion of 
text 
>   g.. experimental: added 'agressive garbage collector' that 
extremely decreases the amount
>   of memory required to run AFL formula by releasing the memory
>   used for temporary variables as soon as possible (previously
>   temporary memory was released at the end of formula execution).
>   A side-effect of new garbage collector is some speed up in 
formula execution. 
>   h.. new tab in preferences for AFL engine settings 
>   i.. experimental feature, NOT for beginners, may be 
removed/modified in future releases: 
>   new _TRACE( "string" ) AFL function added
>   that allows to write debug messages from AFL code to system debug 
viewer.
>   (it calls internally OutputDebugString Win API function).
>   To view debug messages you have to run DebugView freeware program 
>   from http://www.sysinternals.com/ 
> CHANGES FOR VERSION 4.31.1 (as compared to 4.31.0)
> 
>   a.. fixed bug introduced in 4.31.0 causing no text output in 
commentary/interpretation 
> CHANGES FOR VERSION 4.31.0 (as compared to 4.30.0)
> 
>   a.. Workspace window uses "icon font" set in the Windows settings 
instead of hard coded Tahoma 8 
>   b.. for better readability and ClearType(tm) compatibility on 
WinXP, all dialog windows use now 'MS Shell Dlg' face name that maps 
to standard MS Sans Serif on Win 9x/Me/NT and Tahoma on Win 2K and 
XP. 
>   c.. rewritten AFL parser, now formula is parsed and coverted to 
syntax tree and then interpreted. This would allow further 
improvements including compilation. This allowed also to add loops/if-
else statements. 
>   d.. implemented IF/ELSE statement, WHILE and FOR loops:
>   The same basic 'for' loop in AFL is 2..3 times faster than in 
JScript
>   Syntax follows C++/JScript style: 
>   while( conditional_expression ) statement;
> 
>   for( initializer_part; conditional_expression; iterator_part ) 
statement;
> 
>   if( conditional_expression ) statement;
> 
>   if( conditional_expression ) 
>   statement;
>   else
>   statement;
> 
>   e.. implemented compound statements: these are blocks of 
statements enclosed
>   in opening and closing curly brace 
> 
>   {
>   statement1;
>   statement2;
>   ...
>   statementN;
>   }
> 
>   compound statement can appear anywhere when simple statement can.
> 
>   For example:
> 
>   i = 10;
>   while( i < 20 )
>   {
>   Plot( MA( Close, i ), "MA" + WriteVal( i, 0 ), colorBlack + i );
>   i = i + 1;
>   }
> 
>   f.. implemented C-style postfix and prefix increment/decrement 
operators
> 
>   i = 10;
>   WriteIf( i++ );
>   WriteIf( ++i );
>   WriteIf( i );
> 
>   g.. implemented array element access (subscript) operator []:
> 
>   WriteVal( Close[ 0 ] ); // prints the first bar of close array
> 
>   /* a sample low-level implementation of exponential moving 
average in AFL */
> 
>   myema[ 0 ] = Close[ 0 ];
> 
>   for( i = 1; i < BarCount; i++ )
>   {
>   myema[ i ] = 0.1 * Close[ i ] + 0.9 * myema[ i - 1 ];
>   }
> 
> 
> 
>   h.. added built-in constant 'BarCount' that returns number of 
bars available in arrays (the number of elements of array)
>   When QuickAFL is turned on it may be less than true number of 
bars because QuickAFL feature attempts to use only visible bars (and 
few before). You can control how many bars the formula requires using 
SetBarsRequired() function
> 
>   i.. implemented infinite-loop protection. Nice if you forgot to 
increment counter variable in 'for' loop :-)
> 
>   j.. tab key now works without need to press ALT/CTRL in AFL 
editors
> 
>   k.. added C-like synonyms for logical ADD/OR/NOT: &&, ||, ! 
> 
> 
>   /* a sample low-level implementation of Profit-target stop in 
AFL: */
> 
>   Buy = Cross( MACD(), Signal() );
> 
>   priceatbuy=0;
> 
>   for( i = 0; i < BarCount; i++ )
>   {
>        if( priceatbuy == 0 && Buy[ i ] ) 
>        priceatbuy = BuyPrice[ i ];
> 
>        if( priceatbuy > 0 && SellPrice[ i ] > 1.1 * priceatbuy )
>        {
>          Sell[ i ] = 1;
>          SellPrice[ i ] = 1.1 * priceatbuy;
>          priceatbuy = 0;
>        }
>        else
>          Sell[ i ] = 0;
>   }
> 
>   /* sample EMA rainbow */
> 
>   Plot( Close, "Price", colorBlack, styleCandle );
>   for( Range = 15; Range < 100; Range++ )
>      Plot( EMA( Close, Range ), "MA"+WriteVal(Range,0), colorRose + 
Range % 8, styleNoLabel );
> 
> 
> HOW TO REPORT BUGS
> 
> If you experience any problem with this beta version please send 
detailed description of the problem (especially the steps needed to 
reproduce it) to bugs@xxxx


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get A Free Psychic Reading!
Your Online Answer To Life's Important Questions.
http://us.click.yahoo.com/cjB9SD/od7FAA/AG3JAA/GHeqlB/TM
---------------------------------------------------------------------~->

Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/