Issue 1/2002 (No. 22)
| AmiBroker Tips newsletter.
Issue 1/2002. No 22.
9 August 2002.
Copyright (C)2002 Tomasz Janeczko.
All back issues available from:
Welcome to the first issue of AmiBroker Tips newsletter in the 2002. It was quite a long time since last issue of the newsletter was published. In the meantime you enjoyed many exiting releases of AmiBroker. Version 3.90 that brought to you direct data plugins for TC2000 and Quotes Plus databases. Version 4.00 that brought to you support for intraday and realtime charting/analysis and backtesting, Equity() and AddToComposite() functions and many other improvements. Now we are preparing version 4.10 for the launch that will bring again a massive set of enhancements that you were asking for like formula-based visual/sound/e-mail alerts, better support for backtesting futures, Fast Track plugin, and many more smaller enhancements. That said, I hope that you will forgive me that I almost forgot about the newsletters. Anyway I plan to come back to releasing newsletters periodically. They won't appear weekly but once a month - still I think you will enjoy them.
As you are probably aware AmiBroker users community has grown significantly and every day more and more useful things are created by members of our group. The main AmiBroker mailing list at http://groups.yahoo.com/neo/groups/amibroker/info has now over 600 subscribers and is probably the most friendly trading related group ever. Many members of the group actively share their ideas and solutions. For example, all starters in the trading system design world should check out the tutorial created by Herman van den Bergen covering system development in AmiBroker (800KB PDF). This is just one example of fruitful activity of group members.
But back to the newsletter. In this issue I am going to present you some tips and tricks on using new enhanced backtester available in version 4.09.0 beta. These enhancements are mainly targeted for futures traders but stock traders should also find something interesting in the article. In order to test-run this new features you are welcomed to download the latest beta version 4.09.0 from here: http://www.amibroker.com/bin/ab4090beta.exe. Make sure to take a look at installation instructions included in the archive before installing the beta.
Just a reminder: if you have any comments/suggestions or article ideas, please don't hesitate to drop a line to firstname.lastname@example.org
2 Insider guide to the backtester - Part I
Note: Examples provided in this article require AmiBroker 4.09.0 beta or higher
The backtester in AmiBroker was always quite powerful tool. It allows you to backtest and optimize systems on multiple securities with an amazing speed. Backtesting your system on all US stocks in a matter of minutes? Why not - AmiBroker can do this for you. Still the backtester was targeted towards stock traders. Future traders had only a chance to run "points-only" test that helped a bit but was not solving all problems connected with specifics of trading futures contracts. Now with version 4.09.0 and new enhanced backtester these problems are gone.
In the first part of the article I will describe new settings of the backtester and in the second part I will give you a few typical examples various settings affect backtester output.
2.2 The basics
When you open long position on stocks you just buy given number of shares at given price, then after some time you sell them and your profit is given by difference between sell and buy price mutliplied by number of shares. If you want to open long position on future contract you pay a deposit - margin - for each contract. The margin is just a little part of full contract value (for example 10%). So you can buy 10 contracts paying no more than full value of one contract. This gives you a leverage that makes trading futures more risky than trading stocks. When price of the contract changes your profit/loss changes accordingly. If contract's point value is 1 each 1$ change in contract price represents 1$ profit/loss per contract - like in stocks. But futures can have point value different that 1. If, for example, point value is 5 each 1 point change in price of the contract represents 5$ profit/loss in your equity. When you close position you get the margin deposit back, so your profit/loss is given by number of contracts multiplied by point value mutlipled by difference between sell and buy prices.
2.3. New backtester settings
There are 6 new "general-purpose" settings in the backtester
And 3 new settings provided for backtesting futures
Picture 1. Settings - General page
Picture 2. Settings - Report page
Picture 3: Symbol - Information page
Let me explain the meaning of all those settings
2.3.1 Margin requirement
This setting defines percentage margin requirement for entire account. The default value of Margin requirement is 100. This means that you have to provide 100% funds to enter the trade, and this is the way how backtester worked in previous versions. But now you can simulate a margin account. When you buy on margin you are simply borrowing money from your broker to buy stock. With current regulations you can put up 50% of the purchase price of the stock you wish to buy and borrow the other half from your broker. To simulate this just enter 50 in the Margin requirement field (see pic. 1) . If your intial equity is set to 10000 your buying power will be then 20000 and you will be able to enter bigger positions. Please note that this settings sets the margin for entire account and it is NOT related to futures trading at all. In other words you can trade stocks on margin account.
2.3.2 Round lot size
Various instruments are traded with various "trading units" or "blocks". For example you can purchase fractional number of units of mutual fund, but you can not purchase fractional number of shares. Sometimes you have to buy in 10s or 100s lots. AmiBroker now allows you to specify the block size on global and per-symbol level.
You can define per-symbol round lot size in the Symbol->Information page (pic. 3). The value of zero means that the symbol has no special round lot size and will use "Default round lot size" (global setting) from the Automatic Analysis settings page (pic. 1). If default size is set also to zero it means that fractional number of shares/contracts are allowed.
You can also control round lot size directly from your AFL formula using RoundLotSize reserved variable, for example:
RoundLotSize = 10;
2.3.3 Tick size
This setting controls the minimum price move of given symbol. You can define it on global and per-symbol level. As with round lot size, you can define per-symbol tick size in the Symbol->Information page (pic. 3). The value of zero instructs AmiBroker to use "default tick size" defined in the Settings page (pic. 1) of Automatic Analysis window. If default tick size is also set to zero it means that there is no minimum price move.
You can set and retrieve the tick size also from AFL formula using TickSize reserved variable, for example:
TickSize = 0.01;
Note that the tick size setting affects ONLY trades exited by built-in stops and/or ApplyStop(). The backtester assumes that price data follow tick size requirements and it does not change price arrays supplied by the user.
So specifying tick size makes sense only if you are using built-in stops so exit points are generated at "allowed" price levels instead of calculated ones. For example in Japan - you can not have fractional parts of yen so you should define global ticksize to 1, so built-in stops exit trades at integer levels.
2.3.4 New commission setting $ per share/contract
This new commission setting available from the Settings-General page (pic. 1) allows you to define commission based on number of shares/contracts traded. It works quite simple: if you enter position with 25 shares/contracts the commission you will pay is 25 times the number entered in the commission value box.
2.3.5 Activate stops immediatelly
This setting solves the problem of testing systems that enter trades on market open. In versions prior to 4.09 backtester assumed that you were entering trades on market close so built-in stops were activated from the next day. The problem was when you in fact defined open price as the trade entry price - then same day price fluctuations did not trigger the stops. There were some published workarounds based on AFL code but now you don't need to use them. Simply if you trade on open you should mark "Activate stops immediately" (pic. 1).
You may ask why do not simply check the buyprice or shortprice array if it is equal to open price. Unfortunatelly this won't work. Why? Simply because there are doji days when open price equals close and then backtester will never know if trade was entered at market open or close. So we really need a separate setting.
2.3.6 Drawdown figures based on...
Drawdown figures in the backtest report measure equity dip experienced during the trade(s). To calculate the dip you can use the worst case scenario: low price for long trades and high price for short trades or single price (open or close) for both long and short trades. "Drawdown figures based on..." setting (pic. 2) allows you to choose the price(s) used to calculate drowndowns. Using worst case scenario you will get a few percent bigger drawdowns than using close or open price. On the other hand Equity() function always uses shortprice/coverprice array so you may choose open or close field here to match drawdowns as observed in equity line.
2.3.7 Futures mode
This check box in the settings page is the key to backtesting futures. It instructs backtester to use margin deposit and point value in calculations.
2.3.8 Margin deposit
The margin is the amount of money required to open single contract position. You can specify per-symbol margin in the Symbol-Information page (pic. 3). Positive values describe margin value in dollars, while negative express margin value as percentage of contract price. Magin value of zero is used for stocks (no margin). Margin can be also specified in the formula by using MarginDeposit reserved variable:
MarginDeposit = 675;
In the Futures mode margin setting is used to determine how many contacts can be purchased. Let's suppose that your initial equity is set to $50000 and you want to invest upto 20% of equity in single trade and the margin deposit is $675. In that case your "desired" position size is 50'000 * 0.2 = 10'000. Provided that you have set round lot size to 1, the backtester will "buy" 10000/675 = (integer)14.8148 = 14 contracts, and true positon value will be $9450 (18.9% of the initial equity).
To simulate this in AmiBroker you would need to enter 50000 in the Initial Equity field in the backtester, switch on futures mode, and setup remaining parameters in your formula:
PositionSize = -20; // use 20% of equity
MarginDeposit = 675; // this you can set also in the Symbol-Information page
RoundLotSize = 1; // this you can set also in the Settings page
All further trades will use the same logic but position will be sized according to current cumulated equity instead of initial equity level, unless you specify fixed position size in your formula ( PositionSize = 10000 for example).
2.3.9 Point value
Point-value is per-symbol setting (definable in Symbol-Information window - (pic. 3)) that determines the amount of profit generated by one contract for a one point increase in price. Example: copper is quoted in cents per pound, a price quote of 84.65 (or 8465) equals 84 cents and 65/100 of a cent per pound. A change of +.37 or 37 represents 37/100ths of a cent you will normally hear it quoted as 37 points. But because of the fact that point value for copper is 2.5 every point change gives $2.5 profit/loss, so in this example profit/loss for the day would be 2.5 * 37 = $92.50.
You can also set it from the formula level using PointValue reserved variable, for example:
PointValue = 2.5;
Note: When you load old database AmiBroker presets point value field to 1 and assumes that by default 1 point represents one dollar so one dollar change gives one dollar profit/loss. This is done to ensure that you get correct results even if you (by mistake) run futures mode test on stocks.
Note 2: Although point value setting affects (multiplies) profits/losses it does NOT affect built-in stops. The stops ALWAYS operate on price movement alone. So you should be aware that setting 10% profit target stop will result in 25% profit on trade exited by this stop when point value is set to 2.5.
In the first part of this article I have presented new settings available in the backtester in AmiBroker version 4.09. In the next newsletter I will show you some examples on how these settings affect backtesting results.
.... and that's all for this week - hope you enjoyed reading
AmiBroker Tips newsletter. Issue 1/2002. Copyright (C)2002 Tomasz Janeczko. All back issues available from: http://www.amibroker.com/newsletter/