# Portfolio Risk Matrix

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

Source: https://algobarsx.com/docs/ex-38-portfolio-risk-matrix/

```algobarsx
indicator "Portfolio Risk Matrix"
    pane: new
    markets: EURUSD, GBPUSD, USDJPY

input lookback = 100

eur = returns(close_of(EURUSD))
gbp = returns(close_of(GBPUSD))
jpy = returns(close_of(USDJPY))
cov = covariance_matrix([eur, gbp, jpy], lookback)
weights = matrix(3, 1, fill: 0.3333)
portfolio_variance = multiply(multiply(transpose(weights), cov), weights)
eur_gbp_corr = correlation(eur, gbp, lookback)
eur_jpy_corr = correlation(eur, jpy, lookback)

plot eur_gbp_corr as eur_gbp, color: blue
plot eur_jpy_corr as eur_jpy, color: orange
hline 0, style: dotted, color: gray
cells rows: [["Pair", "Correlation"], ["EUR/GBP", "{eur_gbp_corr:0.00}"], ["EUR/JPY", "{eur_jpy_corr:0.00}"]], columns: 2
```
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.
