portfolio_toolkit package

Subpackages

Module contents

Portfolio Toolkit - A comprehensive toolkit for portfolio analysis and management.

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

Bases: object

__init__(name: str, currency: str, assets: List[PortfolioAsset], data_provider: DataProvider, account: Account, start_date: str) None
name: str
currency: str
assets: List[PortfolioAsset]
data_provider: DataProvider
account: Account
start_date: str
class portfolio_toolkit.PortfolioAsset(ticker: str, prices: pandas.core.series.Series, info: Dict, currency: Optional[str] = None, transactions: List[portfolio_toolkit.asset.portfolio_asset_transaction.PortfolioAssetTransaction] = <factory>)[source]

Bases: MarketAsset

__init__(ticker: str, prices: ~pandas.core.series.Series, info: ~typing.Dict, currency: str | None = None, transactions: ~typing.List[~portfolio_toolkit.asset.portfolio_asset_transaction.PortfolioAssetTransaction] = <factory>) None
add_split(split_dict: dict) float[source]

Adds a stock split to the portfolio asset by simulating sell all + buy equivalent. Creates a sell transaction for all held shares and a buy transaction for split-adjusted quantity.

Parameters:

split_dict – Dictionary containing split information with keys: - date: Split date (str) - split_factor: Split ratio as float (e.g., 2.0 for 2:1 split, 0.1 for 1:10 reverse split)

Returns:

Cash amount to be added to account due to fractional shares sold

(only applies to reverse splits where shares are lost)

Return type:

float

add_transaction(transaction: PortfolioAssetTransaction)[source]

Adds a transaction to the portfolio asset.

add_transaction_from_dict(transaction_dict: dict)[source]

Adds a transaction to the account from a dictionary.

classmethod to_dataframe(assets: List[PortfolioAsset]) DataFrame[source]

Convert a list of PortfolioAsset objects to a pandas DataFrame.

transactions: List[PortfolioAssetTransaction]
class portfolio_toolkit.YFDataProvider[source]

Bases: DataProvider

Market data provider using Yahoo Finance.

AAPL = 'AAPL'
AMZN = 'AMZN'
ARCH = 'ARCH'
BA = 'BA'
BBAR = 'BBAR'
BBD = 'BBD'
BMA = 'BMA'
BONO_10_ANIOS_USA = '^TNX'
BRENT = 'BZ=F'
DOLAR_INDEX = 'DX-Y.NYB'
DOW_JONES = '^DJI'
EURUSD = 'EURUSD=X'
EVTL = 'EVTL'
GOOGL = 'GOOGL'
INTC = 'INTC'
JOBY = 'JOBY'
KOS = 'KOS'
LILMF = 'LILMF'
MERVAL = '^MERV'
META = 'META'
MSFT = 'MSFT'
NASDAQ = '^IXIC'
NFE = 'NFE'
NVDA = 'NVDA'
ORO = 'GC=F'
SLDP = 'SLDP'
SP500 = '^GSPC'
TSLA = 'TSLA'
USDARS = 'USDARS=X'
USDEUR = 'USDEUR=X'
VALE = 'VALE'
VIX = '^VIX'
WTI = 'CL=F'
YPF = 'YPF'
__init__()[source]

Initializes the YFDataProvider class with in-memory caches for ticker data, info, and currencies.

get_price(ticker, fecha)[source]

Gets the price of an asset on a specific date.

Parameters:
  • ticker (str) – The ticker symbol.

  • fecha (datetime) – The date for which to get the price.

Returns:

The asset price on the specified date.

Return type:

float

get_price_series(ticker, columna='Close', period='5y')[source]

Gets the price series of an asset for a specific column.

Parameters:
  • ticker (str) – The ticker symbol.

  • columna (str) – The price column to get (default “Close”).

Returns:

Price series of the asset.

Return type:

pd.Series

get_price_series_converted(ticker, target_currency, columna='Close')[source]

Gets the price series of an asset converted to a target currency.

Parameters:
  • ticker (str) – The ticker symbol.

  • target_currency (str) – Target currency code (e.g., ‘EUR’, ‘USD’, ‘CAD’).

  • columna (str) – The price column to get (default “Close”).

Returns:

Price series of the asset converted to target currency.

Return type:

pd.Series

get_raw_data(ticker, periodo='5y')[source]

Gets all historical data for a ticker directly.

Parameters:
  • ticker (str) – The ticker symbol.

  • periodo (str) – The time period for historical data (default “5y”).

Returns:

The historical data for the ticker.

Return type:

pd.DataFrame

Example DataFrame returned:

# Open High Low Close Adj Close Volume #2024-07-01 10.00 10.50 9.80 10.20 10.10 1000000 #2024-07-02 10.20 10.60 10.00 10.40 10.30 1200000 #… … … … … … …

get_ticker_currency(ticker)[source]

Gets the currency of a ticker from its info.

Parameters:

ticker (str) – The ticker symbol.

Returns:

The currency code (e.g., ‘USD’, ‘EUR’, ‘CAD’) or ‘USD’ as default.

Return type:

str

get_ticker_info(ticker)[source]

Gets detailed information for a ticker using yfinance’s Ticker.info.

Parameters:

ticker (str) – The ticker symbol.

Returns:

Dictionary with company information and key statistics (e.g., P/E ratio, market cap, etc.).

Return type:

dict