knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This project aims to provide a macro view on alpha strategies, it includes various calculations for alpha strategies, including PnL, risk, correlation etc.
Ideal to run on high spec ISS, if any of the software below are missing, put up PRISM request to be installed
dplyr
: Package for dataframe manipulation
i. %>%
: piping of output
ii. mutate
: data manipulation
iii. arrange
, filter
, select
: sorting, filtering and selecting of columns
iv. left_join
: joining of dataframes
v. group_by
, summarise
: grouping and summarising across groups
c. ggplot2
d. map
, map2
, apply
, lapply
, sapply
: Functions for iterating functions of vector/arraytidymas
by running commands
a. install.packages("devtools")
b.
devtools::install_github("yunching/tidymas")c. Set up
strategies.csv` with the relevant strategiesBloomberg
and log inactive_risk.Rmd
Knit
> Knit to Word
or Run
> Run All
to run active_risk.docx
to see outputstrategies.csv
: Contains all the executed/intended strategies, the corresponding instruments, sizes and trade dates. Update by approaching members of each division. Instruments identified by identifiers
active_risk.Rmd
: R Markdown file that generates the document containing analysisactive_risk.Rmd
Knit
> Knit to Word
inputs/strategies.csv
owner
: ed
, cat
,europe
,us
b. strategy
: any strategy name will do, use _
instead of spaces
c. type
: Duration
, Spread
, Curve
, Inflation
, FX
, Equity
, Others
d. open_date
& close_date
: In the format yyyymmdd
without spaces or dashes. Do not use Excel built in date formats as it will certainly screw up
e. identifier
: ticker of instrument to be traded, matching the identifiers in the inputs/tickers_XXX
files
f. asset_class
: govt
, ilb
, future
, fx
, equity
, cds
. Program searches identifier in the corresponding asset files
g. size
: e.g. 0.5 can be used for both 0.5mths OR 0.5% of R2
h. size_type
: months
, percent
To updated strategies when there are changes in size, there are two ways to do it:
Method 1: Recommended for partial close of trade, or big changes in trade
Close existing entries for the trade, and key in a new trade on the close date
Method 2: Recommended for add-on of a position
If trade is increase in size, you can add a new trade with the size increase (without closing the previous trade)
This program supports the following asset classes, any other asset classes will need to be developed
Developer note:
If there are instruments that have been omitted, you can add them by opening the inst/extdata/tickers_XXX.csv
file and adding a new row for the asset. Please note for the following assets:
If any return or risk simulation needs to be done over a specified period in history, it can be added to active_risk.rmd
file, it will be automatically added to the outputs which rely on the scenarios.
The size of trades are all converted to percentage terms, meaning % of R2
in order to compute returns.
If input trade sizes are in percent
, the percentage is taken directly to be the weight, no adjustment is performed
When input size is in duration
contribution in months
:
$$percent.weight_{i,t} = \frac{month.weight_{i,t}}{modified.duration_{i,t} \times 12}$$
Size of strategy is calculated by taking first summing all the positive sizes of instruments, and summing all the negative sizes. The higher absolute of positve or negative is taken to be the size of the trade. For example, a flattener of 10y vs 2y with months weighted size of 0.5 months, will be converted to approximately size of 0.5mths / (dur 10y * 12) = 0.42%, while 2years will be -2.08%, the size of the strategy is taken to be 2.08% as it is larger of 0.42% and 2.08%.
$$strategy.weight_{s,t} = max(\sum_i f(instr.weight_{i, t}), \sum_i g(instr.weight_{i,t})) \
f(x) = \begin{cases}
x \text{ for } x > 0 \
0 \text{ for } x \le 0
\end{cases} \
g(x) = \begin{cases}
|x| \text{ for } x < 0 \
0 \text{ for } x \ge 0
\end{cases}$$
Returns are calculated on a daily basis by using Bloomberg data, based on the below calculations
Bonds use total return indices to calculate returns
$$return_{i,t} = \frac{index_{i,t}}{index_{i,t-1}} - 1$$
$$return_{i,t} = \frac{contract.value_{i,t}}{contract.value_{i,t-1}} - 1$$
$$ \begin{aligned} return_{i,t} &= price.return_{i,t} + funding.return_{i,t} \ &= (\frac{price_{i,t}}{price_{i,t-1}} - 1) + (left.depo_{i,t-1} - right.depo_{i,t-1}) \end{aligned} $$
$$return_{i,t} = \frac{price_{i,t}}{price_{i,t-1}} - 1$$
Weighted return of strategies are simply the weight of the instruments multipled by the daily return
$$weighted.return_{s,t} = \sum percent.weight_{i,t} \times return_{i,t} $$
To get the unweighted return of strategy, which is used to calculate correlation of trades and active risks, we take weighted return divided by the percentage weight. Note that we will have to simulate positions in order to calculate unweighted return of strategies. This is done by projecting the sizes forward and backward
$$unweighted.return_{s,t} = weighted.return_{s,t} \div percent.weight_{s,t}$$
$$\textbf{X} =
\begin{bmatrix}
percent.weight_{1, t=last} \ percent.weight_{2, t= last} \ percent.weight_{3, t=last} \ ... \ percent.weight_{n, t=last}
\end{bmatrix}
$$
$$\sigma_{i,j} = cov(unweighted.return_i, unweighted.return_j)$$
$$
\Sigma = cov.matrix =
\begin{bmatrix}
\sigma_1^2 & \sigma_{1,2} & ... & \sigma_{1,n} \
... & ... & ... & ... \
\sigma_{i, 1} & \sigma_{i,2} & ... & \sigma_{i,n} \
... & ... & ... & ... \
\sigma_{1, n} & \sigma_{2, n} & ... & \sigma_n^2
\end{bmatrix}
$$
$$\sigma(X) = \textbf{X}^T \times \Sigma \times \text{X}$$
$$\textbf{MR} = \frac{1}{\sigma(X)}(\textbf{X}^T \times \Sigma)$$ $$\textbf{AR} = \textbf{MR} \cdot \textbf{X} $$
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.