amibroker

HomeKnowledge Base

Deleting symbols with comma in the name

When importing symbols into the database, we may sometimes encounter situations, when as a result of user-mistake we import erroneous ticker names into our database. This may for example happen when we specify an incorrect column separator in ASCII importer (or use incorrect import definition that does not match the imported file) or when we use input file that contains commas when importing watchlist members using Symbol->Watchlist->Import.

As a result – we may end up with a ticker list like this:

Problematic symbols

In this case marking the symbols in Symbols window and using Delete option from the context menu will not work, because AmiBroker treats the comma as a separator between symbols.

To solve the problem – with relatively few tickers we can always just open Symbol->Information window and fix the names in there. However – with a larger group of symbols it will not be very practical.

In such case, in order to delete the incorrect symbols from the database – we can use Symbol->Organize Assignments window, mark the tickers in left-hand-side panel and press Delete to remove these symbols.

Removing problematic symbols

There is also a way to delete the symbols manually from the database folder by removing respective data-files. This requires the following steps:

  1. Exit AmiBroker
  2. go to respective subfolder of the database folder (in Windows Explorer)
  3. delete data-files for the particular symbols
  4. delete broker.master file from the database folder (it stores the symbol list)
  5. restart AmiBroker and load the database

Drawing line extensions on future bars using AFL

AmiBroker allows to display the AFL-based chart output on the future blank bars area with use of XSHIFT argument of the Plot function. This functionality allows to move the particular chart by certain number of bars and place the output within the blank bars area (provided that we use positive value for XSHIFT, i.e. we are shifting the chart to the right).

The following code shows price chart with 20-period MA overlay and additionally – with the same 20-period MA shifted to the right.

PlotClose"Close"colorDefaultstyleBar );

PlotMAClose20 ), "MA-20"colorRedstyledashed );
PlotMAClose20 ), "MA-shift"colorRedstyleThickNullNull10)

Chart with XShift

However – there may be some situations where we not only want to shift the chart position, but actually calculate the position of the line on the blank bars, for example if we are producing an extension of the existing indicator line.

Let us consider a simple example, which draws a line connecting the last record of the input array with the value 50-bars ago, using LineArray() function.

The code is the following:

inputArray Close;
PlotinputArray"input"colorDefaultstyleDots );

bi BarIndex();
lvbi LastValuebi );
x0 lvbi 50;
x1 lvbi;
y0 inputArraylvbi 50 ];
y1 inputArraylvbi ];

PlotLineArrayx0y0x1y1True True ), "line"colorRedstyleThick )

and the output looks like this:
Chart with XShift

LineArray function allows to calculate the extension automatically if we set EXTEND argument to True, however – all the calculations in AFL language are performed within the ‘real bars’ area, i.e. on the available elements of the array, between array item 0 until array item (Barcount-1).

To calculate and display the values that extend past the very last bar available in the array we will use technique explained below:

  1. first we shift the input back (to the left) by N bars, so the real input data would occupy earlier part of the array and we would have extra bars at the end for the calculation of extended arrays
  2. now we calculate the position of arrays on such shifted
  3. shift the displayed output forwards with XSHIFT functionality of Plot function (so the calculated extensions would get aligned onto the blank bars as a result).

If our original input array contains 200 bars and the line was drawn between bar 150 and the bar 200, then our aim is to shift the array that way, so the line would occupy the bars between bar 140 and bar 190, while the remaining 10 bars at the end of the array could be used for calculating the extended part of the line. Then – using XSHIFT we could place the array back into its original position.

inputArray Close;
PlotinputArray"input"colorDefaultstyleDots );

// here we shift the input array to the left
shift 10;
inputArray RefinputArrayshift );

// calculations of the x-y coordinates of the LineArray
// take into account the fact that array has been shifted
bi BarIndex();
lvbi LastValuebi );
x0 lvbi 50 shift;
x1 lvbi shift;
y0 inputArraylvbi 50 shift ];
y1 inputArraylvbi shift ];

// lineArray shifted back to original (correct) position with XSHIFT
PlotLineArrayx0y0x1y1TrueTrue ), "line"colorRedstyleThickNullNullshift );

//positions that were used for calclations before using xshift
PlotinputArray"input shifted"colorLightGreystyleDashed );
PlotLineArrayx0y0x1y1TrueTrue ), "line shifted"colorRedstyleDashed )

Chart with XShift

Dashed lines in the above chart show the shifted positions of the input array and calculated LineArray before using XSHIFT (i.e. on the bars that were used for actual calculations)

How to delete quotes without removing the symbol from a database

In order to delete quotations from a local database manually, we can use Quotations Editor (Symbol–>Quote Editor), then mark the required range of quotes and press Delete button. To mark a range – it is enough to click on the first line of the range, then scroll to the other line, hold SHIFT and click on the end-line of the range. To multi-select individual lines, hold down CTRL key while clicking on the lines.

Delete quotes

There is also a way to delete quotations programmatically with use of OLE automation interface explained here:

http://www.amibroker.com/guide/objects.html

The following code presents how to do it using automation scripts (the code deletes all quotations of MSFT ticker):

// THIS IS NOT AFL
// This is Windows script to be run from the outside of AmiBroker
function RemoveAllQuotes( Name )
{
    AB = new ActiveXObject("Broker.Application");
    Stk = AB.Stocks( Name );
    Quotes = Stk.Quotations;
    iQty = Quotes.Count;
    for( i = iQty - 1; i >= 0; i-- )
    {
       Quotes.Remove( i );
    }
    AB.RefreshAll();
}
RemoveAllQuotes("MSFT");
WScript.Echo ( "Completed" );

The code above is intended to be used from the outside of AmiBroker.

To use above code follow these steps:

  1. Open Notepad
  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: “Running OLE automation scripts with 32- and 64-bit versions of AmiBroker”

Using the very same method you can delete quotes selectively, for example the script below deletes only quotes having zero volume:

// THIS IS NOT AFL
// This is Windows script to be run from the outside of AmiBroker
function RemoveQuotesWithZeroVolume( Name )
{
     AB = new ActiveXObject("Broker.Application");
     Stk = AB.Stocks( Name );
     Quotes = Stk.Quotations;
     iQty = Quotes.Count;
     cnt = 0;
     for( i = iQty - 1; i >= 0; i-- )
     {
        qt = Quotes.Item( i );
        if( qt.Volume == 0 ) 
        { 
           cnt++;
           Quotes.Remove( i );
        }
     }
    
     AB.RefreshAll();

     return cnt;
}
n = RemoveQuotesWithZeroVolume("MSFT");
WScript.Echo ( "Removed " + n + " quotes with zero volume" );

Running OLE automation scripts with 32- and 64-bit versions of AmiBroker

Automation scripts written in JScript or VBScript are supposed to be run from Windows Explorer to control AmiBroker from outside and run certain predefined tasks specified in the script. The recommended documentation explaining the automation interface and AmiBroker object model is presented here:
http://www.amibroker.com/guide/objects.html
http://www.amibroker.com/newsletter/01-2000.html

In case of any problems with execution of automation scripts, the very first thing to check is the .js (JScript) or .vbs (VBScript) file association within the Windows Control Panel. This determines which program in Windows executes the files with .js extension. By default that should be Windows Scripting Host, which is the default scripting engine integrated into Windows.

The following Microsoft help resources show how to change the file associations:
http://windows.microsoft.com/en-us/windows/change-default-programs#1TC=windows-7
http://windows.microsoft.com/en-us/windows-8/choose-programs-windows-uses-default

The other thing to remember is the fact that in 64-bit Windows there are actually two JavaScript engines, 32- and 64-bit. The location of those programs are:

32-bit
C:\Windows\SysWOW64\CScript.exe
64-bit
C:\Windows\System32\CScript.exe

If only one AmiBroker version installed, Windows would launch this installed version regardless of which scripting engine was used.

Things look differently if you installed both 32-bit and 64-bit versions of AmiBroker on the very same machine. In this case 32-bit scripting engine would open 32-bit AmiBroker while 64-bit scripting engine would open 64-bit AmiBroker.Therefore, if we want to specifically launch 32-bit AmiBroker, then pointing at the particular engine by using command line may be required:

C:\Windows\SysWOW64\CScript.exe Filename.js

(this one is for 32-bit version, the path names are in fact quite confusing, since Microsoft uses System32 folder for 64-bit programs in a 64-bit system).

Timestamps explained

When AmiBroker is fed with the data, say 1-minute data, it can create all other time intervals by compressing source data on-the-fly. So if you display say 13-minute chart, AmiBroker takes source 1-minute data and builds 13-minute blocks of data to create 13-minute bars. For this process to work correctly, source data need to have timestamps that point to the START of each bar interval. So with 1-minute data, the bar that has a timestamp 9:30:00 is supposed to cover trades from the period of 9:30:00.000 upto 9:30:59.999. All our data plugins provide data in that format.

Now, provided that we have say 1-minute data, AmiBroker can compress data to any other N-minute interval. When doing so, it can assign timestamps to compressed bars in different ways. This can be controlled through Tools->Preferences->Intraday dialog.

Timestamps

Let us check it on an example of a 5-minute bar based on input 1-minute quotes for e-mini contract.

Timestamps

As explained in the manual (http://www.amibroker.com/guide/w_preferences.html) – there are four choices available:

  1. Time of FIRST tick inside bar – when selected the bar gets the time stamp of the very first trade inside given time slot (bar). With this choice the bar will be stamped with 9:30:00 because this is the first tick (quote) available within that 5-min period
  2. Time of the LAST tick inside bar – when selected the bar gets the time stamp of the very last trade inside given time slot (bar). In this case the bar will be stamped with 9:34:00 because this is the last quote available within that 5-min period
  3. START time of the interval – when selected the bar is time-stamped with start time of the time slot (bar). The bar will be stamped with 9:30:00 because that is a beginning of the selected time period.
    NOTE: This is recommended and the default setting as it provides consistency with how source bars are timestamped. It should not be changed unless you have really good reason to do so.

  4. END time of the interval – when selected the bar is time-stamped with start time of the time slot (bar). The bar will be stamped with 9:34:59 timestamp, because that’s the very end of this 5-min period.

There is also an additional setting available (Override: Weekly/monthly bars use day of last trade), which allow to modify the behaviour in case of Weekly/Monhtly bars, no matter what is the main setting we use. This allows us to e.g. use START time of interval to identify intraday quotes, however – on a weekly chart display e.g. Wednesday date (if that is most recent day in current week) or Friday date for complete weeks.

We need to remember that the timestamps identify the whole bar and all trades within that bar, so if we use START time of interval for time-stamping, in the backtest use Close array for as BuyPrice and 5-minute periodicity, then in our report we will see:

Timestamps

So, we see the time 9:30:00, but this bar refers to trading activity from period 9:30:00-9:34:59 and the actual price is read from the tick being the Close of the whole 5-minute period (at 9:34:00 in the table above).

For the same reason – when we use weekly data for backtesting, we trade at Open, but for time-stamps we use Override box (so weekly bars are stamped with the data of the last day within given week) – then in the report we will see e.g. Friday dates because of the fact that we use such approach to time-stamp bars. This does not really mean that trade happened on Friday, but only that we use Friday date to identify the whole Monday-to-Friday week.

How to display interest gains in the backtest report

The default backtest report shows total Net Profit figure, which includes both trading profits and interest earnings. With Custom Backtest procedure we can easily isolate these components by summing up profits and loses from individual trades, then subtracting trading gains from the Net Profit and report them as separate metrics.

SetCustomBacktestProc"" );

if ( 
Status"action" ) == actionPortfolio )
{
    
bo GetBacktesterObject();
    
bo.Backtest(); // run default backtest procedure

    // read Net Profit, Winners and Losers profits from the report
    
st bo.GetPerformanceStats);
    
netProfit st.GetValue"NetProfit" );
    
tradeProfits st.GetValue("WinnersTotalProfit") + st.GetValue("LosersTotalLoss");

    
bo.AddCustomMetric"Trading profits"tradeProfits );
    
bo.AddCustomMetric"Interest earnings"netProfit tradeProfits );

}

// trading rules here
Buy CrossMACD(), Signal() );
Sell CrossSignal(), MACD() )

After backtest is run, we can see our custom metrics in the backtest report.

More information about creating custom metrics can be found in the manual:
http://www.amibroker.com/guide/a_custommetrics.html

FastTrack data configuration and troubleshooting

General configuration process for FastTrack datasource is explained in the manual:
http://www.amibroker.com/guide/h_extsources.html

Sometimes however, after the configuration process the FastTrack data source is still missing from the list of sources in File->Database Settings. If that happens, please follow the steps listed below to make this source available:

First you need to make sure that you are using 32-bit version of AmiBroker as FastTrack is 32-bit application and only the other 32-bit application can use its data via their API. To check what version of AmiBroker you have go to Help->About window. If you do not have proper version, please download 32-bit one from http://www.amibroker.com/download.html

Secondly you need to install FastTrack for the Web (FT4Web) program. If you are using Windows Vista, Windows 7 or Windows 8, it is good idea to first turn OFF User Access Control (down to “Never notify”). The following video shows the process:
http://windows.microsoft.com/en-US/windows7/Turn-User-Account-Control-on-or-off. Then install FastTrack for the web (FT4Web) program.

If the FastTrack datasource is still missing from the data source combo in Database Settings, then it is necessary to check if:

  1. FT.DLL is inside “AmiBroker/Plugins” folder
  2. FastTrack.DLL file is installed by FT4Web inside Windows folder

If it can not be found in the Windows folder, we need to make sure that FastTrack for the web is installed, then perform a search for FastTrack.DLL file using Windows Explorer file search.

Once the FastTrack.DLL file is found, we need to copy it to AmiBroker main folder – then FastTrack datasource should then become available in AmiBroker.

Using optimum parameter values in backtesting

After Optimization process has found optimum values for parameters of our trading system, typically we want to use optimum values in subsequent backtesting or explorations. In order to achieve that, we need to manually update default_val (second) argument of Optimize function with the values obtained from the optimization report.

The arguments of Optimize function are shown below (note second parameter marked in dark red color – this is the default value parameter we will be changing after optimization run):

some_var = Optimize( "description", default_val, min_val , max_val, step );

Let us consider the following example formula used for optimization process:

SetOption("ExtraColumnsLocation"1);
periods Optimize"Periods"2550); // note that default value is 2
Level Optimize"Level"22150); // note that default value is 2

Buy CrossCCIperiods ), -Level );
Sell CrossLevelCCIperiods ) )

If we perform Optimization process and check the results (for this example we use Net Profit as the optimization target), we can see that the best results use Periods = 6 and Level = 126.

Optimization result

Now in order to run backtest and obtain exactly the same results as in the respective line of the above Optimization results, we need to enter the values into default argument, so the modified code will look like this:

SetOption("ExtraColumnsLocation"1);
periods Optimize"Periods"6550); // we changed default value to 6
Level Optimize"Level"1262150); // we changed default value to 126

Buy CrossCCIperiods ), -Level );
Sell CrossLevelCCIperiods ) )

Now we can use the code with modes other than Optimization and the formula will use optimized values we retrieved from the results.

Drawing indicators on a subset of visible bars

By default, the Plot function draws the graph for all visible bars. In some situations however, we may want to draw some selected bars, leaving remaining chart space unaffected.

To achieve that – we simply assign Null value for the bars that we want to skip. Our graph will just be drawn for the non-null bars.

This simple example draws candlesticks only on Mondays and leaves empty all the other days.

IsMonday DayOfWeek() == 1;
// assign Close for Mondays, otherwise assign Null
Data IIfIsMondayCloseNull );
// plot the data
PlotData"Chart of Mondays"colorDefaultstyleCandle )

The following example shows how to restrict the visibility to last N bars. The code defines a custom function, which can be called later on for the arrays we want to show only partially.

// custom function definition
function LastNBars( array, bars )
{
    
bi BarIndex();
    
lvbi LastValuebi );

    
// use Null value for bars other than last N
    
return IIfbi lvbi bars, array, Null );
}

// price plot
PlotClose"Close"colorDefaultstyleBar );

// MA-50 restricted to last 10-bars only
line MAClose50 );
PlotLastNBarsline10 ), "last 10 bars"colorRed );

// shaded area
PlotLastNbarsTrue10 ), ""colorYellowstyleArea|styleOwnScale|styleNoLabel010, -)

Draw chart only for last N bars

In the above chart both Moving average (red line) and yellow shading area have been restricted to last 10-bars only.

In a similar way we can restrict the visibility to most recent day only in intraday chart:

// custom function definition
function ShowLastDay( array )
{
    
dn datenum();
    
lastDay dn == LastValuedn );

    return 
IIflastDay, array, Null );
}

// price plot
PlotClose"Close"colorDefaultstyleBar );

// daily high / low on last day only
dailyH TimeFrameGetPrice("H"inDaily );
dailyL TimeFrameGetPrice("L"inDaily );
PlotShowLastDaydailyH ), "dailyH"colorGreenstyleThick  );
PlotShowLastDaydailyL ), "dailyL"colorRedstyleThick  );

// shaded area
colorPaleYellow ColorBlend(colorWhitecolorYellow0.1);
style styleArea styleOwnScale styleNoLabel;
PlotShowLastDay), ""colorPaleYellowstyle010, -)

Draw chart only for last day

Other practical implementations of such technique is presented in these formulas:
http://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/
http://www.amibroker.com/kb/2014/10/10/how-to-draw-regression-channel-programatically/

How to increase font size for UI elements

In order to change / increase font size used for AmiBroker user interface elements, we can choose one of the following solutions:

  1. Set higher DPI in Windows Control Panel, or
  2. Set font size for individual UI elements such as list views using Windows Control Panel,

To set higher DPI in Windows Control Panel, follow the instructions below.

If you are using Windows 7 or Windows 8:

Open Screen Resolution by clicking the Start button, clicking Control Panel, and then under Appearance and Personalization, click Adjust screen resolution. Then click Make text and other elements larger or smaller

Control Panel

Now set the size of fonts choosing Medium or Larger in order to increase fonts globally.

Control Panel

For Windows XP – please follow the Microsoft documentation presented here:
https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/display_use_large_or_small_fonts.mspx?mfr=true

It is also possible to avoid changing all fonts globally and enlarge fonts e.g. for list views only. This can be done by adjusting the default icon system font in Control Panel.

If you are using Windows 8:

the Icon font can be changed in the same dialog as DPI settings discussed previously:

Control Panel

If you are using Windows 7:

In order to access the respective dialog – click on the desktop area with right-mouse button, click Personalize in the context menu, click on Window Color, then click on Advanced appearance settings option at the bottom.

Alternatively, navigate to Control Panel and in the search field in the top-right corner enter ‘window color’ – then pick Change window colors and metrics from the search results.

Then adjust Icon font in the Window Color and Appearance window as shown below:

Window Color and Appearance

For Windows XP – the procedure to get to the dialog is explained here:
http://windows.microsoft.com/en-us/windows-xp/help/setup/personalize-windows-xp

Finally, if we just want to change fonts used by the charts, i.e. X/Y axis, Title and text tool font – it is definable directly in Tools->Preferences->Miscellaneous, Axis font field.

If we want to change AFL Formula Editor font, it can be set in Tools–>Preferences–>Editor page.

« Previous PageNext Page »