AFL Templates v2

Templates can be used, to reduce typing time, when writing formulas.

Formulas, that contain frequently used code, can be pre-written for the various modes (back-testing, plotting indicators, scanning, functions, explorations, optimization) and saved in a Templates folder.

Write your own to suit your use-age patterns.

When starting a new formula, open the relevant template, save it under a new name and comment/un-comment or cut and paste the starter snippets where needed.


Organizing Formula Files

New formulas are saved into the Custom folder, in the Workspace Charts Tree, by default. The tree can get pretty messy after a while, especially if Unnamed formulas start to accumulate (new formulas that are not named are automatically saved as Unnamed).

A little organization goes a long way.

New folders can be created by right clicking on the Custom folder and selecting Folder from the context menu that opens.


Folders can be moved by right clicking on the folder, holding down the right mouse button and dragging it to the new location.


If the ‘dragged’ folder is ‘dropped’ over the Databases folder, for example, New Folder will be entered under Databases as a sub-folder.


In the Charts Tree, folders and formulas are arranged alphabetically .


An alternative sort order can be forced by adding a ‘character hack’ to the folder names.

To re-order folders:

1) Right click on the folder and select Rename from the context menu that opens.


2) Position the cursor at the start of the name and enter a single space, or a character e.g. tilde “~”, to the beginning of the name.


Next time the Chart Tree is refreshed the folders will have moved to the top and they will be ordered alphabetically.

Note: In this example a single space was used.


In the authors opinion, this is a much better view. Unassigned formulas are grouped together at the bottom of the Custom hierarchy, which prompts the user to move them to a folder, and Unnamed formulas are grouped together, once again prompting the user to clean them up.

Experimental, or temporary formulas, can be ‘kept’ in the formula list and more permanent formulas moved to folders.

Naming Formula Files

The formula name, as it is entered in the Formula Editor title box, is also the file name that the formula is saved under.


To help make it easier to find formulas prefix the name with:

  • X_ for Exploration,
  • P_ for Plot,
  • S_ for Scan and
  • B_ for Backtest.

The formulas will automatically be arranged, by functionality, in AmiBroker’s Workspace Chart Tree.


The order will be replicated in file browsers, like Windows Explorer, when the file list at C:\Program Files\AmiBroker\Formulas\Custom is sorted by Name.


Modifying Built-in Formulas

Any indicator, or formula, can be easily changed while at the same time preserving the original version.

To create another version of a formula:

1) Right click on the formula, or indicator, in the Workspace >> Charts Tree and pick Edit from the context menu that opens.


The Formula Editor window will open with the code for the selected formula in place.

2) Click inside the Formula name input box, to position the cursor, and over-type to change the name of the formula, or add a version number (if the name is a long one use the arrow keys to move the cursor to the end of the box).


3) Click on Save and the ‘new’ formula will be entered into the same folder as the original (the Formula Editor will remain open).


4) Once the formula has been saved under a different name the code can be changed.

Modify the code and Save the changes.


5) When the changes to the formula are complete Save and close.

Bob’s your uncle (as we say in Orstralia).

Naming Formulas

For sharing formulas, or where the owner keeps notes on formulas outside of AmiBroker, it can be useful to copy the name of the formula into the code. That way, when the formula is copied and pasted back into AmiBroker the name will be ‘shipped’ with the formula. It also helps with version control by recording the version number in the formula.

Note: This helps avoid naming mishaps since changing the version number in the formula requires a deliberate effort that goes hand in hand with formula modifications. 

To copy formula names into the code:

1) Select the formula name by clicking in the name box.

2) Right click in the selected area and pick Copy from the context menu that opens.


3) Right click inside the Formula Editor ‘page’ and select Paste from the context menu that opens.


4) Delineate the formula name, as a comment, by adding the symbol pair /* and */ before and after the name.


5) When the formula is exported, in any format, the name travels with the formula.


field = ParamField(“PriceField”,3);
Color = ParamColor(“Color”,colorBlack);

6) When the formula is ‘imported’ back into AmiBroker the name can be copied, from the formula, and pasted back into the Formula Editor name box. 

Combination Charts

Color Formulas

Extending The Range

Palette Editing

article 3/5

The majority of indicators use a limited number of plots, so a few basic colors can accommodate most charting requirements. Combining this with the fact that the primary colors are easier to distinguish apart, compared to graduations of any one color, makes the colors of the rainbow a stand-out choice for everyday color preferences. Conveniently, ROYGBIV is a mnemonic that makes the rainbow numbers easy to remember when using color indexing for quick code writing.

The first sixteen numbers of the color index are reserved for custom colors, so this provides an opportunity to apply this system. (The color index is based on a zero count system so the available numbers are 0 – 15).


To match the colors of the rainbow to number 2 – 8 of the color index:

a) go to Tools > Preferences > Colors [TAB] and click on the Palette editor button ( the Color window will open);
b) select the first box in the top row of Custom colors;


c) click on White, in the Basic colors table;


d) click on the Add to Custom Colors button (White will become colorCustom1 or index number 0).


e) select the second box in the top row of Custom colors;
f) click on Black, in the Basic colors table;
g) click on the Add to Custom Colors button (Black will become colorCustom2 or index number 1, which is the installed default for black).
e) Repeat the three stop process for Red, Orange, Yellow, Green, Blue, Indigo and Violet using boxes 3 – 9 inclusive.
f) Use the same procedure to set the remaining seven to any preferred color.
g) Close the Color window and click on Apply on the Preferences window before closing it (this will confirm the new color settings).

To use a color that is not in the Basic color table:

a) select the Custom color box to be changed, followed by the Basic color;
b) then modify the selected Basic color’s Luminescence by dragging the arrow head up and down the vertical scale. (In this example the graduated shades of grey were added to Custom Colors 13 – 16 using this technique).


c) Additionally the Hue or the Saturation can be changed by clicking and holding on the moving cursor, inside the color matrix panel, and dragging it up/down (Saturation) or left/right (Hue).
d) When the desired color is produced in the large Color/Solid box click the Add button.
e) Click on Apply on the way out.


(In this example the Basic color Medium Grey has been changed slightly by the addition of a little Blue and a slight reduction in Red (Saturation), as well as changes to the Hue settings, while the Luminescence is constant).

The new Custom Color settings will now be available in the Color Picker, or within formulas via color index numbers 0 – 7 and 8 – 15, or colorCustom1-16.


(Note that for maximum contrast between the colors it is better to use only six colors, along with black and white, as there are only three primary colors (RYB) with three possible primary combinations between them (O,G,V). Indigo is an exceptional color, not being derived solely from the Primary Colors. For this reason it is hard to distinguish between Indigo and Blue or Violet, so for clarity Indigo can be left out if desired).

Colored Plots

article 1/5

Next Page »