Skip to main content

Variables

  • default_metric_registry: Default metric registry instance.

MetricRegistry

MetricRegistry(
    registry: vectorbtpro.utils.config.HybridConfig = None,
)
MetricRegistry class to manage and execute portfolio metrics. Method generated by attrs for class MetricRegistry.

Instance variables

  • registry: vectorbtpro.utils.config.HybridConfig: Metric vbt.HybridConfig registry. This registry should have a retrivable metric name as key and schema. if None, fallbacks to METRIC_REGISTRY.

Methods

get_all_metrics

get_all_metrics(
    self,
    include_non_tunable: bool = False,
) ‑> List[str]
Get a list of metric keys from the registry. Optionally include non-tunable metrics based on user choice. Parameters:
NameTypeDefaultDescription
include_non_tunableboolFalseWhether to include non-tunable metrics. Defaults to False.
Returns:
TypeDescription
List[str]List of metric keys with valid (non-NaNs) rolling_func values.

get_all_rolling_metrics

get_all_rolling_metrics(
    self,
    include_non_tunable: bool = False,
) ‑> List[str]
Get a list of metric keys from the registry that have a valid rolling_func. Optionally include non-tunable metrics based on user choice. Parameters:
NameTypeDefaultDescription
include_non_tunableboolFalseWhether to include non-tunable metrics. Defaults toFalse.
Returns:
TypeDescription
List[str]List of metric keys with valid (non-NaNs) rolling_func values.

get_all_pf_metrics

get_all_pf_metrics(
    self,
    include_non_tunable: bool = False,
) ‑> List[str]
Get a list of metric keys from the registry that have a valid pf_func. Optionally include non-tunable metrics based on user choice. Parameters:
NameTypeDefaultDescription
include_non_tunableboolFalseWhether to include non-tunable metrics. Defaults to False.
Returns:
TypeDescription
List[str]List of metric keys with valid (non-NaNs) pf_func values.

get_all_non_tunable_metrics

get_all_non_tunable_metrics(
    self,
) ‑> List[str]
Get a list of non-tunable metric keys from the registry. Returns:
TypeDescription
List[str]List of non-tunable metrics.

run

run(
    self,
    pf: vectorbtpro.portfolio.base.Portfolio,
    metrics: str | List[str],
    key: str,
    frame_to_dict: bool = True,
    validate_metrics: bool = True,
    **kwargs,
) ‑> Dict[strfloat | pandas.core.series.Series | pandas.core.frame.DataFrame]
Retrieve a dictionary of metric keys and their function values from the registry and execute it using vbt.deep_get_attr. Only includes metrics with non-None and string-type values.
  • Metrics with None or missing are excluded.
  • The function stops iterating once all requested metrics are found.
  • Input validation ensures all provided metrics exist in the registry.
Parameters:
NameTypeDefaultDescription
pfvbt.Portfolio--Portfolio instance.
metricsstr--or list of str A single metric key or a list of metric keys to look up in the registry.
keystr--Type of key to fetch.
frame_to_dictboolTrueChange frame format output to dict type if set to True. Raw pd.DataFrame object data otherwise. Frames are retrieve when group_by is False in the vbt.PF configuration and/or when rolling calculation is triggered. Defaults to True.
validate_metricsboolTrueCheck if metric is valid in the registry. Might slow-down code if set to True. Defaults to True.
kwargstp.Kwargs--Additional key-word arguments.
Raises:
TypeDescription
KeyErrorIf any provided metric key is not found in the registry.
Returns:
TypeDescription
tp.Dict[str, float | tp.SeriesFrame]Dictionary mapping valid metric keys to their corresponding output values.

run_rolling_metric

run_rolling_metric(
    self,
    pf: vectorbtpro.portfolio.base.Portfolio,
    metrics: str | List[str],
    window: int,
    minp: int = None,
    to_numpy: bool = False,
    validate_metrics: bool = True,
    **kwargs,
) ‑> pandas.core.frame.DataFrame | numpy.ndarray
Retrieve a dictionary of metric keys and their rolling function values from the registry and execute it using vbt.deep_get_attr. Only includes metrics with non-None and string-type values. Parameters:
NameTypeDefaultDescription
pfvbt.Portfolio--Portfolio instance.
metricsstr--or list of str A single metric key or a list of metric keys to look up in the registry.
windowint--Rolling window size.
minpintNoneMinimum number of observations required.
to_numpyboolFalseIf False, returns a pandas DataFrame. Otherwise a numpy array.
validate_metricsboolTrueCheck if metric is valid in the registry. Might slow-down code if set to True. Defaults to True.
kwargstp.Kwargs--Additional key-word arguments.
Returns:
TypeDescription
pd.DataFrame | tp.ArrayRolling output.

run_pf_metric

run_pf_metric(
    self,
    pf: vectorbtpro.portfolio.base.Portfolio,
    metrics: str | List[str],
    to_numpy: bool = False,
    validate_metrics: bool = True,
    **kwargs,
) ‑> collections.OrderedDict | numpy.ndarray
Retrieve a dictionary of metric keys and their pf function values from the registry and execute it using vbt.deep_get_attr. Only includes metrics with non-None and string-type values. Parameters:
NameTypeDefaultDescription
pfvbt.Portfolio--Portfolio instance.
metricsstr--or list of str A single metric key or a list of metric keys to look up in the registry.
frame_to_dictboolTrueChange frame format output to dict type if set toTrue. Raw pd.DataFrame object data otherwise. Frames are retrieve when group_by is False in the vbt.PF configuration. Defaults to True.
to_numpyboolFalseIf False, returns a pandas DataFrame. Otherwise a numpy array.
validate_metricsboolTrueCheck if metric is valid in the registry. Might slow-down code if set to True. Defaults to True.
kwargstp.Kwargs--Additional key-word arguments.
Returns:
TypeDescription
OrderedDictFloat or int if metrics is standalone. OrderedDict mapping valid metric keys to their corresponding output.