nick5363 commited on
Commit
3b0dfb8
·
verified ·
1 Parent(s): b5001a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -1,4 +1,6 @@
1
- import streamlit as st
 
 
2
  import pandas as pd
3
  import requests
4
  import yfinance as yf
@@ -40,6 +42,7 @@ def get_stock_data(symbols):
40
  try:
41
  stock = yf.Ticker(symbol)
42
  hist = stock.history(period="1d") # Lấy dữ liệu trong 1 ngày gần nhất
 
43
  if not hist.empty:
44
  open_price = hist['Open'].iloc[-1]
45
  high_price = hist['High'].iloc[-1]
@@ -50,7 +53,8 @@ def get_stock_data(symbols):
50
  "Open": round(open_price, 2),
51
  "High": round(high_price, 2),
52
  "Close": round(close_price, 2),
53
- "Low": round(low_price, 2)
 
54
  })
55
  except Exception as e:
56
  print(f"Không lấy được dữ liệu của {symbol}: {e}")
@@ -60,11 +64,17 @@ def get_stock_data(symbols):
60
  # Ứng dụng Streamlit
61
  st.title("Stocks with Volume > Avg Vol (3M) and Price Data")
62
 
 
 
 
 
 
 
63
  if st.button("Get Stocks Data"):
64
  with st.spinner('Fetching data...'):
65
  df = get_active_stocks() # Lấy danh sách cổ phiếu có volume cao
66
  symbols = df["Symbol"].tolist() # Lấy danh sách mã cổ phiếu
67
- stock_prices = get_stock_data(symbols) # Lấy giá Open, High, Close từ yfinance
68
 
69
  # Kết hợp hai bảng dữ liệu
70
  merged_df = pd.merge(df, stock_prices, on="Symbol", how="left")
 
1
+ import streamlit as st
2
+ from streamlit_autorefresh
3
+ import st_autorefresh
4
  import pandas as pd
5
  import requests
6
  import yfinance as yf
 
42
  try:
43
  stock = yf.Ticker(symbol)
44
  hist = stock.history(period="1d") # Lấy dữ liệu trong 1 ngày gần nhất
45
+ live_price = stock.info.get("regularMarketPrice", None)
46
  if not hist.empty:
47
  open_price = hist['Open'].iloc[-1]
48
  high_price = hist['High'].iloc[-1]
 
53
  "Open": round(open_price, 2),
54
  "High": round(high_price, 2),
55
  "Close": round(close_price, 2),
56
+ "Low": round(low_price, 2),
57
+ "Live Price": round(live_price, 2) if live_price else "N/A"
58
  })
59
  except Exception as e:
60
  print(f"Không lấy được dữ liệu của {symbol}: {e}")
 
64
  # Ứng dụng Streamlit
65
  st.title("Stocks with Volume > Avg Vol (3M) and Price Data")
66
 
67
+ # Cập nhật tự động mỗi 60 giây
68
+ st_autorefresh(interval=60000, key="price_refresh")
69
+
70
+ # Hiển thị thời gian cập nhật mới nhất
71
+ st.write("🔄 **Lần cập nhật gần nhất:**", pd.Timestamp.now())
72
+
73
  if st.button("Get Stocks Data"):
74
  with st.spinner('Fetching data...'):
75
  df = get_active_stocks() # Lấy danh sách cổ phiếu có volume cao
76
  symbols = df["Symbol"].tolist() # Lấy danh sách mã cổ phiếu
77
+ stock_prices = get_stock_data(symbols) # Lấy giá Open, High, Close, Low, Live Price từ yfinance
78
 
79
  # Kết hợp hai bảng dữ liệu
80
  merged_df = pd.merge(df, stock_prices, on="Symbol", how="left")