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.

Getting Started

Prerequisites

Hardware & Software

Ideal to run on high spec ISS, if any of the software below are missing, put up PRISM request to be installed

  1. Bloomberg enabled terminal, ensure Bloomberg Excel API is working
  2. R (v3.4.3 or later)
  3. RStudio (v1.1.423 or later)

Proficiency

  1. User: Minimal
  2. Developer: a. RMarkdown: Markdown language for document generation b. 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/array

Setting up

  1. Open RStudio
  2. install and setup tidymas by running commands a. install.packages("devtools") b.devtools::install_github("yunching/tidymas")c. Set upstrategies.csv` with the relevant strategies

Running program to generate output

  1. Open Bloomberg and log in
  2. Open active_risk.Rmd
  3. Click Knit > Knit to Word or Run > Run All to run
  4. Wait for program to run, about 5-10mins
  5. Open active_risk.docx to see output

Files Overview

Inputs (Needs to be updated)

  1. strategies.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

Main Program

  1. active_risk.Rmd: R Markdown file that generates the document containing analysis

Usage

Generating document

  1. Open active_risk.Rmd
  2. Click Knit > Knit to Word
  3. Wait a few minutes for the document to generate

Adding strategies

  1. Get updated strategies / trades from various divisions
  2. Open inputs/strategies.csv
  3. Fill in the rows as necessary a. 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

Updating sizes of trades

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)

Adding instruments

This program supports the following asset classes, any other asset classes will need to be developed

  1. Futures
  2. Bonds (Govt and ILBs)
  3. Equity indices
  4. FX
  5. CDS

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:

  1. FX: does not need to be added, as the required tickers are computed on the fly*
  2. Bonds: Only total return indices should be used

Adding scenarios

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.

Methodology

Size of trade

The size of trades are all converted to percentage terms, meaning % of R2 in order to compute returns.

Percent

If input trade sizes are in percent, the percentage is taken directly to be the weight, no adjustment is performed

Duration (months)

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

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 of instruments

Returns are calculated on a daily basis by using Bloomberg data, based on the below calculations

Bonds and Equity indices

Bonds use total return indices to calculate returns

$$return_{i,t} = \frac{index_{i,t}}{index_{i,t-1}} - 1$$

Futures

$$return_{i,t} = \frac{contract.value_{i,t}}{contract.value_{i,t-1}} - 1$$

FX

$$ \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} $$

CDS

$$return_{i,t} = \frac{price_{i,t}}{price_{i,t-1}} - 1$$

Weighted Return of strategies

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} $$

Unweighted return of strategy

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}$$

Active Risk

$$\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} $$



yunching/tidymas documentation built on Feb. 5, 2023, 1:42 p.m.