<jupyter_start><jupyter_text>Analysis of Bitcoin volatility<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FuncFormatter
from matplotlib import style

# Basic styling
style.use('seaborn-notebook')
pd.set_option('display.width', 200)

# load data
btc = pd.read_csv("../data/btcusd.csv")

# Index dataset by date
btc['Date'] = pd.to_datetime(btc['Date'])
btc = btc.set_index('Date')
btc = btc.sort_index()

# resample dataset from days to months
btc_monthly = btc.resample('M').ffill()

btc_daily_returns = btc['Close'].pct_change()
btc_monthly_returns = btc_monthly['Close'].pct_change()

ax1 = plt.subplot()
btc_monthly_returns[2:].plot(ax=ax1, figsize=(20,5), label='Monthly return')
plt.ylabel("Price change from previous month (%)")
plt.title("Bitcoin monthly volatility")
plt.grid(linestyle='dotted')
vals = ax1.get_yticks()
ax1.set_yticklabels(['{:,.0%}'.format(x) for x in vals])
plt.axhline(linewidth=2, color='black')
plt.show()

ax1 = plt.subplot()
btc_daily_returns[2:].plot(ax=ax1, figsize=(20,5), label='Daily return')
plt.ylabel("Price change from previous day (%)")
plt.title("Bitcoin daily volatility")
plt.grid(linestyle='dotted')
vals = ax1.get_yticks()
ax1.set_yticklabels(['{:,.0%}'.format(x) for x in vals])
plt.axhline(linewidth=2, color='black')
plt.show()<jupyter_output><empty_output><jupyter_text>Now there's no denying this is definitely swingy. We can also draw a histogram of these swings.<jupyter_code>n, bins, patches = plt.hist(x=btc_monthly_returns[2:], bins=25)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Bitcoin monthly volatility')
maxfreq = n.max()
plt.axvline(linewidth=2, color='black')
plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
# ax = plt.subplot()
# vals = ax.get_xticks()
# ax.set_xticklabels(['{:,.1%}'.format(x) for x in vals])
plt.show()
n, bins, patches = plt.hist(x=btc_daily_returns[2:], bins=50)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Bitcoin daily volatility')
maxfreq = n.max()
plt.axvline(linewidth=2, color='black')
plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)
# ax = plt.subplot()
# vals = ax.get_xticks()
# ax.set_xticklabels(['{:,.1%}'.format(x) for x in vals])


plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1716257007330746, 'fraction_numerical': 0.02112979732643381, 'mean_word_length': 6.5816993464052285, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '149316', 'license': 'Apache-2.0', 'n_tokens_mistral': 957, 'n_tokens_neox': 886, 'n_words': 185, 'path': 'notebooks/btc-volatility.ipynb', 'repo_name': 'FilipSK/btc-investing', 'size': '2319'}