Building a Simple Forex App with AWS Lambda and Telegram Bots | by Loris Occhipinti | Nov, 2022

photo by joshua mayo Feather unsplash

If you work as a freelancer in the digital economy, you probably have a global perspective when looking for new possibilities and collaborators. A vast, worldwide pool of potential clients and opportunities is available to you. For this reason, it is not uncommon for a designer, marketing specialist, web developer, or sales representative to work with business partners located in different countries.

Sometimes, a customer may be willing to pay you in their local currency only. Admittedly, this is a minor inconvenience, but after all, setting up a multicurrency account is usually easier and cheaper, so it may be best to accept the arrangement and plan to convert the money later.

Editor’s Note: This article is not investment advice and is provided for entertainment and educational purposes only.

A few months ago I was in the exact same situation: I was working, I was invoicing monthly, and I was getting paid. Nothing to write home for. However, after a few iterations, I began to notice some issues with this approach. Obviously, from time to time I needed to convert small amounts of money into my local currency to buy stuff, and doing so at an advantageous rate was not trivial:

  • It was not wise to just convert whenever I needed cash and without looking at current rates: currency pairs such as USD/EUR either USD/GBP Lots of ups and downs, and only a few differences price-interest points (aka pips) can give very different results for your bank account. Why leave money on the table?
  • Converting currencies always comes at a cost: conversion fees, spreads and platform subscriptions. No matter what, the broker always finds a way to take a cut.
  • For fiscal purposes, only the rate that matters is the one I used when creating the invoice. However, after I received the payment and wanted to transfer the money a few days later, the market was already operating at a completely different rate. Unfortunately, the tax man didn’t care if the economy had been an insane rollercoaster over the past 2 years and historically strong currencies were falling – he just wanted his money.

As you can see, there were a lot of different variables working here, mostly against me. I did not like it. “damn!” I thought «I’m an engineer, if there’s any chance to earn some beer money I’ll figure it out!»

Well, how about turning the tables in my favor? Let’s start with some assumptions. The harsh reality is that forex trading is complex and risky. I could put hundreds of hours into this to educate myself, trying to come up with brilliant strategies to gain an edge against the market. However, this approach requires a lot of time and money, is incredibly difficult to pull off as a retail investor, and would not play to my strengths. I concluded that I would just follow 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 was convenient to convert my money. Initially, I used to visit websites like TradingView to browse currency pairs rates. However, as a lowly human, I quickly grew tired of this kind of repetitive and boring task: in fact, I realized, a bot would be more than happy to do this kind of work!

The strategy was simple: sending a notification every 2 hours with the current rate and the potential profit or loss I could make at that time. The most important part was getting accurate, timely data about exchanges: there are myriad APIs that can help with this, I just took the path of least resistance and implemented APILayer Exchange Rate Data API Because it’s free and simple but to be honest any API will do just fine. My fetchRate The function would be something like:

where clearly from And to The arguments represent any currency pair I’m interested in. Calculating as soon as I have the current rate pips The difference from my average price is negligible.

Lastly, I need a way to message myself with this juicy information. Again, there’s no need to complicate things: the Telegram API can serve this purpose very well and is very easy to set up. after intervention @BotFather To request a personal bot-friend, I was prepared to be relieved of the messages:

Having a personal forex bot is only really useful if it is always up and running. AWS Lambda is a virtually free solution for hosting a simple program like mine (or even small microservice architectures), so I refactored my code to run as a serverless function. There are some ugly parts though, like the fact that some features are not supported for golang and uploading files requires creating a zip archive (eww).

However, Lambda functions allow for much more flexibility and can be triggered by cronjob tasks, SQS messages, or even API calls. For my use case, I decided to set up an hourly cronjob and add a simple JSON payload specifying some input variables

Let the money roll in!

Leave a Reply