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 tradingshort_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
get_tickers(pf::PortfolioAtDate)
: Vector of tickers for all holdingsget_quantities(pf::PortfolioAtDate)
: Vector of quantities (holdings + 3 cash positions)get_prices(pf::PortfolioAtDate)
: Vector of prices for holdings + 3 cash positions as 1.0.get_values(pf::PortfolioAtDate)
: Vector of values ($\text{price} * \text{quantity}$) for holdings + 3 cash positions.get_total_value(pf::PortfolioAtDate)
: Total portfolio value
HoldingAtDate
Represents a single asset held in a portfolio at a specific point in time.
Key Functions
get_ticker(holding::HoldingAtDate)
: Returns the ticker of the given holding at the given date.get_price(holding::HoldingAtDate)
: Returns the price of the given holding at the given date.get_quantity(holding::HoldingAtDate)
: Returns the quantity of the given holding at the given date.get_value(holding::HoldingAtDate)
: Returns the value (quantity * price) of the given holding at the given date.
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.
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
get_total_values(pfh)
: Vector of total portfolio values before reweightingget_dates(pfh)
: Vector of dates corresponding to each portfolio snapshotget_number_of_holdings(pfh, ind; cutoff=0.0000001)
: Number of holdings at a specific index with quantity above the cutoffcompound_returns(pfh)
: Vector of cumulative returns relative to the initial portfolio valuereturns(pfh)
: Vector of period-to-period returnsreturns_log(pfh)
: Vector of log returns
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:
From a PortfolioAtDate: Use the
propagate_portfolio()
function, which takes an initial PortfolioAtDate, a target date, and a PositionScores object.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 historypfh_days
: Vector of PortfolioHistory objects, each representing a single day's portfolio changes
Key Functions
get_total_values(phd)
: Vector of total portfolio values at the end of each dayget_dates(phd)
: Vector of dates corresponding to each day's PortfolioHistoryreturns(phd)
: Vector of daily returnsreturns_log(phd)
: Vector of daily log returns
PortfolioHistory vs PortfolioHistoryDaily
Use Cases:
PortfolioHistory
: Suitable for end-of-day rebalancing strategies.PortfolioHistoryDaily
: Ideal for intraday trading or when tracking intraday events is neede.
Data Access:
PortfolioHistory
: Access viapf_before_rw[i]
orpf_after_rw[i]
for i-th trading date.PortfolioHistoryDaily
: Access viapfh_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.