Documente Academic
Documente Profesional
Documente Cultură
com/category/categories/amibroker/tutorials/
This is a follow up article on our Introductory post Algorithmic Trading 101. I hope you
understood the basic concepts of Algorithmic Trading and its benefits. Now, lets gear up to
build your own Trading system from scratch. This article would describe every step needed
to create your first Algorithmic Trading system. We shall use our favorite tool Amibroker to
create Trading Algorithm.
Pre-requisites:
//Parameters
MALength1 = 50;
MALength2 = 200;
//Buy-Sell Logic
_SECTION_END();
To backtest this EMA Crossover strategy, we will use NSE Nifty as our preferred scrip, with
the initial capital of 200000 Rupees. Lets say we buy 2 Lots(150 nos) per transaction. Once
you backtest this strategy you will get a detailed report which includes your Annual CAGR,
Drawdown, Net Profit/Loss% etc. You can understand various parameters in Amibroker
Backtest report here.
Value
Parameter
Nifty
Number of Trades 11
Well, this is not bad, but there are still scopes of improvement. Drawdown is on a little higher
side which can put retail investors in trouble.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
This is Part 2 of Amibroker AFL tutorial series. If you havent already gone through Part 1 of
this series, please find it in the below link:-
_SECTION_BEGIN("Parameters");
Plot(Close,"Price",ParamColor("Color",colorBlue),styleLine);
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Plot(Close,"Price",color=colorBlue,style=styleCandle);
Plot(EMA(C,50),"EMA50",ParamColor("EMAColor",colorBlue));
_SECTION_END();
Plot(Close,"Price",color=colorBlue,style=styleCandle);
http://tradingtuitions.com/category/categories/amibroker/tutorials/
EMAPeriod=Param("EMAPeriod",50,1,200,1);
Plot(EMA(C,EMAPeriod),"EMA",ParamColor("EMAColor",colorBlue));
_SECTION_END();
HaClose = (O + H + L + C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot( C, "Close", colorDefault,styleCandle );
_SECTION_END();
_SECTION_BEGIN("Volume");
Plot( Volume, "Volume", color=colorGrey40, ParamStyle( "Style",
styleHistogram | styleOwnScale | styleThick, maskHistogram ) );
writeif( Volume > ma(Volume,5), "The volume is greater than 5 days
average. Indicates a good buying opportunity","None");
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
This is Part 3 of Amibroker AFL tutorial series. If you havent already gone through Part 1 &
2 of this series, please refer the below links:-
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Note: Circle represents Inside Day and Star represents Outside Day
//------------------------------------------------------
// Formula Name: Inside Day Outside Day Patterns
// Author/Uploader: Trading Tuitions
// E-mail: support@tradingtuitions.com
// Website: www.tradingtuitions.com
//------------------------------------------------------
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
_SECTION_BEGIN("MACD Crossover");
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot(MACD(fast,slow),"MACD",color=colorRed,styleOwnScale);
Plot(Signal(fast,slow,signal1),"Signal",color=colorBlue,styleOwnScale);
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
_SECTION_END();
Plot(Close,"Price",color=colorGreen,style=styleCandle);
_SECTION_END();
_SECTION_BEGIN("Popup Alert");
SetChartOptions(0,chartShowArrows | chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
FastEMA=Param("FastEMA",20,1,200,1);
SlowEMA=Param("SlowEMA",50,1,200,1);
Cover = Buy;
if (Buy[BarCount-1]==true)
{
PopupWindow("Your Strategy has given a Buy Signal","Buy Signal",30);
}
if (Sell[BarCount-1]==true)
{
PopupWindow("Your Strategy has given a Sell Signal","Sell Signal",30);
}
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
Plot(Close,"Price",color=colorBlue,style=styleCandle);
TimeFrameSet(inWeekly);
WeeklyEMA20=EMA(C,20);
TimeFrameRestore();
TimeFrameSet(inDaily);
DailyEMA20=EMA(C,20);
TimeFrameRestore();
_SECTION_END();
http://tradingtuitions.com/category/categories/amibroker/tutorials/
This brings and end to our basic tutorial series on Amibroker formula language. Just go
through the syntax carefully for each and every AFL in this and previous posts. Also try these
out yourself in Amibroker. Seriously speaking this wont make you and expert, but it will
strong lay foundation which you can leverage to design complex trading systems. Going
forward well post AFL codes for profitable trading systems with backtest reports. Stay tuned
for the same. As always, feel free to post your queries in comment box.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Amibroker is one of the most versatile tools for Trading system development and testing. It
has a very robust backtest and optimization engine out of the box. Additionally, it also
provide custom backtester interface using which you can play around the default backtest
rules and metrics. It allows customizing the operation of the backtesters second phase which
processes the trading signals. In this post, well try to explore Amibroker custom backtester
features and examples. You should be ready to write your own custom backtest AFL after
reading this post.
The interface also exposes the signal object, stats object and trade object, but only object
directly accessible from AFL is Backtester object, all other objects are accessible by calling
Backtester object methods as shown in the picture below.
Each of these objects have multiple methods which can be accesses from within the AFL
code. The detailed documentation of these methods can be found here.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
The Amibroker custom backtester interface provides three levels of user customization,
simply called high-level, mid-level, and low-level. The high-level approach requires the least
programming knowledge, and the low-level approach the most.
By setting a path to the file holding the procedure in the Automatic Analysis Settings
Portfolio page. This procedure will then be used with all backtests, if the Enable
http://tradingtuitions.com/category/categories/amibroker/tutorials/
By specifying these same two settings in your AFL code using the functions
SetOption(UseCustomBacktestProc, True) and SetCustomBacktestProc(<path to
procedure AFL file>). Note that path separators inside strings need to use two
backslashes, for example
c:\\AmiBroker\\Formulas\\Custom\\Backtests\\MyProc.afl.
By putting the procedure in the same file as the other AFL code and using the
statement SetCustomBacktestProc(). This tells AmiBroker that there is a custom
backtest procedure but theres no path for it, because its in the current file. This
option will be used in the examples going forward in this post.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
The next thing thats required in all backtest procedures is to ensure the procedure only runs
during the second phase of the backtest. Thats achieved with the following
conditional statement:
if (Status(action) == actionPortfolio)
{
. . . .
}
And finally, before anything else can be done, a copy of the Backtester object is needed:
bo = GetBacktesterObject();
So all custom backtest procedures, where theyre in the same file as the other AFL code, will
have a template like this:
SetCustomBacktestProc();
if (Status(action) == actionPortfolio)
{
bo = GetBacktesterObject();
// Rest of procedure goes here
}
This AFL will calculate Profit/Loss % for each individual scrip in Portfolio Backtesting. This
can help to determine on which securities the trading system works well, and on which
securities it doesnt.
}
}
//
SetCustomBacktestProc( "" );
//
/* Now custom-backtest procedure follows */
//
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
//
bo.Backtest(); // run default backtest procedure
//
tradedSymbols = ",";
//
//iterate through closed trades
for ( trade = bo.GetFirstTrade( ); trade; trade = bo.GetNextTrade( ) )
{
ProcessTrade( trade );
}
//
//iterate through open positions
for ( trade = bo.GetFirstOpenPos( ); trade; trade = bo.GetNextOpenPos(
) )
{
ProcessTrade( trade );
}
//
//iterate through the list of traded symbols and generate custom
metrics
for ( i = 1; ( sym = StrExtract( tradedSymbols, i ) ) != ""; i++ )
{
longprofit = VarGet( "long_" + sym );
shortprofit = VarGet( "short_" + sym );
allprofit = Nz( longprofit ) + Nz( shortprofit );
// metric uses 2 decimal points and
// 3 (calculate sum) as a "combine method" for walk forward out-of-
sample
bo.AddCustomMetric( "Profit for " + sym, allprofit, longprofit,
shortprofit, 2, 3 );
}
}
//
SetOption( "MaxOpenPositions", 10 );
//
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;
SetPositionSize( 10, spsPercentOfEquity ) ;
SetOption("InitialEquity",1000000);
http://tradingtuitions.com/category/categories/amibroker/tutorials/
This AFL will add a metric in the trade log which will specify for each winning trade how far
above or below the average winning profit it was as a percentage, and similarly for each
losing trade, how far above or below the average loss it was as a percentage. For this we need
the WinnersAvgProfit and LosersAvgLoss values from the Stats object, and the profit
from the Trade objects for each closed trade (for this example well ignore open positions).
Relative loss percentages are displayed as negative numbers.
SetCustomBacktestProc( "" );
SetOption( "MaxOpenPositions", 10 );
//
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );
Short = Sell;
Cover = Buy;
SetPositionSize( 10, spsPercentOfEquity ) ;
SetOption( "InitialEquity", 1000000 );
All amateur or professional trading system developers would be aware of Backtesting and
Optimization concepts. In-fact, it is the most integral part of Algorithmic system
http://tradingtuitions.com/category/categories/amibroker/tutorials/
development. However, what most people are not aware of is how to interpret the backtesting
and optimization results. This leads to a trap and people lose enough money even when the
trading system was proven profitable during backtesting. Walk Forward Optimization is an
important method to determine the robustness or credibility of your trading system. Like
simple Optimization, it is also a process to determine the best parameters for your trading
system. However, in Walk Forward Optimization the system is optimized for a particular
period of data to find the best parameters, and the obtained parameters are tested on the data
following that period (known as forward data). If the parameters look profitable in both the
data sets, then the system is considered to be trustworthy. This process is iterated over
multiple chunks of data to arrive at the best parameters. The basic purpose of Walk Forward
Optimization is to avoid curve fitting in simple optimization.
Before doing backtesting or optimization, one needs to set up the data required which is the
historical data of a specific time period. This historical data segment is divided into the
following two types:
In-Sample Data: It is a past segment of market data (historical data) reserved for
testing purposes. This data is used for the initial testing and any optimization and is
the original parameters of a system under test.
Out-of-Sample Data: It is the reserved data set (historical data) which is not a part of
the in-sample data. It is important as this ensures that the system is tested on another
period of historical data not earlier thus removing any bias or influences in the
checking of the systems performance.
The process is to first develop a trading system using in-sample data and then apply the out-
of-sample data to the system. The results of both cases can then be compared and tested. The
entire process of Wak Forward Optimization can be better understood using the below
graphic:
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Performing walk forward optimization manually would definitely be a tedious and time
taking process. In order to make it easier, Amibroker has a inbuilt functionality for this.In the
next section, we would go through the step by step process to perform Walk Forward
Optimization.
//------------------------------------------------------
//
// Formula Name: Walk Forward Testing
// Author/Uploader: Trading Tuitions
// E-mail: support@tradingtuitions.com
// Website: www.tradingtuitions.com
//------------------------------------------------------
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo
%g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C ));
//Initial Parameters
http://tradingtuitions.com/category/categories/amibroker/tutorials/
SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(150,spsShares);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;
//Parameters
MALength1 = Optimize("MALength1",20,5,200,5);
MALength2 = Optimize("MALength2",50,5,200,5);
//Buy-Sell Logic
_SECTION_END();
As you can observe, the default periods for slower EMA is 50, and that of faster EMA is 20.
This system would be optimized for NSE:Nifty for the time span of around 16 years (From
2000 to 2016). Other required parameters for this trading system has been mentioned in the
above AFL code.In the AFL, we have used a function called Optimize. This function
instructs Amibroker that the variable needs to be optimized. Below is the signature of this
function:
where,
Step: An interval used for increasing the value from min to max
http://tradingtuitions.com/category/categories/amibroker/tutorials/
For Walk Forward Optimization, click on the backtester settings and then click on Walk
Forward. You would see the below window:
The settings in this window would be defaulted based on your data. So you actually dont
need to change anything, but still you have the liberty to do so.
Start and End dates mark initial period begin /end. This period will be moved
forward by Step until the End reaches the Last date.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
The Start date can move forward by step too, or can be anchored (constant) if
Anchored check is on.
If you mark Use today then Last date entered will be ignored and TODAY (current
date) will be used instead.
By default an EASY MODE is selected which simplifies the process of setting up
WF parameters. It assumes that Out-of-sample segment immediately follows in-
sample segment and the length of out-of-sample segment equals to the walk-forward
step
You should use Easy mode (EOD) when testing on end-of-day data or Easy mode
(Intraday) when testing on intraday data. The difference is that in EOD mode the
END date of previous period and START date of next period are the same thus
avoiding gap between periods. Intraday mode set START date of the next period as
NEXT DAY after END of previous period. That guarantees that boundary day is not
counted twice when testing on intraday data.
In the Advanced mode, the user has complete control over all values, to the extent
that they may not constitute valid WF procedure.
The Optimization target field defines the optimization raport COLUMN NAME
that will be used for sorting results and finding the BEST one. Any built-in column
can be used (as appears in the optimization output), or you can use any custom metric
that you define in custom backtester. The default is CAR/MDD, you can however
select any other built-in metric from the combo.
Once you defined Walk-Forward settings, please go to Automatic Analysis and press the
dropdown ARROW on the Optimize button and select Walk Forward Optimization .This
will run sequence of optimizations and backtest and the results will be displayed in the Walk
Forward document that is open in the main application frame. The optimization process
would run for few minutes or hours depending on the volume of data and number of
parameters.
Was it realistic? It is considered realistic if it could fit to the entire test data (or at
least to a larger segment of the test data) used. It implies that the system has the
characteristics of the real time markets and is robust.
Is it overfitting? If the system does not perform well using the test data and seems to
fit only chance characteristics (not necessarily part of the test data), the system is
considered to be overfitting. It is neither a robust nor reliable one and ought not to be
used for trading.
If the results for the out-of-sample years look good, continue the walk-forward process in
real time to find the parameters to use with real money. Another advantage to this method of
system development and trading is that your system will better adapt to changes in market
behavior over time. Markets do change with time we have all seen systems that have made
money for several years and then simply stopped working because the markets have changed
how often these changes affect the system is related to the best size for the training and out-
of-sample set.
Find below the detailed Walk Forward Optimization results for this Moving average trading
system.
Similar to Walk Forward Optimization, you should also perform Monte Carlo Analysis to
check the robustness of your trading system. Check out the below article to understand it:
http://tradingtuitions.com/category/categories/amibroker/tutorials/
This is a follow up article on our Introductory post Algorithmic Trading 101. I hope you
understood the basic concepts of Algorithmic Trading and its benefits. Now, lets gear up to
build your own Trading system from scratch. This article would describe every step needed
to create your first Algorithmic Trading system. We shall use our favorite tool Amibroker to
create Trading Algorithm.
Pre-requisites:
//Parameters
MALength1 = 50;
MALength2 = 200;
//Buy-Sell Logic
_SECTION_END();
To backtest this EMA Crossover strategy, we will use NSE Nifty as our preferred scrip, with
the initial capital of 200000 Rupees. Lets say we buy 2 Lots(150 nos) per transaction. Once
you backtest this strategy you will get a detailed report which includes your Annual CAGR,
Drawdown, Net Profit/Loss% etc. You can understand various parameters in Amibroker
Backtest report here.
Value
Parameter
Nifty
Number of Trades 11
Well, this is not bad, but there are still scopes of improvement. Drawdown is on a little higher
side which can put retail investors in trouble.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
This is the continuation to our previous post on Algorithmic Trading System development. If
you have missed reading it, refer the below post:
Build your own Algorithmic Trading System: Step by Step Tutorial- Part 1
MALength1 = Optimize("MALength1",50,5,150,5);
MALength2 = Optimize("MALength2",200,10,200,10);
Here we have used a function called Optimize. This function instructs Amibroker that the
variable needs to be optimized. Below is the signature of this function:
where,
Step: An interval used for increasing the value from min to max
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Go to Analysis>Optimize. Make sure that your AFL file is selected in the Optimization
window. This process can take few minutes depending on the amount of Historical data.
Once Optimization is completed, Amibroker would display the detailed results automatically
sorted from best to worst. Below is the snapshot of Optimization result:
Clearly 5/30 combination of EMAs gives the best results with minimum drawdowns. Net
profit has increased from 418% to 585%, also drawdown has decreased from 29% to 13%.
You should try the same combination on different symbols and time-frames to see how robust
is your Trading system.
You might have noticed that the simple Algorithmic system we have developed does not have
any Stop loss component. All the long positions are squared off based on the Sell signal.
Lets try to add Stop Loss in this system. Add the below piece of line in your code:
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Stoploss=2;
ApplyStop(Type=0,Mode=1,Amount=StopLoss);
ApplyStop is a Function which instructs Amibroker to exit the trade when a predefined
Stoploss or Target condition is met. Below is the signature of this function.
type =
0 = stopTypeLoss maximum loss stop,
1 = stopTypeProfit profit target stop,
2 = stopTypeTrailing trailing stop,
3 = stopTypeNBar N-bar stop
mode =
0 disable stop (stopModeDisable),
1 amount in percent (stopModePercent), or number of bars for N-bar stop (stopModeBars),
2 amount in points (stopModePoint);
3 amount in percent of profit (risk)
amount =
percent/point loss/profit trigger/risk amount.
This could be a number (static stop level) or an array (dynamic stop level)
See the snapshot of Monte Carlo Analysis report for our example Trading System:
It is a well-known fact that Markets are Random, so Monte Carlo simulation is a method to
absorb this randomness in your Trading system. If your system performs well in random
market conditions, then it has a huge probability of success. At the end, everything boils
down to probability and that is actually the basis of all profitable Trading systems. Its really
not enough to believe in the Trading system just based on profitable backtest reports. Monte
Carlo analysis weighs equally while designing a system.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
In order to completely automate your strategy, you will need a dealer terminal. Exchanges
dont allow completely automated platform on a retail terminal.To get a dealer terminal for
NSE/BSE Segments, you will need to become an authorized person with your broker and also
clear the NISM-Series-VIII Equity Derivatives Certification Examination.Whereas there is
no such procedure for MCX. There will be a one time cost to register as an Authorized
person(need to check with your broker) and a rental for dealer terminal of Rs
250/segment/exchange.
Download the example AFL Trading system that we discussed throughout this article in the
below link:
Backtesting is the most integral part of any Trading system development. While developing
any trading system from scratch, we have to perform several iterations of backtesting to
arrive at the most optimized parameters. Also, to ensure stability of the system we have to
repeatedly perform backtesting on different securities in different timeframes. This consumes
lot of time and effort on an whole. In order to make this process simpler, Amibroker has
provided OLE Automation interface using which external applications can access various
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Amibroker functionalities like backtest, optimization, data import etc. In this post, we will
understand how to make use of this interface to automate Backtesting in Amibroker.
Open Amibroker and goto menu item Analysis>New Analysis. Load your AFL formula in
the analysis window and make sure that the backtest settings are proper.
Step 2
Goto File>Save. Provide a name to this Analysis Project. For illustration lets say it is
default.apx
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Step 3
try
if ( NewA )
NewA.Export( "D:\\Analysis Projects\\test.csv" ); // export result list to CSV file in the given
path
WScript.echo( "Completed" );
catch ( err )
Step 4
Save this file in .JS extension and then execute it using one of the below options. Its not
neccessary that Amibroker is open before executing this file.
Option 1: Simply double click on the file (You need to have Administrator rights)
Option 2: Goto Command Prompt. Type the JS file name and press enter (You should be in
the same path where JS file is stored)
Either of the two options above will automatically call Amibroker backtest engine and save
the backtest results in the CSV file in the path provided. You will get a confirmation message
once it is completed successfully. To verify, go and check for Test.CSV file in the location
provided.
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Step 5
You can even open the APX file in Notepad (saved in Step 2) and manually change the AFL
name and code. Also,you can automate Optimization by adding a line NewA.Run( 4 ) in the
JS file . The Optimization results can also be saved as CSV file.
This is a very basic yet important script for Automating backtest in Amibroker. You can play
with the JS code to do wonders. Try adding a loop to backtest multiple securities in different
timeframes. Or try optimizing a strategy in different date ranges. Let us know if you have any
million dollar idea which would make this automation more useful for Trading system
development.
For successful trading system development, its very important to understand and analyze
backtest reports. Beginners often overlook this fact as they find it very difficult to
comprehend financial and mathematical terms in these reports. This post will try to simplify
this task of understanding backtest reports. Well consider Amibroker for illustration purpose
since it has one of the most powerful backtest engine. However, most of the parameters in
these reports are same across different trading platforms.
Please download our sample backtest report from the below link.
Lets explore some of the most common and important parameters present in this report:
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Initial Capital
Ending Capital
Net Profit
Net Profit %
Net Profit expressed in % terms. For ex: if Initial Capital=100000, Ending Capital=200000,
then,
Exposure %
It is the net average exposure of your trading system for the period of backtesting. It is
calculated as the sum of individual bar exposures divided by total number of bars. Individual
bar exposure is calculated as the ratio of open positions value to the overall portfolio equity
for that particular bar. Lets suppose you are backtesting your strategy on daily timeframe, if
at the end of Day 1 your open position value is 10000 and portfolio equity is 100000 then bar
exposure for that particular day is 10.
For ex: If net profit %=100 and Exposure %=10, then Net Risk Adjusted Return
%=100/0.1=1000
Annual Return %
It is the compounded annual return %. It is the annualized rate at which capital has
compounded over the backtest period.
For ex: If Annual return %=20 and Exposure %=10, then Net Risk Adjusted Return
%=20/0.1=200
All Trades
http://tradingtuitions.com/category/categories/amibroker/tutorials/
Total number of trades executed as per the backtested strategy in specified period.
Winners
Losers
For Ex: If Total number of Trades=100, and Brokerage per Trade=50, then Total Transaction
costs=100*50=5000
Average Profit
Average Loss
Average Profit/Loss
Also known as Expectancy, Its calculated as (Total Profit+ Total Loss)/(Number of trades).
It represents expected gain/loss per trade.
Average holding period per trade. If you are backtesting on Daily timeframe, then this
represents the average number of days a Trade is held.
This represents the maximum number of consecutive wins in the whole backtest period. High
value is better
This represents the maximum number of consecutive losses in the whole backtest period.
Low value is better.
The largest peak to valley decline experienced in any single trade. The lower the better.
The largest peak to valley percentage decline experienced in any single trade. The lower the
better.
The largest peak to valley decline experienced in portfolio equity. The lower the better.
The largest peak to valley percentage decline experienced in portfolio equity. The lower the
better.
Recovery Factor
It is the ratio of net Profit and maximum system drawdown. Higher the better.
CAR/MaxDD
Compound Annual % Return divided by Maximum system % drawdown. Good if bigger than
2.
For Ex: If Annual Return %=30, and Maximum system % drawdown=10, then
CAR/MaxDD=30/10=3
RAR/MaxDD
Risk adjusted return divided by Maximum system % drawdown. Good if bigger than 2.
For Ex: If Risk adjusted Return %=50, and Maximum system % drawdown=10, then
CAR/MaxDD=50/10=5
Profit Factor
Payoff Ratio
Standard Error
Standard error measures choppiness of equity curve. The lower the better.
Risk-Reward Ratio
Ratio of potential risk and potential reward of Trading system. Higher is better. Calculated as
the slope of equity line (expected annual return) divided by its standard error.
Ulcer Index
A technical indicator that measures downside risk, in terms of both depth and duration of
price declines. The Ulcer Index (UI) increases in value as the price moves farther away from
a recent high, and falls as the price rises to new highs. Mathematically its is the Square root
of sum of squared drawdowns divided by number of bars. Lower the value of Ulcer Index,
better is your trading system. Find detailed calculation example for ulcer index here.
Measure of risk adjusted return of investment. Above 1.0 is good, more than 2.0 is very good.
For detailed calculation click here.
K-Ratio
Detects inconsistency in returns. Should be 1.0 or more. The higher K ratio is the more
consistent return you may expect from the system. For detailed calculation click here.