Color Formulas

article 5/5

When a formula, containing the ParamColor function, is inserted into a new chart the ParamColor default will apply.

Save the following code as P_ForeignRGB and apply it as an indicator to demonstrate this point.

1
2
3
4
5
6
7
8
9
/*P_ForeignRGB*/ 
 
Color = ParamColor("Color",colorCustom2);
//Color = ParamColor("Color",ColorRGB(204,0,204));
 
Plot(C,"Close",Color,styleLine|styleThick);
 
PlotForeign("GM","GM",colorCustom2, styleLine|styleThick);
//PlotForeign("GM","GM",ColorRGB(204,0,204), styleLine|styleThick);



(Note that for this example the style has been changed to a thick line, by adding styleThick to the code, to make it easier to see the color of the plots in a screenshot).

CustomColorsAdvanced008

Any one of the 40 colors in the Color-Picker, or the 16 color favorites can be set as the ParamColor default. This can be extended to the full range of colors available in the 24 bit palette i.e. 255 * 255 * 255, which is approximately equal to 16 million colors, by the use of color functions within formulas.

To do this it is necessary to obtain the Red Green Blue numerical code for the color of choice.

To record the Color Matrix numerical code for an extended color:

a) open the ParamColor extended Color Palette (refer to the Users’ Knowledge Base Article ’Custom Colors - Extending The Range’ for instructions),
b) create the color of choice by maneuvering the pointer, either on the Luminescence Bar or within the Color Matrix and make a note of the numerical code for the chosen color e.g. Red 204, Green 0, Blue 204.

CustomColorsAdvanced001


c) click on OK and the Properties window will open with the custom color settings displayed e.g. RGB (204,0,204).

CustomColorsAdvanced002

The plot color, for the current symbol will change to reflect the parameter setting.

CustomColorsAdvanced006

The plot color can be set to use any color in the extended palette as the default.
There are two ways to do this; first by replacing color with a ColorFunction within the standard plot (or plotForeign code) or alternatively by using a ColorFunction within ParamColor arguments.

To change the foreign plot to an extended color:

a) comment out line 8 in the formula and uncomment line 9,
b) type the Red Green Blue (RGB) numerical code into the ColorRGB function arguments exactly as they were written in the Properties box (refer to the example text highlighted in yellow),
c) Save the formula to enact the changes.

CustomColorsAdvanced012

To change the current (parametrical) plot to use an extended color as the default (and demonstrate the functionality):

a) comment out line 3,
b) uncomment line 4,
c) Save the formula and then close the Formula Editor,
d) close the lower chart pane by clicking on the cross that appears when the mouse is hovered over the top right hand corner,

CustomColorsAdvanced013

e) right click on P_Foreign in the Charts list and then Insert it (this will install a new chart in the lower pane with RGB(204,0,204) as the default color, along with the option of changing the color settings via the Palette Editor, in case you have changed your mind about the color in the few seconds it took to insert the formula).

With the following three lines of code the result in the chart below was produced.

1
2
3
Color = ParamColor("Color",ColorRGB(204,0,204));
Plot(C,"Close",Color,styleLine|styleThick); 
PlotForeign("GM","GM",ColorRGB(204,0,204), styleLine|styleThick);



CustomColorsAdvanced015

Notice that the colors are identical.

The good news is, that if you have changed your mind about the current color, you still have the choice of 56 colors in the Picker, or the rest of the 16 million colors in the extended palette up your sleeve.

And now for a little fun let’s type 255, 255, 255 into the Color Matrix and see what we get.

CustomColorsAdvanced009

No sense in doing things by half. Let’s try 000.

CustomColorsAdvanced010


Finally the ’piece de resistance’.

Try the following code by Tomasz Janeczko (from the AFL Function Reference; ColorHSB examples).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Example 1: 
// 3-d multicolor multiple moving average cloud chart 
side = 1;
increment = Param("Increment",2, 1, 10, 1 );
for( i = 10; i < 80; i = i + increment ) 
{ 
up = MA( C, i );
down = MA( C, i + increment ); 
 
if( ParamToggle("3D effect?", "No|Yes" ) ) 
side = IIf(up<=down AND Ref( up<=down, 1 ), 1, 0.6 ); 
 
PlotOHLC( up,up,down,down, "MA"+i, ColorHSB( 3*(i - 10),
Param("Saturation", 128, 0, 255 ), 
side * Param("Brightness", 255, 0, 255 ) ), styleCloud | styleNoLabel );
}



CustomColorsAdvanced016




1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4 out of 5)
Loading ... Loading ...

Extending The Range

article 4/5

The custom colors in the Color-Picker are like a collection of color favorites; they are available to use within the program wherever graphical color choices are made, however the choice is limited to sixteen. There may be situations, when plotting, where an extended range of colors is required. This is provided for by the Parameters Color-Picker, where additional custom colors can be selected, from a wide range, for any parametrical plot.

The following formula will be used to demonstrate how to apply an extended custom color to a plot.

1
2
3
4
5
6
7
/*P_Foreign*/ 
 
//Color = ParamColor("Color",colorRed); 
//Plot(C,"Close",Color,styleLine); 
 
Plot(C,"Close",colorBlack,styleLine);
PlotForeign("GM","GM",colorCustom2, styleLine);


To apply the formula as a dynamic indicator chart:

a) save the code as P_Foreign,
b) right click on it and select Edit from the context menu (the Formula Editor window will open),
c) click on Apply from the Formula Editor Standard Toolbar. (The indicator will open in a new dynamic pane, however, the dynamics will only hold for the initial instance, while the Formula Editor remains open ).

CustomColorExtensions002

(Note that for this example the DJIA example database installed with AmiBroker has been used. AA, the first stock symbol in that database, is the current chart, and GM has been chosen as the foreign symbol, because, at the time of writing it’s price was close to that of AA. This provides a more compact view in the chart. Readers who are following this tutorial can substitute any other symbols as required).

The ParamColor function then needs to be added to the formula, to add a graphical Color-Picker interface to the chart.

To add color parameters to the current symbol:

a) uncomment line 3 (this assigns the ParamColor function output to the variable Color),
b) uncomment line 5 (this substitutes the ’contents’ of the variable Color, for the reserved variable colorBlack, in the standard plot code),
c) comment out the standard plot code in line7 (it has been replaced by line 5),
c) Save the formula to apply the new settings.

CustomColorExtensions004

The plot color for AA is now the default color of the ParamColor function i.e. colorRed. This can be changed to a custom color, that is not available in the color favorites group, from within the Properties window.

To apply an extended custom color to the current plot:

a) right click in the chart,
b) select Parameters from the context menu (the Properties window will open with the Parameters [tabbed] page in view),
c) use the dropdown on that page to open the Color-Picker,

CustomColorExtensions005

d) click on More at the bottom of the Color-Picker table (the Palette Editor opens),
e) check the second box on the top row of Custom colors (for this example only Custom color box two will be set),
f) then pick a Basic color (the check marks on Custom color box2 will not be retained but it is still the active box),
g) click on the desired base color variation in the Luminescence bar, or, click/hold and drag the arrow head for fine control,
h) in addition, or alternatively, click on the desired color variation in the Color Matrix, or click/hold and drag the moving pointer for fine control, (to allow for ease of replication, this example was created using option ’g’ alone).

CustomColorExtensions006

i) when the desired color is displayed in the Color/Solid box select Add to Custom Colors > OK. (The new color will be applied to the Parameters color setting and appear in the chart).

CustomColorExtensions007

Notice that the foreign plot is still referencing colorCustom2 (Black), from the favorites collection, while the current symbol color is referencing the color selected from the extended range. (The extended Palette Editor does not overwrite the favorites collection!).

CustomColorExtensions009

When using extended custom colors, it is not necessary to set them in the Custom colors table, as per the example, unless two or more will be used and interchanged in the one session. (If that is the case Custom colors can be set, to allow for an easy return to the same color, but they will not be available in a new session). The method used was chosen purely to demonstrate the differences between color favorites and extended custom colors . The easiest way to set extended colors, for one off use, is simply to click OK once the desired color has been produced by manipulating the matrix pointer.

While the Custom color settings are not retained between sessions any preferred Parameter color settings are e.g. the current view will be reinstated after closing and reopening AmiBroker. (In the installed version the current chart is automatically saved, as the default, at the end of the session).

The current view can also be saved for recycling, as a template, so that it will not be overwritten if the default is changed.

To save the current view as a template:

a) right click in the chart and select Template > Save from the available options, (the Save As window will open),

CustomColorExtensions015

b) Name the file and Save it. (The current data folder is the default view in the installed version. Thereafter it will change to the last folder used in this window).

CustomColorExtensions024

The saved template operates globally, as well as with the local database, so, if it is going to be used with other databases it is more convenient to save it in a generic Templates folder.

To create a Template folder and save a template file (continuing from step ’a’ above):

a) click on the Up One Level icon to access the AmiBroker folder list,

CustomColorExtensions016

b) create a new folder under AmiBroker, (a New Folder will appear with the name highlighted in blue),

CustomColorExtensions025

c) hit the Delete key to clear the text and rename the folder by typing Templates into the folder text box,
d) open the Templates folder, after naming it, and save the template file there as P_Foreign.

CustomColorExtensions020

The template, complete with the parameter color settings, can be reinstated anytime, and for any database. (In this case the database would need to contain the symbol used in the formula.).

To open the saved template:

(As preparation for this example, and to demonstrate the functionality of templates, the chart window was closed and AmiBroker was shutdown. After reopening AmiBroker a New > Blank Chart was installed from the File menu. In normal use it is not necessary to do this as the template will overwrite any chart when it is opened).

a) right click in the chart window where the template is to be installed,

CustomColorExtensions021

b) browse the file list and open P_Foreign.ALY that is in the Templates folder,

CustomColorExtensions023

P_Foreign will open using the first symbol in the All symbols list. (This example was created using the DJIA example database installed with AmiBroker. ^DJI is the first symbol in that database, so, in the example it is the current symbol).

c) select AA from the All symbols list to restore the view to the saved version.

Additional foreign plots can be added to the chart, each with their own customized color.

To add additional customized plots:

a) copy/paste line 3 & 8 into the Formula Editor page, as line 10 & 12 respectively, then edit them as shown to add Color2 as a Parameter option, (after saving, dual options will become available in the Properties window),

1
2
3
4
5
6
7
8
9
10
11
12
/*P_Foreign*/
 
Color = ParamColor("Color",colorRed);
 
Plot(C,"Close",Color,styleLine);
 
//Plot(C,"Close",colorBlack,styleLine);
PlotForeign("GM","GM",colorCustom2, styleLine);
 
Color2 = ParamColor("Color2",colorRed);
 
PlotForeign("DIS","DIS",Color2, styleLine);


b) use the Color-Picker to set Color2.

CustomColorExtensions010


All three plots will now be displayed with their own colors. (The first and third symbols are using unique colors that are not included in the Color-Picker or color favorites).


CustomColorExtensions011














1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4 out of 5)
Loading ... Loading ...

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


1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 2 out of 5)
Loading ... Loading ...

Colored Plots

article 1/5

The most used color, for chart plotting, is black, but this can be changed using simple AFL formulas.
This will be demonstrated in this article using the following code as a template.

(The template can be downloaded from the file attached to the end of this post and the lines commented/uncommented as required. Note that the attached file has been stripped of it’s capitals, and the underscore character, by the editing software used by the author. After downloading and saving the file rename it as written in this post).

1
2
3
4
5
6
7
/*P_PriceFields*/
 
Plot(C,"Close",colorBlack,styleLine);
//Plot(C,"Close",colorBlack);
//Plot(C,"Close",colorRed);
//color = ParamColor("color",colorRed); 
//Plot(C,"Close",color);


At the most fundamental level colors are customized from within formulas.

To create a black line price chart in a new pane:

a) open Formula Editor from the Tools Toolbar,

CustomColorsBasic003

b) copy/paste or type line 3 of the code into a new Formula Editor page,
c) check the syntax using the Verify Syntax tool from the Formula Editor Standard Toolbar before saving it as P_PriceFields,


CustomColorsBasic007


d) apply it as an indicator using the icon on the Formula Editor Standard Toolbar, (the chart will be opened in a new pane in the current window),


CustomColorsBasic005


Line view is the default for all charts, so it isn’t necessary to stipulate the styleName on every occasion.
The code can be shortened by leaving out the style setting, if Line is the preferred view.

To change the formula to line view as the default:

a) copy/paste line 3 of the code into line 4,
b) comment out line 3 by putting two forward slashes before the line (this line will then be ignored when the formula is processed),
c) delete styleLine from the line 4 code and save the new version (the result will be visible in the chart).


CustomColorsBasic008


(Note that when an indicator is applied from the Formula Editor toolbar the chart it opens interacts dynamically with the formula, so that the effect of changes to the formula can be immediately seen in the chart, once the formula has to be saved).

To change the color of chart plots:

a) copy line 4 of the code and paste it into line 5,
b) comment out line 4,
c) change colorBlack to colorRed in line 5.


CustomColorsBasic009


For convenience the color parameter can be accessed graphically by adding the Parameter function to the formula.

To add color to the Parameter options list:

a) comment out line 5,
b) add line 6 and 7 to the formula and Save,
c) right click inside the Chart and select Parameters from the context menu that appears, (the Properties window will open with the Parameter tabbed page in view),


CustomColorsBasic010


d) select a color from the Color-Picker that drops down, (the plot will then change color to match).


CustomColorsBasic013

(Note that whenever a formula, with the color parameter included, is inserted into a chart, the Parameter Window will open automatically and offer the user the choice of colors, for plotting, before proceeding).


Attached Files:


right click .afl files and Save Target As to download to the desktop at Program Files/AmiBroker/Formulas/Custom then access them as a formulas within AmiBroker Charts.

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3 out of 5)
Loading ... Loading ...

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










1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 2 out of 5)
Loading ... Loading ...

How Many Trading Days In A Year?

To count the number of trading days in a calendar year (for any equity market):

a) download the .afl file attached to this post and save it in Program Files/AmiBroker/Formulas/Custom,

b) open AmiBroker and display any index, for the market of interest, in a chart e.g. ^DJI;
c) find p-barsinyear in the Charts list and right click on it, then Insert it as an indicator, (it will be plotted in a new pane in the bottom section of the Chart window);
d) click in the Chart to position the Selector Line in the BarsInYear chart, just after the end of the year being measured (where the cumulative line becomes a constant in the upper section of the chart);
e) read the BarsInYear total from the Chart Title (highlighted in yellow).

(Note that this calculation requires daily bars and also that it is set to calculate the number of trading days for the last full year at the time of writing i.e. 2006. To calculate the number of trading days for a different year e.g. 2007, change the 106 to 107 in both of the DateNum() number sequences).

1
2
3
4
5
6
7
8
9
10
/*P_BarsInYear*/
 
InYearFlag = IIf(DateNum() >= 1060101 AND DateNum() <= 1061231,1,0);
InYear = Cum(InYearFlag);
Plot(InYear,"BarsInYear",colorBlack,styleLine);
 
// Logic.
// line 3 returns one if a bar is within the calendar range, or zero if not, and assigns it to the variable InYearFlag;
// line 4 cumulates the ones from line 3 and assigns the result to the InYear variable;
// line 5 plots the cumulated result in a chart;

BIY001

Attached Files:

Right click and Save Target As to save .afl files to the desktop at Program Files/AmiBroker/Formulas/Custom and access them as formulas within AmiBroker Charts.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

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

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
Loading ... Loading ...
« Previous Page