Article

Measuring the impact of hedging on the VaR of an FX portfolio in python with Eikon Data API

Author

    Leonid Sopotnitskiy
    Director, Eikon Platform, Emerging & Frontier Markets

ABOUT THE MODEL

The fx_hedge_var_impact.ipynb supports the calculation of parametric Value-at-Risk of an FX portfolio and allows the user to evaluate the impact of hedging different currency positions on the portfolio's risk profile. This script is designed to be imported as a module into other notebooks using the ipynb python library and used by calling the main calculation function:

The result is presented as a plotly chart that compares the FX risk profile prior and after hedging. Note that the portfolio should be declared as a dictionary following the above structure.

Let's have a look at how the model works in more detail.

 

DEPENDENCIES AND CONSTANTS

Since the model will be consuming Refinitiv data, performing operations with matrices and plotting charts, we will need to import the following libraries:

Once this is done, we will declare 2 constants:

  • direct_qm contains a list of instruments that have a direct quotation method.
  • z_score_const is the z-score that corresponds to a 95% confidence interval. The user can enhance this to a list of commonly used values in case there is a need to run calculations at other confidence intervals.

 

FUNCTION DEFINITIONS

In addition to the main get_data() function, this model contains a few more definitions, that will be used during the VaR calculation:

A few comments on the above functions:

  • convert_to_usd() converts the initial portfolio positions from the original currencies to USD. This is where the script will run a check against the global list of instruments with a direct quotation method.
  • get_annual_std() is used to perform element-wise multiplication of weight of each position in the portfolio.
  • get_port_vol() performs a matrix multiplication to derive the portfolio's variance first by multiplying the row vector of weighted volatilities for each currency, the correlation matrix and the transposed vector of currency volatilities. Then the volatility is returned by taking a sqaure root of the variance.

 

THE CALCULATION

Now that all declarations have been made, let's have a look at the main function using a model FX portfolio:

EXAMPLE

 

EXAMPLE

In this example we will be building a risk profile on 

portfolio of 5 currencies: SGD, EUR, GBP, JPY and CHF

with a total value of 40+ mln USD.

 

The holding structure is displayed on the diagram to 

the right. We will be hedging 3 positions as follows:

- EUR: 50%

- GBP: 30%

- CHF: 20%

portfolio = {
'SGD=':[10000000, 0],
'EUR=':[10000000, 0.5],
'GBP=':[10000000, 0.3],
'JPY=':[100000000, 0],
'CHF=':[10000000, 0.2]
}

Note that the cash values are given in the held currencies.

After intaking the portfolio dictionary, the get_data() function will parse the given data and retrive historical rates for the given RICs using the Data API:

Additionally the cash positions, hedge ratios and spot FX rates are stored in separate variables.

The next step is to calculate the weights of the held currencies in the portfolio, which is achieved by converting the position into USD first:

The calculated weights of FX positions prior to and after hedging are stored in the risk_pct_before_hedge and risk_pct_after_hedge respectively. 

Now we move on to calculating the log returns from the retrieved timeseries with numpy. The result is stored in a new pandas dataframe df_log. 

 

We can quickly check the data in df_log using cufflinks:

In [2]:

Out [2]:

Below is a chart with the distribution of these log returns:

We will be using this data to calculate the correlation matrix for the VaR. The next snippet shows the calculation of the means, standard deviations and excess returns:

We can now at this stage we could calculate the variance-covarianc matrix on the obtained excess returns, however we will not isolate it into a separate operation in the script, and go straight for the correlation matrix using pandas. Let's visualize the result using a heatmap:

In [3]:

Out [3]:

Finally we approach the calculation of Value-at-Risk, which follows the equation:

VaR = V × zα  × σ

where V is the cash value of the portfolio, σ is the portfolio's volatility and zα the z-score corresponding to the confidence inerval α.

We can now look at the result by displaying the VaR before and after hedging in a pandas dataframe.

In [4]:

Out [4]:

  Risk amount, USD Volatility VaR, USD
Before hedge 42619647.56 0.023466 1645061.12
After hedge 31096516.17 0.022117 1131265.59

The final snippet of the get_data() function wraps the analytics into a plotly figure and plots the portfolio's risk profile as follows: