InNoobWeTrust commited on
Commit
1f4b746
·
1 Parent(s): 97ae727

fix: update visualizations

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -2
  2. streamlit_app.py +78 -85
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
  streamlit
2
- numpy
3
  pandas
4
  yfinance
5
- plotly
 
1
  streamlit
 
2
  pandas
3
  yfinance
4
+ altair
streamlit_app.py CHANGED
@@ -1,12 +1,9 @@
1
  import streamlit as st
2
 
3
- import numpy as np
4
  import pandas as pd
5
  import yfinance as yf
6
 
7
- import plotly.express as px
8
- import plotly.graph_objects as go
9
- from plotly.subplots import make_subplots
10
 
11
 
12
  def clean_etf_data(df):
@@ -141,111 +138,107 @@ if __name__ == "__main__":
141
 
142
  # Section trading volume
143
  st.subheader(f"{asset} ETF Trading volume")
144
- trading_vol_fig = px.bar(
145
- etf_volumes, x=etf_volumes.index, y=etf_volumes.columns, barmode="relative"
 
 
 
 
 
 
 
146
  )
147
- st.plotly_chart(trading_vol_fig, use_container_width=True)
148
 
149
  # Section net flow individual funds
150
  st.subheader(f"{asset} ETF Net flow individual funds")
151
- net_flow_individual_fig = px.bar(
152
- etf_flow_individual,
153
- x=etf_flow_individual.index,
154
- y=etf_flow_individual.columns,
155
- barmode="relative",
 
 
 
 
 
 
 
156
  )
157
- st.plotly_chart(net_flow_individual_fig, use_container_width=True)
158
 
159
  # Section net flow total vs asset price
160
  st.subheader(f"{asset} ETF Net flow total vs asset price")
161
- positive_flow = etf_flow_total[etf_flow_total > 0]
162
- negative_flow = etf_flow_total[etf_flow_total < 0]
163
- net_flow_total_fig = make_subplots(specs=[[{"secondary_y": True}]])
164
- # Sea green bar for positive flow
165
- net_flow_total_fig.add_trace(
166
- go.Bar(
167
- x=positive_flow.index,
168
- y=positive_flow,
169
- name="Total (positive)",
170
- marker_color="seagreen",
171
- ),
172
- secondary_y=False,
173
- )
174
- # Orange red bar for negative flow
175
- net_flow_total_fig.add_trace(
176
- go.Bar(
177
- x=negative_flow.index,
178
- y=negative_flow,
179
- name="Total (negative)",
180
- marker_color="orangered",
181
- ),
182
- secondary_y=False,
183
  )
184
  # Line chart of price
185
- net_flow_total_fig.add_trace(
186
- go.Scatter(
187
- x=price.index,
188
- y=price,
189
- name=f"{asset} Price",
190
- mode="lines",
191
- line=dict(color="darkgoldenrod"),
 
 
 
 
 
192
  ),
193
- secondary_y=True,
194
  )
195
- net_flow_total_fig.update_layout(barmode="stack")
196
- st.plotly_chart(net_flow_total_fig, use_container_width=True)
197
 
198
  # Section cumulative flow individual vs asset price
199
  st.subheader(f"{asset} ETF Cumulative flow of individual funds vs asset price")
200
  cum_flow_individual = etf_flow_individual.cumsum()
201
- cum_flow_individual_fig = make_subplots(specs=[[{"secondary_y": True}]])
202
  # Stacking area chart of flow from individual funds
203
- for col in cum_flow_individual.columns:
204
- cum_flow_individual_fig.add_trace(
205
- go.Scatter(
206
- x=cum_flow_individual.index,
207
- y=cum_flow_individual[col],
208
- name=col,
209
- fill="tonexty",
210
- ),
211
- secondary_y=False,
212
  )
213
- # Line chart of price
214
- cum_flow_individual_fig.add_trace(
215
- go.Scatter(
216
- x=price.index,
217
- y=price,
218
- name=f"{asset} Price",
219
- mode="lines",
220
- line=dict(color="darkgoldenrod"),
221
- ),
222
- secondary_y=True,
223
  )
224
- st.plotly_chart(cum_flow_individual_fig, use_container_width=True)
 
 
 
225
 
226
  # Section cumulative flow total vs asset price
227
  st.subheader(f"{asset} ETF Cumulative flow total vs asset price")
228
  cum_flow_total = etf_flow_total.cumsum()
229
- cum_flow_total_fig = make_subplots(specs=[[{"secondary_y": True}]])
230
  # Area chart for cumulative flow
231
- cum_flow_total_fig.add_trace(
232
- go.Scatter(
233
- x=cum_flow_total.index,
234
- y=cum_flow_total,
235
- name="Cumulative flow total",
236
- fill="tonexty",
237
- ),
238
- secondary_y=False,
 
 
 
 
 
239
  )
240
- # Line chart of price
241
- cum_flow_total_fig.add_trace(
242
- go.Scatter(
243
- x=price.index,
244
- y=price,
245
- name=f"{asset} Price",
246
- mode="lines",
247
- line=dict(color="darkgoldenrod"),
248
  ),
249
- secondary_y=True,
250
  )
251
- st.plotly_chart(cum_flow_total_fig, use_container_width=True)
 
1
  import streamlit as st
2
 
 
3
  import pandas as pd
4
  import yfinance as yf
5
 
6
+ import altair as alt
 
 
7
 
8
 
9
  def clean_etf_data(df):
 
138
 
139
  # Section trading volume
140
  st.subheader(f"{asset} ETF Trading volume")
141
+ trading_vol_fig = (
142
+ alt.Chart(etf_volumes.reset_index())
143
+ .transform_fold(etf_volumes.columns, as_=["Funds", "Volume"])
144
+ .mark_bar()
145
+ .encode(
146
+ x=alt.X("index:T", title="Date"),
147
+ y=alt.Y("Volume:Q", title="Volume"),
148
+ color="Funds:N",
149
+ )
150
  )
151
+ st.altair_chart(trading_vol_fig, use_container_width=True)
152
 
153
  # Section net flow individual funds
154
  st.subheader(f"{asset} ETF Net flow individual funds")
155
+ net_flow_individual_fig = (
156
+ alt.Chart(etf_flow_individual.reset_index())
157
+ .transform_fold(
158
+ etf_flow_individual.columns,
159
+ as_=["Funds", "Net Flow"],
160
+ )
161
+ .mark_bar()
162
+ .encode(
163
+ x=alt.X("index:T", title="Date"),
164
+ y=alt.Y("Net Flow:Q", title="Net Flow"),
165
+ color="Funds:N",
166
+ )
167
  )
168
+ st.altair_chart(net_flow_individual_fig, use_container_width=True)
169
 
170
  # Section net flow total vs asset price
171
  st.subheader(f"{asset} ETF Net flow total vs asset price")
172
+ net_flow_total_fig = (
173
+ alt.Chart(etf_flow_total.reset_index())
174
+ .mark_bar()
175
+ .encode(
176
+ x=alt.X("index:T", title="Date"),
177
+ y=alt.Y("Total:Q"),
178
+ color=alt.condition(
179
+ alt.datum.Total > 0,
180
+ alt.value("seagreen"), # The positive color
181
+ alt.value("orangered"), # The negative color
182
+ ),
183
+ )
 
 
 
 
 
 
 
 
 
 
184
  )
185
  # Line chart of price
186
+ price_fig = (
187
+ alt.Chart(price.reset_index())
188
+ .mark_line()
189
+ .encode(
190
+ x=alt.X("index:T", title="Date"),
191
+ y=alt.Y("Close:Q", title="Price"),
192
+ color=alt.value("crimson"),
193
+ )
194
+ )
195
+ st.altair_chart(
196
+ (net_flow_total_fig + price_fig).resolve_scale(
197
+ y="independent",
198
  ),
199
+ use_container_width=True,
200
  )
 
 
201
 
202
  # Section cumulative flow individual vs asset price
203
  st.subheader(f"{asset} ETF Cumulative flow of individual funds vs asset price")
204
  cum_flow_individual = etf_flow_individual.cumsum()
 
205
  # Stacking area chart of flow from individual funds
206
+ cum_flow_individual_net_fig = (
207
+ alt.Chart(cum_flow_individual.reset_index())
208
+ .transform_fold(cum_flow_individual.columns, as_=["Funds", "Net Flow"])
209
+ .mark_area()
210
+ .encode(
211
+ x=alt.X("index:T", title="Date"),
212
+ y=alt.Y("Net Flow:Q", title="Net Flow"),
213
+ color=alt.Color("Funds:N").scale(scheme="tableau20"),
 
214
  )
 
 
 
 
 
 
 
 
 
 
215
  )
216
+ st.altair_chart(
217
+ (cum_flow_individual_net_fig + price_fig).resolve_scale(y="independent"),
218
+ use_container_width=True,
219
+ )
220
 
221
  # Section cumulative flow total vs asset price
222
  st.subheader(f"{asset} ETF Cumulative flow total vs asset price")
223
  cum_flow_total = etf_flow_total.cumsum()
 
224
  # Area chart for cumulative flow
225
+ cum_flow_total_fig = (
226
+ alt.Chart(cum_flow_total.reset_index())
227
+ .transform_calculate(
228
+ negative="datum.Total < 0",
229
+ )
230
+ .mark_area()
231
+ .encode(
232
+ x=alt.X("index:T"),
233
+ y=alt.Y("Total:Q", impute={"value": 0}),
234
+ color=alt.Color("negative:N", title="Negative Flow").scale(
235
+ scheme="set2"
236
+ ),
237
+ )
238
  )
239
+ st.altair_chart(
240
+ (cum_flow_total_fig + price_fig).resolve_scale(
241
+ y="independent",
 
 
 
 
 
242
  ),
243
+ use_container_width=True,
244
  )