portfolio_toolkit.utils package

Subpackages

Submodules

portfolio_toolkit.utils.correlation module

portfolio_toolkit.utils.correlation.calculate_correlation(returns1, returns2)[source]

Calculates the correlation between two return series.

Parameters:
  • returns1 (pd.Series) – The first return series.

  • returns2 (pd.Series) – The second return series.

Returns:

The correlation between the two return series.

Return type:

float

portfolio_toolkit.utils.log_returns module

portfolio_toolkit.utils.log_returns.calculate_log_returns(price_series)[source]

Calculates the logarithmic returns of a price series.

Parameters:

price_series (pd.Series) – Series of prices.

Returns:

Series of logarithmic returns.

Return type:

pd.Series

Module contents

portfolio_toolkit.utils.get_last_periods(n=4, period_type='weeks', include_current=False) List[Period][source]

Returns the last n periods as Period objects.

Parameters:
  • n (int) – Number of periods to return

  • period_type (str) – Type of period (‘years’, ‘quarters’, ‘months’, ‘weeks’)

  • include_current (bool) – Whether to include the current period

Returns:

List of Period objects representing each period

Return type:

List[Period]

Example

# Get last 3 completed weeks get_last_periods(3, ‘weeks’, include_current=False)

# Get last 2 weeks + current week get_last_periods(2, ‘weeks’, include_current=True)

portfolio_toolkit.utils.get_current_period(period_type: str) Period[source]

Returns the current period as a Period object based on the specified type.

Parameters:

period_type (str) – Type of period (‘year’, ‘quarter’, ‘month’, ‘week’)

Returns:

Period object representing the current period

Return type:

Period

Raises:

ValueError – If period_type is not supported

Example

current_week = get_current_period(‘week’) current_quarter = get_current_period(‘quarter’) current_year = get_current_period(‘year’)

class portfolio_toolkit.utils.Period(label: str, start_date: date, end_date: date)[source]

Bases: object

Represents a time period with a label and start/end dates.

label

Human-readable name for the period (e.g., “Q3 2025”, “July 2025”)

Type:

str

start_date

Start date of the period

Type:

date

end_date

End date of the period

Type:

date

Example

quarter = Period(“Q3 2025”, date(2025, 7, 1), date(2025, 9, 30)) month = Period(“July 2025”, date(2025, 7, 1), date(2025, 7, 31))

__init__(label: str, start_date: date, end_date: date) None
__post_init__()[source]

Validate that end_date is not before start_date.

__str__() str[source]

String representation of the period.

label: str
start_date: date
end_date: date