Purpose:
I’ve always have had a fascination for the stock market. The idea of being able to own a portion of any publicly listed company is a fascinating revelation for building ones wealth. My first exposure to ‘playing’ the stock market was helping my dad pick out a few stocks for his IRA. A few went down, but more went up and I was hooked. While not the most active trader, I became a dedicated follower of the stocks that I had a faux vested interest in, and would often listen into the earnings reports in hopes of seeing green. I then went to college and pursued a career in engineering, but my facination with the stock market remained.
With my recent career progression into data science, I gained a strong foundation in Python. Upon starting my career search, I stumbled on quantitative trading analytics, and was enthralled in the possibility that I could merge my old passion for the market with my new coding abilities. I figured a good way to get my feet wet in this new area would be attempting to code my own quantitative models for trading assets. Based on popular and established econometrics, I hope that this running series will help improve my understanding of the trading world and my abilities as a coder.
Without further ado, my first project will be on the Mean Reversion Theory.
Mean Reversion Theory:
In its essence, we are comparing moving averages of an asset. When we look at the short term MA if it is greater than the long term MA, then it can be expected that price will drop, or revert, back to the established mean. So I should program the algorithm to short the stock, and profit from the drop. This will also be true for the inverse.

While I was looking up resources for simulating the ongoing trading that would be typical for the application of such an algorithm, I found Quantopian. On the website you’re able to create algorithms and backtest them in a unique environment.
Quantopian:
First off, we have to initialize the algorithm. This only gets run once, but it sets up the entire code. This is were we can set up a pipeline to feed an assortment of equities. We can also schedule functions that will be run on set intervals. For example in this script, I am using a rebalance function that will redistribute the funds every start of the week at market open.

Next up in the process I created a function to be used inside of the scheduled rebalance function. I first call up the price history for each security in my list. Then I call slice the prices, based on how large I want the each of the moving averages. In an attempt to normalize the differential between the two averages, I divide their differences by the long term MA. In order to make sure that I don’t over leverage my portfolio, I express these weights as fractions adding up to 1.

Here is the rebalance function, where I utilize the compute weights function to buy and sell positions. It tests whether the security is still listed. Then it fills the order based on the fractional amount computed by the formula.

The final function, keeps records the history of specific variables that could be informational. In this example, I want to track how leveraged my account is, how many stocks I’m short, and how many stocks I’m long.

This is now a sufficient algorithm that I can now perform backtesting on, and examine how well it performs based on historical data. This is an important test for how well the algorithm can perform autonomously. Below is a simple backtest with a few vital metrics.
While the returns looks pretty good compared to the S&P500 benchmark (SPY). The amount of volatility my algorithm endured, is not acceptable for production. The Alpha is greater than zero, but not by much. The Sharpe ratio, is indicative of how good the return is on an investment, but my algorithm is penalized by a a large standard deviation, and in comparison to the SPY’s Sharpe ratio over the same time period, there’s definitely room for improvement.

Thanks for reading, I will be doing another type of algorithm next week, and the week after that. The goal by the end of this endeavor would be to implement and automate an algorithm to trade on the market, in real time.