Programmatic Investing in Stocks

Deploying quantopian.com algorithms for live (or paper) trading with Docker

James Fulford
May 27 · 7 min read
Image for post
Image for post

Before we talk code, we will clear up 2 misconceptions about stock investing, namely:

  • stocks are a zero-sum lottery (investing is not, trading may be)
  • investing can be a moral hazard (not acting may be worse)

Then, we will discuss the technical side of stocks, particularly focused on Quantopian algorithms, alpaca.markets, and deployment with Docker on an AWS EC2 instance.

But First, Where’s The Code?

Relax, it’s all right here in GitHub. Don’t forget to star the repo!

This Is Not Gambling

For some reason, some people think trading stocks is an entirely risky affair, akin to the lottery. There is some risk involved (depends on the stock!), yet unlike the lottery, the numbers are in your favor. Investing is not a zero-sum game.

Here’s a graph showing the DOW stock index from ~1980 to today (May 2020), No one talks about this chart. It just goes up. Since 1896, the DOW has gone up 5.42% every year on average, increasing to around 7.5% after 1986 (source). Considering an average inflation of 2.83% 1896–2017 (and lower since), we have more-or-less 5% yearly growth for the last century.

Image for post
Image for post

The DOW consists of a select few large U.S. companies, so we’re not counting many “recent” tech success stories like MSFT, NFLX, AMZN, and GOOG. Recent returns on these investments — despite entering the position “late” — are still quite respectable, often outpacing the market. In short: there is a lot of success you can ride on over the course of years.

Some make a distinction between “trading” and “investing”, where investing has a longer outlook while trading acts on shorter-term market fluctuations. From what I’ve read, trading is riskier but with higher payoff if you develop the emotional and technical skills. The numbers above pertain to investing. (The code approaches below apply to trading and investing alike)

To those who subscribe to the popular Keynesian economic model (GDP = Consumer Spending + Investment + Government Spending + Net Exports), you will recall the multiplier effect of investment (public or private). Investment often ripples through the factors here, increasing exports and/or consumer spending, causing GDP to grow more than just the amount put into investment. This is because investment creates value.

Vote With Your Wallet

In the short-term, some businesses stray from the right path. Some folks may have moral concerns with investing in companies. After all, we hear about corporate scandals like Enron — or, more recently, Luckin Coffee — on the news.

As an observer of several unspeakable 20th century evils once noted:

“The line separating good and evil passes… right through every human heart”

Alexander Solzhenitsyn, Gulag Archipelago

Everyone fights this very same battle between good and evil, companies included (after all, companies just wrap a collection of humans). Yes, some lose the fight, just as some humans do great evils. However, just like humans, other companies win the battle and make the world a better place for a time.

You have a chance to steer companies towards the good by influencing their stock price. When you see a company do good, consider buying the stock so they can leverage more capital. Otherwise, don’t. In a Darwinian/Pavlovian way, you’ll help companies do more good. After all:

“The only thing necessary for the triumph of evil is for good [people] to do nothing.”

commonly attributed to Edmund Burke

Your money has a voice. Make it count. Think your voice is too small to make a difference? Burke also said “Nobody made a greater mistake than [s]he who did nothing because [s]he could do only a little.”

Alright, Let’s Talk Code

Before writing code, we need to understand the developer side of the field a bit more.

You may elect to build your own stock trading system. finnhub.io provides great free data APIs. For executing trades, I hear Interactive Brokers is a favorite among some developers because of its developer-friendly APIs. You can also borrow some of my code for downloading dividend-adjusted stock price CSVs, or my code for “backtesting” using finnhub.io data.

However, it’s all (probably) been built before. There’s a time and a place for building from the ground up, and that time is probably not the first step in your journey. You can gain a lot by standing on the shoulders of giants.

Quantopian.com provides a web-based IDE for writing Python 3 algorithms. It also has notebooks for evaluating algorithm results and forums for sharing and discussing results and debugging source. The platform has been around for somewhere around a decade now, with a decent community, however, platform changes have made many old code samples invalid. They also removed direct support for live and paper trading.

However, alpaca.markets (an API-first brokerage) saw the lost opportunity and moved to support the Quantopian algorithm live/paper trading use case. There are instructions on how to translate from Quantopian algorithms to alpaca-compatible live algorithms located in the alpaca docs. (For reference, the package/platform Quantopian runs on is called zipline, and is open source on GitHub)

Setup

I’m personally not a fan of installing things globally on my laptop, so I wrap my projects in Docker whenever I can. Here’s that repo link again: https://github.com/jamesfulford/pylivetrader

Then, run build.sh . This will build the docker container you need for run.sh to work properly.

Inside the config directory, save a .env file. See the .env.sample file checked into the repo for structure. All of these values should come from your alpaca.markets account. If you want to do paper trading, make sure to grab your configuration from the paper trading dashboard.

When you have your environment variables set, run run.sh . You can give it a path to an algorithm script to run, the path to the config file, then whether the container should run as a daemon (in the background) or not. The script is sensitive to your PWD, because it mounts volumes. You can stop the container with docker stop pylivetrader (or ctrl+c if not running in daemon mode)

This algorithm is meant to run continuously, so consider creating a Free Tier t2.micro instance on AWS, use deploy/install.sh on the Amazon Linux 2 image, and deploy+run continuously on a server. Use daemon mode to keep the container running in the background after your SSH session ends. (Any value other than false will work)

Example Algorithm

My repository has a couple of example algorithms to try out. For example, here’s an algorithm for following a single ticker symbol and buying when the price appears to be trending upward.

How To Read This Algorithm

Zipline/Quantopian (and pylivetrader/alpaca) calls the initialize function when the process first starts up. You can use schedule_function to schedule other functions for regular execution at other times during trading hours. This is the main lifecycle method provided. Another interesting method is before_trading_start, which is called at 8:45 ET (45 minutes before opening bell) in pylivetrader. Many examples use handle_data, which is a function scheduled for every minute.

A context object is passed between scheduled functions and initialize, and in pylivetrader is pickled between process runs. This is the preferred way for passing things around and maintaining state inside your algorithms, especially if the process dies during execution.

From a programming perspective, these are the main highlights for understanding how a script works. The zipline/quantopian docs are a bit disorganized and the find functionality is a bit frustrating, but they do explain a lot of concepts, capabilities, and shortcomings.

There’s a lot in the quantopian docs about Pipelines, which are an interesting add-on feature that seems non-trivial to translate to alpaca algorithms. The zipline/pylivetrader migration doc asserts this is doable, but did not provide too much helpful guidance on how. (If you get it to work, write a Medium article and send in the link, I’ll gladly include a link right here!)

Conclusion

Investing has intrinsic benefits of supporting companies making the world a better place as well as extrinsic benefits of returns. You can build your own system for testing and running algorithms to trade stocks using various APIs (I provide some starting points and docs I like), or you can get started with Quantopian and alpaca.markets today and get results. We covered how to interpret, test, configure, and deploy these algorithms with Docker on AWS using the tools I shared on GitHub.

Hey, thanks for reading this article! If you liked the breadth and depth of this article, you may be interested in this article on Software Testing Philosophy (and eventually about React Hooks).

A note In Plain English

Did you know that we have four publications and a YouTube channel? You can find all of this from our homepage at plainenglish.io — show some love by giving our publications a follow and subscribing to our YouTube channel!

Resources

General information:

Tread your own path:

Quantopian/Alpaca path:

Python In Plain English

Go deeper with the language powering everything.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store