
BTC YTD Cumulative Return based on MA strategy – It says it is time to Sell!
Algorithmic Trading with MATLAB®: Simple Lead/Lag EMA
This demo is an introduction to using MATLAB to develop and test a simple trading strategy using an exponential moving average.
% All rights reserved.
Load in some data (Excel)
Load YTD BTC prices from coinmarketcap.com into workspace
data = csvread(‘data/BTCYTD2.csv’,1,1);
testPts = floor(0.8*length(data(:,4)));
BTCClose = data(1:testPts,4);
BTCCloseV = data(testPts+1:end,4);
Develop a simple lead/lag technical indicator
We’ll use two exponentially weighted moving averages
[lead,lag]=movavg(BTCClose,10,20,‘e’);
plot([BTCClose,lead,lag]), grid on
legend(‘Close’,‘Lead’,‘Lag’,‘Location’,‘Best’)
Develop a trading signal and performance measures. We’ll assume 250 trading days per year.
s = zeros(size(BTCClose));
s(lead>lag) = 1; % Buy (long)
s(lead<lag) = -1; % Sell (short)
r = [0; s(1:end-1).*diff(BTCClose)]; % Return
sh = sqrt(250)*sharpe(r,0); % Annual Sharpe Ratio
Plot results
ax(1) = subplot(2,1,1);
plot([BTCClose,lead,lag]); grid on
legend(‘Close’,‘Lead’,‘Lag’,‘Location’,‘Best’)
title([‘First Pass Results, Annual Sharpe Ratio = ‘,num2str(sh,3)])
ax(2) = subplot(2,1,2);
plot([s,cumsum(r)]); grid on
legend(‘Position’,‘Cumulative Return’,‘Location’,‘Best’)
linkaxes(ax,‘x’)

Copyright 2017, The MathWorks, Inc.

Subscribe to our freeNewsletter
Subscribe to our mailing list to get more free math analysis of Crypto market. Who knows, maybe mathematics leads you to a full understanding of financial markets :)
Congratulations on your choice and we are grateful for your decision!