Ventures of an ex indie game developer

Building a cryptocurrency trading bot tutorial, step 2/x

Ok, so we've downloaded and plotted some data. Over to the analysis. One of the most important things about trading is knowing if you're in a bullish or bearish trend, which is what this part of the tutorial is about.

In this part of the tutorial, we'll look at the Bitcoin currency against something called the Tether currency. Which is essentially a cryptocurrency hooked to the US dollar. Bitcoin on most sites has the trading symbol BTC, Tether has the trading symbol USDT. On Bittrex the product "Bitcoin over Tether" is called "USDT-BTC".

First we clean up our act and download the klines inside a function, let's call it download_klines(). So this is our starting code:

#!/usr/bin/env python3

import requests
from matplotlib import pyplot as plt
import pandas as pd

def download_klines(symbol):
    url = 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=%s&tickInterval=fiveMin' % symbol
    data = requests.get(url).json()
    df = pd.DataFrame(data['result'])
    return df.astype({'T':'datetime64[ns]'})

def analyze_and_plot(symbol):
    pass

analyze_and_plot('USDT-BTC')
plt.show()

So with everything is setup for you, including at the end where you see the analyze_and_plot() function call, you only need to fill in the actual analysis code. That code is going to replace where it says "pass" above. First of all, we want all our curves inside a single chart, so we'll create a coordinate system for that in matplotlib, like so:

    fig,ax = plt.subplots()

Then we download the bitcoin data and put it in a Pandas data frame, which we inventively call "df". Then we copy the close values from the column named "C" into something that is easier to understand for the reader, say "USDT-BTC" (which happens to be in the symbol variable):

    df = download_klines(symbol)
    df[symbol] = df['C']

Good. Now there are a number of different ways to analyze a curve. Some of the more common include something called MA and EMA, which I suppose is short for Mean Average and Exponential Mean Average. These are mathematical functions which help you determine trends of the curve. We'll be using MA-10, which in each point yields an arithmetic mean of the last 10 preceding points. We'll also be using EMA-30, which is an exponential arithmetic mean of the last 30 points. And EMA-200.

The code is using functions built into Pandas/Numpy, so it's very easy to do. Here we create three new columns in our matrix:

    df['ma-10'] = df['C'].rolling(10).mean()
    df['ema-30'] = df['C'].ewm(span=30).mean()
    df['trend'] = df['C'].ewm(span=200).mean()

And as you've seen before, plotting is just as easy:

    df.plot(x='T', y=symbol, ax=ax)
    df.plot(x='T', y='ma-10', ax=ax)
    df.plot(x='T', y='ema-30', ax=ax)
    df.plot(x='T', y='trend', ax=ax)

Run it! The resulting output should look something like this:


In the bearish trends you'd want to be aggressive, and in the bullish ones a bit more cautious. Also, when ma-10 is above ema-30, you'd want to be selling as late as possible, but not after ma-10 crosses and goes below ema-30.

Next part of the tutorial will show you how to do the actual trades with whatever analysis you've done.

About the author

Mitt foto
Gothenburg, Sweden