Automatically following insiders transactions on the belgian stock market with Serverless on AWS

Jelle De Vleminck
datamindedbe
Published in
6 min readMar 31, 2022

Recently I was reading the newspaper and I saw an article in which the owner family of D’Ieteren, a company listed on the Belgian stock market, was heavily buying its own shares.
This was not the first time such an article appeared and I can remember that the stock price almost always evaluated positively in the coming period on this news.

You may wonder if there is a way to get this information yourself before an article appears in the newspaper and get some kind of advantage out of it. This turned out to be the case with insider trading on the FSMA’s website. Unfortunately, there was no public API available to request the data. I wanted to automate following certain companies and get a push message when such insider trading happens. What would be the easiest and fastest way to build and deploy such an automation? Let’s find out!

Photo by Jeremy Bezanger on Unsplash

What is insider trading?

Insider trading is the trading of securities by corporate insiders such as owners, managers and directors.

Insider trading and more specifically the profitability of insider trading is one of the most heavily debated topics. On the one hand, insiders may just have a better understanding of their firm’s economics which may also give them an informational benefit over other investors. However, on the other hand, insiders may also abuse their position within a company to get access to price-sensitive information, unknown to other investors. Accordingly, if insiders would trade on this superior prior knowledge, this would lead to unfair enrichment at the expense of other investors.

Insiders in Belgium are obliged to report their transactions within 3 working days after the transaction date to the Financial Services and Markets Authority (FSMA). The FSMA is responsible for making this information public and publishing all reported insider transactions on its website.

The argument for following insiders makes a lot of sense. Executives and directors have the most up-to-date information on their companies’ prospects. Insiders are way ahead of analysts and portfolio managers, not to mention individual investors.

insiders might sell their shares for any number of reasons, but they buy them for only one: they think the price will rise. — Peter Lynch

Building a web scraper for the FSMA’s website

The first thing we should look up is if there is a web API that returns this data in a structural format and if there are any third party alternatives that provide this data. This is not the case. The only option is to scrape the FSMA website.

If we take a look at the website, we see that we have 3 dropdown fields. One for the company the transaction was made for, and 2 for the start and end dates of the transactions that happened. This input is reflected in the query parameters of the url.
If we then click on “apply”, we get a table with all corresponding transactions.

Each input dropdown field maps to a query parameter in the url

The information on the FSMA website is updated every day around 6PM after the stock market closes (how do I know this you might be wondering, I asked them in an email). So we can trade with this information the next day at the earliest. We want to check every day for the companies we are interested in whether transactions have been reported that day after 6PM. Every day we build a scrape url based on the run date.

Next we want to read the generated table. If the number of rows is greater than 0, we know that transactions have been published that day for that company. Based on this, we can send a push message, like a notification or an email.

Deploying the web scraper with the Serverless framework

Now we want to run our scraper on a daily basis. For this we can use Lambda on AWS. One of the fastest and easiest ways to do this is by using the Serverless framework.

The Serverless Framework helps you develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require. It’s a CLI that offers structure, automation and best practices out-of-the-box, allowing you to focus mostly on your business logic. It supports multiple programming languages and manages both your code as well as your infrastructure.

The main file used in the Serverless framework is the serverless.yml file. Let’s take a look at that file.

The Serverless framework can be used on multiple clouds. First we configure the provider, in our case AWS. We specify an AWS profile (located at ~/.aws/credentials) and an AWS region in which we want to deploy.

Then we can choose from several build options for our code. I’ve used Docker images here by specifying an image name and a path to my Dockerfile relative to the serverless.yml file.

Containers allow us to test most of the interaction with AWS Lambda locally. You can send test events and look at the output of your Lambda. In case you interact with other AWS services in your Lambda, you can pass in AWS credentials as environment variables.

Next up we configure IAM statements. By default you already get a least privileged Lambda execution role that can write to Cloudwatch. You can easily add statements yourself, as we have done here with the Simple Email Service from AWS (ses).

Finally, we can add one or more functions to our Serverless service. In our function we refer to the image name we specified above, pass a Lambda timeout, and configure a trigger event. In our case we want to use EventBridge / Cloudwatch events to run our lambda periodically with a CRON expression from Monday to Friday after 6PM, the days when the Belgian stock exchange is open.

In order not to have to rebuild our code every time we want to follow new companies or if we want to alert new email addresses, we can provide environment variables. Serverless allows you to have it come from another file, which could possibly be in the .gitignore file, this way you can keep secrets out of the serverless.yml file.

Now we can run the deploy command:

serverless deploy

The Serverless Framework translates all syntax in serverless.yml to a single AWS CloudFormation template.

All the required infrastructure will be provisioned automatically, this includes an S3 bucket for the Serverless files, An ECR repository, Any IAM roles, functions and events that are required for the lambda function. A new image will also be build and pushed when running the deploy command.

Our Lambda function will now be scheduled daily and ready to sent push messages:

New insider transactions alert email via AWS SES

Conclusion

As you can see, it is quite easy to develop and deploy these kinds of automations with the Serverless framework. As a developer, you don’t have to worry about the infrastructure and you can focus almost entirely on the business logic. You only pay for what you use, in this case a few seconds of computing power per day. Whether or not you should follow insiders, I’ll leave that up to you. It certainly doesn’t hurt to have more information available to make a decision.

Next steps

  • Checkout the code on GitHub: https://github.com/jelledv/fsma-insider-transactions-notifier and deploy to your own AWS account
  • You could also go much further with this and scrape all the data and compare it with the evolution of the stock prices to find out which insider transactions are the best to track. Please note that the storage and processing of such data in an automated system falls under the Data Protection Regulation on the protection of personal data. Persons whose personal data are processed in this way must give their consent for this, unless the processing is based on a legal obligation or in the public interest. The FSMA benefits from this exception, but this is not the case for other parties.
    This project only allows you not to go to the FSMA website manually every day and check whether there are any new transactions. I would argue that this does not fall under processing in an automated system, right? ;)

--

--

Jelle De Vleminck
datamindedbe

Data Engineer @ Dataminded, Lecturer @ Erasmus University College