portfolio_toolkit.portfolio package

Subpackages

Submodules

portfolio_toolkit.portfolio.portfolio module

class portfolio_toolkit.portfolio.portfolio.Portfolio(name: str, currency: str, assets: List[portfolio_toolkit.asset.portfolio.portfolio_asset.PortfolioAsset], data_provider: portfolio_toolkit.data_provider.data_provider.DataProvider, account: portfolio_toolkit.account.account.Account, start_date: str)[fuente]

Bases: object

name: str
currency: str
assets: List[PortfolioAsset]
data_provider: DataProvider
account: Account
start_date: str
classmethod from_dict(data: dict, data_provider: DataProvider) Portfolio[fuente]
get_stats(year: str) PortfolioStats[fuente]

Returns PortfolioStats for the given year.

get_time_series() PortfolioTimeSeries[fuente]

Returns a PortfolioTimeSeries for the given portfolio.

get_open_positions(date: str) OpenPositionList[fuente]

Returns OpenPositionList for the given date.

get_closed_positions(from_date: str, to_date: str) ClosedPositionList[fuente]

Returns ClosedPositionList for the given date.

__init__(name: str, currency: str, assets: List[PortfolioAsset], data_provider: DataProvider, account: Account, start_date: str) None

portfolio_toolkit.portfolio.portfolio_from_dict module

portfolio_toolkit.portfolio.portfolio_from_dict.portfolio_from_dict(data: dict, data_provider: DataProvider) Portfolio[fuente]

Loads and validates a JSON file containing portfolio information.

Parámetros:
  • data (dict) – The portfolio data as a dictionary.

  • data_provider (DataProvider) – Data provider instance for fetching ticker information.

Devuelve:

The loaded portfolio object.

Tipo del valor devuelto:

Portfolio

portfolio_toolkit.portfolio.portfolio_from_dict.process_transactions(transactions: dict, splits: dict, portfolio_currency: str, data_provider: DataProvider) Tuple[List[PortfolioAsset], Account, datetime][fuente]

Processes transactions to create asset objects and validate them.

Parámetros:
  • transactions (list) – List of transaction dictionaries.

  • portfolio_currency (str) – The currency of the portfolio.

  • data_provider – Optional data provider for fetching ticker information.

Devuelve:

List of real assets (non-cash). dict: Cash account with all cash transactions. datetime: Calculated portfolio start date.

Tipo del valor devuelto:

list

portfolio_toolkit.portfolio.portfolio_from_dict.get_transaction_ticker(transaction, portfolio_currency)[fuente]

Returns the ticker for a transaction. If the transaction does not have a ticker, it returns the synthetic cash ticker based on the portfolio currency.

Parámetros:
  • transaction (dict) – The transaction to process.

  • portfolio_currency (str) – The currency of the portfolio.

Devuelve:

The ticker for the transaction.

Tipo del valor devuelto:

str

portfolio_toolkit.portfolio.portfolio_from_dict.validate_transaction(transaction)[fuente]

Validates that a transaction contains the required fields: date, type, and quantity.

Parámetros:

transaction (dict) – The transaction to validate.

Muestra:

ValueError – If the transaction does not contain the required fields.

Module contents

class portfolio_toolkit.portfolio.Portfolio(name: str, currency: str, assets: List[portfolio_toolkit.asset.portfolio.portfolio_asset.PortfolioAsset], data_provider: portfolio_toolkit.data_provider.data_provider.DataProvider, account: portfolio_toolkit.account.account.Account, start_date: str)[fuente]

Bases: object

__init__(name: str, currency: str, assets: List[PortfolioAsset], data_provider: DataProvider, account: Account, start_date: str) None
classmethod from_dict(data: dict, data_provider: DataProvider) Portfolio[fuente]
get_closed_positions(from_date: str, to_date: str) ClosedPositionList[fuente]

Returns ClosedPositionList for the given date.

get_open_positions(date: str) OpenPositionList[fuente]

Returns OpenPositionList for the given date.

get_stats(year: str) PortfolioStats[fuente]

Returns PortfolioStats for the given year.

get_time_series() PortfolioTimeSeries[fuente]

Returns a PortfolioTimeSeries for the given portfolio.

name: str
currency: str
assets: List[PortfolioAsset]
data_provider: DataProvider
account: Account
start_date: str
class portfolio_toolkit.portfolio.PortfolioStats(realized_profit: float, unrealized_profit: float, initial_cash: float, final_cash: float, initial_valuation: float, final_valuation: float, incomes: float, deposits: float, withdrawals: float, commission: float, closed_positions_stats: Dict[str, Any], closed_positions: DataFrame, open_positions: DataFrame, transactions: DataFrame)[fuente]

Bases: object

Portfolio statistics dataclass containing all financial metrics and data for a given period.

realized_profit

Total profit/loss from closed positions

Type:

float

unrealized_profit

Unrealized gains/losses from current open positions

Type:

float

initial_valuation

Portfolio valuation at start of period

Type:

float

final_valuation

Portfolio valuation at end of period

Type:

float

incomes

Total income transactions in period

Type:

float

deposits

Total deposit transactions in period

Type:

float

withdrawals

Total withdrawal transactions in period

Type:

float

commission

Total commission fees (currently 0.0)

Type:

float

closed_positions_stats

Statistics about closed positions

Type:

Dict[str, Any]

closed_positions

DataFrame of closed positions

Type:

pd.DataFrame

open_positions

DataFrame of current open positions

Type:

pd.DataFrame

transactions

DataFrame of account transactions

Type:

pd.DataFrame

__init__(realized_profit: float, unrealized_profit: float, initial_cash: float, final_cash: float, initial_valuation: float, final_valuation: float, incomes: float, deposits: float, withdrawals: float, commission: float, closed_positions_stats: Dict[str, Any], closed_positions: DataFrame, open_positions: DataFrame, transactions: DataFrame) None
classmethod from_portfolio(portfolio: Portfolio, year: str) PortfolioStats[fuente]

Alternate constructor that builds PortfolioStats from a Portfolio and year.

property net_cash_flow: float

Net cash flow (deposits - withdrawals + incomes)

property return_percentage: float

Portfolio return percentage for the period

to_dict() Dict[str, Any][fuente]

Convert PortfolioStats to dictionary for backward compatibility

property total_profit: float

Total profit including both realized and unrealized gains

property valuation_change: float

Change in portfolio valuation from start to end of period

realized_profit: float
unrealized_profit: float
initial_cash: float
final_cash: float
initial_valuation: float
final_valuation: float
incomes: float
deposits: float
withdrawals: float
commission: float
closed_positions_stats: Dict[str, Any]
closed_positions: DataFrame
open_positions: DataFrame
transactions: DataFrame
class portfolio_toolkit.portfolio.PortfolioTimeSeries(name: str, currency: str, assets: List[PortfolioAsset], data_provider: DataProvider, account: Account, start_date: str)[fuente]

Bases: Portfolio

DataFrame with the following structure:

Columns: - Date (str): Date of the transaction or calculation. - Ticker (str): Asset symbol (including synthetic cash tickers like __EUR). - Quantity (int): Accumulated quantity of shares/units on the date. - Price (float): Share price on the date in original currency (1.0 for cash tickers). - Price_Base (float): Share price converted to portfolio base currency, including fees for purchase transactions. - Value (float): Total value of the shares/units on the date (Quantity * Price). - Value_Base (float): Total value in portfolio base currency (Quantity * Price_Base). - Cost (float): Total accumulated cost of the shares/units on the date in base currency. - Sector (str): Sector to which the asset belongs (Cash for synthetic tickers). - Country (str): Country to which the asset belongs.

Each row represents the state of an asset on a specific date. Cash transactions use synthetic tickers (e.g., __EUR) with constant price of 1.0.

__init__(name: str, currency: str, assets: List[PortfolioAsset], data_provider: DataProvider, account: Account, start_date: str) None
classmethod from_portfolio(portfolio: Portfolio) PortfolioTimeSeries[fuente]

Alternate constructor that builds PortfolioTimeSeries from a Portfolio.

plot_evolution() LineChartData[fuente]
print() None[fuente]
portfolio_timeseries: DataFrame