Too Many Custom Trade Metrics?

I have an interesting situation where AmiBroker reports the following error if I call trade.AddCustomMetric() too many times:

AmiBroker version
( 64-bit, cooltool.dll 6.20.1, mfc42.dll 6.20.1, msvcrt.dll 7.0.17134 )

Microsoft Windows 10 version 10.0 (Build 17134)
Service Pack 0.0, Common Controls: 6.16

Unhandled exception
Type: COleException
The parameter is incorrect.

This error is reported after my AFL completes and after all Report Charts have been generated. My assumption is that AmiBroker is cleaning up after the completion of the back test. I've discovered that whether or not the error will be reported is influenced by at least two factors:

  1. The total number of custom trade metrics. If there are 102 or fewer custom metrics, then the error is not generated with any date range that I've tested.

  2. The date range. If there are 108 or more metrics and the test is sufficiently long, then the error will occur. For example, running a back test from 1-Jan-2004 through 31-Dec-2017 (1760 trades) will not cause the error. However, changing the start date by one day so that the date range is 31-Dec-2003 through 31-Dec-2017 (1776 trades) will cause the error.
    It is worth noting that it does not appear that the problem is related strictly to the number of trades, as I can change my strategy parameters to generate over 3000 trades and the same date pattern applies: start in 2004 and the back test runs fine, start in 2003 and AB reports an error.

In an effort to narrow down the problem, I also wrote a short test AFL to generate dummy metrics. That code can be found below. Unlike my actual strategy code, the test script can handle up to 750 custom metrics (obviously a ridiculous number) on a long date range (1990-2017, 9600 trades). But change the number of custom metrics to 800 and AmiBroker will close without warning during the back test.

Obviously I can avoid the problem with my strategy AFL by writing all the data required by my client to a text file instead of relying on the trade list. However, I would appreciate any guidance on why the problem is happening in the first place.

numMetrics = Param("# Dummy Metrics", 100, 0, 2000, 1);


nMaxPos = 10;
pctPosSize = 100/nMaxPos;

// Set up and verify AB environment
SetOption("usecustombacktestproc", True);

SetPositionSize(pctPosSize, spsPercentOfEquity);
RoundLotSize = 1; 
// High-level AFL logic

// Initialize all arrays so the backtest will always run
Buy = Sell = Short = Cover = 0;
BuyPrice = ShortPrice = C;
SellPrice = CoverPrice = C;

Buy = Cross(C, MA(C,10));
Sell = Cross(MA(C,10), C);

// End of High-level AFL

if (Status("action") == actionPortfolio)
	// Get backtester object
	bo = GetBacktesterObject(); 
	for (trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade()) 
		for (i=1; i<= numMetrics; ++i)
			if (i%2)
				val = Null;
				val = i;
			trade.AddCustomMetric("Custom"+i, val);

You can't have that many because custom metrics are displayed in the Windows List View control. Windows List view control uses GDI bitmap device context and bitmap in windows has limits to 32K pixels width. If so many columns are added to list view you are exceeding the limits of Windows.

Your computers are not made out of gum. They can't stretch. Windows operating system has MANY limits that people are unaware of.

1 Like