import streamlit as st
import numpy as np
from src.utils.visualization import create_technical_chart
def display_technical_analysis(price_data, symbol):
"""
Hiển thị tab phân tích kỹ thuật
Args:
price_data: DataFrame chứa dữ liệu giá và các chỉ báo
symbol: Mã cổ phiếu
"""
st.markdown('
', unsafe_allow_html=True)
# Metrics hiện tại
latest_data = price_data.iloc[-1]
prev_data = price_data.iloc[-2] if len(price_data) > 1 else latest_data
col1, col2, col3, col4 = st.columns(4)
with col1:
price_change = latest_data['close'] - prev_data['close']
price_change_pct = (price_change / prev_data['close']) * 100
st.metric(
"Giá hiện tại",
f"{latest_data['close']:,.0f} VND",
f"{price_change:+,.0f} ({price_change_pct:+.2f}%)"
)
with col2:
st.metric("Khối lượng", f"{latest_data['volume']:,.0f}")
with col3:
high_52w = price_data['high'].max()
low_52w = price_data['low'].min()
st.metric("Cao nhất 52W", f"{high_52w:,.0f}")
with col4:
st.metric("Thấp nhất 52W", f"{low_52w:,.0f}")
# Biểu đồ giá
st.subheader("📈 Biểu đồ Giá và Khối lượng")
# Tạo biểu đồ kỹ thuật
fig = create_technical_chart(price_data)
fig.update_layout(title=f"Phân tích kỹ thuật {symbol}")
st.plotly_chart(fig, use_container_width=True)
# Phân tích kỹ thuật
col1, col2 = st.columns(2)
with col1:
st.subheader("📊 Chỉ báo Kỹ thuật")
latest_rsi = price_data['RSI'].iloc[-1]
rsi_signal = "Quá mua" if latest_rsi > 70 else "Quá bán" if latest_rsi < 30 else "Trung tính"
st.write(f"**RSI (14):** {latest_rsi:.2f} - {rsi_signal}")
# So sánh SMA
current_price = latest_data['close']
sma_20 = price_data['SMA_20'].iloc[-1]
sma_50 = price_data['SMA_50'].iloc[-1]
sma_trend = "Tăng" if current_price > sma_20 > sma_50 else "Giảm" if current_price < sma_20 < sma_50 else "Sideway"
st.write(f"**Xu hướng SMA:** {sma_trend}")
st.write(f"**SMA 20:** {sma_20:.0f}")
st.write(f"**SMA 50:** {sma_50:.0f}")
with col2:
st.subheader("📈 Thống kê Giá")
volatility = (price_data['close'].pct_change().std() * np.sqrt(252) * 100)
st.write(f"**Độ biến động (năm):** {volatility:.2f}%")
avg_volume = price_data['volume'].mean()
st.write(f"**KL trung bình:** {avg_volume:,.0f}")
price_range = ((high_52w - low_52w) / low_52w) * 100
st.write(f"**Biên độ 52W:** {price_range:.2f}%")
current_vs_high = ((current_price - high_52w) / high_52w) * 100
st.write(f"**So với đỉnh 52W:** {current_vs_high:.2f}%")
st.markdown('
', unsafe_allow_html=True)