portfolio_toolkit.position package

Submodules

portfolio_toolkit.position.closed_position module

class portfolio_toolkit.position.closed_position.ClosedPosition(ticker: str, buy_price: float, quantity: float, buy_date: str, sell_price: float, sell_date: str)[source]

Bases: Position

buy_date: str
sell_price: float
sell_date: str
value: float
profit: float
return_percentage: float
classmethod to_dataframe(positions: List[ClosedPosition]) DataFrame[source]

Convert a list of Position objects to a pandas DataFrame.

__init__(ticker: str, buy_price: float, quantity: float, buy_date: str, sell_price: float, sell_date: str) None

portfolio_toolkit.position.compare_open_positions module

portfolio_toolkit.position.compare_open_positions.compare_open_positions(portfolio: Portfolio, periods: List[Period], display='value') DataFrame[source]

Compare open positions across multiple periods.

Creates a DataFrame showing position values or returns at the end of each period. Rows represent assets, columns represent periods.

Parameters:
  • portfolio – Portfolio object containing assets

  • periods – List of Period objects to compare

  • display – ‘value’ shows position values, ‘return’ shows percentage returns

Returns:

DataFrame with assets as rows and periods as columns.

For ‘value’: Values show position market value, “-” for missing positions. For ‘return’: Values show percentage return vs previous period, “-” for missing/first period.

Return type:

pd.DataFrame

Example

# Show values df = compare_open_positions(portfolio, periods, display=’value’) Result:

Q1 2025 Q2 2025

AAPL 1500.00 1650.00 GOOGL 2000.00 -

# Show returns df = compare_open_positions(portfolio, periods, display=’return’) Result:

Q1 2025 Q2 2025

AAPL - 10.00% GOOGL - -

portfolio_toolkit.position.get_asset_closed_positions module

portfolio_toolkit.position.get_asset_closed_positions.get_asset_closed_positions(asset: PortfolioAsset, from_date: str, to_date: str) List[ClosedPosition][source]

Calculates all closed positions for an asset using FIFO logic up to a specific date. Each ‘sell’ transaction closes positions from the oldest ‘buy’ transactions.

Parameters:
  • asset (dict) – Asset dictionary containing transactions.

  • date (str) – The date up to which closed positions are calculated (YYYY-MM-DD).

Returns:

List of ClosedPosition objects representing closed positions.

Return type:

List[ClosedPosition]

portfolio_toolkit.position.get_asset_open_positions module

portfolio_toolkit.position.get_asset_open_positions.get_asset_open_positions(asset: PortfolioAsset, date: str) ValuedPosition[source]

Computes the open position of an asset as of a given date.

Parameters:
  • asset (PortfolioAsset) – The asset containing transactions.

  • date (str) – The date up to which the position is calculated (YYYY-MM-DD).

Returns:

An object representing the open position with valuation.

Return type:

ValuedPosition

portfolio_toolkit.position.get_closed_positions module

portfolio_toolkit.position.get_closed_positions.get_closed_positions(assets: List[PortfolioAsset], from_date: str, to_date: str) List[ClosedPosition][source]

Calculates all closed positions for multiple assets using FIFO logic up to a specific date.

Parameters:
  • assets (List[PortfolioAsset]) – List of PortfolioAsset objects containing transactions.

  • date (str) – The date up to which closed positions are calculated (YYYY-MM-DD).

Returns:

List of all ClosedPosition objects from all assets.

Return type:

List[ClosedPosition]

portfolio_toolkit.position.get_open_positions module

portfolio_toolkit.position.get_open_positions.get_open_positions(assets: List[PortfolioAsset], date: str) List[ValuedPosition][source]

Gets the open positions of a portfolio as of a given date and returns them as a list of ValuedPosition objects.

Parameters:
  • assets (List[PortfolioAsset]) – List of PortfolioAsset objects containing transactions.

  • date (str) – The date up to which the positions are calculated (YYYY-MM-DD).

Returns:

A list of ValuedPosition objects representing open positions.

Return type:

List[ValuedPosition]

portfolio_toolkit.position.get_valuation module

portfolio_toolkit.position.get_valuation.get_valuation(open_positions: List[ValuedPosition]) float[source]

Prints the valuation of open positions.

Parameters:

open_positions (List[ValuedPosition]) – List of ValuedPosition objects representing open positions.

Returns:

None

portfolio_toolkit.position.plot_closed_positions module

portfolio_toolkit.position.plot_closed_positions.plot_closed_positions(closed_positions: List[ClosedPosition]) BarChartData[source]

Plot closed positions grouped by month of sale

portfolio_toolkit.position.plot_closed_positions.plot_closed_positions_by_ticker(closed_positions: List[ClosedPosition]) BarChartData[source]

Plot closed positions grouped by ticker

portfolio_toolkit.position.plot_closed_positions.plot_closed_positions_count_by_month(closed_positions: List[ClosedPosition]) BarChartData[source]

Plot count of closed positions by month

portfolio_toolkit.position.plot_open_positions module

portfolio_toolkit.position.plot_open_positions.plot_open_positions(open_positions: List[ValuedPosition], group_by: str = 'Ticker') PieChartData[source]

Plot open positions in the portfolio

portfolio_toolkit.position.position module

class portfolio_toolkit.position.position.Position(ticker: str, buy_price: float, quantity: float)[source]

Bases: object

ticker: str
buy_price: float
quantity: float
cost: float
classmethod to_dataframe(positions: List[Position]) DataFrame[source]

Convert a list of Position objects to a pandas DataFrame.

__init__(ticker: str, buy_price: float, quantity: float) None

portfolio_toolkit.position.print_closed_positions module

portfolio_toolkit.position.print_closed_positions.print_closed_positions(positions: List[ClosedPosition], date: str) None[source]

Prints the closed positions in a tabular format with calculated returns and totals.

Parameters:
  • positions (List[ClosedPosition]) – List of ClosedPosition objects representing closed positions.

  • date (str) – The date for which the positions are printed.

Returns:

None

portfolio_toolkit.position.print_closed_positions.print_closed_positions_to_csv(positions: List[ClosedPosition], date: str, filepath: str) None[source]

Saves the closed positions to a CSV file with calculated returns and totals.

Parameters:
  • positions (List[ClosedPosition]) – List of ClosedPosition objects representing closed positions.

  • date (str) – The date for which the positions are saved.

  • filepath (str) – The path to the CSV file where data will be saved.

Returns:

None

portfolio_toolkit.position.print_closed_positions.print_closed_positions_summary(positions: List[ClosedPosition], date: str) None[source]

Prints a summary of closed positions with key metrics only.

Parameters:
  • positions (List[ClosedPosition]) – List of ClosedPosition objects representing closed positions.

  • date (str) – The date for which the positions are printed.

Returns:

None

portfolio_toolkit.position.print_open_positions module

portfolio_toolkit.position.print_open_positions.print_open_positions(positions: List[ValuedPosition]) None[source]

Prints the positions in a tabular format with calculated returns and totals.

Parameters:
  • positions (List[ValuedPosition]) – List of ValuedPosition objects representing open positions.

  • date (str) – The date for which the positions are printed.

Returns:

None

portfolio_toolkit.position.print_open_positions.print_open_positions_to_csv(positions: List[ValuedPosition], filepath: str) None[source]

Saves the open positions to a CSV file with calculated returns.

Parameters:
  • positions (List[ValuedPosition]) – List of ValuedPosition objects representing open positions.

  • filepath (str) – The path to the CSV file where data will be saved.

Returns:

None

portfolio_toolkit.position.valued_position module

class portfolio_toolkit.position.valued_position.ValuedPosition(ticker: str, buy_price: float, quantity: float, current_price: float, sector: str, country: str)[source]

Bases: Position

current_price: float
sector: str
country: str
value: float
classmethod to_dataframe(positions: List[ValuedPosition]) DataFrame[source]

Convert a list of Position objects to a pandas DataFrame.

__init__(ticker: str, buy_price: float, quantity: float, current_price: float, sector: str, country: str) None

Module contents

class portfolio_toolkit.position.ValuedPosition(ticker: str, buy_price: float, quantity: float, current_price: float, sector: str, country: str)[source]

Bases: Position

__init__(ticker: str, buy_price: float, quantity: float, current_price: float, sector: str, country: str) None
classmethod to_dataframe(positions: List[ValuedPosition]) DataFrame[source]

Convert a list of Position objects to a pandas DataFrame.

current_price: float
sector: str
country: str
value: float
ticker: str
buy_price: float
quantity: float
cost: float
class portfolio_toolkit.position.ClosedPosition(ticker: str, buy_price: float, quantity: float, buy_date: str, sell_price: float, sell_date: str)[source]

Bases: Position

__init__(ticker: str, buy_price: float, quantity: float, buy_date: str, sell_price: float, sell_date: str) None
classmethod to_dataframe(positions: List[ClosedPosition]) DataFrame[source]

Convert a list of Position objects to a pandas DataFrame.

buy_date: str
sell_price: float
sell_date: str
value: float
profit: float
return_percentage: float
ticker: str
buy_price: float
quantity: float
cost: float
class portfolio_toolkit.position.Position(ticker: str, buy_price: float, quantity: float)[source]

Bases: object

__init__(ticker: str, buy_price: float, quantity: float) None
classmethod to_dataframe(positions: List[Position]) DataFrame[source]

Convert a list of Position objects to a pandas DataFrame.

ticker: str
buy_price: float
quantity: float
cost: float
portfolio_toolkit.position.compare_open_positions(portfolio: Portfolio, periods: List[Period], display='value') DataFrame[source]

Compare open positions across multiple periods.

Creates a DataFrame showing position values or returns at the end of each period. Rows represent assets, columns represent periods.

Parameters:
  • portfolio – Portfolio object containing assets

  • periods – List of Period objects to compare

  • display – ‘value’ shows position values, ‘return’ shows percentage returns

Returns:

DataFrame with assets as rows and periods as columns.

For ‘value’: Values show position market value, “-” for missing positions. For ‘return’: Values show percentage return vs previous period, “-” for missing/first period.

Return type:

pd.DataFrame

Example

# Show values df = compare_open_positions(portfolio, periods, display=’value’) Result:

Q1 2025 Q2 2025

AAPL 1500.00 1650.00 GOOGL 2000.00 -

# Show returns df = compare_open_positions(portfolio, periods, display=’return’) Result:

Q1 2025 Q2 2025

AAPL - 10.00% GOOGL - -