-
Notifications
You must be signed in to change notification settings - Fork 6
/
mempool-vs-btc.py
147 lines (97 loc) · 3.51 KB
/
mempool-vs-btc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
# Blah
from argparse import ArgumentParser
import os.path
import logging
import pandas as pd
from pprint import pprint
import sys
import requests
import utils
# API
import hmac
import time
import hashlib
from future.builtins import bytes
import urllib.request
import json
# Charting
import talib as ta
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle
from PIL import Image
# Configure Logging
FORMAT = '%(asctime)s -- %(levelname)s -- %(module)s %(lineno)d -- %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger('root')
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
FILENAME = SCRIPT_DIR+"/mempool-vs-btc.png"
LOGO_PATH = SCRIPT_DIR+'/media/wp_logo.jpg'
# API URL request
url = 'https://api.bitfinex.com/v2/candles/trade:1D:tBTCUSD/hist?limit=200&start=1476831600000'
request = json.loads(requests.get(url).text)
url = 'https://api.bitfinex.com/v2/candles/trade:1D:tBTCUSD/hist?limit=200&end='+str(request[len(request)-1][0])
request2 = json.loads(requests.get(url).text)
url = 'https://api.bitfinex.com/v2/candles/trade:1D:tBTCUSD/hist?limit=200&end='+str(request2[len(request2)-1][0])
request3 = json.loads(requests.get(url).text)
data_set = request + request2 + request3
candles = pd.read_json(json.dumps(data_set))
candles.rename(columns={0:'date', 1:'open', 2:'close', 3:'high', 4:'low', 5:'volume'}, inplace=True)
candles['date'] = pd.to_datetime( candles['date'], unit='ms' )
candles.set_index(candles['date'], inplace=True)
# Sort it
candles.sort_index(inplace=True)
url = "https://api.blockchain.info/charts/mempool-size?format=json×pan=all"
decodeddata = json.loads(requests.get(url).text)
mempool = pd.DataFrame(columns=('date','bytes'))
for data in decodeddata['values']:
mempool.loc[data['x']] = [data['x'], data['y']]
# Set the index
mempool['date'] = pd.to_datetime( mempool['date'], unit='s' )
# Set the index
mempool.set_index(mempool['date'], inplace=True)
del mempool['date']
# Sort it
mempool.sort_index(inplace=True)
mempool = mempool.resample('1D', closed='left', label='left').agg({'bytes': 'mean' })
mempool = mempool.ix[candles.index[0]:]
#pprint(len(decodeddata))
#pprint(decodeddata)
# pprint(decodeddata)
pprint(candles.head())
pprint(mempool.head())
# Plot chart from csv
# Enable a Grid
plt.rc('axes', grid=True)
# Set Grid preferences
plt.rc('grid', color='0.75', linestyle='-', linewidth=0.5)
# Create a figure, 16 inches by 12 inches
fig = plt.figure(facecolor='white', figsize=(16, 10), dpi=120)
# Draw 3 rectangles
rect_chart = [0.05, 0.05, 0.9, 0.9]
# rect2 = [left, 0.27, width, 0.17]
ax1 = fig.add_axes(rect_chart, facecolor='#f6f6f6')
ax1.set_xlabel('date')
# ax2 = fig.add_axes(rect2, facecolor='#f6f6f6', sharex=ax1)
ax2t = ax1.twinx()
ax1.set_title('Bitcoin Price / Mempool Size - Whalepool.io')
ax1.xaxis_date()
mempool['bytes_ema'] = mempool['bytes'].ewm(span=8,min_periods=8,adjust=False).mean()
ax1.plot( mempool.index.values, mempool['bytes_ema'].values, color='b')
ax1.set_ylabel('bytes', color='b')
ax2t.plot( candles['date'].values, candles['close'].values, color="g")
ax2t.set_ylabel('bitcoin price', color='g')
im = Image.open(LOGO_PATH)
fig.figimage(im, 50, (fig.bbox.ymax - im.size[1])-10)
plt.savefig(FILENAME)
n = utils.Notify()
n.telegram({
'chat_id': '@whalepoolbtcfeed',
'message': 'Bitcoin mempool vs btc',
'picture': FILENAME
})
print('Saved: '+FILENAME)
os.remove(FILENAME)
sys.exit()