-
-
Save evd0kim/7fbb9d82b5c783ab82d72d3d314d9e2b to your computer and use it in GitHub Desktop.
Basic analysis of some bitcoin stats
This file contains hidden or 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
from datetime import timedelta | |
from apscheduler.schedulers.blocking import BlockingScheduler | |
import requests, json, datetime, numpy, requests_cache | |
import matplotlib.dates as mdate | |
import matplotlib.pyplot as plot | |
import numpy as np | |
ALL_HISTORICAL_DATA_ENDPOINT = 'https://blockchain.info/charts/market-price?timespan=all&format=json' | |
NUM_UNIQUE_ADDRESSES = 'https://blockchain.info/charts/n-unique-addresses?timespan=all&format=json' | |
NUM_CONF_TRANSACTIONS = 'https://blockchain.info/charts/n-transactions?timespan=all&format=json' | |
#fees in BTC | |
#NUM_TRANSACTIONS_FEE = 'https://blockchain.info/charts/transaction-fees?timespan=all&format=json' | |
#fees in USD | |
NUM_TRANSACTIONS_FEE = 'https://blockchain.info/charts/transaction-fees-usd?timespan=all&format=json' | |
NUM_DAYS_THREE_YEARS = 365 * 3 | |
AVG_WINDOW = 25 | |
requests_cache.install_cache('request_cache') | |
def plot_moving_average(x, y_data, window, y_label, title): | |
fig, ax = plot.subplots() | |
for (y, symbol, label) in y_data: | |
y_running_mean = np.convolve(y, np.ones((window,))/window, mode='valid') | |
ax.plot_date(x[window - 1:], y_running_mean, symbol, label=label) | |
handles, labels = ax.get_legend_handles_labels() | |
ax.legend(handles, labels) | |
date_formatter = mdate.DateFormatter('%d/%m/%y') | |
ax.xaxis.set_major_formatter(date_formatter) | |
fig.autofmt_xdate() | |
plot.ylabel(y_label) | |
plot.title(title) | |
def plot_data(): | |
data = requests.get(NUM_UNIQUE_ADDRESSES).json()['values'][-NUM_DAYS_THREE_YEARS:] | |
addr_x = [p['x'] for p in data] | |
addr_y = [p['y'] for p in data] | |
data = requests.get(ALL_HISTORICAL_DATA_ENDPOINT).json()['values'][-NUM_DAYS_THREE_YEARS:] | |
price_x = [p['x'] for p in data] | |
price_y = [p['y'] for p in data] | |
data = requests.get(NUM_CONF_TRANSACTIONS).json()['values'][-NUM_DAYS_THREE_YEARS:] | |
trans_x = [p['x'] for p in data] | |
trans_y = [p['y'] for p in data] | |
data = requests.get(NUM_TRANSACTIONS_FEE).json()['values'][-NUM_DAYS_THREE_YEARS:] | |
fee_x = [p['x'] for p in data] | |
fee_y = [p['y'] for p in data] | |
x = [] | |
price_addr_y = [] | |
price_trans_y = [] | |
price_fee_y = [] | |
price_percent_change_y = [] | |
addr_percent_change_y = [] | |
trans_percent_change_y = [] | |
fee_percent_change_y = [] | |
for i in range(1, len(addr_x)): | |
if addr_x[i] != price_x[i] != trans_x[i] != fee_x[i]: | |
print("Data does not match") | |
avgx = (addr_x[i] + addr_x[i-1]) / 2 | |
x.append(mdate.epoch2num(avgx)) | |
price_dy = price_y[i] - price_y[i-1] | |
percent_change_price = price_dy / price_y[i-1] | |
addr_dy = addr_y[i] - addr_y[i-1] | |
percent_change_address = addr_dy / addr_y[i-1] | |
trans_dy = trans_y[i] - trans_y[i-1] | |
percent_change_trans = trans_dy / trans_y[i-1] | |
fee_dy = fee_y[i] - fee_y[i-1] | |
percent_change_fee = fee_dy / fee_y[i-1] | |
price_to_addr = percent_change_price / percent_change_address | |
price_to_trans = percent_change_price / percent_change_trans | |
price_to_fee = percent_change_price / percent_change_fee | |
price_addr_y.append(abs(price_to_addr)) | |
price_trans_y.append(abs(price_to_trans)) | |
price_fee_y.append(abs(price_to_fee)) | |
price_percent_change_y.append(percent_change_price) | |
addr_percent_change_y.append(percent_change_address) | |
trans_percent_change_y.append(percent_change_trans) | |
fee_percent_change_y.append(percent_change_fee) | |
plot_moving_average(x, [ | |
(price_trans_y, 'g-', "| % Δ price / % Δ trans |"), | |
(price_addr_y, 'b-', "| % Δ price / % Δ addr |"), | |
(price_fee_y, 'c-', "| % Δ price / % Δ fee |") | |
], AVG_WINDOW, "", "{} day moving average ratios".format(AVG_WINDOW)) | |
plot_moving_average(x, [ | |
(price_percent_change_y, 'r-', "Price"), | |
(addr_percent_change_y, 'b-', "Addresses"), | |
(trans_percent_change_y, 'g-', "Transactions"), | |
(fee_percent_change_y, 'c-', "Fee") | |
], AVG_WINDOW, "% change", "{} day moving average ratios % change".format(AVG_WINDOW)) | |
plot.show() | |
plot_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment