Back testing your trading ideas
Post on: 8 Апрель, 2015 No Comment
Back-testing your trading ideas
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:
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 .
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.