Using graph styles, colors, titles and parameters in Indicator Builder

AmiBroker provides customizable styles and colors of graphs in custom indicators. These features allow more flexibility in designing your indicators. This article will explain how to use styles and colors. It will also explain how to define chart title that appears at the top of the chart.

Let's begin with a list of special variable names available in Indicator Builder:

Variable Usage Applies to
title

defines title text

(new in 4.20):
If you use Title variable you have to specify colors in the string.
Colors can be specified using \\cXX sequence where XX is 2 digit number specifying color index \\c38 - defines violet, there is a special sequence \\c-1 that resets to default axis color.
For example

Title = "This is written in \\c38violet color \\c27and this in green";

You can also use new AFL function that makes it easier. Function is called
EncodeColor( colornumber ).
And you can write the above example like this:

Title = "This is written in " + EncodeColor( colorViolet ) + "violet color " + EncodeColor( colorGreen ) + "and this in green";

Multi-line caption is possible by simply embedding line break \n, for example:
Title = "This is 1st line\nThis is second line";

Indicator Builder
maxgraph specifies maximum number of graphs to be drawn in custom indicator window (default=3) Indicator Builder
graphN defines the formula for the graph number N (where N is a number 0,1,2,..., maxgraph-1) Indicator Builder

graphNopen,
graphNhigh,
graphNlow,

define additional O, H, L price arrays for candlestick and traditional bar charts Indicator Builder
graphNcolor

defines the color index of Nth graph line. Color indexes are related to the current palette - see Preferences/Color, there are also predefined color constants that are easier to remember than numeric values:

colorCustom1 = 0
colorCustom2 = 1
colorCustom3 = 2
colorCustom4 = 3
colorCustom5 = 4
colorCustom6 = 5
colorCustom7 = 6
colorCustom8 = 7
colorCustom9 = 8
colorCustom10 = 9
colorCustom11 = 10
colorCustom12 = 11
colorCustom13 = 12
colorCustom14 = 13
colorCustom15 = 14
colorCustom16 = 15

colorBlack = 16
colorBrown = 17
colorDarkOliveGreen = 18
colorDarkGreen = 19
colorDarkTeal = 20
colorDarkBlue = 21
colorIndigo = 22
colorDarkGrey = 23

colorDarkRed = 24
colorOrange = 25
colorDarkYellow = 26
colorGreen = 27
colorTeal = 28
colorBlue = 29
colorBlueGrey = 30
colorGrey40 = 31

colorRed = 32
colorLightOrange = 33
colorLime = 34
colorSeaGreen = 35
colorAqua = 35
colorLightBlue = 37
colorViolet = 38
colorGrey50 = 39

colorPink = 40
colorGold = 41
colorYellow = 42
colorBrightGreen = 43
colorTurquoise = 44
colorSkyblue = 45
colorPlum = 46
colorLightGrey = 47

colorRose = 48
colorTan = 49
colorLightYellow = 50
colorPaleGreen = 51
colorPaleTurquoise = 52
colorPaleBlue = 53
colorLavender = 54
colorWhite = 55

Indicator Builder
graphNbarcolor defines the array that holds palette indexes for each bar drawn - see the description below Indicator Builder
graphNstyle

defines the style of Nth graph. Style is defined as a combination (sum) of one or more following flags ( you can use predefined style__ constants instead of numbers)

styleLine = 1 - normal (line) chart (default)
styleHistogram = 2 - histogram chart
styleThick =4 - fat (thick)
styleDots = 8 - include dots
styleNoLine = 16 - no line
styleLog = 32 - semi-logarithmic scale
styleCandle = 64 - candlestick chart
styleBar = 128 - traditional bar chart
styleNoDraw = 256 - no draw (perform axis scaling only)
styleStaircase = 512 - staircase (square) chart
styleSwingDots = 1024 - middle dots for staircase chart
styleNoRescale = 2048 - no rescale
styleNoLabel = 4096 - no value label
stylePointAndFigure = 8192 - point and figure
(new in 4.20):
styleArea = 16384 - area chart (extra wide histogram)
styleOwnScale = 32768 - plot is using independent scaling
styleLeftAxisScale = 65536 - plot is using left axis scale (independent from right axis)

Not all flag combinations make sense, for example (64+1) (candlestick + line) will result in candlestick chart (style=64)

Note on candlestick/bar charts: these styles use indirectly O, H, L arrays in addition to graphN. So ordinary candlestick price chart formula is graph0=close; graph0style=64;.
But if you want to draw something else than close price you have to assign new values to predefined O,H,L arrays.

Indicator Builder
graphzorder GraphZOrder variable allows to change the order of plotting indicator lines. When GraphZOrder is not defined or is zero (false) - old ordering (last to first) is used, when GraphZOrder is 1 (true) - reverse ordering is applied. Indicator Builder

New graphNstyle and graphNcolor variables (where N is 0,1,2...) allow you to change the default plain line style. Let me show you some examples, here is how ADX/DMI chart defined in AFL would look like:

maxgraph = 3;
graph2 = pdi(14);
graph1 = mdi(14);
graph0 = adx(14);
graph2style = graph1style = styleLine;
graph0style = styleLine | styleThick;
graph1color = colorRed;
graph2color = colorGreen;
graph0color = colorBlue;
title=name() + " - +DM" + writeval( graph2 )+ ", -DM" + writeval( graph1 )+
", ADX" + writeval( graph0 );

The first line defines that we will use 3 graphs (this is the default so it is not really needed). Next three lines define the formulas for +DM, -DM and ADX graphs. Then we define that graph2 and graph1 (+DM and -DM) will have plain line style and ADX line (graph0) will have fat line style (1+4). Then we define the colors according to palette settings. AmiBroker uses a pallette that can be changed by user. Go to Tools->Preferences->Colors, click Palette Editor and take a look at "Custom colors" - these 16 color boxes display AmiBrokers pallette - the first box is represent color index 0, next one - 1 and so on. So if you write graph0color = 7 you tell AmiBroker to take the color with index 7 (this is the color with which Pallette editor's box number eight is painted). And now we are at the last line which defines the formula for the chart title. Instead of plain default <TICKERNAME> - <CustomIndicatorName> we can show something more informative. In this example we concatenate a string which contains ticker name and the values of +DM, -DM and ADX. If you assign any string to the title variable it will be displayed instead of the default one.

Next example is a Parabolic SAR chart drawn over traditional bar chart:

graph0 = close;
graph0style = styleBar;
graph0color = 2;
graph1 = sar();
graph1style = styleDots | styleNoLine;
graph1color = 8;
title = "This is my title: " + name() + " - SAR - " + writeval( graph1 );

In this example we plot SAR chart with only dots (graph1style = styleDots + styleNoLine). A price chart (graph0) is drawn with traditional bar chart style (graph0style=styleBar). In order to do that we have to assign a close price to graph0 variable. Note that for candlesticks and traditional bar charts AmiBroker will use indirectly O,H, L arrays (if graphNopen, graphNhigh, graphNlow are not defined in the formula), since more data is needed than one array.

As you already noticed you can combine styles by adding the values of style flags: 128+4 will result in thick traditional bar chart, while 1+8 will give you dotted line chart. Note that style=256 does not draw anything - instead AmiBroker takes graph formula, calculates it and performs chart scaling (in automatic scale mode). This is useful if you want to rescale the chart according to some array values that should not be plotted at all.

However, not all flag combinations make sense, for example (64+1) (candlestick + line) will result in candlestick chart (style=64).

Plot() function

Instead of using graphN variables you can use a new Plot function
Plot( array, name, color/barcolor, style = 1 ) - making writing custom indicators quicker.

An example, the following single function call plots an indicator with defined style/color:

Plot( Close, "Close", colorBlack, styleCandle);

so it replaces 4 statements:

graph0 = Close;
graph0name = "Close";
graph0color = 1;
graph0style = 64;

Color your bars!

You can easily plot multi colored charts using both Plot function and graph.. variables. All you need to do is to define array of color indexes. In the following simple example up bars are plotted with green and down bars are plotted with red color:

col = IIF( Close > Ref( Close, -1 ), colorGreen, colorRed );
Plot( Close, "Price", col, styleBar );

The same can be achieved using old-style graph... variables:

graph0barcolor = IIF( Close > Ref( Close, -1 ), colorGreen, colorRed );
graph0style = styleBar;
graph0 = Close;
graph0name = "Price";

The next example is essentially the same with the difference that we plot volume instead of the price chart:

col = IIF( Close > Ref( Close, -1 ), colorGreen, colorRed );
Plot( Volume, "Volume", col, styleHistogram | styleThick );

Hope you enjoy your charts in new colors!

PlotForeign() function

It is now easy to overlay price plots of multiple symbols using PlotForeign function:

PlotForeign( tickersymbol, name, color/barcolor, style = styleCandle | styleOwnScale, minvalue = {empty}, maxvalue = {empty} )

Plots the foreign-symbol price chart (symbol is defined by tickersymbol parameter). Second argument name defines graph name used for displaying values in a title bar. Graph color could be static (if third argument is a number) or dynamic (when third argument is an array). Color indexes are related to the current palette (see Preferences/Color)
style defines chart plot style (see Plot() function for possible values)

PlotForeign( "^DJI", "Dow Jones", colorRed );
PlotForeign( "^NDX", "Nasdaq 100", colorBlue );
PlotForeign( "^IXIC", "Nasdaq Composite", colorGreen );

Multiple plots using different scaling

Two new styles can be used to plot multiple graphs using different Y-scale: styleOwnScale and styleLeftAxisScale.

It also makes it easy to plot 2 or more "own scale" plots with the same scaling:

minimum = LastValue( Lowest( Volume ) );
maximum = LastValue( Highest( Volume ) );

Plot( Close, "Price", colorBlue, styleCandle );

/* two plots below use OwnScale but the scale is common because we set min and max values of Y axis */
Plot( Volume, "Volume", colorGreen, styleHistogram | styleThick | styleOwnScale, minimum, maximum );
Plot( MA( Volume, 15 ), "MA volume", colorRed, styleLine | styleOwnScale, minimum, maximum );

New style: styleLeftAxisScale = 65536 - allows to plot more than one graph using common scaling but different from regular (right axis) scale.
Example: price plot plus volume and moving average plot:

// Plot price plot and its moving average
Plot( Close, "Price", colorWhite, styleCandle );
Plot( MA( Close, 20 ), "MAC", colorRed );

// Now plot volume and its moving average using left-hand axis scaling
Plot( Volume , "Volume", colorBlue, styleLeftAxisScale | styleHistogram | styleThick );
Plot( MA( Volume,15), "MAV", colorLightBlue, styleLeftAxisScale );

New parameters make it also easy to plot ribbons, for example:

Plot( Close, "Price", colorBlue, styleCandle );
Plot( 2, /* defines the height of the ribbon in percent of pane width */
"Ribbon",
IIf( up, colorGreen, IIf( down, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );

Using custom defined parameters

New functions:

make it possible to define your own parameters in your indicators. Then right click over chart pane and select "Parameters" or press Ctrl+R, and change them via Parameters dialog and get immediate response.

The simplest case looks like this:

period = Param("RSI period", 12, 2, 50, 1 );
Plot( RSI( period ), "RSI( " + WriteVal( period, 1.0 ) + ") ", colorRed );

Right click over the chart and choose "Parameters" and move the slider and you will see RSI plotted with different periods immediatelly as you move the slider.

Sample code below shows how to use ParamStr to get the ticker symbol.

ticker = ParamStr( "Ticker", "MSFT" );
sp = Param( "MA Period", 12, 2, 100 );
PlotForeign( ticker, "Chart of "+ticker, ParamColor( "Price Color", colorLightYellow ), styleCandle );
Plot( MA( Foreign( ticker, "C" ), sp ), "MA(" + WriteVal( sp, 1.0 ) + ")", ParamColor( "MA Color", colorRed ) );

Code below is a little bit more complex and shows how to use ParamColor function to get the color selection.

sp = Param( "RSI Period", 12, 2, 100 );
r = RSI( sp );
Plot( r, "RSI("+WriteVal(sp,1.0)+")", ParamColor("RSI Color", colorRed ) );

Buy = Cross( r, 30 );
Sell = Cross( 70, r );

PlotShapes( shapeUpArrow * Buy + shapeDownArrow * Sell, IIf( Buy, colorGreen, colorRed ) );

The following sample formula (from AmiBroker mailing list) that allows to visually align price peak/troughs with sine curve on the chart:

Cycle = Param("cycle mths",12,1,12,1)*22;//264==12mth,22==1mth
xfactor = Param("stretch",1,0.1,2,0.1);//1==1yr,2==2yr
xshift = Param("slide",0,-22,22,2)/3.1416^2;//slide curve 1==5days

x = 2*3.1416/Cycle/xfactor;
y = sin(Cum(x)-xshift);

Plot(C,"Daily Chart", colorBlack,styleCandle|styleNoLabel);
Plot(y,"cycle = "+WriteVal(Cycle*xfactor/22,1.0)+" months",colorBlue,styleLine|styleNoLabel|styleOwnScale);

Right click over the chart and choose "Parameters" and move the sliders and you will see chart immediatelly reflecting your changes.

The code shown below provides an easy way to get relative-performance chart and thanks to ParamStr() function it is easy to change the tickers being compared.

startpoint = Status("barvisible");
startpoint = startpoint - Ref( startpoint, -1 );

price = Close;
Plot( 100 * ( price/ValueWhen( startpoint, price ) - 1 ), Name(), colorRed, styleThick );

ticker = ParamStr("Ticker 1", "AMD" );
price = Foreign( ticker, "C");
Plot( 100 * ( price/ValueWhen( startpoint, price ) - 1 ), ticker, colorBlue );
ticker = ParamStr("Ticker 2", "MSFT" );
price = Foreign( ticker, "C");
Plot( 100 * ( price/ValueWhen( startpoint, price ) - 1 ), ticker, colorGreen );
ticker = ParamStr("Ticker 3", "IBM" );
price = Foreign( ticker, "C");
Plot( 100 * ( price/ValueWhen( startpoint, price ) - 1 ), ticker, colorBrown );
GraphXSpace = 3;