How to look up a Stock’s Short Interest with Python

Today I was trying to investigate short interest in the Energy sector: the group as a whole has rallied hard over the last few days and I suspect a short covering rally is at play, so some testing is in order.

Much to my dismay, my searches didn’t return an easy way to do this in Python. Lots of websites offer the data but quants want to consume the data programmatically… Luckily it was super easy to do (12 line function) with BeautifulSoup and Pandas:

import pandas as pd
import urllib2
from bs4 import BeautifulSoup as bs

def get_short_interest(symbol):
  url = "" + symbol + "/short-interest"
  res = urllib2.urlopen(url)
  res =
  soup = bs(res)
  si = soup.find("div", {"id": "quotes_content_left_ShortInterest1_ContentPanel"})
  si = si.find("div", {"class": "genTable floatL"})
  df = pd.read_html(str(si.find("table")))[0]
  df.index = pd.to_datetime(df['Settlement Date'])
  del df['Settlement Date']
  df.columns = ['ShortInterest', 'ADV', 'D2C']
  return df.sort()

That’s it! Now we can do neat stuff like:

apa = get_short_interest("APA")

And there you have it, all in a nice neat dataframe for analysis.

Interested in resources for Pairs Trading?

Check out the release of SliceMatrix: a unique tool for visualizing the stock market, including views of filtered correlation networks and minimum spanning trees

Categories: Quant Tools

Tagged as: , ,

2 replies »

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s