Portfolio

PortfolioAtDate

A portfolio at one single instance in time is represented by a PortfolioAtDate object. It contains the information about the quantity of all holdings and cash at that date.

Structure

struct PortfolioAtDate
    holdings::Vector{HoldingAtDate}
    cash::Float64
    short_sell_cash::Float64
    margin_cash::Float64
    date::DateTime
end
  • holdings: Vector of individual asset holdings. See HoldingAtDate for details.
  • Cash positions:
    • cash: Regular cash balance available for trading
    • short_sell_cash: Cash generated from short-selling activities (always equal to the sum of all negative holdings, i.e. all short positions)
    • margin_cash: Cash frozen as a margin in case of margin requirements for short selling.
  • date: Date and time of the portfolio snapshot

If margin requirements are 150% and holdings for a total of $100 are shorted then short_sell_cash will contain $100 and margin_cash will contain $50.

Key Functions

HoldingAtDate

Represents a single asset held in a portfolio at a specific point in time.

Key Functions

Note

The quantity is defined as negative for a short position and thus the value is given by quantity * price will be negative.

Setup of PortfolioAtDate

To setup an initial PortfolioAtDate from a Collection we use the initialize_portfolio() function. The function takes a Collection and a datetime and creates a PortfolioAtDate at that datetime with all the Assets in the Collection available as holdings. It also takes a cash argument which will set the amount of cash available. All holding qunatities are set to 0.

Note

All holdings that are to be potentially bought or sold in the PortfolioHistory needs to exist in the initial PortfolioAtDate.

PortfolioHistory

A PortfolioHistory represents the evolution of a portfolio over time, containing snapshots of the portfolio before and after reweighting at each trading date.

Structure

struct PortfolioHistory
    name::String
    pf_before_rw::Vector{PortfolioAtDate}
    pf_after_rw::Vector{PortfolioAtDate}
end
  • name: A string identifier for the portfolio history.
  • pf_before_rw: Vector of PortfolioAtDate objects representing the portfolio before reweighting at each trading date.
  • pf_after_rw: Vector of PortfolioAtDate objects representing the portfolio after reweighting at each trading date.

Key Functions

Portfolio Reweighting

At every timestep new portfolio weightings are calculated from PositionScores.

The final portfolio weights for each asset $i$ at timestep $k$ are calculated as:

\[W_{i, k} = \frac{S_{i, k}}{\sum_{i} S_{i, k} + S_{c,k}}\]

where: $S_{i, k}$ is the score of holding $i$ at timestep $k$ and $S_{c,k}$ is the cash score at timestep $k$

The cash weight is calculated similarly:

\[W_{c, k} = \frac{S_{c, k}}{\sum_{i} S_{i, k} + S_{c,k}}\]

The sum of all weights (including cash) always equals 1:

\[\sum_{i} W_{i, k} + W_{c, k} = 1\]

Setup And Propagation of PortfolioHistory

A PortfolioHistory can be created in two main ways:

  1. From a PortfolioAtDate: Use the propagate_portfolio() function, which takes an initial PortfolioAtDate, a target date, and a PositionScores object.

  2. Directly from PositionScores: Use the PortfolioHistory() constructor, which creates a PortfolioHistory with all holdings from the PositionScores available.

can also be further propagated... write more

PortfolioHistoryDaily

A PortfolioHistoryDaily represents a collection of daily PortfolioHistory objects, allowing for a more granular view of portfolio changes within each day.

Usually used in conjecture with Discrete Collection

Structure

struct PortfolioHistoryDaily
    name::String
    pfh_days::Vector{PortfolioHistory}
end
  • name: A string identifier for the daily portfolio history
  • pfh_days: Vector of PortfolioHistory objects, each representing a single day's portfolio changes

Key Functions

PortfolioHistory vs PortfolioHistoryDaily

  1. Use Cases:

    • PortfolioHistory: Suitable for end-of-day rebalancing strategies.
    • PortfolioHistoryDaily: Ideal for intraday trading or when tracking intraday events is neede.
  2. Data Access:

    • PortfolioHistory: Access via pf_before_rw[i] or pf_after_rw[i] for i-th trading date.
    • PortfolioHistoryDaily: Access via pfh_days[i] for i-th day, then use PortfolioHistory methods.

Analysis of PortfolioHistory

The following mostly self explanatory functions are useful when evaluationg a trading strategy:

sharpe_ratio(pfh::PortfolioHistory)

maximum_drawdown(pfh::PortfolioHistory)

plot_portfolio(pfh::PortfolioHistory)

plot_portfolio(pfh::PortfolioHistory, pfh2::PortfolioHistory)

plot_portfolio(pfh_vect::Vector{PortfolioHistory})

print_portfolio_table(pfh::PortfolioHistory

Most of these functions also work on PortfolioHistoryDaily.

For all functions see Portfolio Analysis - Functions.