paint-brush
How I Built a Simple Forex App with Telegram Bots and AWS Lambdaby@lorisocchipinti
789 reads
789 reads

How I Built a Simple Forex App with Telegram Bots and AWS Lambda

by Loris OcchipintiDecember 8th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A freelancer in the digital economy has a global perspective when it comes to new prospects and collaborators. Sometimes, a client may be willing to pay you in its local currency only, so it might be best to accept the arrangement and plan to convert the money later. But converting currencies always comes at a cost: variable fees, spreads, and platform subscriptions. The solution was simple: sending a notification every 2 hours with the current rate and the potential gain or loss I could make at that point in time.

Company Mentioned

Mention Thumbnail
featured image - How I Built a Simple Forex App with Telegram Bots and AWS Lambda
Loris Occhipinti HackerNoon profile picture


If you work as a freelancer in the digital economy, it is likely that you have a global perspective when it comes to new prospects and collaborators. A huge, worldwide pool of potential clients and opportunities is available to you. For this reason, it's not uncommon for Designers, Marketing Specialists, Web Developers, or Sales Reps to work with business partners which are based in different countries.


Sometimes, a client may be willing to pay you in its local currency only. Of course, this is a slight inconvenience but, after all, setting up a multicurrency account is usually easy and inexpensive, so it might be best to accept the arrangement and plan to convert the money later.

The problem

This was precisely the situation I was in just a few months ago: I was doing the work, I invoiced monthly, and I was being paid. Nothing to write home about. However, after a few iterations, I started noticing some issues with this approach. Obviously, from time to time I needed to convert small amounts of money to my local currency to buy stuff, and doing so at an advantageous rate wasn't trivial:


  • it wasn't wise to just do the conversion whenever I needed the cash and without looking at the current rates: currency pairs like USD/EUR or USD/GBP fluctuate a lot, and a difference of just a few price-interest points (aka Pips) can lead to very different outcomes for your bank account. Why leave money on the table?


  • converting currencies always comes at a cost: variable fees, spreads, and platform subscriptions. It doesn't matter how, a broker always finds a way to take its cut.


  • for fiscal purposes, the only rate that mattered was the one being used when I created the invoice. However, after I got paid and wanted to transfer the money a few days later, the market was already operating at a totally different rate. Unfortunately, the Tax Man didn't care if in the past 2 years the economy has been a crazy rollercoaster and historically strong currencies were plummeting - he just wanted his money.


As you can see, many different variables were at work here, mostly against me. I didn't like it. «Damn!» I thought «I am an engineer, I will find a solution if there is any chance to make some beer money!»

The solution

Well, how to turn the tables in my favor? Let's start with a couple of assumptions. The harsh reality is that Forex Trading is complex and risky. I could have poured hundreds of hours into it to educate myself, attempting to come up with bright strategies to gain an edge against the market. However, this approach requires lots of time and money, it's incredibly difficult to pull off as a retail investor, and it simply wouldn't play to my strengths. I concluded that I would just adhere to a naive price-based strategy: buy low, sell high.


In the second place, I needed a way to constantly monitor the current rate and act on it whenever it became convenient to convert my money. Initially, I used to visit websites like TradingView to browse the currency pairs rates. As a lowly human, however, I quickly grew tired of such a repetitive and boring task: in fact, I realized, a bot would be much happier to do this kind of work!

The bot

The strategy was simple: sending a notification every 2 hours with the current rate and the potential gain or loss I could make at that point in time. The most critical part was getting correct, timely data about exchanges: there are myriads of APIs that can help with this, I just chose the path of least resistance and implemented APILayer's Exchange Rates Data API because it's free and simple but honestly, any API would work just fine. My fetchRate function would be something like this.


Where, obviously, the from and to arguments represent any currency pair I am interested in. As soon as I have the current rate, computing the pips difference from my average price is trivial.


Finally, I just needed a way to send a message to myself with this juicy info. Again, there is no need to complicate things: Telegram API can serve this purpose very well and is very simple to set up. After interceding with the @BotFather to request a personal bot-friend, I was ready to get loose with the messages.


Deploying on Lambda

Having a personal Forex bot is very useful only if it is always up and running. AWS Lambda is a virtually free solution to host a simple program like mine (or even small microservice architectures), so I refactored my code to run as a serverless function. There are a few ugly parts though, like the fact that some features are not supported for Golang and it's necessary to create a zip archive to upload the files (eww).


However, Lambda functions allow for a lot of flexibility and can be triggered by cronjob tasks, SQS messages, or even API calls. For my use case, I decided to set up a bihourly cronjob and added a simple JSON payload to specify some input variables.


Let the money flow in! 💸💸💸

References



Also published here.