# amibroker

### How to show price ratio between two symbols

Charting ratios between the prices of two symbols can easily be done with AmiBroker. For this purpose we can use Spread built-in formula available in Charts window:

Then select the style as Ratio:

The primary symbol (Symbol1) is the one selected in the chart window, the other symbol (Symbol2) is defined in the Parameters window, as presented in the above screenshot.

It is also possible to create such ratio chart programmatically in AFL using the following code:

`ratio = Foreign("Symbol1", "C") / Foreign("Symbol2", "C");Plot( ratio, "ratio", colorRed )`

Symbol1 and symbol2 names in the above code need to be replaced with the actual symbol names from our database we want to use.

### How to read highest high value of future bars

Built in HHV and LLV functions allow to read highest high or lowest low of n-past bars. If we want to refer to future values, there is an easy way to do it using simple Ref function and just shift HHV or LLV reading from N-bars ahead. A ready to use function showing such approach is presented below:

`// function definitionsfunction futureHHV( array, periods ){   return Ref( HHV( array, periods ), periods );}function futureLLV( array, periods ){   return Ref( LLV( array, periods ), periods );}// sample usePlot( Close, "Close", colorDefault, styleBar );Plot( HHV( H, 20 ), "HHV", colorGreen, styleDashed );Plot( futureHHV( H, 20 ), "Future HHV", colorGreen, styleThick );Plot( LLV( L, 20 ), "LLV", colorRed, styleDashed );Plot( futureLLV( L, 20 ), "Future LLV", colorRed, styleThick )`

And here is the chart produced by the formula above:

### How to fill background between hand-drawn trend lines

Among built-in drawing tools, the Triangle, Rectangle and Ellipse allow to fill the background with custom color. However, if we wanted to fill the space between manually drawn trend lines, then we could use AFL formula with Study function that allows to detect the position of the line. Then – knowing the arrays of top and bottom lines we could fill the area between with a cloud plot.

A sample formula, which shows such implementation is presented below. The code fills the space between L1 and L2 trendlines (red color) and between upper and lower bands of the regression channel (RU and RL study id’s respectively).

`// regular price plotPlot( Close, "Close", colorDefault, styleBar );// custom function definitionfunction FillSpace( ID1, ID2, color ){   // get current chart ID   chartID = GetChartID();   // read the positions of the lines   l1 = Study(ID1, chartID );   l2 = Study(ID2, chartID );   // draw cloud chart   PlotOHLC( l1, l1, l2, l2, "", ColorBlend(color, GetChartBkColor() ), styleCloud|styleNoRescale|styleNoLabel, Null, Null, 0, -1 );}// call function and refer to the assigned study ID'sFillSpace( "L1","L2", colorRed );FillSpace( "RU","RL", colorBlue )`

The chart produced by the formula looks as follows:

We need to remember that each line needs to have unique Study ID assigned in the Properties window.

In case of regression channel the ID’s of the upper and lower lines are defined in Regression Channel tab:

If we wanted to handle more lines, then it may be more practical to process the list of study ID’s defined in a custom string instead of individual function calls.

`// regular price plotPlot( Close, "Close", colorDefault, styleBar );// custom function definitionfunction FillSpace( ID1, ID2, color ){   // get current chart ID   chartID = GetChartID();   // read the positions of the lines   l1 = Study(ID1, chartID );   l2 = Study(ID2, chartID );   // draw cloud chart   PlotOHLC( l1, l1, l2, l2, "", ColorBlend(color, GetChartBkColor() ), styleCloud|styleNoRescale|styleNoLabel, Null, Null, 0, -1 );}function BulkFill( IDlist, color ){    for( i = 0; ( ID1 = StrExtract( IDlist, i ) ) != ""; i += 2 )    {      ID2 = StrExtract( IDlist, i+1 );      FillSpace( ID1,ID2, color );    }}// call function and refer to the assigned study ID'sBulkFill( "L1,L2,RU,RL,R1,R2", colorRed )`

### Automatic support and resistance lines based on last HHV and LLV value

In this example we will present a method to plot automatic support and resistance lines based on recently hit highs/lows. In order to draw horizontal lines showing levels of recent HHV/LLV values and start drawing at the point of the chart, which defines given price level – we first need to identify the bar, where the line should start. This can be done with use of HHVBars / LLVBars functions.

These functions allow to find out the number of bars that have passed since our support or resistance level was established, so we could prevent from drawing the lines before these points.

This example shows how to draw support and resistance lines based on HHV/LLV levels:

`// define reusable functionfunction SupResLevels( bars, colorUp, ColorDn ){   bi = BarIndex();   lvbi = LastValue( bi );   // return HHV value only for bars starting from the bar where HHV level was established   hv = IIf( bi >= lvbi - LastValue( HHVBars( High, bars ) ), LastValue( HHV( High, bars ) ), Null );   // the same approach for LLV   lv = IIf( bi >= lvbi - LastValue( LLVBars( Low, bars ) ), LastValue( LLV( Low, bars ) ), Null );   // plot levels   Plot( hv, "hv", colorUp, styleDashed );   Plot( lv, "lv", ColorDn, styleDashed );}// price plotPlot( Close, "Close", colorDefault, styleBar );// call function with various parametersSupResLevels( 10, colorGreen, colorRed );SupResLevels( 50, colorGreen, colorRed )`

The chart below shows the output produced by the formula:

### How to execute part of the formula only when new bar is added

In realtime conditions we may be interested in executing some parts of our formula only once per bar, when a new bar is created (e.g. for auto-trading purposes or just for notification). To do that – we would need to identify the very moment when new bar appears.

This can be done using static variables to record the timestamp of the most recent bar, then comparing current reading with the recorded value. Once the difference is detected – we can conditionally run our code and update the recorded time info.

Such an approach will work if we use timestamps that don’t change with each tick, so preferred option is to use Start Time of Interval for timestamp display (for daily and higher intervals we should unmark “override” box):

Then we can use the following code (this sample formula will just play a ding.wav system sound when the new bar is detected):

`// read last bar date/timelastBartime = LastValue( DateTime() );// we use per-symbol variable// you may consider to add GetChartID() key if you want// to use the formula in multiple charts shown at the same timevarName = Name() + "_lastdt";// read recorded date/time from last executionrecordedTimestamp = Nz( StaticVarGet( varName ) );// code runs conditionally only when new bar is detectedif( lastBarTime != recordedTimestamp ){    // record new bar datetime    StaticVarSet( varName, lastBartime );    //////////////////////////////////////    // main code here    PlaySound( "c:\\windows\\media\\ding.wav" );    //////////////////////////////////////}// sample indicator codePlot( Close, "Close", colorDefault, styleBar )`

Newer AmiBroker versions (>5.60) can use this for reading last bar timestamp (this is faster than using DateTime() function).
`lastBartime = Status("lastbarend")`

### How to increase maximum periods of built-in indicators

Built-in indicators and averages which are shipped with AmiBroker use Param() function calls to provide the ability to adjust parameter values through Parameters window. Param function in the code specifies default, minimum, maximum values for the input arguments.

The order of arguments in Param function is the following:

`Param( "name", defaultval, min, max, step, sincr = 0 `

In certain situations, we may however want to use larger period settings than the pre-defined maximum. There is an easy way to adjust the code to achieve such task. Let us consider using built-in Price (all in one) indicator and setting e.g. 200 or 300 periods for Bollinger Bands (default maximum is 100).

To modify the underlying code, we need to:

1. Click on the chart with right mouse button and choose Edit Formula from the context menu to bring up the AFL code editor
2. In the code identify Bollinger Band section and the Param function call responsible for setting number of periods and change it from 200 to 300 as shown in the picture below.

3. Approve the changes, by selecting Tools->Apply from the editor’s menu

Now we can go back to Parameters dialog and we will be able to set Bollinger Bands Periods setting up to 300 periods.

### How to browse charts in selected date range

In order to select certain range of dates in the historical chart, then scroll through the history of tickers, we can mark the range of dates we interested in using Range Markers (double-click on the chart or mark the bars and hit F12, SHIFT+F12).

Then, use View->Zoom->Range menu to zoom the chart to our range selection.

We can also assign a keyboard shortcut to View->Zoom->Range command or create a custom toolbar button to have it easily accessible (e.g. CTRL+SHIFT+R). This can be done in Tools->Customize (http://www.amibroker.com/guide/h_customizeui.html)

After we switch the symbol, in situations when the chart shifts e.g. due to different history length in those symbols – all we have to do is to hit CTRL+SHIFT+R to bring back the view to the range we wanted.

Browsing through the list of symbols can be automated further with scripts. Below is a sample auto-play script that will automatically browse through the list of symbols and will set the zoom to the dates we set in the code. All we need is just to start the script and watch it all switching automatically, without any manual actions required.

Here is a sample script that will browse through the list of symbols and set the zoom to show year 2010:

`iWatchList = 0; /* you can define watch list number here */AB = new ActiveXObject( "Broker.Application" );Qty = AB.Stocks.Count;for ( i = 0; i < Qty; i++ ){    Stk = AB.Stocks( i );    if ( iWatchList < 32 )    {        if ( Stk.WatchListBits & ( 1 << iWatchList ) )        {            AB.ActiveDocument.Name = Stk.Ticker;            AW = AB.ActiveWindow;            AW.ZoomToRange( "2010-01-01", "2011-01-01" );            WScript.Sleep( 2000 ); // 2 seconds delay        }    }    else    {        if ( Stk.WatchListBits2 & ( 1 << ( iWatchList - 32 ) ) )        {            AB.ActiveDocument.Name = Stk.Ticker;            AW = AB.ActiveWindow;            AW.ZoomToRange( "2010-01-01", "2011-01-01" );            WScript.Sleep( 2000 ); // 2 seconds delay        }    }`

To use above code follow these steps:

2. Copy-paste above the code
3. Save the file with .JS extension (which means that system will treat this as JScript code)
4. Make sure that AmiBroker is running with desired chart as active one
5. Double click on .JS file to execute the JScript code

IMPORTANT: if you are running 64-bit Windows and have BOTH 32-bit and 64-bit versions of AmiBroker installed the OLE scripts by default would only talk to 64-bit instance. To use 32-bit version instead you would need to follow advice given in this article: http://www.amibroker.com/kb/2015/01/12/ole-automation-scripts-with-32-and-64-bit/

It is worth noting that going through series of charts is not the only way to compare performance of various symbols over time. There are other dedicated built tools for comparing several securities (without switching symbols, but within one chart window), including Price (Foreign) and Relative Performance indicator. These functionalities are shown in the following video:
http://www.amibroker.com/video/TwoSymbolsOneChart.html

### How to move a window to another monitor

By default document windows like Charts, Analysis, Account Manager, Web Research all open inside of main AmiBroker frame window. In multi-monitor setups it may be useful however to move some of them to another screen.

Let us say we want to move Analysis window to second monitor screen. This can be done by switching the window to special “Floating” mode by using Window->Floating option from the menu after opening Analysis.

Using floating mode detaches Analysis from the main frame and then the window can be moved outside of it.

Exactly the same procedure works for Chart windows, Account Manager or Web Research windows.

The procedure (for chart window) has also been shown in this video:

### Positioning area plots behind the grid lines

When we want to paint the background with custom colors to indicate certain states or conditions – we can use area plots style for this purpose. The code example presented below shows green background when Close stays above 50-period moving average and red when below MA-50.

`Plot( Close, "Close", colorDefault, styleThick);Plot( MA50 = MA(Close,50), "MA50", colorBlue, styleThick);color = IIf( Close > MA50, colorGreen, colorRed );Plot( 1, "", color, styleArea|styleOwnScale,0,1 )`

However – by default both grid lines and the selector line would get covered by the area plot:

There is an easy fix for that – AmiBroker allows to specify the Z-axis position too, so we can shift the visibility and order of plots (including their position against grids and other elements) by means of Z-order argument of Plot function.

If we specify the Z-order argument to -1 that means we move the particular plot one level behind and this would also be located below the grids.

`Plot( Close, "Close", colorDefault, styleThick);Plot( MA50 = MA(Close,50), "MA50", colorBlue, styleThick);color = IIf( Close > MA50, colorGreen, colorRed );Plot( 1, "", color, styleArea|styleOwnScale,0,1,0,-1 ); // the 8th argument specifies z-orde`

`_N( TickerList = ParamStr( "Tickers", "^DJI,MSFT,GE" ) );fontsize = Param("Label font size", 10, 4, 30, 1 );fvb = Status( "firstvisiblebar" );for( i = 0; ( symbol = StrExtract( Name() + "," + TickerList, i ) ) != ""; i++ ){    fc = Foreign( symbol, "C" );    if( ! IsNull( fc[ 0 ] ) )    {        relP = 100 * ( fc - fc[ fvb ] ) / fc[ fvb ];        Plot( relP , symbol, color = colorLightOrange + ( ( 2 * i ) % 15 ), styleLine );        x = LastValue( BarIndex() ) + 1;        y = LastValue( relP );        PlotTextSetFont( symbol, "Arial", fontsize, x, y, GetChartBkColor(), color, -fontsize/2 );    }}PlotGrid( 0, colorYellow );_N( Title = "{{NAME}} - Relative Performance [%]: {{VALUES}}" )`