Analysing Inside Trading within a Company with Python

What are company insiders (e.g. CEO, CFO, etc.) doing respect to the stocks they own in their own company? Are they selling or buying the stock? Let’s find out by retrieving inside trading within a company with Python.

Retrieving Inside Trading within a company with Python
Photo by The Lazy Artist Gallery on

Why to Analyse what Company Insiders do?

When we are analysing a company, in addition to perform the widely used fundamental analysis, we should search for other ways to get valuable information that can give us additional insights. For example, in my previous post, we already saw how to perform a technical analysis using Python. Another option is to look at company insiders trading. This is what we will do in this post.

While insider trading is not illegal, there are certain timelines that company insiders need to follow in order to buy sell inside stocks (i.e. buy or sell stocks from the company they work). For instance, directors or officers from a company must not buy or sell company stocks when they are in possession of internal material information that is not yet public.

Outside of this “quite period” (i.e. period where they are not allowed to buy or sell stocks), they actually can buy and sell the stocks. To do that they need to report their transactions to the SEC through the SEC form 4 – Statement of Changes in Beneficial Ownership Overview.

What is the SEC Form 4 – Statement of Changes in Beneficial Ownership Overview?

All company directors and officers of the company must report in the SEC Form 4 if they buy or sell shares of the company for which they operate within 48 hours. Also, shareholders owning more than 10% of the shares must fill in this form.

The SEC form 4 covers any buy or sells of stocks as well as execution of company stock options.

Why is important for an investor to know what insiders are doing?

For company analysts, knowing what company insiders are doing can offer a very valuable insight on what are the upcoming prospects of the company.

For example, if the CEO of a company is buying shares, it may mean that he/she perceives the company as currently undervalued in the market or that he see some growth potential within the next months. So that may be a bullish signal.

On the other side, a company insider may be selling stocks if the current stock price is above what is considered fair value. Of course, it could also be that insiders are selling because they want to cash out some of the stocks that they own.

Having said this, I perceive as a bullish signal when company insiders are buying the company stock. However, this is just my personal opinion and you should not take it as an investment advise.

Looking into Inside Trading with Python

While looking into the SEC filings may be super time consuming, we can use Python and openinsider to retrieve the latest traders performed by company insider within a company. They provide a great overview of the latest inside transactions for a selected company.

See below a screenshot from where we can see the latest transactions that Apple directors took regarding their Apple stocks.

While having this information in the web is great, I find it much nicer to be able to retrieve it with Python.

In the next section, we will see how we can automate the retrieval of above table with Python into an Excel file for multiple companies.

How to Analyse Insider Trading with Python

The code from this post is very easy. We are going to use Pandas and the method pd.read_html to retrieve all HTML tables within opensider. Then, we will only keep the table that contains the insider trading information.

What is important here is to go into opensider and search for one stock. In my example, I will use Apple (AAPL). Then, copy the url from the site in order to paste inside of the read_html method in the Python script.

That is exactly what we do in the code below. Note that we use f-strings in order to pass the variable company within the url. In this manner, we can extract dynamically this information for different companies.

Then, we store the result within insider1. Since pandas read_html returns all table existing in the opensinder site, we need to select the third table starting from the end. To do that, we pass the index -3 within square brackets.

Finally, we concatenate the different company inside trades that we retrieve. This allows us to show all companies within a single Pandas DataFrame.

To sum up, in below code we:

  • Pass in a Python list the tickers of the company that we want to retrieve information from.
  • Create an empty Pandas DataFrame.
  • Loop through each of stocks that we want to retrieve inside traders for.
  • Make an http request to retrieve the insider trading table from openinsider. Note that we need to use f-strings and {company} within the url.
  • Keep only the third latest table in the page since it is the one containing the insider tradings from the selected company.
  • Concatenate each of the DataFrames into a single one so that we have the information from all companies together.
  • Save the Pandas DataFrame into Excel.
import pandas as pd

company = ['AAPL','GME']

insider = pd.DataFrame()
for company in companies:
     insider1 = pd.read_html(f'{company}&o=&pl=&ph=&ll=&lh=&fd=730&fdr=&td=0&tdr=&fdlyl=&fdlyh=&daysago=&xp=1&xs=1&vl=&vh=&ocl=&och=&sic1=-1&sicl=100&sich=9999&grp=0&nfl=&nfh=&nil=&nih=&nol=&noh=&v2l=&v2h=&oc2l=&oc2h=&sortcol=0&cnt=500&page=1')
     insider1 = insider1[-3]
     insider1['company'] = company
     insider = pd.concat([insider,insider1])


After running the code, we should have a csv file containing the insider trading table for all companies passed in the script. In my example, Apple and Game Stop.

Retrieving Insider Trading with Python

Wrapping Up

After running the code, we can see that for Apple and GME, there were a few insider transactions happening within the last two years.

For Apple, there were only sales of stocks by insiders and any purchase. Does this mean that insiders see the stock as overvalued or they just want to get some cash? To try to find an answer to that question, we would need to look at the fundamentals of the company.

For example, we could value the company using the Discounted Cash Flow Method as already did in my previous post. Or look into the different financial ratios and key financial statistics.

I hope that you have enjoyed this new post on the Python for Finance series. In case you are more keen on watching video tutorials, feel free to have a look at below video on Analysing Inside Trading within a company with Python:

Inside Trading within a Company with Python

3 thoughts on “Analysing Inside Trading within a Company with Python

  1. We stumbled over here coming from a different website and thought I might as well check things out. I like what I see so now i’m following you. Look forward to looking over your web page yet again.

Comments are closed.