Cryptocurrency Data Analysis Part III: Backtesting, Evaluating and Optimising a Trading Strategy

Ed Silantyev
Oct 13, 2017 · 9 min read

Loading Data

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = CryptoData(symbol = 'BTC_LTC', frequency = 300)

Simple Moving Average Crossover

Source: Wikipedia
df['SMA_1000'] = df['close'].rolling(1000).mean()
df['SMA_5000'] = df['close'].rolling(5000).mean()
df[['close','SMA_1000','SMA_5000']][270000:].plot(figsize = (16,10))
ma_df = test_ma(df, 1000, 5000).dropna()
ma_df['regime']plot(figsize=(16,5))
ma_df[['Market','Strategy']].iloc[-1]
Market       0.422360
Strategy 10.384434
Name: 2017-10-11 13:10:00, dtype: float64
ma_df[['Market','Strategy']][200000:].plot(figsize = (16,10))

Optimising and Visualising Strategy Parameters

leads = np.arange(100, 4100, 100)
lags = np.arange(4100, 8100, 100)
lead_lags = [[lead,lag] for lead in leads for lag in lags]
pnls = pd.DataFrame(index=lags,columns = leads)
for lead, lag in lead_lags:
pnls[lead][lag] = test_ma(df, lead, lag)['Strategy'][-1]
print(lead,lag,pnls[lead][lag])
100 4100 0.109696350799
100 4200 0.168002440847
100 4300 0.129377447402
100 4400 0.10318276452
100 4500 0.165473622191
100 4600 0.250176153044
100 4700 0.17482270303
100 4800 0.192016648903
100 4900 0.261229591629
100 5000 0.182849263658
100 5100 1.4943823881
100 5200 1.94480944908
100 5300 2.71494860071
100 5400 3.88657722516
100 5500 2.70262997391
100 5600 2.75105468698
...
PNLs = pnls[pnls.columns].astype(float)
plt.subplots(figsize = (14,10))
sns.heatmap(PNLs,cmap=’PiYG’)
PNLs.max()
100     10.864477
200 19.334406
300 22.269144
400 32.638814
500 44.395492
600 42.225812
700 35.842106
800 34.182762
900 44.826745
1000 37.054376
1100 30.945689
1200 22.989689
1300 20.970539
1400 23.822840
1500 20.404080
1600 17.185955
1700 13.923057
1800 15.732017
1900 12.414719
2000 14.433515
2100 10.450186
2200 9.771460
2300 11.302012
2400 14.350452
2500 12.639792
2600 14.260199
2700 19.634837
2800 22.676327
2900 20.287311
3000 14.714964
3100 15.916137
3200 15.177753
3300 15.109187
3400 18.476793
3500 15.270295
3600 16.246062
3700 19.790292
3800 24.930189
3900 22.171754
4000 24.840235
dtype: float64

Subtleties

Conclusion

Ed Silantyev

Written by

Perpetual Beta. @fsp_network creator. Electronic trading systems developer. Cryptocurrency market microstructure specialist.📉📈

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade