It is sometimes hard to keep up with cryptocurrency markets. Whether you install widgets on your smartphone or spend your day on Cryptowatch, you will always waste some time to access the data. Another option is to have an always on display with the latest prices. In this post, we will create an affordable, Inky-pHAT-based phidget to display a candlestick chart.

The Inky-pHAT is a 2.13" 212x104 e-paper (or e-ink) Raspberry Pi HAT made by Pimoroni. It is based on the GDEW0213Z16 e-paper display and has a small form factor specially suited for Zero or Zero W version. It is three-color (black, white and red), crisp and is readable in bright sunlight which makes it ideal for our purpose. In addition, as it is based on e-ink technology, displaying still pictures doesn't consume any power until refreshed.

The first step is to install the inky-phat library and test the display:

from PIL import Image
import inkyphat

inkyphat.set_image(Image.open("the-great-tsar.png"))

inkyphat.show()

inky-putin

Все работает так, как ожидалось!

Refreshing the display takes a merely 15 seconds. It also keeps wobbling during that time which makes it unsuitable for frequent refreshes. Overall, the display perform as expected!

Next step is to draw some financial charts on the display. We can take advantage of the Matplotlib libarary and build upon their finance_demo example to do something similar. Unfortunately, matplotlib.finance is now deprecated but has since been moved to the mpl_finance external module.

finance-demo

Output of finance_demo

First, we will pull some OHLC data from Kraken API using https://api.kraken.com/0/public/OHLC endpoint. This is easily achieved with the requests library:

import requests

def quotes_historical_kraken_ohlc(pair, since, interval=15):
    unix_time = since.strftime("%s")
    payload = {'pair': pair, 'since': unix_time, 'interval': interval}
    r = requests.get('https://api.kraken.com/0/public/OHLC', params=payload)

    def parse_ohlc(data):
        return [[int(l[0]), float(l[1]), float(l[2]), float(l[3]), float(l[4])] for l in data]

return parse_ohlc(r.json()['result'][pair])

Since some values returned by the API are encoded as String instead of double-precision floating-point Number (for arbitrary prevision?), we should parse returned items as floats.

We can then feed the candlestick_ohlc function from mpl_finance:

yesterday = datetime.now() - timedelta(hours=24)
quotes = quotes_historical_kraken_ohlc("XXBTZUSD", yesterday, interval=60)
candlestick_ohlc(ax, quotes, width=1)

After a lot of tweaks (e.g. using a tiny bitmap font), we got a decent result:

candlestick_ohlc.

This project is distributed as a Python 3 command line utility. You can test it without the inkyphat module installed using the --output argument which will generate an image file.

usage: cryptochart.py [-h] [--pair PAIR] [--flip] [--output OUTPUT]

optional arguments:
  -h, --help       show this help message and exit
  --pair PAIR      currency pair (default: XETHZUSD)
  --flip           rotate the display (default: False)
  --output OUTPUT  save plot as png (default: None)

inky-cryptochart.

Final result on the Inky-pHAT

The latest version is available on GitHub. Don't forget to make a pull request if you add some providers (BTC-e, Poloniex...) or new features.