Algorithmic Trading Simulations Part III

Maximilian Wimmer
4 min readNov 20, 2021

Setup Exchange and Asset Metadata to run a Single Factor Pipeline with Zipline-Reloaded

This articel is based on the custom data we ingested in tbe priovious articel and the basics coding for backtesting in zipline. So if you didn’t read the last two articles about algorithmic trading simulations you should.

1. The Pipeline API

A pipeline enables faster and more memory-efficient execution by optimizing the computation of factors during a backtest. To demonstrate the power of using a pipeline we going to develope and test a simple mean-reversion factor that measures how much recent performance has deviated from the historical average.

Run the jupyter notebook extension.

The following code demonstrates a zipline backtest for a simple mean-reversion factor that measures how much recent performance has deviated from the historical average. We are not going to place any orders to simply illustrate the implementation of a custom factor pipeline. We are going to record the results during the simulation so we can inspect the results later on.

Well after you launch the algorithm you are going to see an error from zipline-reloaded that it faild to find any assets with the country_code ‘US’. Well thats because there is a bug that requires (manually) fixing the SQL database.

2. Fixing the SQL Database

The challenge that we face when we run a zipline backtest with custom data is the country code. Lets have a look on how we can set the country code to ‘US’.

2.1. Edit Database Files with DB4S

To open and edit the data that we ingested in part 1 I recommend the DB Browser for SQLite Version.

Once you downloaded the DB Browser and you start it you should see a database overview.

2.2. Showing Hidden Files on Mac

Where do we find the data we ingested into zipline? Well you can search for ever because the data is in a hidden folder named “.zipline”. Keyboard shortcuts are probably the easiest way to display hidden files and folders in the Finder on a Mac.

  1. Open Macintosh HD or the folder where you want to display the hidden files in the Finder.
  2. Hold down the Command, Shift and Period keys: cmd + shift + [.]
  3. The hidden files and folders will then appear partially transparent.
  4. You can hide the files again using the same keyboard shortcut.

Now open the DB4S and click “Open Database” then we navigate to our user home directory and press cmd + shift + [.]. Select the ‘.zipline’ go to “data” and open the folder ‘yahoo_direct’. Then we open the “asset-7.sqlite” file.

We move from “Database Structure” to “Browse Data” and change the table from “asset_router” to “exchanges”.

Finally we see the column “country_code”. The current value is “??”.

Now we click on the “??” and type “US”.

After doing so we can close the DB4S and click on “save”.

3. Backtesting Single Factor Pipeline

Go back into the jupyter notebook and run the code from Single_Factor_Pipepline.py again. Now you should see the backtest results and not the country_code error.

4. Inspect Results

We can get the dataframe with the results by using _ which captures the last cell output.

4.1. Plot the Results

Appendix

Sources:

--

--