-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.py
More file actions
60 lines (53 loc) · 1.43 KB
/
visualizer.py
File metadata and controls
60 lines (53 loc) · 1.43 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
# visualizer.py
from __future__ import annotations
import pandas as pd
import plotly.graph_objects as go
def plot_price_history(
df: pd.DataFrame,
ticker: str,
*,
company_name: str | None = None,
y_column: str = "Close",
chart_type: str = "line", # "line" | "area" | "candlestick"
) -> go.Figure:
"""
Gera figura Plotly com histórico de preços.
chart_type pode ser:
• line – linha simples
• area – linha preenchida
• candlestick – OHLC (requer colunas Open/High/Low/Close)
"""
fig = go.Figure()
if chart_type == "candlestick":
fig.add_trace(
go.Candlestick(
x=df.index,
open=df["Open"],
high=df["High"],
low=df["Low"],
close=df["Close"],
name=ticker,
)
)
else:
fill = "tozeroy" if chart_type == "area" else None
fig.add_trace(
go.Scatter(
x=df.index,
y=df[y_column],
mode="lines",
fill=fill,
name=ticker,
)
)
title = f"Histórico de Preços – {ticker}"
if company_name:
title += f" — {company_name}"
fig.update_layout(
title=title,
xaxis_title="Data",
yaxis_title="Preço",
template="plotly_white",
hovermode="x unified",
)
return fig