-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
436 lines (390 loc) · 17 KB
/
streamlit_app.py
File metadata and controls
436 lines (390 loc) · 17 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from __future__ import annotations
from math import ceil, floor
from pathlib import Path
import altair as alt
import pandas as pd
import streamlit as st
DATA_PATH = Path("data/crypto_us_yields.parquet")
BTC_DATA_PATH = Path("data/btcusd_1-min_data.csv")
def to_utc_ts(value: pd.Timestamp) -> int:
ts = pd.Timestamp(value)
if ts.tzinfo is None:
ts = ts.tz_localize("UTC")
else:
ts = ts.tz_convert("UTC")
return int(ts.timestamp())
@st.cache_data(show_spinner="Loading BTC price data...")
def load_btc_daily_median(
csv_path: Path, start_ts: int | None, end_ts: int | None
) -> pd.DataFrame:
usecols = ["Timestamp", "Close"]
df = pd.read_csv(csv_path, usecols=usecols)
df["Timestamp"] = pd.to_numeric(df["Timestamp"], errors="coerce")
df["Close"] = pd.to_numeric(df["Close"], errors="coerce")
df = df.dropna(subset=["Timestamp", "Close"])
if start_ts is not None:
df = df[df["Timestamp"] >= start_ts]
if end_ts is not None:
df = df[df["Timestamp"] <= end_ts]
df["date"] = (
pd.to_datetime(df["Timestamp"], unit="s", utc=True)
.dt.tz_convert(None)
.dt.normalize()
)
daily = (
df.groupby("date", as_index=False)["Close"]
.median()
.rename(columns={"Close": "median_price"})
)
return daily.sort_values("date")
def add_forward_returns(daily_df: pd.DataFrame, forward_days: int) -> pd.DataFrame:
out = daily_df.sort_values("date").copy()
out["forward_return"] = (
out["median_price"].shift(-forward_days) / out["median_price"] - 1
)
return out
def build_fixed_width_bins(series: pd.Series, width: float) -> pd.Series:
clean = pd.to_numeric(series, errors="coerce").dropna()
if clean.empty:
return pd.Series(dtype="object")
min_val = float(clean.min())
max_val = float(clean.max())
if min_val == max_val:
edges = [min_val - width / 2, max_val + width / 2]
else:
start = floor(min_val / width) * width
end = ceil(max_val / width) * width
if start == end:
end = start + width
steps = int(round((end - start) / width))
edges = [start + i * width for i in range(steps + 1)]
if edges[-1] < max_val:
edges.append(edges[-1] + width)
bins = pd.cut(series, bins=edges, include_lowest=True, right=False)
labels = bins.cat.categories
label_map = {
interval: f"{interval.left:.1f} to {interval.right:.1f}" for interval in labels
}
return bins.map(label_map)
st.set_page_config(page_title="Crypto vs US Yields", layout="wide")
st.title("Crypto vs US Treasury Yields")
if not DATA_PATH.exists():
st.error(f"Missing data file: {DATA_PATH}")
st.stop()
df = pd.read_parquet(DATA_PATH)
if "date" in df.columns:
df["date"] = pd.to_datetime(df["date"], errors="coerce")
df = df.sort_values("date")
controls = st.columns(4)
with controls[0]:
rate_type = st.selectbox("Aave rate", ["supply", "borrow"])
with controls[1]:
tenor = st.selectbox("Treasury tenor", ["6m", "2y", "5y", "10y"])
with controls[2]:
ma_window = st.selectbox("Spread MA window (days)", [10, 21, 55, 100, 200], index=0)
with controls[3]:
forward_days = st.number_input(
"BTC forward return days", min_value=1, max_value=365, value=30, step=1
)
aave_col = f"aave_{rate_type}_apy"
yield_col = f"yield_{tenor}"
spread_col = f"{rate_type}_minus_yield_{tenor}"
rates_df = df.set_index("date")[[aave_col, yield_col, spread_col]].copy()
rates_df[spread_col] = (
rates_df[spread_col].rolling(window=ma_window, min_periods=1).mean()
)
plot_df = rates_df.copy()
plot_df = plot_df.rename(
columns={
aave_col: f"Aave {rate_type} APY",
yield_col: f"Treasury {tenor}",
spread_col: f"Spread ({ma_window}d MA)",
}
).dropna(how="all")
long_df = plot_df.reset_index().melt(
id_vars="date", var_name="series", value_name="value"
)
long_df = long_df.dropna(subset=["value"])
selection = alt.selection_multi(fields=["series"], bind="legend")
base_chart = (
alt.Chart(long_df)
.mark_line()
.encode(
x=alt.X("date:T", title="Date"),
y=alt.Y("value:Q", title="Percent"),
color=alt.Color("series:N", title="Series"),
opacity=alt.condition(selection, alt.value(1.0), alt.value(0.15)),
tooltip=["date:T", "series:N", "value:Q"],
)
.add_selection(selection)
.properties(height=450)
)
zero_rule = (
alt.Chart(pd.DataFrame({"y": [0]}))
.mark_rule(color="#E53935", size=2.5)
.encode(y="y:Q")
)
chart = zero_rule + base_chart
st.altair_chart(chart, use_container_width=True)
last_date = plot_df.index.max().date() if not plot_df.empty else "n/a"
st.caption(f"Rows: {len(plot_df)} | Last date: {last_date}")
st.subheader("BTC Forward Returns vs Rate Spread")
if not BTC_DATA_PATH.exists():
st.warning(f"Missing BTC data file: {BTC_DATA_PATH}")
else:
spread_series = rates_df[[spread_col]].dropna()
if spread_series.empty:
st.warning("No spread data available for the selected configuration.")
else:
spread_start = spread_series.index.min()
spread_end = spread_series.index.max()
start_ts = to_utc_ts(spread_start)
end_ts = to_utc_ts(spread_end + pd.Timedelta(days=1) - pd.Timedelta(seconds=1))
btc_daily = load_btc_daily_median(BTC_DATA_PATH, start_ts, end_ts)
btc_daily_full = load_btc_daily_median(BTC_DATA_PATH, None, None)
btc_returns = add_forward_returns(btc_daily, int(forward_days))
spread_ma_df = spread_series.rename(columns={spread_col: "spread_ma"}).reset_index()
scatter_df = spread_ma_df.merge(
btc_returns[["date", "forward_return"]], on="date", how="inner"
).dropna()
if scatter_df.empty:
st.warning("No overlapping BTC forward returns for the selected window.")
else:
x_title = f"{rate_type} - {tenor} spread ({ma_window}d MA, pp)"
y_title = f"BTC {forward_days}d forward return (median daily price)"
scatter = (
alt.Chart(scatter_df)
.mark_circle(size=60, opacity=0.7)
.encode(
x=alt.X("spread_ma:Q", title=x_title, axis=alt.Axis(format=".2f")),
y=alt.Y("forward_return:Q", title=y_title, axis=alt.Axis(format=".2%")),
tooltip=[
alt.Tooltip("date:T", title="Date"),
alt.Tooltip("spread_ma:Q", title="Spread (pp)", format=".2f"),
alt.Tooltip(
"forward_return:Q", title="Forward return", format=".2%"
),
],
)
.properties(height=450)
)
scatter_zero = (
alt.Chart(pd.DataFrame({"y": [0]}))
.mark_rule(color="#E53935", opacity=0.5, size=2)
.encode(y="y:Q")
)
st.altair_chart(scatter_zero + scatter, use_container_width=True)
st.subheader("Conditional BTC Forward Returns by Selected Spread")
bin_width = st.selectbox("Spread bin width (pp)", [1.0, 2.0], index=0)
horizons = st.multiselect(
"Forward horizons (days)", [30, 60, 90, 180], default=[30, 60, 90, 180]
)
selected_spread = (
rates_df[[spread_col]].dropna().rename(columns={spread_col: "spread_ma"})
)
if selected_spread.empty:
st.warning("No spread data available for the selected configuration.")
else:
spread_df = selected_spread.reset_index()
spread_df["spread_bin"] = build_fixed_width_bins(
spread_df["spread_ma"], float(bin_width)
)
distribution_df = spread_df.merge(
btc_returns[["date", "forward_return"]], on="date", how="inner"
).dropna(subset=["spread_bin", "forward_return"])
if distribution_df.empty:
st.warning("No overlapping BTC forward returns for the selected bins.")
else:
def bin_sort_key(label: str) -> float:
try:
return float(label.split(" to ")[0])
except (AttributeError, ValueError):
return 0.0
order = sorted(
distribution_df["spread_bin"].dropna().unique(), key=bin_sort_key
)
bin_median = distribution_df.groupby("spread_bin")["forward_return"].median()
distribution_df = distribution_df.assign(
bin_color=distribution_df["spread_bin"].map(
lambda label: "#2E7D32" if bin_median.get(label, 0) > 0 else "#C62828"
)
)
box = (
alt.Chart(distribution_df)
.mark_boxplot(size=20, extent="min-max", opacity=0.85)
.encode(
x=alt.X(
"spread_bin:N",
title=f"Selected spread ({ma_window}d MA, pp bins)",
sort=order,
),
y=alt.Y(
"forward_return:Q",
title=f"BTC {forward_days}d forward return",
axis=alt.Axis(format=".2%"),
),
color=alt.Color("bin_color:N", scale=None, legend=None),
tooltip=[
alt.Tooltip("spread_bin:N", title="Spread bin"),
alt.Tooltip(
"forward_return:Q", title="Forward return", format=".2%"
),
],
)
.properties(height=420)
)
box = box.configure_boxplot(
rule=alt.MarkConfig(color="#FFFFFF"),
ticks=alt.MarkConfig(color="#FFFFFF"),
)
win_table = (
distribution_df.groupby("spread_bin")["forward_return"]
.agg(
median="median",
p25=lambda s: s.quantile(0.25),
p75=lambda s: s.quantile(0.75),
win_rate=lambda s: (s > 0).mean(),
count="count",
)
.reset_index()
)
win_table = win_table.sort_values(
"spread_bin", key=lambda col: col.map(bin_sort_key)
)
st.altair_chart(box, use_container_width=True)
st.caption("Summary stats per bin (returns in decimal form).")
st.dataframe(win_table, use_container_width=True)
st.subheader("Heatmap: Spread Bin vs Forward Horizon")
if horizons:
heat_rows = []
for horizon in sorted(horizons):
horizon_returns = add_forward_returns(
btc_daily, int(horizon)
)[["date", "forward_return"]]
horizon_df = spread_df.merge(
horizon_returns, on="date", how="inner"
).dropna(subset=["spread_bin", "forward_return"])
if horizon_df.empty:
continue
medians = (
horizon_df.groupby("spread_bin")["forward_return"]
.median()
.reset_index()
)
medians["horizon"] = int(horizon)
heat_rows.append(medians)
if not heat_rows:
st.warning("No data available for the selected horizons.")
else:
heat_df = pd.concat(heat_rows, ignore_index=True)
heat_df["spread_bin"] = pd.Categorical(
heat_df["spread_bin"], categories=order, ordered=True
)
heatmap = (
alt.Chart(heat_df)
.mark_rect()
.encode(
x=alt.X(
"spread_bin:N",
title=f"Selected spread ({ma_window}d MA, pp bins)",
sort=order,
),
y=alt.Y(
"horizon:O",
title="Forward horizon (days)",
),
color=alt.Color(
"forward_return:Q",
title="Median return",
scale=alt.Scale(scheme="redyellowgreen"),
legend=alt.Legend(format=".2%"),
),
tooltip=[
alt.Tooltip("spread_bin:N", title="Spread bin"),
alt.Tooltip("horizon:O", title="Horizon (days)"),
alt.Tooltip(
"forward_return:Q", title="Median return", format=".2%"
),
],
)
.properties(height=300)
)
st.altair_chart(heatmap, use_container_width=True)
else:
st.info("Select at least one horizon to render the heatmap.")
st.subheader("BTC Year-Start Momentum: 5-Day Total Return > 0")
year_horizons = [30, 60, 90]
year_start_df = btc_daily_full.sort_values("date").copy()
year_start_df["year"] = year_start_df["date"].dt.year
first_five = year_start_df.groupby("year", group_keys=False).head(5)
year_stats = (
first_five.groupby("year")["median_price"]
.agg(first="first", fifth="last", count="size")
.reset_index()
)
year_stats["total_return"] = year_stats["fifth"] / year_stats["first"] - 1
valid_years = year_stats.loc[
(year_stats["total_return"] > 0) & (year_stats["count"] == 5), "year"
]
if valid_years.empty:
st.info(
"No years found where the first 5 trading days had a positive total return."
)
else:
fifth_day = (
year_start_df[year_start_df["year"].isin(valid_years)]
.groupby("year")
.nth(4)
.reset_index()
)
base_prices = year_start_df[["date", "median_price"]].copy()
rows = []
for horizon in year_horizons:
horizon_returns = add_forward_returns(base_prices, int(horizon))
merged = fifth_day[["year", "date"]].merge(
horizon_returns[["date", "forward_return"]],
on="date",
how="left",
)
merged["horizon"] = int(horizon)
rows.append(merged)
year_return_df = pd.concat(rows, ignore_index=True).dropna(
subset=["forward_return"]
)
if year_return_df.empty:
st.info(
"Not enough BTC history to compute the 30/60/90d returns for those years."
)
else:
year_chart = (
alt.Chart(year_return_df)
.mark_line(point=True)
.encode(
x=alt.X("horizon:O", title="Forward horizon (days)"),
y=alt.Y(
"forward_return:Q",
title="BTC forward return",
axis=alt.Axis(format=".2%"),
),
color=alt.Color("year:O", title="Year"),
tooltip=[
alt.Tooltip("year:O", title="Year"),
alt.Tooltip("horizon:O", title="Horizon (days)"),
alt.Tooltip(
"forward_return:Q",
title="Forward return",
format=".2%",
),
alt.Tooltip("date:T", title="5th trading day"),
],
)
.properties(height=380)
)
st.altair_chart(year_chart, use_container_width=True)
year_list = ", ".join(
str(year) for year in sorted(year_return_df["year"].unique())
)
st.caption(
"Years meeting the 5-day positive condition: "
f"{year_list if year_list else 'n/a'}"
)