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 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.
Prerequisites for this tutorial
- Basic understanding of Amibroker and AFL .Read here if you are an absolute beginner.
- Access to any basic Trading system developed in Amibroker. Find the list of Trading systems here.
- Exposure to scripting languages like JScript or VB Script
Steps to automate Backtesting in Amibroker
Step 1
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 let’s say it is default.apx
Step 3
Open Notepad and paste the following code:
AB = new ActiveXObject( "Broker.Application" ); // creates AmiBroker object try { NewA = AB.AnalysisDocs.Open( "D:\\Analysis Projects\\Default.apx" ); // Give path of the location where you saved APX file // NewA represents the instance of New Analysis document/window if ( NewA ) { NewA.Run( 2 ); // start backtest asynchronously while ( NewA.IsBusy ) WScript.Sleep( 500 ); // check IsBusy every 0.5 second NewA.Export( "D:\\Analysis Projects\\test.csv" ); // export result list to CSV file in the given path WScript.echo( "Completed" ); NewA.Close(); // close new Analysis } } catch ( err ) { WScript.echo( "Exception: " + err.message ); // display error that may occur }
Step 4
Save this file in “.JS” extension and then execute it using one of the below options. It’s 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.
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.
how can i unprotect any system of amibroker from dotnet
Hi Manish,
That would be tricky, you can decompile the DLL using various software utilities but it might still require some manual work to restore the original AFL. Check out the below decompiler utility that is open source:
http://ilspy.net/
Nice, man! How would you to backtest many AFLs?
Hi Eric,
In order to backtest many AFL’s you would need to use Analysis Object instead of AnalysisDoc . Through this you can load AFL on the fly. Use javascript looping to test as many AFL’s as you want. Check out the below link for Amibroker Object model:
https://www.amibroker.com/guide/objects.html
Hello Admin,Just have a Basic Query,Is it Necessary to have Amibroker to do Automated Trading.Can you please let me know how would i automate my strategies so that no Manual work is Required.What is the Process Involved after automation of Strategies?Does NSE or MCX needs to Approve these Startegies?What is Cost Involved involved in this..
Hi Rohit,
Its not necessary to have Amibroker for Automated Trading, but you would need a platform to code your trading strategies. And I think Amibroker is one of the best for this purpose. In order to do fully automated trading you would require NISM certification and approval from NSE. The approval cost is less than 20K INR. Check out the below link for some popular Automation softwares:
https://tradingtuitions.com/auto-trading-softwares-and-plugins/
Thank you Admin for Valuable information..Also Could you please let me know which NISM. Certification Module is Required and what is process for approval.
Hi Rohit,
Check out the below link for detailed info:
http://www.marketcalls.in/algo-trading/understanding-autotrading-nse-future-markets.html
How to minimize the analysis window that opens up on running the js.
as this hampers the the charts on the amibroker screen.
would be of great help
Good day. Thanks for the guidance. Any idea how I can manipulate the output to include the AFL file name and perhaps only select certain columns? Thanks
Please help me code my strategy logics in AFL which is based MACD Crossover & RSI Values
Hi Sir,
I get following error in amibroker when I run .js file
[Failed to open file: “file path”
The file may have incorrect format or some other error occurred.]
Please help.
Hi Manish,
Try to put JS file ans APX file in the same location
Dear admin, I have tried to save results of Individual backtesting by using your above afl to a csv file. I have used apply filter to a watch list. Individual optimization is done for all scripts in the watch list and the trade list is correctly displayed in the window, but when I open the csv file, I find only the heading line in that. There is no trade list in csv file. Can you kindly help?
I have used following afl:-
EnableScript( “jscript” );
;
I dont know much of other websites, but this is one of the few Indian websites which has genuinely helping trader with very relevant AFLs and other information.
Thanks for your kind words, Kislay