How I Automated investing through bunq, Zapier, DeGiro and a bit of Node.js

Peter
8 min readFeb 1, 2020

--

2020 is just a few weeks old, so I took the opportunity to reflect on my spending habits and reset my expenses in line with the goals of the new year. Usually this is a bit of a tedious task, but not this year. Because not only did January bring around a ton of new-years resolutions, it was also the month that ended with bunq update 13. bunq is one of the hottest fintechs around and it is basically a bank run as an IT company. bunq offers a full API and in their latest update they even introduced an integration with Zapier as well as a payment sorter that splits out your income over several sub-accounts. Mind you: We’re talking about a bank with an API and Zapier integration 😍.

The combination of these things makes that I found myself the perfect weekend project, with the goal to fully automate my finances. 🤖

bunq is one of the hottest fintechs around. It is basically a bank run as an IT company.

Current Set up

As a financially responsible adult, I try to save up some money every month and move any surplus over to an investment account at DeGiro. This is a manual process and I do not like that. The main reason for disliking this is that every time I want to transfer money to DeGiro, I start negotiating with myself on the exact amount that I want to invest that month. The outcome: I invest less than I want. Because there is always a reason to buy XYZ.

Even worse: every time I log into DeGiro I see how my investments are doing. When they are doing good I start to think: “Should I take some of the profits now?”. When they are doing bad I think: “Should I sell?”. This is completely against the strategy that I had when I started investing. Which was:

  • Don’t time the market. So I intend to do Dollar-Cost averaging every month(read more on that here).
  • Sit out the bumps and look at the long term. So I decided to at least not touch it for 10 years*.

TL:DR By manually managing my investments, I save less than I want and fail to stick to my long-term investment strategy.

*I failed miserably at that in 2019 and took some money out of my portfolio.🤦🏻‍♂️

The goal

After concluding that my involvement does not bring me where I want to be, I see no other option than to remove me from the process and automate all of it. 🎉

This is the intended set up:

  • My paycheck comes in on bunq 💸
  • The bunq salary sorter splits my paycheck over several bank accounts within bunq. (For rent, gasoline, groceries, Netflix and beer money 🍻)
  • The remainder is put into my savings. This money is for unexpected bills, holidays and will cover me for at least a few months in case I lose my job.
  • The surplus over a set limit moves to my account at DeGiro
  • Every 1st of the month the cash in DeGiro is invested in line with the strategy I intended.
  • When I’m old and grey. I’ll be loaded. 🤑💹

Step 1. Getting the money automatically into DeGiro

Setting up the salary sorter

Setting up the payment sorter could not be easier. Whenever your salary comes in bunq will prompt you to sort the amount over different sub-accounts. The next time you get your paycheck, it will sort automatically 🤖.

More information on the payment sorter can be found on bunq’s forum.

If you do not have bunq, you can always just schedule a regular SEPA transfer to your DeGiro Account, but apart from bunq there is no bank that supports draft payments through Zapier so bonus points for bunq.

Setting up Zapier

With Zapier you can connect your favorite apps to one another. In my case I connected bunq to bunq with some logic in between. The goal here is:

Whenever the balance of my savings account is updated, it triggers to do:

  • Check if the amount is more than €5.000,-
  • If it is more than €5.000,-
  • Calculate the surplus that should be transferred by subtracting:

Balance — 5.000 = Surplus

  • Move the surplus to the verified IBAN that is known to DeGiro (they only allow you to transfer from 1 account, I bet to prevent money laundering)
  • Create a draft Payment towards DeGiro. The advantage of the draft payment is that money never can leave my account without my approval

In Zapier this looks like this:

Setup of Zapier

Approving draft payment

Now every time the balance of my savings account is updated and the balance is more than €5.000, the app will ask me to approve a payment to DeGiro. That looks like this:

Step 2: Automating purchases in DeGiro

Now we managed to automatically move part of my paycheck into a savings account and then the surplus of the savings over to DeGiro. The next step is to invest the money that arrived in DeGiro in the products that I’d like to purchase.

DeGiro does not have an API, or Zapier integration like bunq does, but there are some people on the internet that did a terrific job on explaining how to set this up. I read into Bramton’s implementation for Python, but after fiddling about I decided to go with the node.js implementation that Pladaria wrote. To both: Many thanks for your incredible work 👌🏻.

In order to get the script running, I needed a couple of things:

  • You will need to install NPM or YARN to your desktop or server if you do not have that already. This took me some time, as I never played around with node.js before. More on installing NPM here
  • Then you can install the package for DeGiro from the command line interface
# using npm
npm install --save degiro

# using yarn
yarn add degiro
  • Then download Pladaria’s files (here) and install them to a folder that you want to run them from
  • In that folder you’ll find some great examples and I used the buy.js file for example. But there is also a sell.js or get-client-info.js
  • In the file, you replace your DeGiro username and password and the type of product that you want to purchase. (Do keep in mind that these credentials give access to your account at DeGiro, so I highly recommend adding in the 2FA on your account). Instead of pasting your username and password you can also parse them along in the command. Here is the .js file:
const DeGiro = require('..');// Set your username and password below or run from terminal as:
// DEGIRO_USER=username DEGIRO_PASS=password node examples/buy.js
const degiro = DeGiro.create({
// username: 'your-username',
// password: 'your-password',
});
degiro.login().then(() =>
degiro.setOrder({
buySell: DeGiro.Actions.buy,
orderType: DeGiro.OrderTypes.limited,
productId: '8066561', // Google
timeType: DeGiro.TimeTypes.permanent,
size: 1,
price: 900,
})
.then(console.log)
.catch(console.error));
  • Now you can run the command from the command line to see if it works.
node buy.js

It should output something like this if it worked:

{ orderId: '6e5cfad6-bd40-42e0-9bab-8t38d7f1f0pf' }

When logging in to DeGiro we can confirm that it worked. In the order overview you should see that the transaction was successful:

Pro tip: if you build this over the weekend, you can order as much as you like. The orders are not executed since the markets are closed. You can then cancel the order from the DeGiro interface or leave them to be executed.

Step 3: Scheduling the purchase of shares

Now that we managed to trigger a transaction from our Command Line, we need to think about how to schedule this. I like the command to run on every first day of the month.

I first figured to trigger the task from my macbook using automator. But what if I was on a holiday or forget the task, then my command would not run. So not the best solution. A cronjob is also an option, however then it will not run if your computer is turned off. So I’ve decided to move to the cloud.

The easiest option for me was a Google Cloud Instance and to start the Cronjob from there. You just have to install NPM and node again on the cloud instance. If you have your own server (read Raspberry pi) running or have an AWS instance, you should be able to do the very same thing.

To edit the Cronjobs:

crontab -e

Then add in the timestamp, and the commands to execute. (Remember to include a full path to the node package, otherwise it will not run)

00 10 1 * * /usr/bin/node /home/degiro-master/examples/buy.js

This cronjob is set to run 10AM, on the 1st day of any month.

You should read cronjobs as follows:

  • first pair digit is minutes (0–59)
  • second pair is hours (0–23)
  • third pair is day of the month (0–30)
  • fourth digit is month of the year (0–11)
  • fifth digit is the day of the week (0–6, where 7 is also sunday).
  • the * stands for any, so the first * in my cronjob says run on any month.

If you need to know more about cronjobs check out this link.

That’s it

And that’s it, fully automated my savings and investing. Many thanks to all the great people sharing their experiences on the internet.

A word about security and investing:

You can lose part of the money you invest, I specifically did not go into details on what stocks I invest in. The internet is full of tips. This is a decision everyone should make for theirself. The only advise I can give: spread your risk and keep a diverse portfolio. Never invest with money you cannot afford to lose.

I do not accept any responsibility for damages as a result of using the software mentioned above. You are free to try it out but at your own risk. I suggest testing with small amounts and use cheap stocks.

Future goals

I’ll let this job run for some time and if it works as intended, I’ll probably fix the 2 factor authentication and set up some system that will inform me when the the command is executed. If that all works I might hook up some algorithmic trading.

--

--