rebalance.portfolio

Submodules

rebalance.portfolio.portfolio

class Portfolio[source]

Bases: object

Portfolio class.

Defines a Portfolio of Asset s and Cash and performs rebalancing of the portfolio.

__init__()[source]

Initialization.

_combine_cash(currency=None)[source]

Converts cash in portfolio to one currency.

Parameters:currency (str, optional) –
_sell_everything()[source]

Sells all assets in the portfolio and converts them to cash.

_smart_exchange(currency_amount)[source]

Performs currency exchange between Portfolio’s different sources of cash based on amount required per currency.

Parameters:currency_amount (Dict[str, float]) – Amount needed per currency. The keys of the dictionary are the currency.
Returns:List[tuple]
tuple containing:
  • from_amount (float): Amount exchanged from currency indicated by from_currency
  • from_currency (str): Currency from which to perform the exchange
  • to_amount (float): Amount exchanged to currency indicated by to_currency
  • to_currency (str): Currency to which to perform the exchange
  • rate (float): Currency exchange rate from from_currency to to_currency
add_asset(asset)[source]

Adds specified Asset to the portfolio.

Parameters:asset (Asset) – Asset to add to portfolio.
add_cash(amount, currency)[source]

Adds cash to portfolio.

Parameters:
  • amount (float) – Amount of cash
  • currency (str) – Currency of cash
asset_allocation()[source]

Computes the portfolio’s asset allocation.

Returns:Dict[str, Asset] – Asset allocation of the portfolio (in %). The keys of the dictionary are the tickers of the assets.
assets

Dictionary of assets in portfolio. The keys of the dictionary are the tickers of the assets.

No setter allowed.

Type:Dict[str, Asset]
buy_asset(ticker, quantity)[source]

Buys (or sells) the specified amount of an asset.

Parameters:
  • ticker (str) – Ticker of asset to buy.
  • quantity (int) – If positive, it is the quantity to buy. If negative, it is the quantity to sell.
Returns:

float – Cost of transaction (in asset’s own currency)

cash

Portfolio’s dictionary of cash. The keys are currency symbols.

Type:Dict[str, Cash]
cash_value(currency)[source]

Computes the cash value in the portfolio.

Parameters:currency (str) – The currency in which to obtain the value.
Returns:float – The total cash value in the portfolio.
easy_add_assets(tickers, quantities)[source]

An easy way to add multiple assets to portfolio.

Parameters:
  • tickers (Sequence[str]) – Ticker of assets in portfolio.
  • quantities (Sequence[float]) – Quantities of respective assets in portfolio. Must be in the same order as tickers.
easy_add_cash(amounts, currencies)[source]

An easy way of adding cash of various currencies to portfolio.

Parameters:
  • amounts (Sequence[float]) – Amounts of cash from different curriencies.
  • currencies (Sequence[str]) – Specifies curriency of each of the amounts. Must be in the same order as amounts.
exchange_currency(to_currency, from_currency, to_amount=None, from_amount=None)[source]

Performs currency exchange in Portfolio.

Parameters:
  • to_currency (str) – Currency to which to perform the exchange
  • from_currency (str) – Currency from which to perform the exchange
  • to_amount (float, optional) – If specified, it is the amount to which we want to convert
  • from_amount (float, optional) – If specified, it is the amount from which we want to convert

Note: either the to_amount or from_amount needs to be specifed.

market_value(currency)[source]

Computes the total market value of the assets in the portfolio.

Parameters:currency (str) – The currency in which to obtain the value.
Returns:float – The total market value of the assets in the portfolio.
rebalance(target_allocation, verbose=False)[source]

Rebalances the portfolio using the specified target allocation, the portfolio’s current allocation, and the available cash.

Parameters:
  • target_allocation (Dict[str, float]) – Target asset allocation of the portfolio (in %). The keys of the dictionary are the tickers of the assets.
  • verbose (bool, optional) – Verbosity flag. Default is False.
Returns:

(tuple)

tuple containing:
  • new_units (Dict[str, int]): Units of each asset to buy. The keys of the dictionary are the tickers of the assets.
  • prices (Dict[str, [float, str]]): The keys of the dictionary are the tickers of the assets. Each value of the dictionary is a 2-entry list. The first entry is the price of the asset during the rebalancing computation. The second entry is the currency of the asset.
  • exchange_rates (Dict[str, float]): The keys of the dictionary are currencies. Each value is the exchange rate to CAD during the rebalancing computation.
  • max_diff (float): Largest difference between target allocation and optimized asset allocation.

selling_allowed

Flag indicating if selling of assets is allowed or not when rebalancing portfolio.

Type:bool
value(currency)[source]

Computes the total value (cash and assets) in the portfolio.

Parameters:currency (str) – The currency in which to obtain the value.
Returns:float – The total value in the portfolio.