Basis risk and minimum variance hedge ratio with C++

This time we leave QuantLib aside. We show that some basic financial calculations can be dealt with with C++.

We wish to write a piece of C++ code to calculate :
– the basis risk of a portfolio made of 1 spot contract and a certain number of futures contracts
– and, we define another C++ function to compute the minimum variance hedge.

The hedge ratio (h) is the number of futures contracts one should use to hedge his exposure in the spot market.

Basic risk is the variance of the basis, the latter being the difference between the spot price and the price of the futures contract.

The minimum variance hedge ratio (h*) is the hedge ratio that minimizes the basis risk.

The guy of explains all that nicely in a video . And, what we do is writing the C++ code corresponding to the Excel spreadsheet in this video. Here is a similar spreadsheet.

Below, we show our C++ code.


using namespace std;

struct Inputs{
	double spot_vol;
	double futures_vol;
	double rho;

void setupExample (Inputs &i){
i.spot_vol = 0.2;
i.futures_vol = 0.3;
i.rho = 0.9;

void basis_risk (const Inputs &i){
	double hedgeRatio = 0.3;
	do {
		cout << "Hedge ratio = " << hedgeRatio
			<< " Basis risk = "
			<< (i.spot_vol*i.spot_vol+(hedgeRatio*i.futures_vol)
			- (2*hedgeRatio*i.spot_vol*i.futures_vol*i.rho)) << endl;
		hedgeRatio = hedgeRatio + 0.1;
	 } while (hedgeRatio < 1) ;

void min_var_hedge(const Inputs &i){
	cout << endl;
	cout << "Min Var Hedge : " << i.rho*i.spot_vol/i.futures_vol << endl;

int main (int, char*[]){
	Inputs i;
	setupExample (i);
	basis_risk (i);
	min_var_hedge (i);

2 pensamientos en “Basis risk and minimum variance hedge ratio with C++

  1. Angshuman

    Your BLOG is brilliant. Kindly put more of Finance – Code mappings such that developers (like me) who have ZERO knowledge of Finance will undergo good learning and upgradation.

    Keep Blogging !



Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de

Estás comentando usando tu cuenta de Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s