Investing with Python: Monitoring the SEC’s Edgar Database to Create an Event Driven Investment Strategy

 Link to full code:
https://github.com/ericlighthofmann/EdgarScrape/blob/master/edgar_email.py

“An investment in knowledge pays the best interest.”

– Benjamin Franklin

Introduction

It has always been a goal of mine to use Python to collect and parse large amounts of data and create an investment strategy based on those indicators. I originally had the plan of using Quantopian, a browser-based, Python framework for automated investing, but my strategy didn’t end up quite fitting with what Quantopian was able to do. As a substitute for full automation, my script monitors the SEC’s Edgar database and texts and emails me whenever there is a stock that fits my criteria to buy or sell. I then use Robinhood, a commission-free brokerage, to manually buy and sell those stocks.

Investing Strategy

There is a common argument in investing that when someone high up in a company buys a large amount of their own company’s stock, it is wise to follow suit. This is because investors believe these company leaders may be privy to information about future products or sales, believe their stock is undervalued, or have a general confidence about their company’s future prospects. Essentially, the idea is they must know something we, everyday investors, don’t. Therefore, I wanted to develop a strategy around these insider buys and see whether that theory held water. Whenever an “insider” (CEO, COO, director, etc.) in a public company makes a purchase, sale, or is granted shares of that company’s stock, they are required to submit a “Form 4” to declare those movements to the Securities Exchange Commission (SEC). These Form 4s are stored digitally in the SEC’s Edgar database. The Edgar database also has an accompanying RSS (Rich Site Summary) page which updates in near real time. We can use Python to continuously read and parse this RSS for constant news about submitted Form 4s.

First, we need to create a list or “screen” of stocks that we want to be looking at. I screened mine based on a few different criteria: market cap, price to book ratio, etc. A robust, free stock screener can be found at Zacks Investment Research. Second, if a Form 4 comes up in the RSS feed from a company in our stock screen, we need to navigate to that Form 4 and parse its XML. Third, we need the script to notify us of a stock to buy if it meets our criteria. I’ve chosen to receive both a text and an email for this using Twilio and Gmail. Fourth, we need to have Python tell us when to sell our stocks by maintaining a portfolio and constantly checking prices through Yahoo Finance.

Explanation of Code: Monitoring the SEC’s RSS Feed

script_body

 

Here is the initial part of the script that makes sure the two functions, edgar_feed and check_price are constantly running. The URL that is listed is the link to the SEC’s Edgar RSS feed. I have a simple run counter just to keep track and make sure the script is running.

edgar_feed

I initially load in the companies in my stock screen using Excel (openpyxl in Python) and append each company to CompanyNameList. Using the feedparser module, we call feedparser.parse on the URL of our RSS feed. Since I have the view set to the latest 100 Forms filed with the SEC, I have it loop over all 100 entries, then format the company names so that they match with the format of the companies in my stock screen. If there is a match in company names and the form is a Form 4, I get the link of that entry and send it to another function, scrape_xml.

Explanation of Code: Parse the XML of Form 4s

scrape_xml1

scrape_xml2

scrape_xml3.PNG

We begin by passing the link from the RSS feed to the function. That link is a summary page and, as such, has many hyperlinks on it, but there’s only one relevant XML link that we want to find and parse. If the word “Archives” and “xml” are in the address, we take that link as that link structure will always point to the correct XML. Next, we parse that XML using ElementTree and look for the criteria that would tell us that an insider buy is valid for our strategy. My personal strategy was to look at whether or not the buyer was an officer (CEO, COO, etc), was a large enough buy (greater than $10,000), and was a direct acquisition through the market. This is to weed about events like the sale of stock, the granting of stock through options, etc. If an insider buy does match our criteria, we use Python to send out an email alert and a text alert, telling us that there is a stock to buy. That company is also added to a “Portfolio” text file so that we can keep track of our owned stocks and the current price of the stock (found by querying Yahoo Finance).

Explanation of Code: Automatic Emailing and Texting

com_functions

To email me an alert, I used the built in smtplib module in Python. The code is pretty straightforward, just replace the asterisks with your email address and password.

To text me an alert, I used the Twilio module. Twilio has a free user membership that allows you to register a number and text an unlimited amount of times. You need to register on the Twilio website and create an app/phone number to receive your accountSID and authToken.

Explanation of Code: Maintaining a Portfolio and Checking Prices

Lastly, I wanted Python to automatically alert me when I should sell stocks in my portfolio. As mentioned, when there was a stock that matched my criteria, Python writes to a text file that includes the name of the stock and the price at the time of the alert. I use the same query as above to look at the current price on Yahoo Finance.

sell_stock.PNG

If there is a 2% gain on the current price compared to my purchase price, Python sends me an email, text, and removes it from the “Portfolio” text file. This stock is now out of my portfolio until I purchase it again. I repeat the same behavior but with a 5% loss. Of course, you can tweak these numbers to whatever fits your strategy. You can get fancier, if you want, by comparing trends in small cap stocks (Russell 2000) or the market overall (S&P). This would allow you to hold onto a stock if the market was trending upwards and vice versa.

Conclusion

While this script is not fully automated as I had originally hoped, it does come close. I have to manually do the actual purchasing and selling of the stock, but Python does automatically alert me which stocks to buy. Of course, you can use this script and tailor your strategy to different factors or look at a different universe of stocks. Feel free to give me any tips related to my code, investment strategy, etc!

Again, you can find the complete code here:

https://github.com/ericlighthofmann/EdgarScrape/blob/master/edgar_email.py

 


13 thoughts on “Investing with Python: Monitoring the SEC’s Edgar Database to Create an Event Driven Investment Strategy

  1. I thought this was a really interesting read. Definitely going to try rewriting something similar in node.js. Might be cool to extend it to do simulation testing w/ analytics added on. E.g. don’t alert buy orders, just simulate a real transaction at current prices and keep a db of your “portfolio.” Then have different portfolios which have buy rules based off different investment strategies. Tie it all together with some analytics to see which one of your investment models have the best returns.

    Liked by 1 person

    1. Thanks! Ya, that would be a good idea. I actually have a back testing script set up for the last year and a half or so and should be testing it on different portfolios – you’re right. Let me know if you end up doing something in node.js – I’d like to see the results!

      Like

    1. Thanks! I’ve only been trading on it for a few weeks and so far I’m basically even with the market. I did back test for a year and a half and it seemed to beat the market by a couple percent so we shall see 🙂

      Like

    1. Up about 2% so far (11.5% annualized) but the whole market has been pretty up during this time frame. It’s about even with the S&P. I’m thinking it’s a better strategy for a sideways or down trending market but I need to do more extensive backtesting. Have you been trying it out at all?

      Like

  2. Hi Eric, this is very interesting! Edgar recently changed shut down ftp access, I was wondering if you are still running this strategy with updated code?

    Like

    1. Hi John,

      It actually just monitors the RSS feed that Edgar puts out – it never connects to any FTP servers. So the strategy still works but the code that is currently in this writeup is a bit out of date. I’ve been rewriting the code to save buys and sells to a database using Django – when that’s completed I’ll update the code here 🙂

      Like

  3. Awesome! I am also trying to figure out the best solution to bucketing all stocks by sector, I’ll send you an email to see if you have any ideas!

    Like

    1. I always understood the strategy as looking to lock in near-term gains (i.e. the stock rises the most in the short-term after the insider purchase). I haven’t done my own extensive backtesting though, so that may or may not be true.

      Like

Leave a reply to Nik Cancel reply