Ventures of an ex indie game developer

Building a cryptocurrency trading bot tutorial, step 1/x

I'll be assuming you know merely the basics of Python3. If not, go here first.

We're going to use Bittrex, one of the biggest exchanges, based in Las Vegas. When we use Chrome to surf to, for instance https://bittrex.com/Market/Index?MarketName=BTC-VIA, and press F12, we find that one of the URLs loaded is:
https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-RLC&tickInterval=fiveMin
Aha! Let's curl it. Go do the command line (download MSYS if you use Windows).

$ curl 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-RLC&tickInterval=fiveMin'

The curl command outputs a whole bunch of data in JSON format. This shows us that we don't need any special HTTP requests, we can just fetch the data directly from Bittrex. Great! Over to coding then.

First we need a couple of nice utility libraries for Python. You install them using the pip command, which is included in the Python installation:

$ pip install requests
...
$ pip install matplotlib
...
$ pip install pandas
...

The first is used to neatly download data from the web, the second to plot graphs, the third is used to manipulate data matrices.

We're going to start out in Python's interactive mode, so just start Python from the command line.

$ python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> _

 Enter the URL and the code to download it:

>>> import requests
>>> url = 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-RLC&tickInterval=fiveMin'
>>> data = requests.get(url).json()

Now the coin data from Bittrex (as shown when you ran the curl command above) is stored in the variable called "data." We drag this data into Pandas, which is riddled with powerful functions for us to make good use of later on.

>>> import pandas as pd
>>> df = pd.DataFrame(data['result'])
>>> df
            BV         C         H         L         O                    T  \
0     0.306612  0.000190  0.000190  0.000188  0.000188  2018-01-16T19:15:00
1     0.067572  0.000190  0.000190  0.000190  0.000190  2018-01-16T19:20:00
2     2.641678  0.000184  0.000189  0.000184  0.000188  2018-01-16T19:25:00
3     0.449269  0.000185  0.000186  0.000185  0.000186  2018-01-16T19:30:00
4     0.248711  0.000188  0.000188  0.000185  0.000185  2018-01-16T19:35:00
...

And let's plot the close price over time (C over T in the data given to us by Bittrex):

>>> import matplotlib.pyplot as plt
>>> df.plot(x='T', y='C')
>>> plt.show()


Step one is complete -- I'm your father! Clean it up and put it in a file together with a shebang (first line below) and you've got yourself the start of a bot. Call the script bot1.py.

#!/usr/bin/env python3

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

url = 'https://bittrex.com/Api/v2.0/pub/market/GetTicks?marketName=BTC-RLC&tickInterval=fiveMin'
data = requests.get(url).json()
df = pd.DataFrame(data['result'])
df = df.astype({'T':'datetime64'})
df.plot(x='T', y='C')
plt.show()

(Note that I added a line to type convert the time from a string into a datetime object, so the plotting will show time stamps on the X-axis.) Next time we'll structure a bit and implement some analysis code. It's going to be dandy!

About the author

Mitt foto
Gothenburg, Sweden