Zorro on Steroids I - Backtest Automation Using Python

I've fallen in love with Zorro from day one. There were several reasons for that.

First, I used to work in R a lot and I liked the language. One of the things that convinced me to give Zorro a try was its R bridge which allowed to include R functions in trading and backtesting. I haven't used R much since but it's only because I didn't need to.

Second, Zorro offers really powerful syntax which allows for rapid prototyping. Quite often I was able to rewrite strategies spanning hundreds of lines into only a few lines of script. Large built-in library of indicators and functions for data analysis available in Zorro certainly helps in this regard.

Finally, thanks to MetaTrader 4 bridge it's possible to immediately deploy a prototype which saves even more time.

One of the things I struggled with when starting with Zorro had been precise simulation of high frequency strategies using custom data. The first two parts of "Zorro on Steroids" series will deal with this issue.

Motivation

Zorro doesn't handle large price history files well even when there's plenty of free memory available. With tick data it's usually necessary to split backtests by asset and by year otherwise you get the annoying Error 060: Memory fragmented / limit exceeded. The consequence is that the testing with tick data has bigger overhead. A possible solution is to make use of Zorro command line abilities.

In this example I use Python although it's certainly possible to achieve the same thing using only Zorro and loops in the strategy code. The reason is that I'm trying to build a whole framework for data conversion, backtesting and report generation. Python is a suitable language for that thanks to its simplicity and wide range of useful libraries.

The main purpose of the effort is better automation of the whole strategy testing process and minimization of user input. This is the first step towards the goal.

Implementation

You can download the script here: backtest.py

I use Cygwin to run it. It is a collection of tools providing functionality similar to Linux on Windows systems. This particular script can be probably run from Windows command line as well but the scripts in the following parts of the series will require Cygwin. If you install Cygwin for the first time you'll probably want to install Python with it too. Anything can be also downloaded and installed later using the same setup file.

Before running the Python script it's important to modify Zorro strategy accordingly. I have a certain template which I use for all strategies which allows for example to easily switch between OHLC and tick-precise simulation (see TICKS flag in Zorro manual). Or to choose a date range and give it to Zorro through command line arguments.

To achieve that add the following code to the strategy's run function.

function run()  
{
   if(Command[0] > 1000) StartDate = Command[0];
   if(Command[1] > 1000) EndDate = Command[1];

   #ifdef TCK
      History = ".t1";
      set(TICKS);
   #endif

  // the rest of the code
}

With this small modification it's possible to run the aforementioned Python script for automatic backtesting. Don't forget to edit the script and set the zorro_path variable to your Zorro installation folder.

Usage

The command line syntax is as follows:

python backtest.py -s=Strategy -a=Assets -b=StartDate -e=EndDate -d=TCK  
  • Strategy is the name of Zorro strategy to be run and is case sensitive.
  • Assets is a string of comma separated asset names (as defined in AssetsFix.csv file in Zorro/History folder).
  • StartDate and EndDate are numbers in the format described in Zorro manual.
  • TCK is a name of #define directive and the argument is optional. If included it causes Zorro to switch to high-precision simulation with T1 data and TICKS flag set.

More concrete example:

python backtest.py -s=Workshop4_1 -a='EUR/USD,USD/JPY' -b=2015 -e=2016 -d=TCK  

If you followed all the steps you should end up with a folder structure like this (located in Zorro/Log directory):

Workshop4_1  
|--EURUSD
|  |--2015
|  |  |--testtrades.csv
|  |  |--Workshop4_1.dbl
|  |  |--Workshop4_1.htm
|  |  |--Workshop4_1_EURUSD.txt
|  |  |--Workshop4_1test.log
|  |  `--zorro.css
|  `--2016
|     |--testtrades.csv
|     |--Workshop4_1.dbl
|     |--Workshop4_1.htm
|     |--Workshop4_1_EURUSD.txt
|     |--Workshop4_1test.log
|     `--zorro.css
`--USDJPY
   |--2015
   |  |--testtrades.csv
   |  |--Workshop4_1.dbl
   |  |--Workshop4_1.htm
   |  |--Workshop4_1_USDJPY.txt
   |  |--Workshop4_1test.log
   |  `--zorro.css
   `--2016
      |--testtrades.csv
      |--Workshop4_1.dbl
      |--Workshop4_1.htm
      |--Workshop4_1_USDJPY.txt
      |--Workshop4_1test.log
      `--zorro.css

Possible Improvements

You can pass only one #define directive to Zorro using command line but up to four numbers using the -i command line argument. So it's possible to automate backtests over some range of strategy parameters in a similar way as described above (it probably means more nested loops).

It's also easy for example to automatically add the backtest results to a version control system (e.g. Git). This might be useful for collaboration or just backup and content management purposes.

Schizo Frenetik

Read more posts by this author.

Subscribe to StatsMage — Quant Ideas Worth Sharing

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!