Last active
January 5, 2021 14:57
-
-
Save fferri/1a822290b7ac9bea73597ad46bd8ad2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
market, period = 'XBTEUR', 60 | |
imgw, imgh = 22, 22 | |
import krakenex | |
from pykrakenapi import KrakenAPI | |
api = krakenex.API() | |
k = KrakenAPI(api) | |
ohlc, last = k.get_ohlc_data(market, period) | |
a = ohlc.to_numpy() | |
a = a[:imgw,:] | |
rows, cols = a.shape | |
a_open, a_high, a_low, a_close = a[:,1], a[:,2], a[:,3], a[:,4] | |
a_min, a_max = min(a_open.min(), a_close.min()), max(a_open.max(), a_close.max()) | |
n_open = (a_open - a_min) / (a_max - a_min) | |
n_high = (a_high - a_min) / (a_max - a_min) | |
n_low = (a_low - a_min) / (a_max - a_min) | |
n_close = (a_close - a_min) / (a_max - a_min) | |
from PIL import Image, ImageDraw | |
im = Image.new('RGB', (imgw, imgh)) | |
draw = ImageDraw.Draw(im) | |
for x in range(imgw): | |
if n_open[x] > n_close[x]: | |
c, ch = (255, 0, 0), (127, 0, 0) | |
else: | |
c, ch = (0, 255, 0), (0, 127, 0) | |
draw.line((imgw-x, (1-n_low[x])*imgh, imgw-x, (1-n_high[x])*imgh), fill=ch) | |
draw.line((imgw-x, (1-n_open[x])*imgh, imgw-x, (1-n_close[x])*imgh), fill=c) | |
#im.save('kraken_xbteur.png', format='PNG') | |
from io import BytesIO | |
imdata = BytesIO() | |
im.save(imdata, format='PNG') | |
import base64 | |
import json | |
print(json.dumps({ | |
'text': f'{market} {int(a[0,4])}', | |
'icon_data': base64.b64encode(imdata.getvalue()).decode('ascii'), | |
'background_color': '0,0,0,255', | |
'font_color': '200,200,200,255', | |
'font_size': 12 | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment