When we create custom indicators in AmiBroker, then by default the program will automatically create chart title line based on the selected ticker and the information we have provided in Plot function calls.
If we use the following formula:
Plot( Close, "Close", colorDefault, styleBar );
Plot( MA( Close, 20), "MA-20", colorRed );
Plot( MA( Close, 50), "MA-50", colorBlue )
Then the auto-generated chart title line will contain:
First item is the symbol name selected for that particular chart window (BA in this case), then the output is based on the plot names (provided in the 2nd argument of Plot function calls within the code) and colors will also match the respective plot colors. Number of decimal places depends on the settings in Tools->Preferences->Miscellaneous: Decimal places in chart titles/tools
If we do not want certain plot to affect the chart titles, we can use styleNoTitle chart style, for example changing the 3rd line of the above code into:
Plot( MA( Close, 50), "MA-50", colorBlue, styleNoTitle )
Would result in removing MA-50 values from the title output presented above, even though MA-50 values are still presented in the chart.
We can customize the chart title output even further by means of dedicated Title variable. If we define Title string within the formula, it will override the automatic title generation. Therefore, starting with most basic example, defining an empty string with use of:
Title = ""
would hide the title output completely, while using statement like this:
Title = "Close Price: " +NumToStr( Close, 1.2 )
would generate default-color output such as the following (the price is formatted to use 2 decimal places by using NumToStr function in this case):
It is also possible to define chart color by using EncodeColor() function, so changing the above line into:
Title = "Close Price: " +EncodeColor( colorRed ) + NumToStr( Close, 1.2 )
would result in displaying the price value (and other text that follows the EncodeColor call) in red color.
Now let us analyse the Title definition included in the built-in Price chart.
_N(Title = StrFormat("{{NAME}} - " +FullName() +
" - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ))
Among other elements shown above, the Title definition uses templates like {{NAME}} or {{VALUES}} etc. in the defined string.
These are special tokens that are replaced by appropriate values at run-time:
- {{VALUES}} inside Title string will be replaced by automatic-values generated by Plot function calls
- {{NAME}} will be replaced by the ticker symbol
- {{DATE}} will be replaced by selected date
- {{INTERVAL}} will be replaced by the name of the interval
- {{OHLCX}} will be replaced at runtime by string “Open …, Hi …. Lo … Close (…%)” showing current price
The built-in title definition uses also StrFormat function. This function allows us to specify the string followed by the list of arguments that will be inserted into the string in places, where %f, %g or %e specifications are entered.
If we use the code like the one above, it would produce the following output:
Those specifications allow us to format the output string accordingly.
- %.0f will output a number without decimal places (with rounding if necessary), such as 393
- %.1f will output a number with 1 decimal place, such as 392.7
- %.2f will output a number with 2 decimal places, such as 392.65
- %.3f will output a number with 3 decimal places, such as 392.651
- %.4f will output a number with 4 decimal places, such as 392.6510
- %e will output scientific notation (mantissa/exponent), such as 3.92651e+2 (3.92651 * 10 ^ 2 )
- %g uses automatic formatting and displays as many decimal places as required to show full precision of given number (392.651)
The documentation of the StrFormat function is available in the manual
http://www.amibroker.com/f?StrFormat
In the above example the whole title definition is enclosed with _N() function. This just prevents from displaying the title string within the Interpretation window, so using _N() does not really affect the output within the chart.