Applying Data Science To Options Charting (Phase 1)
By: Alex Mitrani
Tags: Investing, Options, Live Chart, Matplotlib, Python

If you’ve ever had an idea for a solution to a problem but lacked the technical expertise to bring it to fruition you may have brushed it off and said, “Someone should invent (insert idea here).”

Six years ago I was enrolled in a course called “Futures, Options and Financial Risk Management.” The course covered, among numerous topics, the examination of the payoffs of options strategies and their effect on a portfolio. In layman’s terms this means determining the profits of an options investment through formulas and charting.

Options are a type of derivative security; their value is derived from an underlying asset. For the purposes of this post I will focus on options based on underlying equities, i.e. stocks. The two plain-vanilla options are calls and puts. A call option is an agreement where the holder of the option has the right to purchase an asset at a specified price at some time in the future. A put option is an agreement where the holder of the option has the right to sell an asset at a specified price at some time in the future.

Throughout my financial courses and preparations for the FINRA exams options were first taught to me formulaically. The value of a call option is expressed below:

Bodie, Zvi, et al. Essentials of Investments. McGraw-Hill, 2010. Page 481

To determine the profit of a call you need:

(1) The price of the underlying stock at expiration (St)

(2) The Strike Price (X)

(3) The Premium (P)

Where the Strike Price is the agreed future price that the holder of a call may purchase the asset and the Premium is the cost of the options agreement at the time of entry (For a more in depth explanation I recommend the investopedia article on call options).

After delving into the formulas my professors would always encourage us (the students) to chart the profits and payoffs for the options. A payoff chart for the June 21, 2019 Apple Call at $185 is below:

Options can be combined with interest in the underlying asset to create intricate hedging strategies. I always found options easier to explain graphically; especially as more complicated options strategies were introduced. (A thorough overview of the profits and charts for advanced option strategies can be found here at theoptionsguide.com).

During my stint as a market analyst, option strategies were first expressed by a profit formula. If a concept needed further explanation we were encouraged to draw a chart. I had access to a Bloomberg Terminal, however I found the customization options of their charting interface to be overbearing and time consuming for my needs. Our firm could only afford two terminals and I would not be given access to one for enough time to master options charting. These hand drawn charts became the norm for both educational and professional presentation purposes.

This experience got me thinking about whether there was a better way to chart and analyze the profitability of options strategies given live data. Wouldn’t it be great if a live stock price would be highlighted on my graphs on command? At the time I lacked any knowledge beyond my high school coding courses and shelved the idea. (Someone should invent a better and cheaper way to plot the profitability of options strategies with live data).

Flash-forward to 2019. I am currently enrolled in a Data Science boot camp which coverers the Python coding language, Matplotlib charting library, and how to utilize an API to obtain live data. In fewer than two weeks I obtained enough technical knowledge to address my charting problem from 2013.

I began my side project by researching methods of importing live stock price data from the Internet. Like most students I approached this conundrum by asking Google. The most popular results took me to the github pages of two individuals. I began by importing the method to my python program and then asking for a specific stock price (Apple):

I spent a great deal of time researching the error message in the documentation, however I was disappointed to find a comment by the author indicating that he had given up on the project. I tried the next method on google only to find that this author had given up as well.

The next search result lead me to a Cambridge, MA based company called UniBit (website). UniBit has an API (Application Programming Interface)that allows the user to obtain live stock prices. I was hesitant about using an API as they seemed more complicated than using a method (given my experiences with certain unfriendly APIs in the past), however I gave it my best shot. After skimming the documentation I set up the API in my program and gave it a simple test query for a stock price.

A query for ‘Dicks Sporting Goods’ stock price

Success! I now had the necessary information to approach my problem. I proceeded to create functions that calculated the profits of stock options using the formula at the beginning of this article. I then created charts based on that formula and finally I integrated the live stock price in my chart.

A profitability chart for an option on the Oshkosh Corporation. The green line represents the current stock price. The intersection of the green and blue lines indicates the profit/loss of an option.

Partial Success! In order to display my graph I nested a new live data request if a while loop. All this means is that whenever my program received new data from the API, the old chart would disappear and, after a second of a blank screen, a new chart would appear. I was able to plot a new chart with live data although the transition was not smooth.

Within two weeks I was able to lay the blueprints for an idea I shelved six years earlier. I can chart both the profitability of an option and the live data of the underlying stock. I still need to add other option strategies to my program and fix the flashing chart problem, but for now I am satisfied with my progress.