17

Automate Backtesting in Amibroker

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.

New Analysis

Step 2

Goto File–>Save. Provide a name to this Analysis Project. For illustration let’s say it is default.apx

Save Analysis Project

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.

Command Prompt

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.

Related Posts

17 Comments

    • 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/

    • 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

  1. 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..

  2. 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

  3. 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

  4. 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.

  5. 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?

  6. 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.

Leave a Reply

Your email address will not be published.