File size: 9,750 Bytes
cf1b1b4 97ae727 fd50cbd 445d48c 1f4b746 4b6a96c 1609dbf fd50cbd 1609dbf 97ae727 1609dbf fd50cbd 4b6a96c fd50cbd cf1b1b4 37a1056 fd50cbd 4b6a96c fd50cbd 97ae727 0fa6f3b cf1b1b4 0fa6f3b 37a1056 cf1b1b4 445d48c cf1b1b4 37a1056 cf1b1b4 97ae727 37a1056 cf1b1b4 fd50cbd 97ae727 fd50cbd 37a1056 cf1b1b4 97ae727 37a1056 cf1b1b4 97ae727 37a1056 cf1b1b4 1f4b746 97ae727 37a1056 cf1b1b4 fd50cbd 37a1056 97ae727 fd50cbd cf1b1b4 37a1056 1609dbf 0fa6f3b 1609dbf cf1b1b4 37a1056 fd50cbd cf1b1b4 fd50cbd 1609dbf 4b6a96c 1609dbf 4b6a96c 1609dbf cf1b1b4 1609dbf 37a1056 445d48c 37a1056 cf27d1b 445d48c cf27d1b 445d48c cf27d1b 1609dbf 4b6a96c 1609dbf 37a1056 445d48c cf27d1b 1609dbf 4b6a96c 1609dbf 4b6a96c 1609dbf 4b6a96c 1609dbf cf1b1b4 |
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 |
#!/usr/bin/env -S pkgx +python@3.7 uv run -- streamlit run
import pandas as pd
import streamlit as st
from streamlit.components.v1 import iframe, html
import altair as alt
from pygwalker.api.streamlit import StreamlitRenderer, init_streamlit_comm
from types import SimpleNamespace
from df import fetch
alt.renderers.set_embed_options(theme="dark")
@st.cache_data(ttl="30m")
def fetch_asset(asset):
return fetch(asset)
def gen_charts(asset, chart_size={"width": 560, "height": 150}):
# Gen data
data = fetch_asset(asset)
etf_volumes = data.etf_volumes
price = data.price
etf_flow_individual = data.etf_flow_individual
etf_flow_total = data.etf_flow_total
cum_flow_individual = data.cum_flow_individual
cum_flow_total = data.cum_flow_total
# Create bindings for interval selection
scale_selection = alt.selection_interval(encodings=["x"], bind="scales")
# Line chart of price
price = (
(
alt.Chart(price)
.mark_line()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("close:Q").scale(zero=False),
color=alt.value("crimson"),
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
trading_vol_individual = (
(
alt.Chart(etf_volumes)
.transform_fold(
etf_volumes.drop(columns="Date").columns.to_list(),
as_=["Funds", "Volume"],
)
.mark_line()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("Volume:Q", title="Trading Volume Individual"),
color="Funds:N",
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
trading_vol_total = (
(
alt.Chart(etf_volumes)
.transform_fold(
etf_volumes.drop(columns="Date").columns.to_list(),
as_=["Funds", "Volume"],
)
.mark_rule()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("sum(Volume):Q", title="Trading Volume Total"),
color=alt.value("teal"),
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
# Net flow individual
net_flow_individual = (
(
alt.Chart(etf_flow_individual)
.transform_fold(
etf_flow_individual.drop(columns="Date").columns.to_list(),
as_=["Funds", "Net Flow"],
)
.mark_line()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("Net Flow:Q", title="Net Flow Individual"),
color="Funds:N",
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
net_flow_total = (
(
alt.Chart(etf_flow_total)
.mark_rule()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("Total:Q", title="Net Flow Total"),
color=alt.condition(
alt.datum.Total > 0,
alt.value("seagreen"), # The positive color
alt.value("orangered"), # The negative color
),
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
# Stacking area chart of flow from individual funds
cum_flow_individual = (
(
alt.Chart(cum_flow_individual)
.transform_fold(
cum_flow_individual.drop(columns="Date").columns.to_list(),
as_=["Funds", "Net Flow"],
)
.mark_area()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("Net Flow:Q", title="Cumulative Flow Individual"),
color=alt.Color("Funds:N", scale=alt.Scale(scheme="tableau20")),
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
# Area chart for cumulative flow
cum_flow_total = (
(
alt.Chart(cum_flow_total)
.transform_calculate(
negative="datum.Total < 0",
)
.mark_area()
.encode(
x=alt.X(
"Date:T",
axis=alt.Axis(tickCount={"interval": "month", "step": 1}),
title="",
),
y=alt.Y("Total:Q", title="Cumulative Flow Total", impute={"value": 0}),
color=alt.Color(
"negative:N", title="Negative Flow", scale=alt.Scale(scheme="set2")
),
)
)
.add_params(scale_selection)
.properties(
width=chart_size["width"],
height=chart_size["height"],
)
)
return SimpleNamespace(
price=price,
trading_vol_individual=trading_vol_individual,
trading_vol_total=trading_vol_total,
net_flow_individual=net_flow_individual,
net_flow_total=net_flow_total,
cum_flow_individual=cum_flow_individual,
cum_flow_total=cum_flow_total,
)
def asset_charts(asset: str, chart_size={"width": "container", "height": 150}):
charts = gen_charts(asset, chart_size)
# Vertical concat the charts in each asset into single column of that asset
all_charts = (
(
charts.price
& charts.trading_vol_individual
& charts.trading_vol_total
& charts.net_flow_individual
& charts.net_flow_total
& charts.cum_flow_individual
& charts.cum_flow_total
)
.resolve_scale(
color="independent",
)
.properties(
title=f"{asset} ETF",
)
)
return all_charts
def app():
# Set page config
st.set_page_config(layout="wide", page_icon="π")
# Initialize pygwalker communication
init_streamlit_comm()
dashboard_tab, single_view, flow_tab, volume_tab, price_tab = st.tabs(
[
"Dashboard",
"View Single ETF",
"Explore ETF Flow",
"Explore ETF Volume",
"Explore ETF Asset Price",
]
)
btc = fetch_asset("BTC")
eth = fetch_asset("ETH")
with dashboard_tab:
btc_charts = asset_charts(
"BTC", chart_size={"width": "container", "height": 150}
)
eth_charts = asset_charts(
"ETH", chart_size={"width": "container", "height": 150}
)
# Display charts
btc_chart_col, eth_chart_col = st.columns(2)
with btc_chart_col:
st.altair_chart(btc_charts, use_container_width=True)
with eth_chart_col:
st.altair_chart(eth_charts, use_container_width=True)
# Display iframes
btc_col, eth_col = st.columns(2)
with btc_col:
html(btc.tdv, height=640)
iframe(btc.url, height=1200, scrolling=True)
with eth_col:
html(eth.tdv, height=640)
iframe(eth.url, height=1200, scrolling=True)
with single_view:
asset = st.selectbox(
"Asset to view",
("BTC", "ETH"),
)
charts = asset_charts(asset, chart_size={"width": "container", "height": 300})
st.altair_chart(charts, use_container_width=True)
match asset:
case "BTC":
html(btc.tdv, height=640)
case "ETH":
html(eth.tdv, height=640)
iframe(fetch_asset(asset).url, height=1200, scrolling=True)
with flow_tab:
btc_flow, eth_flow = btc.etf_flow, eth.etf_flow
btc_flow["Asset"] = "BTC"
eth_flow["Asset"] = "ETH"
df = pd.concat([btc_flow, eth_flow])
StreamlitRenderer(df).explorer()
with volume_tab:
btc_volume, eth_volume = btc.etf_volumes, eth.etf_volumes
btc_volume["Asset"] = "BTC"
eth_volume["Asset"] = "ETH"
df = pd.concat([btc_volume, eth_volume])
StreamlitRenderer(df).explorer()
with price_tab:
btc_price, eth_price = btc.price, eth.price
btc_price["Asset"] = "BTC"
eth_price["Asset"] = "ETH"
df = pd.concat([btc_price, eth_price])
StreamlitRenderer(df).explorer()
if __name__ == "__main__":
app()
|