Portfolio Risk Matrix

Portfolio Risk Matrix is an indicator drawn in its own pane.

several marketsstatistics and matrices

portfolio-risk-matrix.abx
1indicator "Portfolio Risk Matrix"
2pane: new
3markets: EURUSD, GBPUSD, USDJPY
4
5input lookback = 100
6
7eur = returns(close_of(EURUSD))
8gbp = returns(close_of(GBPUSD))
9jpy = returns(close_of(USDJPY))
10cov = covariance_matrix([eur, gbp, jpy], lookback)
11weights = matrix(3, 1, fill: 0.3333)
12portfolio_variance = multiply(multiply(transpose(weights), cov), weights)
13eur_gbp_corr = correlation(eur, gbp, lookback)
14eur_jpy_corr = correlation(eur, jpy, lookback)
15
16plot eur_gbp_corr as eur_gbp, color: blue
17plot eur_jpy_corr as eur_jpy, color: orange
18hline 0, style: dotted, color: gray
19cells rows: [["Pair", "Correlation"], ["EUR/GBP", "{eur_gbp_corr:0.00}"], ["EUR/JPY", "{eur_jpy_corr:0.00}"]], columns: 2
Watch it run · EURUSD · 1h bars
eur_gbpeur_jpy

Sample data. Not a result. The real AlgoBarsX engine ran this exact script on made-up bars, and this replays what it drew and did. It shows no profit or loss, and it says nothing about how the script would do in a real market.

Replay of this script on 150 sample bars: 2 lines drawn.

What this script says

Portfolio Risk Matrix is an indicator drawn in its own pane.

You can change one input: lookback (default 100).

It calculates eur as the returns of the close of EURUSD, gbp as the returns of the close of GBPUSD, jpy as the returns of the close of USDJPY, cov as the covariance matrix (series eur, gbp and jpy, length lookback), weights as the matrix (rows 3, columns 1, fill 0.3333), portfolio_variance as the multiply (a the multiply (a the transpose (m weights), b cov), b weights), eur_gbp_corr as the correlation (a eur, b gbp, length lookback) and eur_jpy_corr as the correlation (a eur, b jpy, length lookback).

On the chart, it plots eur_gbp_corr as eur_gbp, plots eur_jpy_corr as eur_jpy, draws a horizontal line at 0 and draws a cells.

Try this in the Terminal. AlgoBars is free: $0 a month, no card needed.

Create my free account