-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
92 lines (81 loc) · 2.85 KB
/
streamlit_app.py
File metadata and controls
92 lines (81 loc) · 2.85 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
# streamlit_app.py
import time
import streamlit as st
import psycopg2
import pandas as pd
import altair as alt
from datetime import datetime
st.set_page_config(page_title="StockPulse Live", layout="wide")
@st.cache_resource
def get_conn():
return psycopg2.connect(
host="localhost",
dbname="stocks",
user="postgres",
password="password"
)
def load_data():
conn = get_conn()
query = """
SELECT stream_ts, index, close, volume
FROM ticks_raw
ORDER BY stream_ts DESC
LIMIT 500
"""
return pd.read_sql(query, conn)
# --- Main app ---
df = load_data()
tab1, tab2, tab3, tab4 = st.tabs(
["Raw Close", "% Change", "Rebased to 100", "Volume Share"]
)
with tab1:
st.subheader("Raw Close Prices (absolute levels)")
highlight = alt.selection_point(fields=["index"], on="mouseover")
chart = alt.Chart(df).mark_line().encode(
x="stream_ts:T",
y="close:Q",
color=alt.Color("index:N", scale=alt.Scale(scheme="category20")),
opacity=alt.condition(highlight, alt.value(1), alt.value(0.2)),
tooltip=["index", "close", "stream_ts"]
).add_params(highlight).interactive()
st.altair_chart(chart, use_container_width=True)
with tab2:
st.subheader("Percentage Change (relative moves)")
df_pct = df.sort_values("stream_ts").copy()
df_pct["pct_change"] = df_pct.groupby("index")["close"].pct_change() * 100
chart = alt.Chart(df_pct).mark_line().encode(
x="stream_ts:T",
y="pct_change:Q",
color=alt.Color("index:N", scale=alt.Scale(scheme="category20")),
tooltip=["index", "pct_change", "stream_ts"]
).interactive()
st.altair_chart(chart, use_container_width=True)
with tab3:
st.subheader("Rebased to 100 (growth from baseline)")
df_rebased = df.sort_values("stream_ts").copy()
df_rebased["rebased"] = df_rebased.groupby("index")["close"].transform(
lambda x: (x / x.iloc[0]) * 100
)
chart = alt.Chart(df_rebased).mark_line().encode(
x="stream_ts:T",
y="rebased:Q",
color=alt.Color("index:N", scale=alt.Scale(scheme="category20")),
tooltip=["index", "rebased", "stream_ts"]
).interactive()
st.altair_chart(chart, use_container_width=True)
with tab4:
st.subheader("Volume Share by Index (latest window)")
volumes = df.groupby("index")["volume"].sum().reset_index()
volumes["share"] = (volumes["volume"] / volumes["volume"].sum()) * 100
chart = alt.Chart(volumes).mark_bar().encode(
x="index:N",
y="share:Q",
color=alt.Color("index:N", scale=alt.Scale(scheme="category20")),
tooltip=["index", "share"]
)
st.altair_chart(chart, use_container_width=True)
# Sidebar info
st.sidebar.success(f"Last updated: {datetime.now().strftime('%H:%M:%S')}")
# --- Auto-refresh every 5 seconds ---
time.sleep(5)
st.rerun()