Fetch Insider Trading Data Using Python
Insider trading is when a high-ranking officer within a publicly-traded company buys or sells shares of their company’s stock. These transactions are made public through the company’s form 4 SEC filing and are a critical source of data for investors. As I was struggling to get my hands on this information in an automated fashion (was working on a scraping bot to crawl through SEC filings), I came across a much simpler process than I believe most people are not taking advantage of at the moment. I decided to write this article in hopes of saving people time and effort.
As outsiders looking in, investors are tasked with gauging the future success of a company using only what information is released to the public. This can be a difficult task as public data is normally retrospective and does not foretell more than basic estimates when it comes to a company’s future. To combat this lack of predictive data, we look to insider transactions to get an idea of a company’s future prospects. For example, if the CEO of a company has been selling off a significant percentage of their shares, then it may be fair to assume as an investor that this CEO (with their intimate knowledge of the company’s inner workings) does not believe their company will achieve long term success. Using these kinds of assumptions, investors can tweak their strategies accordingly. Access to insider transaction data is imperative to the stock analysis process and can be achieved in three simple steps using python.
Create a finnhub.io API key.
Request insider transaction data.
Convert the resulting json file into a data frame and analyze.
In the following sections, I will break down these steps and then demonstrate how this data can be utilized and leveraged in your own stock analysis.
Finnhub Stock API
A one-stop-shop for fundamental and alternative stock data, oh yeah, and it's free. Finnhub.io is a real-time RESTful API that offers users access to many different forms of stock data. For our case, we will only be utilizing their insider transactions API request. But before we can begin making calls to the API, you need to set up a free account.

The simple registration form needed to get a free API key from finnhub.io.
This step is painless, once you decide on a suitable email and password you will be given a unique API key that you can use to access their data for free.
Requesting Insider Transactions
Now that you have an API key, we can begin reviewing the python code necessary to access this data.
Import Necessary Libraries
As always, we need to bring in the libraries that we will be utilizing in this code (descriptions below).
import requests
import json
import pandas as pd
import numpy as np
Pandas: Working with large datasets.
Numpy: Enforcing multiple conditions to create new columns.
Requests: Connecting to the API.
JSON: Working with the JSON file returned by the API.
API Request
Now we will make our request to the API. This request allows us to specify the stock for which we want to see the insider transactions. Make sure that you input your unique API key from the above step into the request where it says “YOUR API KEY”.
# Specify the stock that you want to analyze.
stock='TSLA'
# Request for data from Finhub.io (30 calls per second limit: https://finnhub.io/docs/api/rate-limit).
r=requests.get('https://finnhub.io/api/v1/stock/insider-transactions?symbol='+stock+'&token=YOUR API KEY')
# Load the JSON file as a string.
test=json.loads(r.text)
After making the API call, we will then receive the data in JSON format. The variable ‘test’ then translates the JSON into a string value.
Handling the Insider Trades
At this point, we have received the insider transaction data from the API and are now looking to extract insights from it. To do this we begin by converting the string into a more digestible format, a data frame. We then add some additional elements like the dollar amount of the transaction, the percent change in the insider’s overall share of the company, and whether the transaction was a buy, sale, or gift.
# Convert the data into a dataframe.
df=pd.DataFrame(data=test['data'])
# Derived attributes from the data.
df['dollarAmount'] =df['change']*df['transactionPrice']df['insiderPortfolioChange'] =df['change']/(df['share'] -df['change'])
# print(type(df['transactionPrice'][0]))
conditions= [
(df['change'] >=0) & (df['transactionPrice'] >0),
(df['change'] <=0) & (df['transactionPrice'] >0),
(df['transactionPrice'] ==0)]
values= ['Buy', 'Sale', 'Gift']
df['buyOrSale'] =np.select(conditions, values)
print(df)
df.to_csv('YOUR PATH\\'+stock+'.csv', index=False)
Analyzing Insider Transactions
Using the above process resulted in a final dataset that looks like this (using Tesla as the example stock).

As you can see, each record represents a single transaction done by one of the company executives. Observing each transaction, we see exactly how many shares were purchased/sold, when they were purchased/sold, and what kind of transaction it was (transaction code).