Bitcoin and other cryptocurrencies are becoming increasingly popular, and I am interested in their applications and inner workings. As an experiment, I opened an account on Kraken and bought and sold a few bitcoins and lightcoins. Incidentally, I also managed to make a nice profit. With interest rates on savings accounts and bonds at low levels, a profit in bitcoin trading is an extra motivation for a larger experiment.

In order to trade in bitcoins, you need a strategy: when should you buy a bitcoin, and when should you sell it? I managed to make a profit in my initial experiment, but I did not use a particular strategy. I simply opened an app on my phone, and looked at a graph of the price of a bitcoin over the last day/week/month. If the current price seemed low to me, I bought one. Once the price increased enough for me to make a nice profit, I sold the bitcoin. Is it possible to learn the strategy that I applied in the past, and test how good it is?

Collecting data

Well, yes, that is possible, but first I needed to collect a few more samples of my own buy and sell decisions. Bitcoincharts offers a historical dataset of the bitcoin price on Kraken, and I used that as input. I made a simple script in Matlab showing a graph with the bitcoin price over the last month, week, and day. Then I showed a dialog asking me if I am interested in buying a bitcoin at the current price. The first figure below has an example of the three graphs, and a dialog asking me if I want to buy a bitcoin at the current price of 403.14 euros. The graphs shows that there was a steady increase in the last month, with a slight drop a few days ago, and a minor recovery during the current day. It seems to me that there is some potential for profiting from the price drop earlier this week, but the price is also high compared to the rest of the month. In this case, I would not buy a bitcoin (I am quite risk-averse). The second figure shows the same graphs, this time asking me if I want to sell a bitcoin, given a certain buy price. In this case I would not sell the bitcoin, since the amount of profit is not so high. With this Matlab tool, I collected 250 records containing my own buy and sell decisions.




Feature selection

Next I needed the features on which to base my decisions. As you have read above, I make decisions on trends on the graph: Is the bitcoin price increasing or decreasing? Is it high or low, compared to the day/week/month? Have I made enough profit? In the end, I settled on the following features:

  1. gradientDay: Fit a line of the form p=c*t+d through the data (p=price, t=time) and add the constant ‘c’ as a feature. Values c<0 indicate a decreasing trend, c>0 an increasing trend.
  2. gradientWeek: similar, but for a week
  3. gradientMonth: similar, but for a month
  4. avgDay/avgWeek: [average price of the current day]/[average price of the current week]
  5. avgDay/avgMonth: similar
  6. avgWeek/avgMonth: similar
  7. currentPrice/buyPrice: only for sell decisions

I expect that a buy decision is mainly based on features 1-3, and selling only on feature 7.

Sample data

This process yields the following dataset for buying (containing 251 decisions of which 124 were ‘yes’):


and for selling (containing 251 decisions of which 53 were ‘yes’):


Training a decision tree

I used the data collected above to train two decision trees: one for buying, and one for selling. To avoid overly complex trees, I pruned the trees to a certain level (determined by crossvalidation). I chose decision trees rather than other machine learning techniques, because it is very simple to understand the decision tree once it has been learned. The figure below show the decision tree for buying a bitcoin. Apparently I buy a bitcoin (label ‘1’ in the tree) when

  1. the price has clearly been decreasing this week (gradientWeek<-0.746). Or:
  2. the price has not clearly been decreasing  this week, but has shown a clear decrease in the past month (gradientWeek>= -0.746 and gradientMonth<0.747). Or:
  3. the price has been increasing this week, but decreasing the past month (gradientMonth>=0.747 and gradientWeek>=13.950).

Note also that the gradient-based features are included in the tree, and that the average-based features were pruned from the tree (as expected).


The decision tree for selling a bitcoin is simpler: I typically sell when I have reached a profit of at least 7.494%.



The first two criteria of the ‘buy tree’ reflect buying a bitcoin when its price has been decreasing in the recent past. If I run the algorithm above on the historical data (from January 2014 to January 2016), it immediately decides to buy a few bitcoins, because in January 2015 the price of a bitcoin quickly drops from around 690 euro to 320 euro. The losses incurred there by the decision trees are never fully recovered. But even though my buying strategy is flawed, it is nice to see that the decision tree captures it well. There are few other things I learned in this experiment that I can use in the future:

  1. I need a mechanism to prevent the algorithm from buying a lot of bitcoins in a short period of time.
  2. I should take my losses in certain situations. Shit happens!
  3. I want to maximize the amount of money invested, and decide not to buy a bitcoin if this maximum is exceeded.
  4. I am willing to lower my profit margin a bit, if this results in a less risky strategy.

Automatic trading with the Kraken API, Python and a RaspberryPi

I used the insights gained from this experiment and developed a better algorithm for trading bitcoins. As you probably can understand, I am not outlining the details here 😉 However, I would like to share the next phase of my experiment. Kraken has an API that you can use to interact with the exchange, and it includes the ability to place orders. Now that I have a working algorithm, I can use Kraken’s API to automatically buy and sell bitcoins! The algorithm has been running for a few days now on my RaspberryPi, and I can’t wait for the first automated buy. On the historical data, the algorithm trades about once every month, so I may have to patient for a few weeks…