The implementation, in c++, of digital call and put option can be as followed:

Files
2 Header file
3 cpp file

Header file 1: BinModel01.h

// ensures the file is only included once within a single compilation.
#pragma once
//computing risk-neutral probability
double RiskNeutProb(double U, double D, double R);
//computing the stock price at node n,i
double S(double S0, double U, double D, int n, int i);
//inputting, displaying and checking model data
int GetInputData(double& S0, double& U, double& D, double& R);

Header file 2: Options03.h

#ifndef Options03_h
#define Options03_h
//inputting and displaying option data
int GetInputData(int& N, double& K);
//pricing European option
double PriceByCRR(double S0, double U, double D, double R, int N, double K,
	double (*Payoff)(double z, double K));
//computing call payoff
double CallPayoff(double z, double K);
//computing put payoff
double PutPayoff(double z, double K);
//computing digital call payoff
double DigitalCallPayoff(double z, double K);
//computing digital put payoff
double DigitalPutPayoff(double z, double K);
#endif

Cpp file 1: BinModel01.cpp

#include <iostream>
#include <cmath>


using namespace std;
double RiskNeutProb(double U, double D, double R)
{
	return (R - D) / (U - D);
}
double S(double S0, double U, double D, int n, int i)
{
	return S0 * pow(1 + U, i) * pow(1 + D, n - i);
}
int GetInputData(double& S0, double& U, double& D, double& R)
{
	//entering data
	cout << "Enter S0: "; cin >> S0;
	cout << "Enter U: "; cin >> U;
	cout << "Enter D: "; cin >> D;
	cout << "Enter R: "; cin >> R;
	cout << endl;
	//making sure that 0<S0, -1<D<U, -1<R
	if (S0 <= 0.0 || U <= -1.0 || D <= -1.0 ||
		U <= D || R <= -1.0)
	{
		cout << "Illegal data ranges" << endl;
		cout << "Terminating program" << endl;
		return 1;
	}
		//checking for arbitrage
		if (R >= U || R <= D)
		{
			cout << "Arbitrage exists" << endl;
			cout << "Terminating program" << endl;
			return 1;
		}
	cout << "Input data checked" << endl;
	cout << "There is no arbitrage" << endl << endl;
	return 0;
}

Cpp file 2: Options03.cpp

#include "Options03.h"
#include "BinModel01.h"
#include <iostream>
#include <cmath>
using namespace std;
int GetInputData(int& N, double& K)
{
	cout << "Enter steps to expiry N: "; cin >> N;
	cout << "Enter strike price K: "; cin >> K;
	cout << endl;
	return 0;
}
double CallPayoff(double z, double K)
{
	if (z > K) return z - K;
	return 0.0;
}
double PutPayoff(double z, double K)
{
	if (z < K) return K - z;
	return 0.0;
}


double DigitalCallPayoff(double z, double K)
{
	if (z > K) return 1;
	return 0.0;
}

double DigitalPutPayoff(double z, double K)
{
	if (z < K) return 1;
	return 0.0;
}





double PriceByCRR(double S0, double U, double D, double R, int N, double K,
	double (*Payoff)(double z, double K))
{
	double q = RiskNeutProb(U, D, R);
	//double Price[N + 1];
	double Price[9];
	for (int i = 0; i <= N; i++)
	{
		Price[i] = Payoff(S(S0, U, D, N, i), K);
	}
	for (int n = N - 1; n >= 0; n--)
	{
		for (int i = 0; i <= n; i++)
		{
			Price[i] = (q * Price[i + 1] + (1 - q) * Price[i]) / (1 + R);
		}
	}
	return Price[0];
}
 

cpp file 3 : Main.cpp

#include "BinModel01.h"
#include "Options03.h"
#include <iostream>
#include <cmath>



using namespace std;
int main()
{
	double S0, U, D, R;
	if (GetInputData(S0, U, D, R) == 1) return 1;
	double K; //strike price
	int N; //steps to expiry
	cout << "Enter call option data:" << endl;
	GetInputData(N, K);
	cout << "European call option price = " << PriceByCRR(S0,U,D,R,N,K,CallPayoff) << endl << endl;

	cout << "European digit call option price =" << PriceByCRR(S0, U, D, R, N, K, DigitalCallPayoff) << endl << endl;
	

		cout << "Enter put option data:" << endl;
	GetInputData(N, K);
	cout << "European put option price = " << PriceByCRR(S0, U, D, R, N, K, PutPayoff) << endl << endl;
	
	cout << "European digit put option price =" << PriceByCRR(S0, U, D, R, N, K, DigitalPutPayoff) << endl << endl;
	return 0;
}



/// OUTPUT
/*
Enter S0 : 106
Enter U : 0.15125
Enter D : -0.13138
Enter R : 0.00545

Input data checked
There is no arbitrage

Enter call option data :
Enter steps to expiry N : 8
Enter strike price K : 100

European call option price = 21.6811

European digit call option price = 0.575897

Enter put option data :
Enter steps to expiry N : 8
Enter strike price K : 100

European put option price = 11.4261

European digit put option price = 0.381553
*/

Leave a comment

Trending