"""Charting Extension Query Params."""
# pylint: disable=unused-variable,unused-argument
from typing import Any, Dict, List, Literal, Optional, Union
from openbb_core.provider.abstract.data import Data
from openbb_core.provider.abstract.query_params import QueryParams
from pydantic import Field, model_validator
from openbb_charting.core.to_chart import ChartIndicators
MAMODES = Literal["ema", "sma", "wma", "hna", "zlma", "rma"]
def _get_type_name(t):
"""Get the type name of a type hint."""
if hasattr(t, "__origin__"):
if hasattr(t.__origin__, "__name__"):
return f"{t.__origin__.__name__}[{', '.join([_get_type_name(arg) for arg in t.__args__])}]"
if hasattr(t.__origin__, "_name"):
return f"{t.__origin__._name}[{', '.join([_get_type_name(arg) for arg in t.__args__])}]" # pylint: disable=W0212
if isinstance(t, str):
return t
if hasattr(t, "__name__"):
return t.__name__
if hasattr(t, "_name"):
return t._name # pylint: disable=W0212
return str(t)
class BaseQueryParams(QueryParams):
"""Base Query Parmams Base Model."""
def __init__(self, **data):
"""Initialize the BaseQueryParams."""
super().__init__(**data)
self.__doc__ = self.__repr__()
def __repr__(self):
"""Return the string representation of the model."""
fields = self.__class__.model_fields
repr_str = (
"\n"
+ self.__class__.__name__
+ "\n\n"
+ " Parameters\n"
+ " ----------\n"
+ "\n".join(
[
f"\n {k} : {_get_type_name(v.annotation)}\n {v.description}".replace(
". ", ".\n "
)
for k, v in fields.items()
]
)
)
return repr_str
class ChartQueryParams(BaseQueryParams):
"""ChartParams."""
data: Optional[Union[Data, List[Data]]] = Field(
default=None,
description="Filtered versions of the data contained in the original `self.results`."
+ " Columns should be the same as the original data."
+ " Example use is to reduce the number of columns, or the length of data, to plot.",
)
class EquityPricePerformanceChartQueryParams(ChartQueryParams):
"""Equity Price Performance Chart Query Params."""
title: Optional[str] = Field(
default=None,
description="Title of the chart.",
)
orientation: Literal["v", "h"] = Field(
default="v",
description="Orientation of the bars.",
)
limit: Optional[int] = Field(
default=None,
description="Limit the number of bars to plot, from the most recent."
+ " If None, the periods from one-day to five-years will be plotted, if available.",
)
layout_kwargs: Optional[Dict[str, Any]] = Field(
default=None,
description="Additional keyword arguments to pass to the Plotly `update_layout` method.",
)
class EtfPricePerformanceChartQueryParams(EquityPricePerformanceChartQueryParams):
"""ETF Price Performance Chart Query Params."""
class EtfHoldingsChartQueryParams(ChartQueryParams):
"""ETF Holdings Chart Query Params."""
title