Issue 6/2000
AmiBroker Tips weekly newsletter.
Issue 6/2000.
Copyright (C)2000 Tomasz Janeczko.
All back issues available from:
1 Welcome
2 AmiBroker 3.41 available
3 Tip of the week: How to write your own chart commentary
1 Welcome

Welcome to the 6th issue of AmiBroker Tips newsletter.

This issue is a little bit shorter than usual because I was busy preparing a new version. The main topic of this issue is writing your own chart commentaries.

By the way: Do you find this newsletter useful? Have any comments/suggestions or article ideas. Please don't hesitate to drop a line to

2 AmiBroker 3.41 available

A new version (3.41) is available now for download from AmiBroker web site. There are two important issues connected with this release. First: all format definition files are now moved to \Formats subdirectory! If you have written your own files please move them to that directory. And the second thing - setup now includes new script for Sharenet downloader. It is installed by default along with proper Custom Tools item setup. I think that all Sharenet customers will welcome this feature.

Among other new features worthe mentioning are: Automatic analysis, Indicator Builder and Commentary windows are now resizable (see the sizing gripper in the bottom right corner of these dialogs) and could be also minimized; ASCII importer now warns that errors occurred during import process and displays the log on your request; "Rename" button added to Indicator Builder window for easier change of custom indicator names.

Also completely new setup has much better look&feel and is now self-extracting so you won't need unzipping tool anymore.

3 Tip of the week: How to write your own chart commentary

One of the interesting aspects of using AmiBroker Formula Language is writing automatic chart commentaries. The idea behind this technique is as follows:

  1. You write the commentary formula that consists of two basic elements: static texts and AFL expressions
  2. AmiBroker evaluates expressions using currently selected stock data and generates dynamic content
  3. The mixture of static text and evaluated formulas are displayed in commentary output window
  4. Additionally buy/sell arrows are plotted on the chart

Commentaries are available from Analysis->Commentary menu. When you open commentary window you will see two tabs: Commentary and Formula. In the Formula tab you can type the AFL statements which will be evaluated by AmiBroker resulting in dynamic commentary that appears in Commentary tab. The following sections will guide you through the steps needed to write your very own commentary formulas.

3.1 Writing static texts

Static text elements written in the formula should be enclosed in the quotation marks and terminated by semicolon sign as shown below:

"This is sample static text statement";

You can write several statements and each statement will be placed in a new line in the commentary output window:

"This is first line of text";
"This is second line of text";

Please type these examples into edit field in the Formula tab and switch to Commentary tab. You will see the texts displayed in the output area but without any quotation marks or semicolons. This is because AmiBroker has evaluated this simple text statements into strings and it displayed the strings in the output window.

To write several lines of text you can use a couple of statements as shown above or you can do this using single statement and line break sequence ('\n'):

"This is first line of text\nThis is second line of text\nThis is third line of text";

You can also concatenate the string constants which will result in single line text:

"This" +
" is" +
" single"+
" line" + " of text";

I guess that you are quite bored with these simple examples, let's start with some dynamic content.

3.2 Dynamic content

To enable dynamic commentaries AFL has a couple of special functions available, but two of them are the most important: WriteVal() and WriteIF(). WriteIF() function is used for conditional text display and will be described later in this article, now let us see what we can do using WriteVal() function.

The AFL reference manual says:

SYNTAX writeval( NUMBER );
writeval( ARRAY );
FUNCTION This function can only be used within an Guru commentary. It is used to display the numeric value of NUMBER or ARRAY.

So, if you want to display a value of a number or currently selected bar of the array you should use writeval() function. But... wait a minute - what does it mean "currently selected bar of the array"? Let me explain this using simple formula (please type it in the Formula tab):

writeval( close );

When you switch to Commentary tab you will see the value of closing price (the same one which is displayed at the top of main price chart). But when you click on the chart in another place, selecting different date and then you click "Refresh" button you will see different value - the closing price at day you have selected. So writeval( close ) function displays the value of currently selected bar of close array. And it works exactly the same way with other arrays. If you write

writeval( macd() );

you will see the exact value of MACD indicator at the day you have selected in the main chart. Having our current know-how we are able to write some statistics:

"Closing price = " + WriteVal( close );
"Change since yesterday = " + WriteVal( close - ref( close, -1 ) );
"Percent chg. since yesterday = " + WriteVal( roc( close, 1 ) ) + " %";
"MACD =" + WriteVal( macd() ) + " , Signal line =" + WriteVal( signal() );

When you switch to Commentary tab you will see output similiar to this one:

Closing price = 17.940
Change since yesterday = -0.180
Percent chg. since yesterday = -0.993 %
MACD = -0.001 , Signal line = 0.063

Quite nice, isn't it? You can also write current stock ticker and selected date using name() and date() functions as shown below:

"Statistics of " + name() + " as of " + date();

But what we miss here is an ability to write something if some condition is met and write something different otherwise...

3.3 Conditional text output

AFL is equipped with very nice function called WriteIF() that can output different texts depending on the condition. Let us look what documentation says:

FUNCTION This function can only be used within an Guru commentary. If EXPRESSION evaluates to "true", then the TRUE TEXT string is displayed within the commentary. If EXPRESSION evaluates to "false", then the FALSE TEXT string is displayed.

So we can easily output different text depending on expession, for example:

writeif( macd() > signal(), "The MACD is bullish because is is above it's signal line", "The MACD is bearish because it is below its signal line" );

You can also combine several WriteIf() function calls in order to handle more possibilities:

"The current market condition for "+ name() + " is: ";

avgcond1 = ( c > ema( close, 200) ) + 0.1 * ( close > ema( close, 90) ) + 0.1 * ( close > ema( close , 30 ) );
avgcond2 = -( c < ema( close, 200) ) - 0.1 * ( close < ema( close, 90) ) - 0.1 * ( close < ema( close , 30 ) );

WriteIf( avgcond1 == 1.2,
"Very Bullish",
WriteIf( avgcond1 == 1.1,
WriteIf( avgcond1 == 1.0,
"Mildly Bullish", "") ) ) +

WriteIf( avgcond2 == -1.2,
"Very Bearish",
WriteIf( avgcond2 == -1.1,
WriteIf( avgcond2 == -1.0,
"Mildly Bearish", "") ) );

The formula above will return the text "The current market condition for {your ticker here} is: Very Bullish" if 30 day average is above 90 day average and 90 day average is above 200 day average. In other cases the formula will give you Bullish, Mildly Bullish, Mildly Bearish, Bearish or Very Bearish ratings.

For more examples on AFL commentaries please check AFL formula library especially MACD commentary formula which demonstrates all techniques presented here.

Now you are ready to start with your own commentaries... Good luck!

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

AmiBroker Tips weekly newsletter. Issue 6/2000. Copyright (C)2000 Tomasz Janeczko. All back issues available from: