Issue 8/2001 (No. 16)
AmiBroker Tips newsletter.
Issue 8/2001. No 16.
25 March 2001.
Copyright (C)2001 Tomasz Janeczko.
All back issues available from:
http://www.amibroker.com/newsletter/
IN THIS ISSUE
1 Welcome
2 AFL Library: New functions in AmiBroker Formula Language
3
Tip of the week: Using studies in your AFL formulas
1 Welcome

Welcome to the 8th issue of AmiBroker Tips newsletter in the 2001. This week I will focus on new functions in AFL introduced in AmiBroker 3.52 beta (available for download from here: http://www.amibroker.net/bin/ab352beta.zip) code for detecting channel breakouts.

Just a reminder: if you have any comments/suggestions or article ideas, please don't hesitate to drop a line to newsletter@amibroker.com

2 AFL Library: New functions in AmiBroker Formula Language

A new AmiBroker update (3.52 beta - available for download from here: http://www.amibroker.net/bin/ab352beta.zip) brings some exiting new features to AFL including access to hand-drawn studies from your formulas, adaptive moving averages, access to other stock data and more. In this issue of the newsletter I will describe these new functions along with application examples.

2.1 STRLEN - string length

SYNTAX strlen( STRING)
RETURNS NUMBER
FUNCTION calculates the length of the string
EXAMPLE

This function could be used for (for example) filtering out only 3 letter stock codes:

buy = something AND strlen( name() ) == 3;

2.2 AMA - adaptive moving average

SYNTAX ama( ARRAY, SMOOTHINGFACTOR )
RETURNS ARRAY
FUNCTION calculates adaptive moving average - simliar to EMA() but smoothing factor could be time-variant (array).
EXAMPLE

The example of volatility-weighted adaptive moving average formula:

graph0 = ema( close, 15 );
fast = 2/(2+1);
slow = 2/(30+1);
dir=abs(close-ref(close,-10));
vol=sum(abs(close-ref(close,-1)),10);
ER=dir/vol;
sc =( ER*(fast-slow)+slow)^2;

graph0 = ama( close, sc );

2.3 EXREM - remove excessive signals

SYNTAX exrem( ARRAY1, ARRAY2 )
RETURNS ARRAY
FUNCTION removes excessive signals:
returns 1 on the first occurence of "true" signal in Array1
then returns 0 until Array2 is true even if there are "true" signals in Array1
EXAMPLE

buy = ExRem( buy, sell );
sell = ExRem( sell, buy );

2.4 FLIP

SYNTAX flip( ARRAY1, ARRAY2 )
RETURNS ARRAY
FUNCTION works as a flip/flop device (electronic/electric engineers will know what I mean)
returns 1 from the first occurence of "true" signal in Array1
until a "true" occurs in Array2 which resets the state back to zero
unil next "true" is detected in Array1...
EXAMPLE

buy = ExRem( buy, sell );

buy = Flip( buy, sell ); // this essentially reverts the process of ExRem - multiple signals are back again

2.5 ISEMPTY

SYNTAX isempty( ARRAY )
RETURNS ARRAY
FUNCTION returns 1 (or 'true') when given point in array is {empty}
Note: {empty} value is used internaly by AFL to mark
bars when the value is not available - for example for the first
20 bars the value of 20-day simple moving average is not available
({empty})
EXAMPLE

movagv = ma( close, 30 );

WriteIF( IsEmpty( movavg ), "Moving average not available yet", WriteVal( movavg ) );

2.6 ISTRUE

SYNTAX istrue( ARRAY )
RETURNS ARRAY
FUNCTION returns 1 (or 'true') when given point is not {empty} AND not zero
EXAMPLE

 

2.7 FOREIGN

SYNTAX foreign( TICKER, DATAFIELD )
RETURNS ARRAY
FUNCTION Allows referencing other (than current) tickers in the AFL formulas. TICKER is a string that holds the symbol of the stock. DATAFIELD defines which array is referenced. Allowable data fields: "open", "close", "high", "low", "volume", "interest".
EXAMPLE

This function provides an easy way to implement overlays, as shown in the following code:

/* Red line - current security price */
/* Blue line -overlaid index chart (scaled to fit) */

graph0 = close;
graph1 = foreign( "XAO", "Close" ) ;
/* replace "XAO" by your index, or any other ticker to graph along with you stock */

graph1 = lastvalue( highest( graph0 ) )/ lastvalue( highest( graph1 ) ) * graph1;

2.8 STUDY

SYNTAX study( STUDYID, CHARTID = 1 )
RETURNS ARRAY
FUNCTION

generates an array equivalent to a trendline study drawn by the user - allows detecting trendline breakouts from AFL.
STUDYID is a two-character identifier of the study. Predefined identifiers are: "UP" - uptrend, "DN" - downtrend, "SU" - support, "RE" - resistance, "ST" - stop loss, however you can use ANY identifiers (there are no limitations except that AmiBroker accepts only 2 letter codes).
CHARTID - identifies the chart pane where the study was drawn - the default 1 refers to main price chart. First 20 custom indicator charts have identifiers 1000, 1001, ..., 1019. Other charts types have the following chart IDs:
MAIN 1
MACD 2
RSI 3
ROC 4
STOCH 5
VOLUME 6
ULTIMATE 7
OBV 8
TRIX 9
RS 10
CCI 11
MFI 12
ACCDIST 13
CHAIKIN 14
NVI 15
TRIN 16
ADLINE 17
ADX 18
SAR 19

EXAMPLE

buy = cross( close, study( "re" ) );

Detailed description on using studies in AFL is provided in the below.

3 Tip of the week: Using studies in your AFL formulas

AmiBroker 3.52 introduces ability to reference hand-drawn studies from AFL formulas. This feature is quite unique among trading software (for example Metastock can not do this) and as you will find out using this feature is quite easy.

I will show you an example how to check if the trend line is broken from AFL code. All we need to do is three simple steps:

  1. Draw a trend line
  2. Define study ID
  3. Write the formula that checks trend line break

3.1 Drawing trend line

A trend line is a sloping line drawn between two prominent points on a chart.

In this example we will draw the rising trend line that defines the uptrend. This kind of trend line is usually drawn between two (or more) troughs (low points) to illustrate price support.

For sure you know how to draw a trend line in AmiBroker - just select a "Trend line" tool from "Draw" toolbar, find at least two recent troughs and just draw the line.

3.2 Define study ID

As you probably know, you can modify the properties of each line drawn in AmiBroker by clicking with the right mouse button over the study and selecting "Properties" from the menu. The properties dialog that shows up allows you to define exact start/end points and choose line colour, style and left and/or right extension mode.

For further analysis we will use the right-extended trend line (click on appropriate checkbox) to make sure that the trend line is automaticaly extended when new data are added.

Since version 3.52 the properties dialog allows also to define "Study ID" (the combo below colour box). "Study ID" is a two-letter code of the study that can be assigned to any study within a chart that allows AmiBroker to reference it from AFL. Predefined identifiers are: "UP" - uptrend, "DN" - downtrend, "SU" - support, "RE" - resistance, "ST" - stop loss, however you can use ANY identifiers (there are no limitations except that AmiBroker accepts only 2 letter codes). This way if you draw the support lines in many stocks and give them all "SU" identifier then you will be able to reference the support line from AFL code.

So we will assign the "SU" study ID to the rising support trend line we have just drawn.

3.3 Write the formula that checks trend line break

In this example we will detect if the closing price drops BELOW support trend line. This is actually very simple:

sell = cross( study( "SU" ), close );

Note that study() function accepts two arguments: the first is StudyID two letter code that corresponds to one given in properites dialog; the second argument is chart ID - by default it is 1 (when it is not given at all) and then it references the studies drawn in the main price pane. For checking studies drawn in other panes you should use the codes given above (in the table describing study() function).

.... and that's all for this week - hope you enjoyed reading


AmiBroker Tips newsletter. Issue 8/2001. Copyright (C)2001 Tomasz Janeczko. All back issues available from: http://www.amibroker.com/newsletter/