demo_deploy_streamlit_app / src /utils /visualization.py
thinh-vu's picture
Upload 12 files
021d91d verified
Raw
History Blame Contribute Delete
4.49 kB
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
def create_technical_chart(price_data):
"""
Tạo biểu đồ phân tích kỹ thuật với giá, khối lượng và RSI
Args:
price_data: DataFrame chứa dữ liệu giá và các chỉ báo
Returns:
Đối tượng biểu đồ Plotly
"""
# Tạo subplot
fig = make_subplots(
rows=3, cols=1,
shared_xaxes=True,
vertical_spacing=0.05,
subplot_titles=('Giá cổ phiếu', 'Khối lượng', 'RSI'),
row_heights=[0.6, 0.2, 0.2]
)
# Biểu đồ nến
fig.add_trace(
go.Candlestick(
x=price_data['time'],
open=price_data['open'],
high=price_data['high'],
low=price_data['low'],
close=price_data['close'],
name="Giá"
), row=1, col=1
)
# Thêm SMA
fig.add_trace(
go.Scatter(
x=price_data['time'],
y=price_data['SMA_20'],
mode='lines',
name='SMA 20',
line=dict(color='#4CAF50', width=2)
), row=1, col=1
)
fig.add_trace(
go.Scatter(
x=price_data['time'],
y=price_data['SMA_50'],
mode='lines',
name='SMA 50',
line=dict(color='#8C52FF', width=2)
), row=1, col=1
)
# Biểu đồ khối lượng
colors = ['#4CAF50' if close >= open else '#FF5252'
for close, open in zip(price_data['close'], price_data['open'])]
fig.add_trace(
go.Bar(
x=price_data['time'],
y=price_data['volume'],
name='Khối lượng',
marker_color=colors
), row=2, col=1
)
# RSI
fig.add_trace(
go.Scatter(
x=price_data['time'],
y=price_data['RSI'],
mode='lines',
name='RSI',
line=dict(color='#8C52FF', width=2)
), row=3, col=1
)
# Thêm đường RSI 30 và 70
fig.add_hline(y=30, line_dash="dash", line_color="red", row=3, col=1)
fig.add_hline(y=70, line_dash="dash", line_color="red", row=3, col=1)
fig.update_layout(
height=800,
xaxis_rangeslider_visible=False,
template="plotly_white"
)
return fig
def create_revenue_profit_chart(income, year_col, revenue_col, profit_col):
"""
Tạo biểu đồ doanh thu và lợi nhuận
Args:
income: DataFrame chứa dữ liệu báo cáo kết quả kinh doanh
year_col: Tên cột chứa năm
revenue_col: Tên cột chứa doanh thu
profit_col: Tên cột chứa lợi nhuận
Returns:
Đối tượng biểu đồ Plotly
"""
fig = go.Figure()
fig.add_trace(go.Scatter(
x=income[year_col],
y=income[revenue_col],
mode='lines+markers',
name='Doanh thu',
line=dict(color='#4CAF50', width=3)
))
fig.add_trace(go.Scatter(
x=income[year_col],
y=income[profit_col],
mode='lines+markers',
name='Lợi nhuận',
line=dict(color='#8C52FF', width=3)
))
fig.update_layout(
title="Doanh thu và Lợi nhuận (Tỷ đồng)",
xaxis_title="Năm",
yaxis_title="Tỷ đồng",
template="plotly_white"
)
return fig
def create_return_chart(ratio, year_col, roe_col, roa_col):
"""
Tạo biểu đồ ROE và ROA
Args:
ratio: DataFrame chứa dữ liệu chỉ số tài chính
year_col: Tên cột chứa năm
roe_col: Tên cột chứa ROE
roa_col: Tên cột chứa ROA
Returns:
Đối tượng biểu đồ Plotly
"""
fig = go.Figure()
fig.add_trace(go.Scatter(
x=ratio[year_col],
y=ratio[roe_col],
mode='lines+markers',
name='ROE (%)',
line=dict(color='#4CAF50', width=3)
))
fig.add_trace(go.Scatter(
x=ratio[year_col],
y=ratio[roa_col],
mode='lines+markers',
name='ROA (%)',
line=dict(color='#8C52FF', width=3)
))
fig.update_layout(
title="Tỷ suất Sinh lời (%)",
xaxis_title="Năm",
yaxis_title="Phần trăm (%)",
template="plotly_white"
)
return fig