Finance Python RandomCharts Visualisations

Bitcoin a Big Winner during the Pandemic Crisis

The chart of this week combines a line plot with a vertical bar one by making use of a secondary y-axis. This is to illustrate weakly levels and returns of Bitcoin (BTC) over the pandemic period.

As of today (3rd March 2022) Bitcoin is trading at around \$43K. This is about 50% down from its November 2021 record high. However, this is more than 80% up compared with its value at the beginning of the covid-19 crisis (around $7K in December 2019). This makes the largest cryptocurrency one of the biggest winners over the pandemic period. In particular, we can identify two periods of sustained increase:

  • The first one started in October 2020 and ended in April 2021  after speculation on government regulation.
  • The second period started in July 2021 and ended in November 2021 when Bitcoin achieved its all-time high at around $65K.

Weekly returns were positive on 67 of the 113 weeks in the period. The average weakly return was 2.08% with volatility of around 10%. For comparison, the average weakly return and volatility for the S&P 500 over the same period were 0.3% and 3% respectively.

Python Code

Python modules required datetime, numpy, plotly, and yfinance.

# Author: @Quant_Girl
# Title: Bitcoin Weakly Movement
# Type: Line Chart + Vertical Bar Chart

import datetime as dt
import numpy as np
import plotly.graph_objects as go
import yfinance as yf
from plotly.subplots import make_subplots

start = dt.datetime(2019, 12, 30)
end = dt.datetime(2022, 2, 28)

btc = yf.download('BTC-USD', start=start, end=end, interval="1wk")
btc = btc.assign(returns=btc['Close'].pct_change(1))
btc = btc.assign(returns_sign=np.sign(btc["returns"]))
colors = {-1.0: "rgba(255,0,0, 0.75)", 1.0: "rgba(0,153,76, 0.75)"}

# Create figure
fig = make_subplots(specs=[[{"secondary_y": True}]],
                    )
# Add traces
fig.add_trace(
    go.Bar(x=btc.index, y=btc.returns, marker=dict(color=btc.returns_sign.map(colors)),
           name="Bitcoin weakly % change"),
    secondary_y=False)

fig.add_trace(
    go.Scatter(x=btc.index, y=btc.Close, mode='lines', line_color='rgba(0, 102 , 204, 1.0)',
               name="BTC/USD DOLLAR"),
    secondary_y=True, )

# Figure title and labels
fig.update_layout(title_text="Bitcoin Weakly Movement", height=600, width=1000)

fig.update_xaxes(title_text="")
fig.update_yaxes(title_text="<b></b>USD", secondary_y=True)
fig.update_yaxes(title_text="<b></b>Percent", secondary_y=False)

fig.update_layout(dict(yaxis2={'anchor': 'x', 'overlaying': 'y', 'side': 'left', 'range': [0, 80000],
                               'domain': [0, 1], 'position': 0.5},
                       yaxis={'anchor': 'x', 'range': [-0.5, 0.5],
                              'domain': [0.0, 1.0],
                              'side': 'right'}))

# Footnotes
fig.add_annotation(xref='paper', x=0, yref='paper', y=-0.4,
                   text="Sources: Yahoo Finance.", showarrow=False, font_size=10
                   )

fig.add_annotation(xref='paper', x=0, yref='paper', y=-0.45,
                   text="Time Series: BTC-USD from 2019-12-30 to 2022-02-28.",
                   showarrow=False, font_size=10
                   )

fig.add_annotation(xref='paper', x=1, yref='paper', y=-0.4,
                   text="@Quant_Girl", showarrow=False,
                   font=dict(size=12, color='#a70684', family='PT Serif Caption')
                   )


fig.show()

Chart of the Week: A weekly series of quick random charts made with Python 🐍