Issue 9/2001 (No. 17)
AmiBroker Tips newsletter.
Issue 9/2001. No 17.
8 April 2001.
Copyright (C)2001 Tomasz Janeczko.
All back issues available from:
http://www.amibroker.com/newsletter/
IN THIS ISSUE
1 Welcome
2 AFL Library: Color your bars!
3
Tip of the week: Understanding AmiBroker Formula Language - Array Processing - by Geoff Mulhall
1 Welcome

Welcome to the 9th issue of AmiBroker Tips newsletter in the 2001. This week I will show you how to implement coloured up, down, inside and outside bars using new features available in AFL since AmiBroker version 3.50.

This issue features also the article written by Geoff Mulhall about understanding the way AFL works. By the way - a big "thank-you" goes to Geoff for the contribution.

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: Color your bars!

AmiBroker supports various types of charts: line, candlestick, bars, dotted and various styles including normal and thick. Until recently single graph line could have only single colour defined using AmiBroker Formula Language (AFL) special variable called graphNcolor. Since version 3.50 AmiBroker Formula Language features new special array called graphNbarcolor, that allows multi-coloured bar charts. The graphNbarcolor array simply defines color palette indexes for each bar drawn. This allows for example color-price bar charts that are green for up bars, red for down bars and black for outside bars. An inside bars are treated as preceding bar (up or down).

/* Color price - bar chart */
/* Last modified 8 Apr 2001 */
/* AFL implementation by */
/* (C)2001 Tomasz Janeczko */

outsidebar = outside();
insidebar = H <= Ref( H, -1 ) AND L >= Ref( L, -1 );
/*
built-in inside() works with < > instead of <= >= */

upbar = H > Ref( H, -1 ) AND L >= Ref( L, -1 );
downbar = L < Ref( L, -1 ) AND H <= Ref( H, -1 );

/* Colourized price bars drawn here */
graph0 = close;
graph0style = 128;
barcolor = IIF( outsidebar, 1, IIF( downbar, 4, IIF( upbar, 5, 0 ) ) );
graph0barcolor = ValueWhen( barcolor != 0, barcolor );

First lines simply define up/down/inside/outside bars. Please note the last two lines of the code - they define the palette indexes for each kind of bar. The multi-IIF statement assigns index 1 for an outside bar, index 4 for down bar abd index 5 for up bar. An inside bar is temporarily marked with index 0. The last line ensures that inside bar gets the most recent value of the barcolor that was either up, down or outside.

The next example is essentially the same with the difference that we plot volume instead of the price chart:

/* Color volume-bar chart */
/* Last modified 8 Apr 2001 */
/* AFL implementation by */
/* (C)2001 Tomasz Janeczko */

outsidebar = outside();
insidebar = H <= Ref( H, -1 ) AND L >= Ref( L, -1 );

/* built-in inside() works with < > instead of <= >= */
upbar = H > Ref( H, -1 ) AND L >= Ref( L, -1 );
downbar = L < Ref( L, -1 ) AND H <= Ref( H, -1 );

/* Colourized price bars drawn here */
barcolor = IIF( outsidebar, 1, IIF( downbar, 4, IIF( upbar, 5, 0 ) ) );
graph1 = volume;
graph1style = 6;
graph1barcolor = ValueWhen( barcolor != 0, barcolor );

Hope you enjoy your charts in new colors!

3 Tip of the week: Understanding AmiBroker Formula Language - Array Processing - by Geoff Mulhall

3.1 General

AmiBroker is perhaps the most powerful stock charting package available. All this power comes from AmiBroker Formula Language (AFL). This is an array processing language and works in a similar way to Microsoft Excel. Anyone familiar with MS Excel should have no trouble quickly picking up AFL. - In fact all the examples in this article were all created using MS Excel.

3.2 What is an Array?

An array is simply a list of values. In some books it may be referred to as a vector. Each numbered row of values in the example represents an individual array. Amibroker has stored in its database 6 arrays for each stock. One for opening price, one for the low price, one for the high price, one for the closing price and one for volume (see the rows labelled 1-5 below) and one for open interest. These can be referenced in AFL as open, low, high, close, volume, openint or o, l, h, c, v, oi.

Any other array is calculated from these 6 arrays using formulae built into AFL. These arrays are not stored in the database but calculated where necessary.

Each individual value in an array has a date associated with it. If you have the tool tip option turned on (Preferences -> Miscellaneous Tab - > Price data tool tips), when you move your cursor over candle on a daily candle chart, a small yellow rectangle appears. AFL then looks up the open, low, high, close, volume values in the appropriate array and displays them inside the tool tip.

3.3 Why is AFL so fast?

There are something like 1300 stocks on the Australian Stock Exchange (ASX) yet AFL can scan through these extremely quickly because it does not individually lookup a values. It simply scans through an array from left to right calculating or comparing values as necessary.

3.4 How does AFL work?

If in your AFL code you need to see if the closing price is greater than say a 3 day simple moving average AFL will first run through the close array creating a new array called ma(close,3) for the stock being analysed. Each cell in the new array can then be compared one for one in the close array. In the example an array called Cond1 is created this way. For each cell where the closing price is greater than the corresponding cell value in ma(close,3) the cell value for new array 'Cond1' is set to '1'. If the closing price is not greater than the corresponding price in the close array the value in 'Cond1' is set to '0'.

AFL can also look forwards or backwards a number of cells in an array using the 'ref' function.
In row 8 a new array called Cond2 has been created by comparing the value of each cell in the volume array with its previous cell setting the Cond2 cell value to '1' if true and '0' if false.

Row 9 shows an array called 'Buy' created by comparing the cell values in Cond1 with the cell values in Cond2. If the cell in Cond1 has a '1' AND so does the corresponding cell in Cond2 then a '1' is placed in the 'Buy' array cell.

Row 10 shows an array called 'Sell' created whenever the cell value in the close array is greater than $1.30.

Day
1
2
3
4
5
6
7
8
9
10
1 Open
1,23
1,24
1,21
1,26
1,24
1,29
1,33
1,32
1,35
1,37
2 High
1,24
1,27
1,25
1,29
1,25
1,29
1,35
1,35
1,37
1,29
3 Low
1,2
1,21
1,19
1,2
1,21
1,24
1,3
1,28
1,33
1,27
4 Close
1,23
1,26
1,24
1,28
1,25
1,25
1,31
1,3
1,32
1,28
5 Volume
8310
3021
5325
2834
1432
5666
7847
555
6749
3456
6 ma(close,3)
#
#
1,243
1,260
1,257
1,260
1,270
1,287
1,310
1,300
7 Cond1 = close > ma(close,3)
0
0
1
0
1
1
0
0
0
1
8 Cond2 = volume > ref(volume,-1)
 
0
1
0
0
1
1
0
1
0
9 Buy = Cond1 AND Cond2
0
1
0
0
1
0
0
0
0
10 Sell = high > 1.30
0
0
0
0
0
0
1
1
1
0

Obviously Buy and Sell are special arrays whose results can be displayed in the Analyser window or on screen using a red or green value as needed.

If you're having trouble coding AFL I suggest you generate the arrays in the example in Excel for yourself. If that's a problem get some help from a friend - especially if that friend is an accountant.

Once you've got the hang of it you can code any system from a book on trading - or build one yourself.

Best Wishes for Successful Trading.

Geoff Mulhall
Melbourne, Australia.

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


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