Algorithmic Trading with Python
I found a Python package for quant trading called zipline, which is currently used in production as the backtesting engine powering Quantopian.
Zipline is a Pythonic algorithmic trading library. The system is fundamentally event-driven and a close approximation of how live-trading systems operate. Currently, backtesting is well supported, but the intent is to develop the library for both paper and live trading, so that the same logic used for backtesting can be applied to the market.
Machine Learning with Python
The most popular package for machine learning in python is scikit-learn.
scikit-learnis a Python module for machine learning built on top of SciPy and distributed under the 3-Clause BSD license.
Combine algorithmic trading with machine learning models
As it becomes more and more easy to collect data, we have so much information that is beyond our traditional understanding machnism of our brain. Also, the full past is summarized into the data. Rules like behind.
Algo trading is not new. Nor is machine learning, thought it’s still quite young. But they have someting in common: they both try to draw the future according to history. Machine learning tries very hard to fit a flexible pattern on the past and use it to predict what we dont know yet, and in some circumstance its quite sucessfull. So I wondered the possibility of integrating this two methods together.
Implementation
This post is not aimed at showing a successfull trading algo. Instead, I will just show the way how you can embed a machine learning model into a quant trading algo. With Python and the zipline environemnt, you only neeed several lines to do that. I do all of this on Quantopian.com, and I suggest you try it too.
First, let’s look at a MACD demo provided by Quantopian, of which the algo will never lead you a attractive profit of course, just a demo of the code. (I changed it slightly, the original version can be found here)
# This example algorithm uses the Moving Average Crossover Divergence (MACD) indicator as a buy/sell signal. |
The code is simple to understand. If you are not familar with zipline I suggest you read this tutorial first.
The besktest summary:

Now, let’s say, we’d like a double check on the long/short signal. For example, we decid to apply a SVM regression model using the prices of related stocks, and predict current price. Only if the predicted price is greatter than 0.9*current_price(predicted a not-too-low price), we shot a long order. This logic is not likely to imporve our algo, but I just want to demonstrade that it’s simple and easy to embed a machine leraning node into a trading algo.
# This example algorithm uses the Moving Average Crossover Divergence (MACD) indicator as a buy/sell signal. |
The magic is a new function ml for training a SVM model and predicting, and then we combine the result with original long/short decision rule.

Amazingly there’s some improvement! No they don’t count because we don’t consider about various bias and the logics behind, but using algorithmic trading and machine learning is not that hart.
Quantopian platform for backtesting provide a very detailed backtest result, as shown below. Personaly I found this platform very attractiving.


