Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,25 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import yfinance as yf
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import plotly.graph_objects as go
|
| 5 |
import requests
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
@st.cache_data
|
| 12 |
-
def get_most_active_options():
|
| 13 |
-
url = "https://finance.yahoo.com/markets/options/most-active/?start=0&count=100"
|
| 14 |
-
response = requests.get(url)
|
| 15 |
-
tables = pd.read_html(response.text)
|
| 16 |
-
most_active_df = tables[0]
|
| 17 |
-
return most_active_df['Symbol'].unique().tolist()
|
| 18 |
-
|
| 19 |
-
# Lấy danh sách các mã cổ phiếu hoạt động nhất
|
| 20 |
-
most_active_symbols = get_most_active_options()
|
| 21 |
-
|
| 22 |
-
# Hiển thị danh sách các mã cổ phiếu để người dùng lựa chọn
|
| 23 |
-
ticker_input = st.selectbox("Chọn mã cổ phiếu", most_active_symbols)
|
| 24 |
-
premium_min = st.slider("Premium tối thiểu để cảnh báo MUA ($)", min_value=0.1, max_value=10.0, value=2.0, step=0.1)
|
| 25 |
-
|
| 26 |
-
if ticker_input:
|
| 27 |
-
ticker = yf.Ticker(ticker_input.upper())
|
| 28 |
-
|
| 29 |
-
try:
|
| 30 |
-
# Biểu đồ giá cổ phiếu
|
| 31 |
-
hist = ticker.history(period="5d", interval="30m")
|
| 32 |
-
fig = go.Figure(data=[go.Candlestick(
|
| 33 |
-
x=hist.index,
|
| 34 |
-
open=hist['Open'],
|
| 35 |
-
high=hist['High'],
|
| 36 |
-
low=hist['Low'],
|
| 37 |
-
close=hist['Close']
|
| 38 |
-
)])
|
| 39 |
-
fig.update_layout(title=f"Biểu đồ {ticker_input.upper()} (5 ngày gần nhất, 30 phút)", xaxis_title="Thời gian", yaxis_title="Giá")
|
| 40 |
-
st.plotly_chart(fig, use_container_width=True)
|
| 41 |
-
|
| 42 |
-
# Tải option chain gần nhất
|
| 43 |
-
exp_dates = ticker.options
|
| 44 |
-
if not exp_dates:
|
| 45 |
-
st.warning("Không có dữ liệu quyền chọn.")
|
| 46 |
-
else:
|
| 47 |
-
selected_exp = exp_dates[0]
|
| 48 |
-
st.info(f"Option Chain - Ngày đáo hạn gần nhất: {selected_exp}")
|
| 49 |
-
chain = ticker.option_chain(selected_exp)
|
| 50 |
-
calls = chain.calls.copy()
|
| 51 |
-
puts = chain.puts.copy()
|
| 52 |
-
|
| 53 |
-
current_price = ticker.history(period='1d')['Close'].iloc[-1]
|
| 54 |
-
|
| 55 |
-
# Lọc quyền chọn CALL
|
| 56 |
-
filtered_calls = calls[
|
| 57 |
-
(calls['volume'] > 50) &
|
| 58 |
-
(calls['openInterest'] > 20) &
|
| 59 |
-
(calls['impliedVolatility'] < 0.85) &
|
| 60 |
-
(abs(calls['strike'] - current_price) / current_price < 0.2)
|
| 61 |
-
].copy()
|
| 62 |
-
|
| 63 |
-
# Cảnh báo MUA cho CALL
|
| 64 |
-
filtered_calls['Alert'] = (
|
| 65 |
-
(abs(filtered_calls['strike'] - current_price) / current_price < 0.1) &
|
| 66 |
-
(filtered_calls['lastPrice'] > premium_min) &
|
| 67 |
-
(filtered_calls['volume'] / (filtered_calls['openInterest'] + 1) > 0.5) &
|
| 68 |
-
(filtered_calls['impliedVolatility'] < filtered_calls['impliedVolatility'].quantile(0.5))
|
| 69 |
-
).apply(lambda x: "MUA" if x else "")
|
| 70 |
-
|
| 71 |
-
# Cảnh báo SCALP cho CALL
|
| 72 |
-
filtered_calls['ScalpAlert'] = (
|
| 73 |
-
(filtered_calls['lastPrice'] >= 0.3) &
|
| 74 |
-
(filtered_calls['lastPrice'] <= 2.0) &
|
| 75 |
-
(filtered_calls['volume'] > 100) &
|
| 76 |
-
(filtered_calls['openInterest'] < 300) &
|
| 77 |
-
(abs(filtered_calls['strike'] - current_price) / current_price < 0.15)
|
| 78 |
-
).apply(lambda x: "SCALP" if x else "")
|
| 79 |
-
|
| 80 |
-
st.subheader("Quyền chọn CALL nổi bật")
|
| 81 |
-
st.dataframe(filtered_calls[[
|
| 82 |
-
'contractSymbol', 'strike', 'lastPrice', 'volume', 'openInterest',
|
| 83 |
-
'impliedVolatility', 'Alert', 'ScalpAlert'
|
| 84 |
-
]])
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
(puts['volume'] > 50) &
|
| 89 |
-
(puts['openInterest'] > 20) &
|
| 90 |
-
(puts['impliedVolatility'] < 0.85) &
|
| 91 |
-
(abs(puts['strike'] - current_price) / current_price < 0.2)
|
| 92 |
-
].copy()
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
(filtered_puts['lastPrice'] > premium_min) &
|
| 98 |
-
(filtered_puts['volume'] / (filtered_puts['openInterest'] + 1) > 0.5) &
|
| 99 |
-
(filtered_puts['impliedVolatility'] < filtered_puts['impliedVolatility'].quantile(0.5))
|
| 100 |
-
).apply(lambda x: "MUA" if x else "")
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
(filtered_puts['openInterest'] < 300) &
|
| 108 |
-
(abs(filtered_puts['strike'] - current_price) / current_price < 0.15)
|
| 109 |
-
).apply(lambda x: "SCALP" if x else "")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import pandas as pd
|
| 4 |
|
| 5 |
+
url = "https://www.barchart.com/options/unusual-activity/stocks"
|
| 6 |
+
headers = {
|
| 7 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
| 8 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
response = requests.get(url, headers=headers)
|
| 11 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Tìm bảng dữ liệu
|
| 14 |
+
table = soup.find("table")
|
| 15 |
+
rows = table.find_all("tr")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
data = []
|
| 18 |
+
for row in rows[1:]:
|
| 19 |
+
cols = [col.text.strip() for col in row.find_all("td")]
|
| 20 |
+
if cols:
|
| 21 |
+
data.append(cols)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Tạo DataFrame
|
| 24 |
+
df = pd.DataFrame(data)
|
| 25 |
+
print(df.head())
|