Algorithmic Trading Simulations Part I
Get Started with Zipline-Reloaded
In this articel we will look at how to set up an environment so we can install the Zipline-Reloaded package and ingest data in order to run an algorithm trading simulation.
Setup
- Install Anaconda
- Create an Environment with Mamba and Python 3.8.
- Install Zipline-Reloaded and Libraries for Performance Analysis
- Handling Zipline-Bundles
- Add Custom Data to Zipline-Bundles
- Ingest Custom Data
- Data Access Using Zipline in a Jupyter Notebook
1. Install Anaconda
Windows:
Mac:
Linux:
2. Create a Virtual Environment
Launch the terminal and install mamba.
3. Install Zipline-Reloaded and Libraries for Performance Analysis
4. Handling Zipline-Bundles
The standard quandl libary installed with zipline is not recommended for proper backtestting because the data ends in 2018. Thats why we install zipline_bundles as a helping tool from git so we can easly ingest our own data.
4.1. Modify “extension.py”
If you want to change parameters or add a new bundle you go into the folder of zipline_bundles and edit the extension.py. I will give you a quick example. I want to change the name of the exchange from the yahoo_dircet bundle so I change the name in the extension.py.
5. Add Custom Data to Zipline-Bundles
Now we need to download and ingest the data into zipline. It seems to be a complicated process to ingest custom data into zipline. We are going to go throw the code for the hole process.
5.1. Jupyter Notebook
Run a Jupyter Notebook and import the following modules.
5.2. IPython Magic Command
Zipline provides an easy way to run your algorithm inside the Notebook. To use it you have to write your algorithm in a cell and let Zipline know that it is supposed to run this algorithm. This is done via the IPython magic command that is available after you import zipline
from within the IPython Notebook.
6. Ingest Data
Now we are able to call the yahoo_direct
together with zipline ingest
function. The tickers that you want to download and ingest are set at the beginning of the function.
7. Data Access Using Zipline in a Jupyter Notebook
The following code illustrates how zipline permits us to access daily stock data for a range of companies. You can run zipline scripts in the Jupyter Notebook using the magic function of the same name. First, you need to initialize the context with the desired security symbols. We’ll also use a counter variable. Then zipline calls handle_data, where we use the `data.history()` method to look back a single period and append the data for the last day to a .csv file:
7.1. Plot the Data
We can plot the data in the Jupyter Notebook easily.