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

`import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport seaborn as sns%matplotlib inline`
`df = CryptoData(symbol = 'BTC_LTC', frequency = 300)`

# Simple Moving Average Crossover

`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.422360Strategy    10.384434Name: 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.109696350799100 4200 0.168002440847100 4300 0.129377447402100 4400 0.10318276452100 4500 0.165473622191100 4600 0.250176153044100 4700 0.17482270303100 4800 0.192016648903100 4900 0.261229591629100 5000 0.182849263658100 5100 1.4943823881100 5200 1.94480944908100 5300 2.71494860071100 5400 3.88657722516100 5500 2.70262997391100 5600 2.75105468698...`
`PNLs = pnls[pnls.columns].astype(float)plt.subplots(figsize = (14,10))sns.heatmap(PNLs,cmap=’PiYG’)`
`PNLs.max()`
`100     10.864477200     19.334406300     22.269144400     32.638814500     44.395492600     42.225812700     35.842106800     34.182762900     44.8267451000    37.0543761100    30.9456891200    22.9896891300    20.9705391400    23.8228401500    20.4040801600    17.1859551700    13.9230571800    15.7320171900    12.4147192000    14.4335152100    10.4501862200     9.7714602300    11.3020122400    14.3504522500    12.6397922600    14.2601992700    19.6348372800    22.6763272900    20.2873113000    14.7149643100    15.9161373200    15.1777533300    15.1091873400    18.4767933500    15.2702953600    16.2460623700    19.7902923800    24.9301893900    22.1717544000    24.840235dtype: float64`

# Conclusion

Written by

## Ed Silantyev

#### 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