Download notebook (.ipynb)

Bitcoin Daily Trend Visualization (2024)#

This notebook was inspired by a Stack Overflow question.

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
year = 2024
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/Bitstamp_BTCUSD_d.csv", parse_dates=["date"])
df = df[df["date"].dt.year == year].sort_values(by="date").reset_index(drop=True)
df = df.assign(trend=(df["close"].diff().fillna(0) > 0).astype(int))
print(df.shape)
df.head()
(366, 10)
unix date symbol open high low close Volume BTC Volume USD trend
0 1704067200 2024-01-01 BTC/USD 42258.0 44192.0 42188.0 44187.0 1633.498618 7.217940e+07 0
1 1704153600 2024-01-02 BTC/USD 44187.0 45922.0 44167.0 44970.0 3948.659076 1.775712e+08 1
2 1704240000 2024-01-03 BTC/USD 44972.0 45510.0 41454.0 42865.0 3980.828056 1.706382e+08 0
3 1704326400 2024-01-04 BTC/USD 42863.0 44795.0 42656.0 44186.0 2918.119349 1.289400e+08 1
4 1704412800 2024-01-05 BTC/USD 44195.0 44366.0 42500.0 44184.0 3155.466383 1.394211e+08 0
ggplot(df) + \
    geom_line(aes("date", "close"), color="pen", size=1.2, tooltips='none') + \
    geom_line(aes("date", "close", color="trend"), size=.6,
              tooltips=layer_tooltips().title("^x").format("^x", "%A, %b %e")
                                       .line("Close price|^y")
                                       .anchor('top_left')
                                       .min_width(200)) + \
    scale_x_datetime(expand=[0]) + \
    scale_y_continuous(format="$.4~s") + \
    scale_color_manual(name="Trend", values={0: "red", 1: "green"},
                       labels=["Downtrend", "Uptrend"]) + \
    ylab("Close Price") + \
    ggtitle("Bitcoin Daily Close Price with Up/Down Trend Coloring ({0})".format(year)) + \
    ggsize(1000, 500) + \
    theme_light() + \
    flavor_darcula() + \
    theme(plot_title=element_text(size=18, face='bold'),
          legend_position='bottom',
          axis_title_x='blank') + \
    ggtb()