Professional Documents
Culture Documents
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.
Writing your trading rules
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:
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.
Changing your back testing settings
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.
Variable
Usage
Applies to
buy
Automatic
Analysis,
Commentary
sell
Automatic
Analysis,
Commentary
short
Automatic
Analysis
cover
defines "cover" (close short position - buy to cover) trading rule Automatic
Analysis
buyprice
defines buying price array (this array is filled in with the default Automatic
values according to the Automatic Analyser settings)
Analysis
sellprice
defines selling price array (this array is filled in with the default Automatic
values according to the Automatic Analyser settings)
Analysis
shortprice
defines short selling price array (this array is filled in with the
default values according to the Automatic Analyser settings)
Automatic
Analysis
coverprice
defines buy to cover price array (this array is filled in with the
default values according to the Automatic Analyser settings)
Automatic
Analysis
exclude
Automatic
Analysis
roundlotsize
Automatic
Analysis (new
in 4.10)
ticksize
defines tick size used to align prices generated by built-in stops Automatic
(see explanations below) (note: it does not affect entry/exit
Analysis (new
prices specified by buyprice/sellprice/shortprice/coverprice)
in 4.10)
pointvalue
Automatic
Analysis (new
in 4.10)
positionsize
Automatic
Analysis (new
in 3.9)
Advanced concepts
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.
Short trade support
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:
buy - "true" or 1 value opens long trade
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;
Note that in this example if CCI is between -100 and 100 you are out of the market.
Controlling trade price
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
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)
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
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 );
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
Account margin setting defines percentage margin requirement for entire account. The default
value of Account margin 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
Account margin 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.
Additional settings
"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.
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.
See Also:
Portfolio-level backtesting article.
Backtesting systems for futures contracts article.
APPLYSTOP function description
Using AFL editor section of the guide.
Insider guide to backtester (newsletter 1/2002)