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).

WARNING – THE FOLOWING PROCEDURE WILL CHANGE ALL THE DEFAULT CUSTOM COLOR INDEX NUMBERS, EXCEPT FOR BLACK, WHICH IS ONE. THIS WILL AFFECT ALL SAVED INDICATORS THAT USE CUSTOM COLOR INDEXES, OTHER THAN ONE FOR BLACK.

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;

CustomColors001

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

CustomColors002

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

CustomColors003

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).

CustomColors009

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.

CustomColors006

(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.

CustomColors007

(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

Color Picking

article 2/5


In AmiBroker, the colors of many of the chart elements can be customized from within simple AFL formulas. The most common application of this feature is when plotting indicators, where the third parameter of the Plot() function allows users to set the color to their own preference e.g. Plot(C,"Close",colorRed, styleLine).

A short hand method, to specify the color, is to substitute colorRed with the number 32, which is the color index number for Red e.g. Plot(C,"Close",32, styleLine). It can, however, be difficult to remember the color names and the index numbers. (Refer to the AFL Reference Manual reserved variables table for the complete list).

The Color-Picker, on the Format Toolbar, is used as a graphical method for setting the color of drawing objects placed on the chart, as well as chart plots. It can also act as a handy reminder tool when stipulating color preferences in formulas. A prompt appears, when the mouse is hovered over the Color-Picker, and provides the correct name for each color. The name can then be appended to the color parameter within formulas.

To use the Color-Picker as a name prompt:

a) open it from the drop-down on the Format Toolbar. (Note that the Color-Picker can be opened in the background, to act as a dynamic prompt, while working with the Formula Editor window open);

CP004

b) position the mouse over the color of choice.

CP011

c) replace Name, in colorName parameters, with the color title exactly as written e.g. colorName becomes colorRed. (Remove all gaps, hyphens and special characters e.g. Grey-50% becomes Grey50. Note that there are a few exceptions; Grey-80% becomes DarkGrey, Grey-25% becomes LightGrey, Light Green becomes PaleGreen and Light Turquoise becomes PaleTurquoise).

To use the Color-Picker as a color index prompt:

a) open the Color-Picker;
b) choose the preferred color, and obtain it’s index number, by counting to the right from the first number in each row (the index is zero based, and the first sixteen colors are reserved for custom colors, so the first row starts with 16 and the second starts with 16 + 8 = = 24 etc. (The color index number for Red, as the first color in the third row, is therefore 32).

CP013

Color indexing is a better method of changing colors, at the formula level, than using colorNames, as it is quicker and there are no exceptions to remember. As an example, the formula in the Editor image above, which was originally used in the Users’ Knowledge Base post ’How Many Trading Days In A Calendar Year?’ with colorBlack and styleLine settings, has been changed to a colorRed styleHistogram chart using the shorthand color index 32 (text highlighted in yellow).

CP003

The Color-Picker can be torn-off the toolbar if required:

a) drop the Color-Picker down;
b) click and hold the mouse pointer on the tear-off line;

CP014

c) when a moving pointer appears the window can be dragged to the desired position.

CP015










How Many Trading Days In A Year?

Sometimes it is useful to know the number of trading days in a year e.g. to annualize returns from a system that trades on a daily basis.

On other occasions, index data can contain errors, and the number of daily bars, in a year, can be compared to the exchange calendar, for the relevant index, to check on this failing.

Detecting Look-Ahead Problems

Look-ahead problems occur when, during Backtesting, your code uses future prices to calculate signals. When this happens, your system’s performance may be heavily inflated, but obviously since you are looking into the future, the system cannot be traded.

While the Indicator formula editor menu has a Check function, it often fails when debugging complex systems, especially if they use Plug-Ins or DLLs. Also, when it discovers a problem, it does not show you at which bar the problem occurred. The simple code presented here detects all look-ahead problems that might occur during a Backtest, even if your system uses Plugins or DLLs, and it can be appended to any system.

The code makes use of the fact that setting a future bar to NULL will cause dependent variables and signals to change. To use this technique your Arrows must be placed using the PlotShapes() function so that they will change when you NULL future bars. This would not happen if the arrows are placed by the Backtester. Thus, you should turn Off Arrows in your Param() menu.

To test for Look-Ahead problems, you should copy the code to your system formula and Apply it to an Indicator pane. Make sure your chart is fully right justified so that you can see the last bar, and zoom so that you can see some signal arrows. Next, open the Param window and move the slider to change the Remove Bars count. As you do this you will see bars disappearing, last bar first. When the disappearance of a bar causes a preceding arrow to disappear, your system looks ahead.

Note that this code can also be used to detect Indicator dependence on future bars. If your indicator changes ahead of your NULL bar, your Indicator is dependent on future data. When changes are very small, this may be hard to see. It would be an interesting coding challenge to save a number of bars and compare so that even small changes can be detected. Perhaps someone will write a DLL to do all this for us.

A minor drawback of this code is that you have to scan your chart manually, which may take a few minutes. You could add an automated scan and run the code using an exploration, generating an error table for an entire Watchlist. It wouldn’t be very fast, but it would increase the chance of detecting all possible errors.

The demo code below uses the Zig() function to place signals and arrows on the chart. Since Zig() looks ahead by varying amounts, it is perfect to demonstrate how this code works.

EnableNulling = ParamToggle("NULLing of Data","DISABLED|ENABLED",0);
RM = Param("NULL Bars L<-R",0,0,1000,1);
if( EnableNulling )
{
Z = Null;
LB = LastValue(BarIndex());
O = IIf(BarIndex()>(LB-RM),Z,O);
H = IIf(BarIndex()>(LB-RM),Z,H);
L = IIf(BarIndex()>(LB-RM),Z,L);
C = IIf(BarIndex()>(LB-RM),Z,C);
}
ZChange = Param("%Zig",0.1,0,1,0.05);
Z = Zig(C,ZChange);
Buy = Z < Ref(Z,1) AND Z < Ref(Z,-1);
Sell = Z > Ref(Z,1) AND Z > Ref(Z,-1);
Short = Sell;
Cover = Buy;
Plot(C,"",1,128);
PlotShapes(IIf(Buy, shapeSmallUpTriangle, shapeNone),5,0,BuyPrice,0);
PlotShapes(IIf(Sell, shapeHollowDownTriangle, shapeNone),4,0,SellPrice,0);
PlotShapes(IIf(Cover, shapeHollowUpTriangle, shapeNone),5,0,CoverPrice,0);
PlotShapes(IIf(Short, shapeSmallDownTriangle, shapeNone),4,0,ShortPrice,0);

Edited by Al Venosa

« Previous Page