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.

Photo by The Lazy Artist Gallery on Pexels.com
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, we may take a look at company insiders trading.
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 an Insider Trading API endpoint to retrieve the latest traders performed by company insider within a company. It provides a great overview of the latest inside transactions for a selected company.
Please note that this is a special endpoint and you need to use advanced plan to deal with it.
So the code will do the next thing:
-
It will request the latest insider transactions for a specific company.
-
Then it will subsctract date and volume of transacted securities.
-
If the type of transaction would be "acquistion", it will show that as a positive number, otherwise we should count that a negative number.
So here is the code:
api_key = 'demo'
company = 'AAPL'
response = requests.get(f'https://financialmodelingprep.com/api/v4/insider-trading?symbol={company}&page=0&apikey={api_key}')
company_insiders_transactions = []
for res in response:
date = res['transactionDate']
volume = res['securitiesTransacted']
acquistion_or_disposition = res['acquistionOrDisposition']
change = volume if acquistion_or_disposition == 'A' else volume * (-1)
transaction = { "date": date, "change": change }
company_insiders_transactions.append(transaction)
print(company_insiders_transactions)
Wrapping Up
After running the code, we can see that for Apple there were a few insider transactions happening within the last few weeks.
Can we rely on that information without any additional information? No, we can't, espesially when we don't see a huge amout of transactions last days. Anyway, insider transactions gives us a lot material to think about. For additional information, we could use, for example, different financial ratios and key financial statisctics.
Financial data, valuation, and FMP API workflows
Rajnish Katharotiya writes about financial data, valuation, and the practical use of the FMP API. His work focuses on connecting structured market data to real analysis and repeatable workflows for developers and investors.
Financial data for every need
Real-time quotes and 30+ years of historical data, including prices, fundamentals, and insider transactions — all accessible via API.
Create Free Account