This R function `calcExcessBenchmarkAll` computes the excess benchmark returns for...
This R function calcExcessBenchmarkAll
computes the excess benchmark returns for a series of trades, mapped to their respective strategies and benchmarks, while retaining the order of tickers from the input matrix. Here's a breakdown of the key tasks performed:
Key Purpose
The function calculates the excess returns for each trade relative to the benchmark associated with it and appends this information to the provided core.mat
matrix as a new column (ret_excessbm
).
Inputs
-
core.mat
:- A matrix containing information on trades.
- Contains columns like
"benchmark"
,"ret"
(returns), and"dt_record"
(dates on which the trades occurred).
-
datasource
:- A data source connection or an object used by the
getExecReturns
function to fetch external data (e.g., benchmark price data).
- A data source connection or an object used by the
Steps
-
Initialization:
- The function identifies all unique benchmarks in the
core.mat
(bm.list
). - It gathers all unique recorded dates (
all.dates
).
- The function identifies all unique benchmarks in the
-
Create a Temporary Working Matrix:
- A new column,
ret_excessbm
, is added tocore.mat
to store the computed excess benchmark returns. Initially, it is set to the same value as the"ret"
column.
- A new column,
-
Iterate Over Each Benchmark:
- For each unique benchmark (excluding the value
"NONE"
):- Subset the
core.mat
matrix for records tied to the current benchmark.
- Subset the
- For each unique benchmark (excluding the value
-
Get Benchmark Returns:
- The external function
getExecReturns
retrieves the benchmark's returns (bm.ret
) for the relevant dates (start.date
andend.date
) from the data source (datasource
). - The retrieval starts from the previous trading day (via
prevCalDay
) and excludes the first returned value (e.g., the first day's return isn't used).
- The external function
-
Calculate Excess Returns:
- The function
calcExcess
computes the excess returns of the trades relative to the benchmark (bm.ret
), using the subsetted data.
- The function
-
Update the Output:
- The computed excess returns are assigned back to the
ret_excessbm
column for the relevant benchmark group.
- The computed excess returns are assigned back to the
-
Return Result:
- After processing all benchmarks, the updated matrix is returned, with an added column (
ret_excessbm
) containing the excess benchmark returns for each trade.
- After processing all benchmarks, the updated matrix is returned, with an added column (
Outputs
The function returns a matrix (core.temp.mat
) with all the original columns from core.mat
, plus an additional column (ret_excessbm
) that includes the computed excess benchmark returns.
Dependent Functions
The function depends on these external functions:
-
source("execscheme_simple.r")
:- Presumably loads necessary logic (e.g., implementations of
getExecReturns
andcalcExcess
).
- Presumably loads necessary logic (e.g., implementations of
-
getExecReturns
:- Fetches benchmark returns for a given ticker and time range, associated with the provided
datasource
. - Returns an object with a
$gross
property representing the returns.
- Fetches benchmark returns for a given ticker and time range, associated with the provided
-
calcExcess
:- Calculates the excess returns of the strategy compared to the benchmark based on the input matrix and benchmark returns.
-
prevCalDay
:- Determines the previous calendar day relative to a given day.
Summary
This function processes a matrix of trades, calculates the excess benchmark return for each trade using associated benchmark data (queried from datasource
), and outputs the modified matrix with the added excess return information. It is designed for use in portfolio management or financial performance analysis tasks.