Forex trading bot with Typescript and AWS Lambda.
Before we start let me clarify that I used to be a professional trader working for a financial institution but not anymore, so all information found here, including any ideas, opinions, views, predictions, forecasts, commentaries, suggestions, or stock picks, expressed or implied herein, are for informational, entertainment, or educational purposes only and should not be construed as personal investment advice. Trading Forex, Stocks, ETFs, Derivates, etc. represents a risk and you can lose all your money so if you trade, only risk what you can afford to lose.
Who am I and Why did I do all of this?
As usual, when I start a new pet project (like this one) I do it mostly to learn, challenge myself and keep up to date with current tech trends because at my current job as Director of Engineering at Travelperk I have to do a wide variety of things other than coding, by the way, we are hiring engineers, join us.
In addition to learning, I have always loved to find ways to connect finance + math + software engineering, three areas I have studied back in college, and three areas where I have worked professionally so this was a project that fitted everything.
I used to be a trader working for a major Spanish bank and learned a lot from the great senior traders I was lucky to work with, along the years I improved my ways to trade and included algorithmic trading to my trading arsenal.
Having a full-time job does not allow me to trade on a daily basis and it is incompatible with several of my trading strategies which require constant monitoring of the market.
All this situation combined was the trigger to tell me “what if I automate one of the algorithmic strategies I used to use?, Could the bot make some profit as I did manually when I did not have a full-time job?”.
The quick answer is
I was able to automate one of my strategies and ended up creating a forex bot that so far is profitable (I’ll share stats later on)
Why did I choose NodeJs + Typescript + AWS Lambda?
I know the “industry standard” for Forex Bots are Expert advisors written in MQL language but as of today, I do not know that language, its API, etc. so it would have taken me months to master the language and build a robust bot so after checking I had everything I needed to build a bot I went ahead and started to code in NodeJS + Typescript.
Key parts needed to build the bot
- AWS account
Needed to have access to AWS Lambda and being able to run the trading bot.
Forex MT4 account
There are many brokers options, I won’t name any, Google it and pick the most reliable broker you can find (How to pick a good broker)
Forex MT4 API
Since I am not using an expert advisor I do not have direct access to control an MT4 account so I needed an API to control my MT4 account, to place/edit/close traders.
Finding a reliable and not expensive MT4 API was not an easy task. After research, I ended up using MetaAPI because it was the best in terms of price/features that I needed (being able to create trades, close positions, edit positions, get the history of my account, etc.)
Forex price data API
There are many options, paid or free, if you want quality data you will have to pay if you want to test out your idea you can use “yahoo API”
Technical indicators library
Doing algorithmic trading means using technical indicators and math, since I decided to build my bot in Nodejs I have no access to powerful libraries like ScikitLearn/Pandas/NumPy in Python but there is one library that does technical analysis for you. Tulind
This is where the “magic” happens, it is a process that scans the major pairs that my strategy uses, the forex pairs are EURUSD, AUDCAD, GPBJPY, USDCAD, GPBJPY, EURGPB, AUDUSD, USDCHF, USDJPY.
It scans in real-time for trading opportunities, if all the conditions are met the process will send a “signal” to the SQS with the trade that we have to execute, for example:
Current price: 1.20
StopLoss: 20 (in pips)
TakeProfit: 40 (in pips)
This is a very “dumb” lambda, what it does is execute the trade with the conditions that it receives in the SQS message. Nothing more than that, it buys or sells with the correct volume (depending on current equity, configured risk to assume, etc).
Why a lambda for this?
- Price: Lambdas are very cheap, at least for what I am using them for, and when investing money every penny counts. In fact, right now AWS free lambdas execution policies allow me to pay almost nothing per month for the bot. (I pay less than 5 euros).
- Scalability and single responsibility: the market scanner process is scanning 9 forex pairs (and I might add more in the future), at any given time a new trade opportunity can appear, I can have 9 trade opportunities in the same second so I wanted the system to be as scalable as possible and I wanted every trade execution to be isolated, with this approach, every trade will be executed by one lambda.
When trading, unless you are a swing trader and take positions for several days, even weeks, you need to actively manage your trades until you either close in profit or take the loss and close, so what this lambda does is that it trails the profits, allows the SL to be executed, or it applies price averaging if needed, etc.
Given the fact that a trade position can be opened for more than 15 mins (lambda execution time limit), a key part of this lambda is that it calls itself when 10 mins have already passed and the position is still open. That way we make sure that we always have a “Position manager” lambda handling the position until it hits our desired take profit or it is closed due to stop loss.
Results so far
I launched the bot on Feb 16th, 2021 and until today the metrics (from MyFxBook) are:
- ROI: 32.89% in three months
- Max. Drawdown: 7.05%
- Avg. Successful trades: 63%
The bot seems promising,
but (there is always a but),
at one moment in time the total balance of the account was down 7% which means it is not a low-risk trading strategy, the bot could have closed positions and I could have lost 7% of my balance, also there is no guarantee that in the future instead of 7% of Drawdown we might have more and I might lose all the ROI achieved until today. Remember that past performance does not guarantee future performance.
- There are not that many Forex MT4 APIs with a good balance between price/quality. The whole project depended on finding an API / SDK that will allow me to code in NodeJS, luckily I found one and it performed as expected.
- Due to the architecture, and the 15 mins limitations on AWS lambda, I had to adapt the strategy to be resilient to some seconds of not observing the market. In the end, this is not a High-frequency trading bot so if the lambdas used are warming up, the bot should not be heavily impacted.
- Now that I have an “ecosystem” in nodejs to build algorithmic trading bots I will probably open source it, not the brains sorry, the algorithm to pick up trading opportunities is proprietary but the rest, the trade executor, the trade manager, aws lambda infra code, etc.
- I might put my bot into what is called CopyTrading, this allows any person to execute on their own account the same trades my bot executes so even if you do not have my algorithm to run your own bot, you could profit from it.
Creating a Forex Financial Trading Robot Nodejs
Posted by: derivbinary.com