My First Attempt of Implementing Cryptocurrency Trading Bot using LSTM

When machine learning meet cryptocurrencies

Tisana Wanwarn
Geek Culture
7 min readDec 20, 2021

--

The purpose of this article is to share my approaches of implementing simple cryptocurrency trading bot using pure supervised machine learning technique implemented in python. Since I am not an expert in cryptocurrency and trading stuff, any comment or suggestion would be very much appreciated!

Objective

The objective is to maximize the average percent gain in fiat currency from before and after buying and selling cryptocurrencies by determining the best time to buy and best time to sell given the input of only the historical price.

Constraint

  1. If the current action is buy, the next action can either be sell or do nothing. While if the current action is sell, the next action can either be buy or do nothing.
  2. We spend same amount of fiat currency every time we buy cryptocurrency and sell all the amount of cryptocurrency we have on hand every time we sell cryptocurrency.
  3. There is a transaction fee of 0.25% whenever buying or selling action is execute.

Experiment setup and Evaluation metric

The performance evaluation is done by back testing the average percent gain in fiat currency from trading on various cryptocurrencies such as BTC, BNB, ETH, and LTC.

The resolution that I used is 1 minute. 20000 observations (1 observation = 1 row = 1 minute) is split into 80:20 ratio. 80% which is 16000 minutes (about 11 days) is used to train the model while another 20% which is 4000 minutes (about 3 days) is used to test the model performance.

For each trade, the buying price and selling price are recorded in order to calculate 2 metrics for evaluation consist of:

1.Average percent increase of selling price with respect to buying price

  • this is to check whether the bot buy at lower price and sell at higher price or not
  • if the value is positive meaning that the bot usually sell at higher price. But if the value is negative meaning that the bot usually sell at lower price.

2. Average percent gain considering transaction fee

  • this is to check whether on average the bot gain or lose
  • gain is the additional fiat currency we got after each trade with respect to the starting amount
  • the only difference between percent gain and the percent increase is that the gain take into account the transaction fee charge on every buying and selling action.
  • if the value of average percent gain is positive, this mean that the bot usually gain from trade.

Phase 1 of implementing cryptocurrency trading bot

The very first phase of implementing trading bot is just using single LSTM (Long Short Term Memory) layer followed by single fully connected layer to predict the future price of cryptocurrency. If the prediction of future price is higher than current price, we buy. But if the prediction of future price is lower than current price, we sell.

LSTM falls in category of deep learning since it used the mechanism of ANN (Artificial Neural Network) to learn which is made specifically to deal with time series data (the sequence of each observation order by time). The origin of LSTM is RNN (Recurrent Neural Network) which differ from original feed forward neural network by its flow of signal where the output at time t will be used as input at time t+1, this is the reason why it is called “Recurrent” neural network. Given this mechanism, RNN can have better understanding of sequential data than normal feed forward neural network. LSTM modified RNN by adding 4 more components to RNN structure:1. Input gate 2.Forget gate 3. Output gate 4. Memory. The Input gate decide which new data should be recorded to the Memory. The Forget date decide which data should be remove from the Memory. And the output gate is to decide which data in the Memory should effect the prediction result. Since price of cryptocurrency is sequential data with time order, this type of data can be dealt by LSTM.

performance of trading bot phase 1

Clearly the result show that the bot usually buy at lower price and sell at higher price as the average percent increase of selling price with respect to buying price across coins are positive. For instance, the BTC trading bot, on average, usually sell at 0.1325% higher price than buying price. However, by looking at the average percent gain, none of the bot gain from trading as all values are negative. For instance, the BTC trading bot usually lost 0.3675% of fiat amount invest in each trade. This is because of the transaction fee. Even at moment when we bought the coin, we already lose.

Even though the current approach of trading bot perform well in terms of determining whether the price will go up or down in the future as the average percent increase from buying to selling price are all positive across every coins, the magnitude of determining the price change is not enough. The increase in selling price from buying price need to be high enough in the sense that after deducting the transaction fee charged when the cryptocurrency was bought and sold, we still profit.

Phase 2 of implementing cryptocurrency trading bot

In order to improve from phase 1 trading bot, the buying condition is modified a little bit. Currently it was, if the predicted price in the future is higher than current price, we buy. We modified this by adding some threshold that take into account the transaction fee in the sense that the predicted future price should be at least higher than current price by some threshold, then we buy. Using the function below we can calculate the gain in fiat currency using 3 inputs: buying price, selling price, and fiat spend.

For instance, the current cryptocurrency price is 20 unit fiat per unit crypto, the transaction fee is 0.25%,and we spend 100 unit fiat for this coin. The amount of coin we should get without considering the fee is 5 unit crypto. However, due to the transaction fee, we could only get 4.9875 unit crypto. This is why by just purchasing cryptocurrency, we already lose. Then if we decide to sell when the price rise to 20.01 unit fiat per unit crypto, the amount of fiat we got from selling all we have is 99.550 unit fiat instead of 99.799875 unit fiat due to transaction fee. We still result in lost even if we buy at lower price and sell at higher price.

But if the price rise to 21 unit fiat per unit crypto and we decide to sell. The amount of fiat we finally get is 104.4756 unit fiat, now we gain! Therefore, the increase in price should be high enough in order to have the profit left to pay for the fee.

But how high is high enough?

By using the function ‘required_magnitude’ below, we can find the minimum increase in price in order to be profit by keep increasing the price until the gain is higher than zero for the first time, this is the threshold or magnitude. This function will answer how much rise in price we need with respect to current price that will still result in gain after considering the transaction fee from buying and selling.

In this case, in order to be profit from buying the cryptocurrency at price 20 unit fiat per unit crypto, the price should be higher at least by 0.109 which is 20.109 unit fiat per unit crypto in order to be profit after selling including the transaction fee.

Finally we modify the buying signal of the trading bot to be…

performance of trading bot phase 2

Using the modification of buying condition, the bot performed better in terms of both average percent increase from buying price to selling price as well as average percent gain as shown in figures below.

compare average percent increase from buying price to selling price between 2 approaches
compare average percent gain in fiat currency between 2 approaches

Summary

My approach use sequential-based supervised machine learning model to predict future price of cryptocurrency. The buying signal is trigger when the predicted future price is higher than current price while selling signal is trigger when the predicted future price is lower than current price. Using the minimum threshold to determine whether the increase in price is high enough to get profit really helps improve the model performance. Even though the bot could provide only average gain in fiat currency of less than 0.5 percent, it is still a good start.

--

--