A Different Approach for Bitcoin Day Trading

Sertac Ozker
Game of Life
Published in
6 min readOct 10, 2018

Neural Network for a Classification Problem

For the last one year, there are possibly very few people that didn’t hear about cryptocurrencies or especially Bitcoin. In January 2018, when the hype was at the top, total market cap for cryptocurrencies reached 834 billion USD[1]. If this was the GDP of a country in that time, it would be in top 20, outstripping the Netherlands.

However, it continuously fell afterward and according to coinmarketcap.com as of September 2018, total market cap hit new bottom levels at around 193 billion USD (approximately November 2017 levels).

Even though the price of Bitcoin is constantly falling for the last 8 months, the market is still highly volatile. In the graph below you can see the daily price volatility of Bitcoin (daily max price / daily min price). As you can clearly see 8% change in the day is almost normal. I will try to build an ML algorithm that will exploit this volatility to earn money.

Figure 1: Bitcoin daily price volatility in percentage

Trending Cryptocurrency Hub Articles:

1. It’s Time to Take Back Control! How Pocketinns Will Democratize Trade

2. Thirteen questions for the Blockchain as We Enter 2018

3. The Crypto game

4. A Crypto that will Pay You

I’m willing to make a very simple and easy to use model. My aim is not to hold BTC for a long time as its price is highly volatile and I want to keep my money in USD. I will approach this problem as a classification problem. First I will choose among some of the technical analysis indicators to use. After that in the training data, I will find the minutes that BTC price increased 1% in the following 30 minutes (according to close price) and label them. Then in the test data, I will let the algorithm label each minute. If the algorithm labels a minute (meaning a buy signal) I will accept that I’ve bought BTC with the closing price.

Since I believe that 1-hour or 30-minutes data is the best for technical analysis, I will use 30-minutes data for my algorithm.

Figure 2: 30-minutes Bitcoin price graph from tradingview.com

In my simulation, after buying the BTC, I will give a sell order just 1% above the price I’ve bought the BTC. If the highest price in the following minutes goes over this price, I will accept that BTC is sold with 1% profit. If the price falls under 5% less than the price I’ve bought, I will accept that stop-loss is activated and the BTC is sold with 5% loss. I will also pay 0.05% commission for each transaction (both buy and sell).

For this simulation, because I’m using 30-minutes data for my algorithm, I can just have buy signals on the beginning or 30th minutes of an hour (2 data points for each minute). However on reality I can take the 30-minutes data each minute and arrange the parameters accordingly; so I can check for buy signals each minute.

Acquiring the Data

The dataset is acquired using Binance API. Binance is selected because as of October 2018 it is the biggest exchange according to volume[2] and has a very good API for bots[3]. The data we receive from Binance API has these columns:

Time Start: An integer showing the start of the minute which can be converted to day-time-hour-minute.

Open Price: The price of BTC against USD at the beginning of the minute.

Highest Price: The maximum price BTC reached against USD during the minute.

Lowest Price: The minimum price BTC reached against USD during the minute.

Close Price: The price of BTC against USD at the end of the minute.

Volume: Traded volume in the minute as BTC.

Time End: End of minutes as an integer in Time Start.

Volume USD: Traded volume in the minute as USD.

Number of Trades: How many trades have been made in the minute.

Open Order Volume: Amount of placed orders in the minute as BTC.

Open Order Volume USD: Amount of placed orders in the minute as USD.

The data is very clean and doesn’t have any missing points.

I have downloaded 1-minute and 30-minutes data and exported them to an excel file using the code below.

Calculating the Technical Indicators

I have chosen the technical indicators that I will use according to my previous experience and also some correlation check that I’ve done before. I will not write down this correlation study here since it’s too long but at the end I’ve decided to use indicators below. You can check investopedia.com if you want to have more information about them.

Volume Parameters: “volume_usd”, “number_of_trades” and “open_order_volume_usd” are used as they have been received from Binance. I’ve added “_adjusted” to these columns as if you want to use the code in minute basis, you can arrange the values accordingly (if this is the 18th minute in this half hour, you can multiply these values with 30/18).

Exponential Moving Average (EMA): I have calculated the ema-9 and ema-25 with the data. However I cannot use these values as they are since they show a price at the moment. Instead I’ve divided these values with the price in that minute to get a “ratio”. I’ve also calculated the slope of these ema values (named as “ema_inc”).

Moving Average Convergence Divergence (MACD): macd calculated using 12 and 26 ema values.

Bollinger Bands: BB up and bottom calculated for a window of 20 (10 hours) and 2 standard deviation. Then I’ve divided these values with the price in that minute to get a ratio.

Relative Strength Indicator (RSI): rsi calculated for a window of 14.

Training the Algorithm

I’ve built a neural network using Keras library. In the algorithm 53 days (days 0–53) of data used for training while following 7 days (days 54–60) was used for validation. Then using the best weights, algorithm predicted the following 7 days (days 61–67). This process was repeated by moving 7 days at each step. This made sure that algorithm never before saw the 7 days data that it’s predicting.

At the end of the training classification report and F-Beta score is calculated using Beta as 0.01, because I’ve focused on precision rather than recall. One single true prediction makes me gain 1% in USD. When thinking about the current interest rates for USD, this is better than holding the money in the bank. On the other and a wrong prediction can cause me to lose 5%. I don’t want the algorithm make many guesses but true guesses.

Algorithm predicted 23 buy signals with 0.80 F-Beta score. I’m satisfied with the results and decided to test the buy signals.

Testing the Results

I will test what would have happened to my 200 USD if I used these labels as buying points. The algorithm will buy 100 USD worth of BTC when it receives a buy signal. I will let the algorithm hold only one position at a time so it cannot buy a new position until selling the current holding. I will use the 1-minute data to make this simulation.

When translated into yearly interest rate, it’s equal to 23% which is an incredible rate for USD.

By using the code at the bottom of this section, we saw that algorithm made 17 successful transactions without activating the stop-loss :) I gave it 200 USD and in 4 months it made it 215.3 USD which is equal to 7.7% increase. When translated into yearly interest rate, it’s equal to 23% which is an incredible rate for USD.

Figure 3: What happened to my 200 USD in time? :)
Figure 4: Bitcoin price for the same time period

You are free to use the code to see results for yourselves but don’t forget that no algorithm can guarantee to make you win constantly. Please do not blame me if you lose some money with this code.

Resources

[1] https://coinmarketcap.com/charts/

[2] https://coinmarketcap.com/rankings/exchanges/

[3] https://python-binance.readthedocs.io/en/latest/overview.html

--

--