Coding for Stock Traders in the GameStop Era

Patrick Sparrow
Broadlume Product Development
3 min readFeb 2, 2021

--

Maybe you’re a developer who became obsessed with the stock market during the COVID pandemic. Maybe you’re a former hedge fund manager in desperate need of a new career after the Robinhood / GameStop ($GME) fiasco. Either way, there are interesting things to learn about trading and programming by looking at how some aspects of the stock market can be represented in code.

Photo of stock charts

Object-Oriented Programming

Object-oriented programming — OOP for short — is a paradigm that groups together data and behavior into entities known as objects. Objects can be numbers, strings of characters, collections of other objects, and even custom types. Here are some examples of objects in the Ruby programming language.

Data

Objects can have attributes. Let’s examine one of the fundamental elements of stock trading: the candlestick. Candlesticks represent the price of a stock over a given period of time. The attributes of a candlestick include the opening, closing, high, and low prices. Let’s create a Ruby class that represents a candlestick.

Now, we can create candlestick objects and ask them about their attributes.

If a candlestick’s low is below the open/close range, it has a lower wick. If a candlestick’s high is above the open/close range, it has an upper wick. We can define methods that reference those attributes to determine if the candlestick has either type of wick.

Behavior

Just like in the real world, objects can have behavior. We can send messages to objects that will cause them to have an effect on their own data or other objects that they know about. In an extremely oversimplified example of how the stock market works, we can set up an account object that can buy and sell shares of a stock.

The account object has a portfolio represented by a hash of key/value pairs. It is configured to default to a value of zero for missing keys. When we tell an account to buy or sell shares, it updates the portfolio.

Of course, the real stock market is much more complicated with different types of orders, short selling, options, margin, moving averages, account balances, etc. These examples are simply meant to introduce how a programmer might think about representing the market.

Market Data APIs

If you’d like to work with real market data, there are multiple APIs that can be used to obtain both technical and fundamental information. Some examples include AlphaVantage, Polygon, and Finnhub.

If you’re interested in writing code that performs real or paper stock trades, checkout Alpaca. Here’s an example of an API response containing weekly prices for a given stock (in this case IBM).

JSON containing weekly IBM price data

Charting Libraries

If you’d like to build a tool for visualizing market data, there are many JavaScript, HTML, and CSS libraries for displaying stock charts. Some libraries include CanvasJS, AM Charts, and AnyChart. Here’s an example of a Apple stock chart built with AnyChart.

Example stock chart display

Conclusion

Programming and trading are both valuable skills that can be fun as well as extremely frustrating. They require staying sharp and being at the top of your game. Luckily, there is an immense amount of free and paid learning materials in the form of books, videos, blog posts, and discussion groups. Whether you’re a bull or a bear, I hope you found something interesting in this blog post and that you’ll continue learning to code and learning to trade. Good luck!

--

--