Standing on the Shoulder of Giants
Using a Simple Python Live Trading Framework with Zipline Interface
In the last articels we looked at how to backtest with the Zipline-Reloaded and then introduced Zipline-Trader as an alternative for backtesting and live-trading service provided by the crowd-sourced investment fund Quantopian. Now tighten your seatbelts and get ready to test your algorithm live in the cloud with pylivetrader a simple python live trading framework with zipline interface provided by Alpaca.
1. Create New Environment
Download and install Anaconda. I recommend using Visual Studio Code as an IDE. Create a new environment in your conda navigator or terminal and use Python version 3.6 given dependencies. I recommend that you setup a new environment to use pylivetrader. Some packages create a conflict with pylivetrade and that will cause errors while running the algorithm.
2. Install Packages
So now we install pylivetrader together with other useful packages.
3. Configuration for API Access
Before running any algorithm with pylivetrader or pipeline-live, you need to set up your environment for API access. To do so you can either store your connection information in environment variables or in a yaml config file. All relevant information can be obtained via your Alpaca dashboard.
3.1. Environment Variables
The environment needs to be activated via terminal. Then use the following code to export the variables.
3.2. Config File Parameters
Create a yaml file named and name it “config.yaml”. Then add these configuration parameters.
4. The Trading Algorithm
To make the process a little more visual we are going to use a Jupyter-Notebook to build our first algorithm. Then we create a python file so we can run the algorithm from the CLI tool named pylivetrader.
4.1. Algorithm & Pipeline Walkthrough
We are going to build the algorithm together with the pipeline step by step. For a better undestanding we are going to go step by step through the notebook. You can finde the complete notebook on GitHub. We start with importing all the modules we need to connect to the API.
And now we import pylivetrader and the pipeline-live packages.
If you need any help understanding the pylivetrader package better you can run this code in the notebook.
The next step is all about setting up the pipeline in order to get the securities we want for trading. Groups of tradeable stocks are refered to as “universes”, because all your trades will use these stocks as their “universe” of available stock, they won’t be trading with anything outside these groups.
Let’s apply our own filters following along with some of the examples above. Let’s select the following securities:
- US Stocks
- They must be relatively highly traded stocks in the market (by dollar volume traded, need to be in the top 5% traded)
Then we’ll calculate the percent difference as we’ve done previously. Using this percent difference we’ll create an unsophisticated strategy that shorts anything with negative percent difference (the difference between the 10 day mean and the 30 day mean).
Now we are going to graph the pipeline we designed.
Lets run the pipeline.
Lets inspect the pipeline results a little.
5. Executing the Strategy
To launche the algorithm with pylivetrader we create a python file and copy the following code into the file. If you take a closer look you see that pylivetrader follows the same logic as zipline. If you have no experience with zipline yet I recommend you have a look at the previous articels and the documentation for the zipline-reloaded package.
5.1 Terminal Commands to run the Strategy
Open your terminal and activate the ziplive environment. Then export the config data and run pylivetrader with reference to the local path where the long_short_pipeline_algo.py file is saved. Use the following code to run the algorithm.
After you enter the last command to run the long_short_pipeline_algo.py you should see the pipeline fetching data and 20 min before market close the rebalance will start and trade according to the pipeline results.
Medium/Standing_on_the_Shoulder_of_Giants at main · Moccazio/Medium
conda create -n ziplive python=3.6 $ conda activate ziplive Install pylivetrader $ pip install pylivetrader Install the…