-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglassnode.py
192 lines (141 loc) · 4.86 KB
/
glassnode.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from datetime import date, datetime, timedelta
import pydf
import pandas as pd
from glassnode_client import GlassnodeClient
GN_API_KEY = "xxx"
gn = GlassnodeClient(api_key=GN_API_KEY)
DAYS = 7
def get_sopr():
sopr = gn.get(
"https://api.glassnode.com/v1/metrics/indicators/sopr",
a="BTC",
s=(date.today() - timedelta(days=DAYS)).strftime("%Y-%m-%d"),
i="24h",
in_df=False,
)
d = {
"Date": [
datetime.fromtimestamp(entry["t"]).strftime("%Y-%m-%d") for entry in sopr
],
"Value": [entry["v"] for entry in sopr],
}
df = pd.DataFrame(data=d)
return df.to_html(index=False)
def get_resource(uri, asset):
rsp = gn.get(
"https://api.glassnode.com/" + uri,
a=asset,
s=(date.today() - timedelta(days=DAYS)).strftime("%Y-%m-%d"),
i="24h",
)
if "ssr" in uri:
return (rsp[-1]["o"]["v"], rsp[-2]["o"]["v"], rsp[0]["o"]["v"])
return (rsp[-1]["v"], rsp[-2]["v"], rsp[0]["v"])
def get_nupl():
return get_resource("v1/metrics/indicators/net_unrealized_profit_loss", "BTC")
def get_s_to_f():
return get_resource("v1/metrics/indicators/stock_to_flow_deflection", "BTC")
def get_mvrv_z():
return get_resource("v1/metrics/market/mvrv_z_score", "BTC")
def get_puell():
return get_resource("v1/metrics/indicators/puell_multiple", "BTC")
def get_reserve_risk():
return get_resource("v1/metrics/indicators/reserve_risk", "BTC")
def get_ssr():
return get_resource("v1/metrics/indicators/ssr", "BTC")
def get_ex_inflow_volume(asset="BTC"):
return get_resource(
"v1/metrics/transactions/transfers_volume_to_exchanges_sum", asset
)
def get_ex_outflow_volume(asset="BTC"):
return get_resource(
"v1/metrics/transactions/transfers_volume_from_exchanges_sum", asset
)
def get_ex_deposits(asset="BTC"):
return get_resource("v1/metrics/transactions/transfers_to_exchanges_count", asset)
def get_ex_withdrawals(asset="BTC"):
return get_resource("v1/metrics/transactions/transfers_from_exchanges_count", asset)
def get_ex_balance(asset="BTC"):
return get_resource("v1/metrics/distribution/balance_exchanges", asset)
def get_btc_key_stats():
nupl = get_nupl()
s_to_f = get_s_to_f()
mvrz_z = get_mvrv_z()
puell = get_puell()
reserve_risk = get_reserve_risk()
ssr = get_ssr()
last_value = [nupl[0], s_to_f[0], mvrz_z[0], puell[0], reserve_risk[0], ssr[0]]
one_day_change = [
get_change(nupl[1], nupl[0]),
get_change(s_to_f[1], s_to_f[0]),
get_change(mvrz_z[1], mvrz_z[0]),
get_change(puell[1], puell[0]),
get_change(reserve_risk[1], reserve_risk[0]),
get_change(ssr[1], ssr[0]),
]
seven_days_change = [
get_change(nupl[2], nupl[0]),
get_change(s_to_f[2], s_to_f[0]),
get_change(mvrz_z[2], mvrz_z[0]),
get_change(puell[2], puell[0]),
get_change(reserve_risk[2], reserve_risk[0]),
get_change(ssr[2], ssr[0]),
]
d = {
"Metric": [
"NUPL",
"Stock-to-Flow Deflection",
"MVRV Z-Score",
"Puell Multiple",
"Reserve Risk",
"SSR",
],
"Value": last_value,
"Changes (1D)": one_day_change,
"Changes (7D)": seven_days_change,
}
df = pd.DataFrame(data=d)
return df.to_html(index=False)
def get_btc_ex_activity():
return get_ex_activity("BTC")
def get_eth_ex_activity():
return get_ex_activity("ETH")
def get_ex_activity(asset="BTC"):
ex_inflow = get_ex_inflow_volume(asset)
ex_outflow = get_ex_outflow_volume(asset)
ex_balance = get_ex_balance(asset)
last_value = [
f"{'{:,.2f}'.format(ex_inflow[0])} {asset}",
f"{'{:,.2f}'.format(ex_outflow[0])} {asset}",
f"{'{:,.2f}'.format(ex_balance[0])} {asset}",
]
one_day_change = [
get_change(ex_inflow[1], ex_inflow[0]),
get_change(ex_outflow[1], ex_outflow[0]),
get_change(ex_balance[1], ex_balance[0]),
]
seven_days_change = [
get_change(ex_inflow[2], ex_inflow[0]),
get_change(ex_outflow[2], ex_outflow[0]),
get_change(ex_balance[2], ex_balance[0]),
]
d = {
"Metric": [
"Exchange Inflow Volume (Total)",
"Exchange Outflow Volume (Total)",
"Exchange Balance (Total)",
],
"Value": last_value,
"Changes (1D)": one_day_change,
"Changes (7D)": seven_days_change,
}
pd.set_option("display.float_format", lambda x: "%.2f" % x)
df = pd.DataFrame(data=d)
return df.to_html(index=False)
def get_change(base, latest):
return "{:.2%}".format((latest - base) / base)
if __name__ == "__main__":
# print(get_ex_activity())
pdf = pydf.generate_pdf(get_ex_activity(), zoom=2.5)
with open("sopr.pdf", "wb") as f:
f.write(pdf)