Crypto Trading on Autopilot: My Experience with a Trading Bot

 HarshI.T.
6 min readApr 2, 2023

While cleaning the store room, my wife stumbled upon a dusty credit card-sized circuit that held a lot of sentimental value for me. It was my first Raspberry Pi, a miniature computer that I had purchased six years ago for a home automation project that I was unable to complete due to hardware limitations.

Unsure whether to throw it out or not, we decided that I could keep it only if I found a way to monetize it. This led me to develop a Crypto trading bot that transformed my Raspberry Pi into a profitable money-making machine. In this blog, I will share my experience of building and using this bot, along with some tips for others looking to do the same.

Small wonder vicki making money

Bot Functionalities

Bot is suppose to monitor live price of Bitcoin and place limit buy order when it finds price will go up. Make limit sell order on higher price after buying it. It must not act greedy even if market is bullish. 📈

Well, let’s face it, I’m good at coding and not a Wall Street wizard. I don’t know the first thing about stock market analysis or those fancy terms traders throw around like confetti 🎊. So, I’m leaving all the heavy lifting to my bot. It’s going to be my own little financial advisor, making all the smart decisions while I sit back and relax. 🧘🏻‍♂️

Architecture

The Raspberry Pi has limited resources, with only 1 GB of RAM and a small ARM processor. Therefore, it’s important that the software used on the Pi is lightweight. To ensure this, I chose to develop a microservice-based bot that can run smoothly on the Pi’s hardware.

To prevent the Raspberry Pi from becoming overloaded, I chose to use a microservice architecture. This approach involves dividing the workload across multiple servers(if required), which helps to ensure that the Pi can handle the load without becoming overwhelmed.

For my bot, I chose to use Python due to its simple coding syntax and the ability to integrate machine learning code. To optimize performance, I opted for a SQLite database, which uses minimal resources. Additionally, I used Redis as an event bridge between services to improve the bot’s ability to handle multiple tasks. This was necessary because Python is not well-suited for multi-threading and multi-processing, and Redis helps to address this limitation.

Bot Architecture

I selected Binance as Crypto Exchange. It provides API for all trading purposes. There are total 8 micro services.

Buy and Sell to place buy and sell orders gradually.

Price Observer to track crypto price realtime (100 times per second).

Idle Order Remover, monitors un-executed buy orders and cancels them.

Trade Observer, checks for recently filled buy order and immediately places a sell order.

Budget, makes sure we don’t exceeed budget while buying.

Analytics Data Logger, collects price data for future analytics.

I added all these micro services as systemctl service in Pi so I don’t manually need to start them and they auto-restart incase of failure.

♟️ Strategy

I have gone in rule based approach instead of ML model. So bot will calcualte average price of crypto for last 20 seconds, and place buy order at average price or price less than average price.

It will cancel a buy order if it hasn’t executed for 30 seconds.

Bot will make sure it doesn’t make multiple buy orders at same price. Every buy order should be between $10 — $11 ($10 is the minimal transaction amount by Binance).

The initial three buy orders must have a price difference of $10 in Bitcoin price, while all subsequent orders should have a price difference of $50.

Bot in Action 🤖

I initially provided fund of $30 so bot can make maximum 3 purchases for 1 week. It purchased Bitcoin worth $10 and sold it when Bitcoin price goes $5 higher.

Assuming that the bot buys Bitcoin at $27,000 and sells it at $27,005, it earns a profit of $5 per Bitcoin. This equates to a profit percentage of 0.0185% per trade (i.e., $5 / $27,000). Therefore, for every successful trade, the bot earns $0.00037 ($5 x 0.0185%). While this may seem like a small amount, over time and with multiple trades, it can add up to a significant profit.

Here is the trick. As it is a Bot and performs action using API calls. It made average 500 trades per day. it earned $1 in a week.

It was testing phase to monitor bot’s performance. Later I increased budget to $100. It earned $3 in second week.

Monitoring 📊

I have added a Grafana dashboard to keep track of my trades and monitor their performance.

Monitoring Trades

Additionally implemented a health monitoring system to keep track of the status and performance of my microservices.

Health Monitor

Conclusion

It actually took me four hours of intense coding and a lot of caffeine to develop this bot 🤣. But hey, all the hard work is paying off now that it’s making me some sweet, sweet profits. Who says laziness doesn’t pay off?

It’s been three weeks since I unleashed my trading bot onto the crypto markets, and I gotta say, it’s been holding its own pretty well! While it’s been racking up profits like a champ during bullish market conditions, there have been a few moments where it’s had to wait around for days just to get all its sell orders filled.

That got me thinking — what if I could give my bot a little more brainpower to make even smarter decisions? And that’s where machine learning comes in! I’m seriously considering experimenting with some ML algorithms in the future to help my bot stay ahead of the curve and maximize its profitability.

I have following choices:

  1. Price Prediction
  2. Buy / Sell action prediction
  3. Sell price suggestion

As I mentioned earlier, I’m super excited to experiment with machine learning algorithms to boost my trading bot’s performance. While running a heavy-duty model like LSTM may not be feasible for my trusty little RaspberryPi, I’m determined to explore more lightweight options that can get the job done.

I am happy to use Binance as they do not charge any trading fees, making micro trading possible. If trading fees were involved, I would need to adjust my strategy to factor in those fees when calculating sell price.

In the next part of my blog, I’ll be diving deeper into the process of selecting, training, and implementing an ML model to take my bot’s profitability to the next level. Feel free to share them in the comments section below!

So stay tuned, folks — things are about to get even more interesting around here!

--

--