Step 2 — Wrangle that data

The next step is to consolidate your raw data extracts into a usable format. Pandas is a great way to organize your data. A simple way to combine the data extracts is by looping through the directory where you store them and appending them all to a central dataframe. See the illustrative snippet below.

Example of a loop to combine raw extracts

Use this point as an opportunity to clean your data. This means sorting by timestamp, checking for NaNs or Null values to fill or remove. You will also want to remove unnecessary data and transform the format so that it can easily be broken down into arrays based on timestamp, currency and price.

Step 3— Optimize!

Now for the fun part — running a Monte Carlo style simulation to calibrate the ideal portfolio weightings! The idea here is to use random number generation to simulate over 30,000 portfolios with various holding configurations. This example uses Bitcoin, Ethereum, Ethereum Classic, Litecoin, Stellar, Monero and ZCash. The time period used was January 1 to November 1, 2017.

Example of optimization process

The goal is to find the portfolio configuration with the highest Sharpe ratio, a ratio of a portfolio’s return relative to its volatility.

Plotting the results using matplotlib should give you something like this:

Results of an optimization simulation with a red star on the simulation with the highest Sharpe and green star on the simulation with the lowest volatility. The color bar is relative Sharpe ratio.

The results from this simulation can inform your next buy or sell decision. Looking at a visualization of the output , the simulation with the highest Sharpe ratio is highlighted with a red star and the simulation with the lowest volatility is highlighted with a green star. For those interested, the simulation with the best historical Sharpe had the following weights :

ETH 48.0210%

LTC 37.8341%

ETC 6.9683%

STR 4.2331%

BTC 1.4463%

ZEC 0.8631%

XMR 0.6341%

These results do not reflect the current market! The model shown was limited to data up to late October and therefore would likely look very different when calculated today given the recent BTC rally.

How can you use this in practice? — Rebalance Regularly!

Going forward, this technique can be used on a regular basis to rebalance your portfolio. When backtesting this strategy, it is important to consider how often you should rebalance. More rebalancing may lead to a more finely-tuned approach but it also means higher trading fees. Also consider how much historic data is still relevant to today’s trends. For example, Bitcoin data from 2013 might not be that meaningful in the current market environment.

Thanks for taking the time to read this article. I hope to post more in the future about using Python to improve the investing process.

Happy Trading!