My first trading bot
January 27, 2022
The story of how I create my first trading bot and why you should do the same !
Motivation and Introduction
As a developer, I constantly want to apply my knowledge and build efficiency around new subjects, especially a complex yet interesting one that could use mathematics and software engineering. I’ve realized that decision making is a very cool topic but only a few people care (or aren’t afraid) about it.
What if an algorithm could regularly check a curve, it’s trend, then detect when “it’s time” do something based on some knowledge?
You probably realize, this project will dig a little inside time-series analysis and how we can use them to automate decision. Welcome to the world of trading bot!
Defining a trading strategy and applying it
There are a few major approach or rules to define a trading strategy. Obviously, trading is very simple, you must either: buy, wait, or sell. However, if you want to fight against other human, win and make money with it, you better be good. By good I mean having a large range of knowledge to simulate possible scenario, understand the most probable issues to take good enough decisions.
Most of the people will tell you: it simple, buys when it’s low and sell when it’s high.
Yeah, great but how? How to apply this idea and generalize it in real-life scenario?
The major strategy I’ve been introduced try to follow the evolution of curve, then, when the Close point cross the MACD, we must buy (works also for the opposite).
I’ve realized that as expected, it mostly works well when you analyze a past curve with a 1h interval between points. How does it work for real-time data on a bot that use 1min intervals for its fresh data? Is it even possible to base decision-making system based on this strategy?
I suppose but as always: depending on how you see the problem and want to solve it, it works or not at all. Another problem: how can you react to this? Should you wait to be able to measure how broad should the action be?
My purpose on this subject is to learn and try new things, so I didn’t really want to follow rules or define and go too much on the analysis. Something wasn’t clear enough for me and for this new project I wanted to discover and just enjoy so I came up with a new approach: detecting multiple bottoms, measuring the average, and comparing it to a few top averages. It isn’t perfect but intuitively it should be able to approximate properly a trend and react on it.
Building real-time infrastructure and data models
Querying data from an API of using a CSV file is cool but how to get fresh data then react with a minimum amount of time?
WebSocket! It’s a protocol like HTTP but where you can subscribe to a stream of messages. In short, you get every few milliseconds a response from an API and you can do whatever you want with it. I will store those response, let’s call them messages to make it more appropriate, on a timeseries database for future usage 😉
By the way, what’s a timeseries database and why is it useful? It’s a type of database specialized around points of data based on time. It has special feature that make it easier to apply transformation on raw data and get new dataset with calculated new data based on intervals of times. There is also life cycle for your data which you can use to remove useless data after a certain amount of ingestion, plus it makes sure to be scalable enough for pretty much all needs.
I’ve choose InfluxDB to ingest timeseries data because it’s an open-source engine and I didn’t wanted to pay for AWS Timestream.
To store the other objects needed by the bot, I designed a MongoDB with a few collections.
Both where first designed around Docker. My bot ran continually on my NAS server using docker containers. After a few network issues, I decided to launch a virtual machine using AWS EC2 instance and move my databases on Mongo Atlas and Influx Cloud (both are free for small projects). It was perfect!
Deployment and first results
After building this piece of software, I could appreciate my hard labor and see the results stored on my dbs.
On my simulation, it seemed like in a few days I will become a new billionaire so obviously I was happy but looked up for bugs and found a LOT of them… It was frustrating but very funny to iterate over this issue until my analysis seems logical.
The results I got started to become honest but still pretty good, even if I didn’t put much effort on my trading strategy, that’s where the devil inside me decided to fund my account with a hundred bucks and let it run to see what will happen #yolo XD
You will never believe it but then I found something magical, something marvelous, so enormous that you could not even think about it, fees! I realized that the logical model I build completely ignore the “broker taxes”! Some brokers apply fees on every transaction you made. They took a few percent of it (like 0.01% or something), whatever you buy or sell. When you only buy then sell transaction with a huge positive margin: it’s good, when you made a lot of transaction and most of them only have +0.01% or +0.02% benefits: you start to lose money because the fees take most of it.
In a few hours I was able to lose 20% of my incredible investment of 100$, then stop this bot.
End of this version and idea about its future
My first line on this project was the 10 March 2020, the last one was the 15 June 2020, it’s four months on which I’ve worked with pleasure, and this makes me glad. Probably because it was interesting but also the first project where I could connect software engineering to a subject, I knew nothing about.
This brings a spark around the usage of mathematics in the context of software engineering. I realize a few stuffs about the usage of mathematical analysis, statistics, and probabilities and how they could help us classify a situation (buy, hold, sell). Not only on huge data-science scenario or software engineering scenario but all the possible project around both areas.
I also realize that simulating an algorithm on a determinist scenario (like CSV data) is very boring but could be incredibly useful to make sure our productivity time is used well, then switch to a real-life scenario: It was interesting but I I’ve lost much time wondering if my software was bugged and my infrastructure running properly.
If you want to access the code stored on github: https://github.com/florent-haffner/trading-bot-v1-rules
I am writing there to push my ideas out of my mind, structure my project and share what I learned from experience.