Skip to content

ktslabbie/Midas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Midas - A machine learning-based Bitcoin trading bot

Midas is a bot that implements a trading algorithm based on technical analysis, using supervised machine learning on historical data to train its parameters. It was developed for the now-defunct Mt. Gox Bitcoin exchange, so it isn't actually usable in its current form (I haven't gotten around to adding support for other exchanges, mainly because it would also require running a server keeping short histories of ticker data, since none of the current main exchange APIs provide this). Training is performed by using linear regression and k-fold cross-validation to find parameters that strike an optimal balance between generality (i.e. simple enough to likely be profitable in the unknown future) and profitability. It is fundamentally an EMA-based trading algorithm, but includes additional indicators and conditionals to react to certain situations more quickly and effectively. On the whole, the basic idea of the algorithm is to trade conservatively during low volume, low volatility periods, and more aggressively during high volatility periods to exploit large price swings.

The trading algorithm

The algorithm operates under 3 basic assumptions:

  1. The price trends, so we ride upward trends as long as possible, while cutting short downward trends as soon as possible. This is the assumption underlying most basic moving average-based trading strategies. We use multiple, time-shifted exponential moving averages and make decisions dependent on how many of these EMAs cross for more stable prediction compared to relying on a single EMA, which is more sensitive to price anomalies.
  2. Sudden, extreme price movements up or down tend to lead to short- or long-term trend reversals. A MACD cross with its signal line is good for predicting trend changes based on these signals, but is very noise-sensitive. So we introduce an additional conditional - the speed (or angle) of crossing - to filter out false predictions. Also, rather than MACD, we use PPO (Percentage Price Oscillator), which is a price-normalized version of MACD (essential when absolute value changes as much across history as Bitcoin does).
  3. Extreme rises/dives in price tend to be fueled by panic and are unsustainable - a significant rebound is expected to occur shortly after. We judge this based on the speed at which PPO rises or falls ({value at current sample} - {value at previous sample}). If it rises at a speed beyond a certain threshold, we sell. If it falls at a speed beyond a certain threshold, we buy. History tells us that more often than not, the price will rebound and we can re-buy/re-sell at a profit.

Training and evaluation

The project includes a training suite, which is essentially an optimization and back-testing tool written in Java that evaluates the algorithm on 14 months (Jul. 2012 to Sep. 2013) of trading data from Mt. Gox. The module includes a runnable .jar and its source code (as a Maven project). The .jar can be run by typing java -jar MidasTrainingSuite.jar in a command line window. There are no arguments - all settings are set in the midas.properties file.

The training suite includes an optimization mode, where we run the algorithm multiple times, over a discrete range of eight parameters, within given time periods, and find the output parameters yielding the highest profit. Statistically, optimizing the parameters over a single time period (i.e. the full 14 months) is less likely to yield profitable results in the future, because we end up overfitting the algorithm to this particular data. Instead, we want to obtain an ideal middle-ground between profitability and generality, so as to optimize the expected future profit. The following steps are applied:

  1. The data is split up into a number of smaller periods of a pre-defined length.
  2. For each time period, we rank the trading results for each possible parameter combination over this period according to profitability.
  3. We end up with a ranking where every parameter combination for each time period is assigned a rank.
  4. For each unique parameter combination, we sum the ranking values obtained for each time period to obtain a cumulative ranking.
  5. The parameter combination with the highest cumulative rank (i.e. the lowest number) is chosen as the ideal setting.

No bias is added for actual profitability of time periods to avoid skewing the parameter selection in favor of anomalies (e.g. the April 2013 bubble crash and its following extreme volatility). This will increase the overall likelihood that the algorithm will continue to be profitable in the future, as it has been shown to be the most likely to be profitable on average regardless of the current market situation. In the default case, we test over 14 months (Jul. 2012 to Sep. 2013) of data from Mt. Gox split up into 2-month periods, which includes various types of possible future market situations (low-volume lulls, slow run-ups, flash-crashes, extreme volatility).

During training, three distinct "profitability fields" emerged, i.e. parameter settings tended to converge to three distinct optima in the eight-dimensional parameter space. One optimum has low sell thresholds, so is quite reluctant to sell, and makes few trades. The second optimum has more neutral settings. The last optimum sells quickly, and makes a lot of trades. I've included these in the bot as "low-frequency", "neutral" and "high-frequency" default settings (see the results file).

Results

Results (ranking for each run, trades belonging to the highest ranked parameter combination) are recorded and viewable in the log file generated by the back-testing tool (Midas.log). Some pre-run results are included in the results file. In the basic setup as explained above, maximizing expected future profit by evaluating over 2-month periods, testing 1H, 2H, 3H and 4H sample intervals, and assuming a flat 0.6% trading fee, this algorithm outperforms simple buy-and-hold and EMA trend following algorithm baselines by almost 13x over the full 14 months in terms of USD profit. Profit is calculated by comparing the starting USD price of 10 BTC (Jul. 2012) with the USD value at the end of the period (Sep. 2013).

Results are better when optimized over the single, full 14-month period, but this has a significantly lower likelihood of continuing to be as profitable in the future due to data overfitting. Also, the price data is based on last traded price (which can be ask or bid), so there is bound to be some slippage when an actual trade is made on the exchange. Hence some more realistic results given a slippage correction on top of the trading fee are also included.

Of course, despite cross-fold validation, the real evaluation of the algorithm must be done on new, unseen data. Starting roughly where the simulation left off with 10 BTC, 0.88% flat fee, a roughly 50% profit over a two-month period was observed, slightly outperforming a buy-and-hold baseline. Considering that an EMA trading algorithm is essentially a hedge against price drops, this is excellent performance given a consistently rising market. Notably, for all three default settings, the bottom of the Silkroad-seizure crash was correctly predicted, where a buy order was made. Detailed trading data for this period can be found in the results file.

Midas trading bot (Chrome plugin)

The algorithm is implemented into a Chrome plugin that keeps track of price, updates parameters, and can make trades on the exchange in real-time. It was originally forked from another open source bot, but more or less completely rewritten. It's found in the Midas module, and can be installed just by downloading the folder and adding it to your Chrome extensions as an unpacked extension.

About

Machine learning-based Bitcoin trading

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published