Cryptocurrency Analysis with Python - MACD

Dec 17, 2017

Cryptocurrencies are becoming mainstream so I’ve decided to spend the weekend learning about it.
I’ve hacked together the
code
to download daily Bitcoin prices and apply a simple trading strategy to it.

Note that there already exists tools for performing this kind of analysis, eg.
tradeview, but this way enables more in-depth analysis.

Check out my next blog post,
where I describe buy and hold strategy and follow me on twitter to get latest updates.

Disclaimer

I am not a trader and this blog post is not a financial advice. This is purely introductory knowledge.
The conclusion here can be misleading as we analyze the time period with immense growth.

volumefrom, the volume in the base currency that things are traded into,

volumeto, the volume in the currency that is being traded.

We download the data and store it to a file.

importrequestsfromdatetimeimportdatetimedefget_filename(from_symbol,to_symbol,exchange,datetime_interval,download_date):return'%s_%s_%s_%s_%s.csv'%(from_symbol,to_symbol,exchange,datetime_interval,download_date)defdownload_data(from_symbol,to_symbol,exchange,datetime_interval):supported_intervals={'minute','hour','day'}assertdatetime_intervalinsupported_intervals,\
'datetime_interval should be one of %s'%supported_intervalsprint('Downloading %s trading data for %s %s from %s'%(datetime_interval,from_symbol,to_symbol,exchange))base_url='https://min-api.cryptocompare.com/data/histo'url='%s%s'%(base_url,datetime_interval)params={'fsym':from_symbol,'tsym':to_symbol,'limit':2000,'aggregate':1,'e':exchange}request=requests.get(url,params=params)data=request.json()returndatadefconvert_to_dataframe(data):df=pd.io.json.json_normalize(data,['Data'])df['datetime']=pd.to_datetime(df.time,unit='s')df=df[['datetime','low','high','open','close','volumefrom','volumeto']]returndfdeffilter_empty_datapoints(df):indices=df[df.sum(axis=1)==0].indexprint('Filtering %d empty datapoints'%indices.shape[0])df=df.drop(indices)returndfdata=download_data(from_symbol,to_symbol,exchange,datetime_interval)df=convert_to_dataframe(data)df=filter_empty_datapoints(df)current_datetime=datetime.now().date().isoformat()filename=get_filename(from_symbol,to_symbol,exchange,datetime_interval,current_datetime)print('Saving data to %s'%filename)df.to_csv(filename,index=False)

Read the data

importpandasaspddefread_dataset(filename):print('Reading data from %s'%filename)df=pd.read_csv(filename)df.datetime=pd.to_datetime(df.datetime)# change type from object to datetimedf=df.set_index('datetime')df=df.sort_index()# sort by datetimeprint(df.shape)returndfdf=read_dataset(filename)

Reading data from BTC_USD_Bitstamp_day_2017-12-25.csv
(1124, 6)

Trading strategy

A trading strategy is a set of objective rules defining the conditions that must be met for a trade entry and exit to occur.

We are going to apply Moving Average Convergence Divergence (MACD) trading strategy, which is a popular indicator used in technical analysis.
MACD calculates two moving averages of varying lengths to identify trend direction and duration.
Then, it takes the difference in values between those two moving averages (MACD line)
and an exponential moving average (signal line) of those moving averages.
Tradeview has a great blog post about MACD.

Visualizing trading strategy

We use bokeh interactive charts to plot the data.

The line graph shows daily closing prices with candlesticks (zoom in).
A candlestick displays the high, low, opening and closing prices
for a specific period. Tradeview has a great blogpost about
candlestick graph.

Below the line graph we plot the MACD strategy with MACD line (blue), signal line (orange) and histogram (purple).