mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-29 04:32:43 +00:00
* add accessors to the charting extension * equity views * get charting functions from the extensions/accessors * use the private variable instead * etf views * index views * crypto and currency views * technical views * economy views * fixedincome views * right description for price historical * unused * adjust test and deprecate the charting router * import views only with charting installed * adding views throught dependencies instead * removing unnecessary infra and adding entry point to charting extension * fixed income toml * removing accessor * typo * typo * adding extra params to obbject * remove things from init * rename utils to charts instead * updated readme --------- Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com> Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
"""Views for the ETF Extension."""
|
|
|
|
from typing import Any, Dict, Tuple, Union
|
|
|
|
import pandas as pd
|
|
from openbb_charting.charts.generic_charts import bar_chart
|
|
from openbb_charting.charts.price_historical import price_historical
|
|
from openbb_charting.charts.price_performance import price_performance
|
|
from openbb_charting.core.openbb_figure import OpenBBFigure
|
|
from openbb_core.app.utils import basemodel_to_df
|
|
from plotly.graph_objs import Figure
|
|
|
|
|
|
class EtfViews:
|
|
"""Etf Views."""
|
|
|
|
@staticmethod
|
|
def etf_historical( # noqa: PLR0912
|
|
**kwargs,
|
|
) -> Tuple[OpenBBFigure, Dict[str, Any]]:
|
|
"""Etf Price Historical Chart."""
|
|
return price_historical(**kwargs)
|
|
|
|
@staticmethod
|
|
def etf_price_performance( # noqa: PLR0912
|
|
**kwargs,
|
|
) -> Tuple[OpenBBFigure, Dict[str, Any]]:
|
|
"""Etf Price Performance Chart."""
|
|
return price_performance(**kwargs)
|
|
|
|
@staticmethod
|
|
def etf_holdings(**kwargs) -> Tuple[Union[OpenBBFigure, Figure], Dict[str, Any]]:
|
|
"""Equity Compare Groups Chart."""
|
|
if "data" in kwargs and isinstance(kwargs["data"], pd.DataFrame):
|
|
data = kwargs["data"]
|
|
elif "data" in kwargs and isinstance(kwargs["data"], list):
|
|
data = basemodel_to_df(kwargs["data"], index=None) # type: ignore
|
|
else:
|
|
data = basemodel_to_df(kwargs["obbject_item"], index=None) # type: ignore
|
|
|
|
if "weight" not in data.columns:
|
|
raise ValueError("No 'weight' column found in the data.")
|
|
|
|
orientation = kwargs.get("orientation", "h")
|
|
limit = kwargs.get("limit", 20)
|
|
symbol = kwargs["standard_params"].get("symbol") # type: ignore
|
|
title = kwargs.get("title", f"Top {limit} {symbol} Holdings")
|
|
layout_kwargs = kwargs.get("layout_kwargs", {})
|
|
|
|
data = data.sort_values("weight", ascending=False)
|
|
limit = min(limit, len(data)) # type: ignore
|
|
target = data.head(limit)[["symbol", "weight"]].set_index("symbol")
|
|
target = target.multiply(100)
|
|
axis_title = "Weight (%)"
|
|
|
|
fig = bar_chart(
|
|
target.reset_index(),
|
|
"symbol",
|
|
["weight"],
|
|
title=title, # type: ignore
|
|
xtitle=axis_title if orientation == "h" else None,
|
|
ytitle=axis_title if orientation == "v" else None,
|
|
orientation=orientation, # type: ignore
|
|
)
|
|
|
|
fig.update_layout(
|
|
hovermode="x" if orientation == "v" else "y",
|
|
margin=dict(r=0, l=50) if orientation == "h" else None,
|
|
)
|
|
|
|
fig.update_traces(
|
|
hovertemplate=(
|
|
"%{y:.3f}%<extra></extra>"
|
|
if orientation == "v"
|
|
else "%{x:.3f}%<extra></extra>"
|
|
)
|
|
)
|
|
|
|
if layout_kwargs:
|
|
fig.update_layout(**layout_kwargs) # type: ignore
|
|
|
|
content = fig.show(external=True).to_plotly_json() # type: ignore
|
|
|
|
return fig, content
|