Portfolio Configuration Service#

Portfolio Configuration Service Module.

This module provides a comprehensive service for orchestrating portfolio loading and processing operations. The service acts as a coordination layer that manages configuration state and implements business logic.

class PortfolioConfigurationService(configuration: Configuration)#

Bases: object

Portfolio configuration orchestration service.

This service provides a unified interface for loading and processing portfolio configurations from multiple sources. It manages configuration state and implements the complete portfolio processing pipeline.

The service orchestrates: - Portfolio entity creation and validation - Weight transformation and normalization - Date range filtering based on configuration - Zero-weight ticker filtering - Backtest start date extraction

Variables:

_configuration (Configuration) – The backtest configuration containing date ranges and portfolio settings.

Examples

>>> config = Configuration(start_date="2020-01-01", end_date="2020-12-31")
>>> service = PortfolioConfigurationService(config)
>>> result = service.load_and_process_portfolio(portfolio_handlers=[handler])
>>> entity = result.get_portfolio_entity()
>>> weights = result.get_portfolio_dict()
load_and_process_portfolio(portfolio_handlers: list[PortfolioInputHandlerInterface] | None = None, portfolio_weights: dict[str, dict[str, float]] | None = None) PortfolioLoadResult#

Load and process the complete portfolio from either file or dictionary.

Exactly one of portfolio_handlers or portfolio_weights must be provided. If both are provided, portfolio_weights takes precedence.

Parameters:
  • portfolio_handlers (Optional[list[PortfolioInputHandlerInterface]]) – Handlers for file-based loading.

  • portfolio_weights (Optional[dict[str, dict[str, float]]]) – Dictionary-based weights mapping. Format: {‘date_string’: {‘ticker’: weight, …}}

Returns:

Object with getters for entity, table, processed weights and start date.

Return type:

portfolio_helpers.PortfolioLoadResult

Raises:

PortfolioEntityError – If neither source is provided or processing fails.

Example

>>> # Load from file
>>> result = service.load_and_process_portfolio(portfolio_handlers=[handler])
>>> # Load from dictionary
>>> weights = {"2020-01-01": {"AAPL": 0.6, "MSFT": 0.4}}
>>> result = service.load_and_process_portfolio(portfolio_weights=weights)
load_from_file(portfolio_handlers: list[PortfolioInputHandlerInterface]) PortfolioLoadResult#

Load and fully process a portfolio from file sources.

This method performs: 1) Load entity & raw table via handlers 2) Transform entity to weights dictionary 3) Run the common processing pipeline (date filter, zero-weight filter) 4) Return a single PortfolioLoadResult with all fields populated

Parameters:

portfolio_handlers (list[PortfolioInputHandlerInterface]) – Handlers to attempt portfolio loading from different sources/formats.

Returns:

Fully processed portfolio result object.

Return type:

portfolio_helpers.PortfolioLoadResult

Raises:
  • MissingPortfolio – If no handler can successfully load the portfolio.

  • PortfolioEntityError – If processing or validation fails.

Examples

>>> result = service.load_from_file([csv_handler, excel_handler])
>>> entity = result.get_portfolio_entity()

See also

Data Formats

Expected column layout of portfolio files (horizontal vs vertical, weight rules, date format).

load_from_dict

Sibling method that loads from an in-memory dict.

load_from_dict(portfolio_weights: dict[str, dict[str, float]]) PortfolioLoadResult#

Load and fully process a portfolio from a dictionary of weights.

Steps performed: 1) Create a PortfolioEntity from the weights dictionary 2) Convert entity to weights dictionary 3) Run the common processing pipeline 4) Return a single PortfolioLoadResult

Parameters:

portfolio_weights (dict[str, dict[str, float]]) – Weights mapping where keys are date strings and values are “ticker -> weight”.

Returns:

Fully processed portfolio result object.

Return type:

portfolio_helpers.PortfolioLoadResult

Raises:

PortfolioEntityError – If portfolio creation or processing fails.

Examples

>>> weights = {"2020-01-01": {"AAPL": 0.6, "MSFT": 0.4}}
>>> result = service.load_from_dict(weights)

See also

Data Formats

Expected structure of the weights mapping (date format, decimal weights, summing rules).

load_from_file

Sibling method that loads from CSV/Excel files.