Introduction

One of the most useful things that you can do in the analysis window is to back-test your trading strategy on historical data. This can give you valuable insight into strengths and weak points of your system before investing real money. This single AmiBroker feature is can save lots of money for you.

First you need to have objective (or mechanical) rules to enter and exit the market. This step is the base of your strategy and you need to think about it yourself since the system must match your risk tolerance, portfolio size, money management techniques, and many other individual factors.

Once you have your own rules for trading you should write them as buy and sell rules in AmiBroker Formula Lanugage (plus short and cover if you want to test also short trading).

In this chapter we will consider very basic moving average cross over system. The system would buy stocks/contracts when close price rises above 45-day exponential moving average and will sell stocks/contracts when close price falls below 45-day exponential moving average.

The exponential moving average can be calculated in AFL using its built-in function EMA. All you need to do is to specify the input array and averaging period, so the 45-day exponential moving average of closing prices can be obtained by the following statement:

`ema( close, 45 );`

The close identifier refers to built-in array holding closing prices of currently analysed symbol.

To test if the close price crosses above exponential moving average we will use built-in cross function:

`buy = cross( close, ema( close, 45 ) );`

The above statement defines a buy trading rule. It gives "1" or "true" when close price crosses above ema( close, 45 ). Then we can write the sell rule which would give "1" when opposite situation happens - close price crosses below ema( close, 45 ):

`sell = cross( ema( close, 45 ), close );`

Please note that we are using the same cross function but the opposite order of arguments.

So complete formula for long trades will look like this:

```buy = cross( close, ema( close, 45 ) ); sell = cross( ema( close, 45 ), close );```

NOTE: To create new formula please open Formula Editor using Analysis->Formula Editor menu, type the formula and choose Tools->Send to Analysis menu in Formula editor

Back testing

To back-test your system just click on the Back test button in the Automatic analysis window. Make sure you have typed in the formula that contains at least buy and sell trading rules (as shown above). When the formula is correct AmiBroker starts analysing your symbols according to your trading rules and generates a list of simulated trades. The whole process is very fast - you can back test thousands of symbols in a matter of minutes. The progress window will show you estimated completion time. If you want to stop the process you can just click Cancel button in the progress window.

Analysing results

When the process is finished the list of simulated trades is shown in the bottom part of Automatic analysis window. (the Results pane). You can examine when the buy and sell signals occurred just by double clicking on the trade in Results pane. This will give you raw or unfiltered signals for every bar when buy and sell conditions are met. If you want to see only single trade arrows (opening and closing currently selected trade) you should double click the line while holding SHIFT key pressed down. Alternatively you can choose the type of display by selecting appropriate item from the context menu that appears when you click on the results pane with a right mouse button.

In addition to the results list you can get very detailed statistics on the performance of your system by clicking on the Report button. To find out more about report statistics please check out report window description.

Back testing engine in AmiBroker uses some predefined values for performing its task including the portfolio size, periodicity (daily/weekly/monthly), amount of commission, interest rate, maximum loss and profit target stops, type of trades, price fields and so on. All these settings could be changed by the user using settings window. After changing settings please remember to run your back testing again if you want the results to be in-sync with the settings.

For example, to back test on weekly bars instead of daily just click on the Settings button select Weekly from Periodicity combo box and click OK, then run your analysis by clicking Back test.

Reserved variable names

The following table shows the names of reserved variables used by Automatic Analyser. The meaning and examples on using them are given later in this chapter.

Until now we discussed fairly simple use of the back tester. AmiBroker, however supports much more sophisticated methods and concepts that will be discussed later on in this chapter. Please note that the beginner user should first play a little bit with the easier topics described above before proceeding.

So, when you are ready, please take a look at the following recently introduced features of the back-tester:

a) AFL scripting host for advanced formula writers
b) enhanced support for short trades
c) the way to control order execution price from the script
d) various kinds of stops in back tester
e) position sizing
f) round lot size and tick size
g) margin account
h) backtesting futures

AFL scripting host is an advanced topic that is covered in a separate document available here and I won't discuss it in this document. Remaining features are much more easy to understand.

In the previous versions of AmiBroker, if you wanted to back-test system using both long and short trades, you could only simulate stop-and-reverse strategy. When long position was closed a new short position was opened immediatelly. It was because buy and sell reserved variables were used for both types of trades.

Now (with version 3.59 or higher) there are separate reserved variables for opening and closing long and short trades:

sell - "true" or 1 value closes long trade
short - "true" or 1 value opens short trade
cover - "true" or 1 value closes short trade

Som in order to back-test short trades you need to assign short and cover variables.
If you use stop-and-reverse system (always on the market) simply assign sell to short and buy to cover

```short = sell; cover = buy;```

This simulates the way pre-3.59 versions worked.

But now AmiBroker enables you to have separate trading rules for going long and for going short as shown in this simple example:

```// long trades entry and exit rules: buy = cross( cci(), 100 ); sell = cross( 100, cci() );```

```// short trades entry and exit rules: short = cross( -100, cci() ); cover = cross( cci(), -100 );```

Note that in this example if CCI is between -100 and 100 you are out of the market.

AmiBroker now provides 4 new reserved variables for specifying the price at which buy, sell, short and cover orders are executed. These arrays have the following names: buyprice, sellprice, shortprice and coverprice.

The main application of these variables is controlling trade price:

```BuyPrice = IIF( dayofweek() == 1, HIGH, CLOSE ); // on monday buy at high, otherwise buy on close```

So you can write the following to simulate real stop-orders:

```BuyStop = ... the formula for buy stop level; SellStop = ... the formula for sell stop level; // if anytime during the day prices rise above buystop level (high>buystop) // the buy order takes place (at buystop or low whichever is higher) Buy = Cross( High, BuyStop ); ```

```// if anytime during the day prices fall below sellprice level ( low < sellstop ) // the sell order takes place (at sellstop or high whichever is lower) Sell = Cross( SellPrice, SellStop);```

```BuyPrice = max( BuyStop, Low ); // make sure buy price not less than Low SellPrice = min( SellStop, High ); // make sure sell price not greater than High ```

Please note that AmiBroker presets buyprice, sellprice, shortprice and coverprice array variables with the values defined in system test settings window (shown below), so you can but don't need to define them in your formula. If you don't define them AmiBroker works as in the old versions.

During back-testing AmiBroker will check if the values you assigned to buyprice, sellprice, shortprice, coverprice fit into high-low range of given bar. If not, AmiBroker will adjust it to high price (if price array value is higher than high) or to the low price (if price array value is lower than low)

Profit target stops

As you can see in the picture above, new settings for profit target stops are available in the system test settings window. Profit target stops are executed when the high price for a given day exceedes the stop level that can be given as a percentage or point increase from the buying price. By default stops are executed at price that you define as sell price array (for long trades) or cover price array (for short trades). This behaviour can be changed by using "Exit at stop" feature.

"Exit at stop" feature

If you mark "Exit at stop" box in the settings the stops will be executed at exact stop level, i.e. if you define profit target stop at +10% your stop and the buy price was 50 stop order will be executed at 55 even if your sell price array contains different value (for example closing price of 56).

Maximum loss stops work in a similar manner - they are executed when the low price for a given day drops below the stop level that can be given as a percentage or point increase from the buying price

Trailing stops

This kind of stop is used to protect profits as it tracks your trade so each time a position value reaches a new high, the trailing stop is placed at a higher level. When the profit drops below the trailing stop level the position is closed. This mechanism is illustrated in the picture below (10% trailing stop is shown):

<

The trailing stop, as well as two other kind of stops could be enabled from user interface (Automatic analysis' Settings window) or from the formula level - using ApplyStop function:

To reproduce the example above you would need to add the following code to your automatic analysis formula:

ApplyStop( 2, 1, 10, 1 ); // 10% trailing stop, percent mode, exit at stop ON

or you can write it using predefined constants that are more descriptive

ApplyStop( stopTypeTrail, stopModePercent, 10, True );

Trailing stops could be also defined in points (dollars) and percent of profit (risk). In the latter case the amount parameter defines the percentage of profits that could be lost without activating the stop. So 20% percent of profit (risk) stop will exit your trade that has maximum profit of \$100 when the profit decreases below \$80.

Dynamic stops

The ApplyStop() function allows now to change the stop level from trade to trade. This enables you to implement for example volatility-based stops very easily.

For example to apply maximum loss stop that will adapt the maximum acceptable loss based on 10 day average true range you would need to write:

ApplyStop( 0, 2, 2 * ATR( 10 ), 1 );

or you can write it using predefined constants that are more descriptive

ApplyStop( stopTypeLoss, stopModePoint, 2 * ATR( 10 ), True );

The function above will place the stop 2 times 10 day ATR below entry price.

As ATR changes from trade to trade - this will result in dynamic, volatility based stop level. Please note that 3rd parameter of ApplyStop function (the amount) is sampled at the trade entry and held troughout the trade. So in the example above it uses ATR(10) value from the date of the entry. Further changes of ATR do not affect the stop level.

See complete APPLYSTOP function documentation for more details.

Coding your own custom stop types

ApplyStop function is intended to cover most "popular" kinds of stops. You can however code your own kind of stops and exits using looping code. For example the following re-implements profit target stop and shows how to refer to the trade entry price in your formulas:

```/* a sample low-level implementation of Profit-target stop in AFL: */ Buy = Cross( MACD(), Signal() ); priceatbuy=0; for( i = 0; i < BarCount; i++ ) {      if( priceatbuy == 0 && Buy[ i ] )      priceatbuy = BuyPrice[ i ];      if( priceatbuy > 0 && SellPrice[ i ] > 1.1 * priceatbuy )      {        Sell[ i ] = 1;        SellPrice[ i ] = 1.1 * priceatbuy;        priceatbuy = 0;      }      else        Sell[ i ] = 0; } ```

Position sizing

This is a new feature in version 3.9. Position sizing in backtester is implemented by means of new reserved variable

PositionSize = <size array>

Now you can control dollar amount or percentage of portfolio that is invested into the trade

• positive number define (dollar) amount that is invested into the trade for example:

PositionSize = 1000; // invest \$1000 in every trade

• negative numbers -100..-1 define percentage:
-100 gives 100% of current portfolio size,
-33 gives 33% of available equity for example:

PositionSize = -50; /* always invest only half of the current equity */

• dynamic sizing example:

PositionSize = - 100 + RSI();

as RSI varies from 0..100 this will result in position depending on RSI values -> low values of RSI will result in higher percentage invested

If less than 100% of available cash is invested then the remaining amount earns interest rate as defined in the settings.

There is also a new checkbox in the AA settings window: "Allow position size shrinking" - this controls how backtester handles the situation when requested position size (via PositionSize variable) exceeds available cash: when this flag is checked the position is entered with size shinked to available cash if it is unchecked the position is not entered.

To see actual position sizes please use a new report mode in AA settings window: "Trade list with prices and pos. size"

For the end, here is an example of Tharp's ATR-based position sizing technique coded in AFL:

```Buy = <your buy formula here> ````Sell = 0; // selling only by stop`

```TrailStopAmount = 2 * ATR( 20 ); Capital = 100000; /* IMPORTANT: Set it also in the Settings: Initial Equity */```

```Risk = 0.01*Capital; ``````PositionSize = (Risk/TrailStopAmount)*BuyPrice; ````ApplyStop( 2, 2, TrailStopAmount, 1 );`

The technique could be summarized as follows:

The total equity per symbol is \$100,000, we set the risk level at 1% of total equity. Risk level is defined as follows: if a trailing stop on a \$50 stock is at, say, \$45 (the value of two ATR's against the position), the \$5 loss is divided into the \$1000 risk to give 200 shares to buy. So, the loss risk is \$1000 but the allocation risk is 200 shares x \$50/share or \$10,000. So, we are
allocating 10% of the equity to the purchase but only risking \$1000. (Edited excerpt from the AmiBroker mailing list)

Round lot size and tick size

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;`

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.

Margin account

• "Reverse entry signal forces exit" check box to the Backtester settings.
When it is ON (the default setting) - backtester works as in previous versions and closes already open positon if new entry signal in reverse direction is encountered. If this switch is OFF - even if reverse signal occurs backtester maintains currently open trade and does not close positon until regular exit (sell or cover) signal is generated.
In other words when this switch is OFF backtester ignores Short signals during long trades and ignores Buy signals during short trades.

• "Allow same bar exit (single bar trade)" option to the Settings
When it is ON (the default settings) - entry and exit at the very same bar is allowed (as in previous versions)
if it is OFF - exit can happen starting from next bar only (this applies to regular signals,there is a separate setting for ApplyStop-generated exits). Switching it to OFF allows to reproduce the behaviour of MS backtester that is not able to handle same day exits.
• "Activate stops immediately"

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.
• "Use QuickAFL"

QuickAFL(tm) is a feature that allows faster AFL calculation under certain conditions. Initially (since 2003) it was available for indicators only, as of version 5.14+ it is available in Automatic Analysis too.

Initially the idea was to allow faster chart redraws through calculating AFL formula only for that part which is visible on the chart. In a similar manner, automatic analysis window can use subset of available quotations to calculate AFL, if selected “range” parameter is less than “All quotations".

Detailed explanation on how QuickAFL works and how to control it, is provided in this Knowledge Base article: http://www.amibroker.com/kb/2008/07/03/quickafl/

Note that this option works not only in the backtester, but also in optimizations, explorations and scans.