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

[amibroker] AmiBroker Formula Language - Part 2

  • To: "AmiBroker Mailing List" <amibroker@xxxx>
  • Subject: [amibroker] AmiBroker Formula Language - Part 2
  • From: "Tomasz Janeczko" <tjaneczk@xxxx>
  • Date: 2 Feb 1999 22:57:28 -0000

PureBytes Links

Trading Reference Links

Here comes the 2nd part of AFL description.

Best regards,
Tomasz Janeczko

eGroup home: http://www.eGroups.com/list/amibroker
Free Web-based e-mail groups by eGroups.com


AmiBroker Formula Language (AFL)


AFL is a special programming language used to define and
create custom indicators, analyses and simulation tests and guru
commentaries. Unfortunatelly it is not compatible with previous
Rule Definition Language used by pre-2.9 releases of AmiBroker.
However it is very easy to port all formulas to a new language.
On the other hand AFL is similiar to very widely spread Metastock
formula language. AFL is comprised of high-level functions (e.g.,
ma(), rsi(), abs()
), mathematical operators (e.g., +, -,
/, *), and parameters (open,
high, low, close,
etc.). Each of these basic components can be combined in a unique
way to create your own indicators or trading rules or Guru
comments. AFL is the foundation for all AmiBroker analysis tools.


Each formula in AFL contains of one or more statements
(lines). Each statement MUST be terminated by semicolon (;). In
this way you are able to break long expressions into several
physical lines (in order to gain clarity) and AmiBroker will
still treat it like a single statement until terminating
semicolon. Example (single variable assignment statement): 

my_indicator = iif( macd() > 0, 
		 close - ma(close,9), 
		 ma( close, 9 ) - close ); 

One of the most important building blocks of a formula is
called a price array identifier. It identifies specific
price fields that the formula should operate on. The valid price
array identifiers are open, high,
low, close, volume,
sales, average. Price array
identifiers can be abbreviated as shown in the following table.
Note that these are not case-specific. 

Long name








Examples of the use of price array identifiers in formulas are
shown below. 

ma( close, 10 ); iif( h > ref(h,-1), ma(h,20), ma(c,20) ); 


Mathematical operators are the "glue" that binds
formulas. Formulas can contain the following mathematical

+ Addition 
- Subtraction (or negative) 
* Multiplication 
/ Division 
^ Exponentiation (raising to a power) 

The following formulas illustrate the use of operators in a

( H + L ) / 2; 
ma(c,10)-ma(c,20) / (h + l + c); 
close + ((1.02 * high)-high); 


As you probably noticed AFL supports parentheses in formulas. 

Parentheses can be used to control the operation precedence
(the order in which the operators are calculated). AmiBroker
always does operations within the innermost parentheses first.
When parentheses are not used, the precedence is as follows
(higher precedence listed first): 

^ Exponentiation 
- Negation - Unary minus 
* Multiplication 
/ Division 
+ Addition 
- Subtraction 
< Less than 
> Greater than 
<= Less than or equal to 
>= Greater than or equal to
= = Equal to 
!= Not equal to 
Logical "Not" 
AND Logical "And" 
OR Logical "Or"
= Variable assignment operator 

The expression 

H + L / 2; 

(without parenthesis) would be calculated by AmiBroker as
"L / 2" plus "H", since division has a higher
precedence. This would result in a much different value than 

(H + L)/2; 


In addition to mathematical operators, AmiBroker contains
nearly 100 functions that perform mathematical operations. 

The following formula consists of a single function that plots
the square roots of the closing prices: 

sqrt( CLOSE ); 

The following formula consists of a single function that plots
a 14-period RSI indicator: 


The following formula consists of two functions. The result is
the difference between the MACD indicator and a 9-period
exponential moving average of the MACD:

macd()- ema(macd(),9); 

All functions must be followed by a pair of parentheses. 

As has been eluded to in earlier examples, a function can be
"nested" within a function. The nested function can
serve as the main function's data array parameter. The following
examples show functions nested within functions:

ma( rsi(15), 10 ); 

ma( ema( rsi(15), 20), 10 ); 

The first example calculates a 10-period simple moving average
of a 15-period Relative Strength Index (RSI). The second example
calculates a 20-period exponential moving average of a 15-period
RSI, and then calculates a 10-period simple moving average of
this moving average. 

Conditional function IIF()

The iif() function is used to create conditional (i.e.,
"if-then") statements. It contains three parameters as
shown in the following example. 

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 plot a 9-day RSI, otherwise,
plot a 14-day RSI. The next formula plots “positive
volume” if the close is greater than the median price.
Otherwise, "negative volume" is plotted. 

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 if()
function. The following formula will result in either a 1 (true)
or a 0 (false):

rsi(14) > 70; 

The same done with iif() gives the same results, but the
formula is longer. 

iif(rsi(14) > 70, 1, 0 ); 

Logical operators

If a formula requires multiple conditions, you can combine the
conditions with AND and OR operators. For example, maybe you'd
like to plot a +1 when the MACD is greater than zero and the RSI
is greater than 70: 

macd() > 0 AND rsi(14) > 70; 

You can add as many conditions within a formula as you like. 

You can even combine AND and OR operators within the same
formula as follows: 

( macd() > 0 OR close > mov(close,10,e) ) AND rsi(14) > 70; 


In order to shorten, simplify, enhance, and make the
maintenance of complex formulas easier, you may want to use
variables. In fact using variables you can significantly improve
formula calculation speed. So it is strongly recommended to use

A variable is an alphanumeric name that is assigned to an
expression or a single value. Unlimited number of variables can
be used in a formula. Variables must be assigned before the
variable is used in the formula. Variables cannot be assigned
within a function call. 

AmiBroker uses some reserved variable names in its formulas,
for example in Auto-Analysis window you have to assign values to
2 variables named 'buy' or 'sell' to specify the conditions where
"buy" and "sell" conditions occur. For
example (system that buys when MACD rises above 0 line, and sells
when MACD falls below 0 line) 

buy = cross( macd(), 0 ); 
sell = cross( 0, macd() ); 

The following rules apply to the naming of variables: 

Variable names cannot contain commas, parenthesis,
spaces, underscores, etc. 
Variable names cannot duplicate names already used by
functions (e.g., ma, rsi, cci, iif, etc.). 
A variable cannot be assigned a name that matches the
parameters reserved for use in formulas (e.g., open,
high, low, close, simple, o, c, l, h, s, a). 
Variable names must contain at least one alpha letter
(e.g., T1234 ). 
Variable names are not case sensitive (e.g.,
"PERIODS" is the same as "periods"). 

eGroup home: http://www.eGroups.com/list/amibroker
Free Web-based e-mail groups by www.eGroups.com