The implementation, in c++, of bull spread and bear spread option pricer can be as followed:
Files
2 Header file
3 cpp file
Header file 1: BinModel02.h
#pragma once
class BinModel
{
private:
double S0;
double U;
double D;
double R;
public:
//computing tisk neurtal prob
double RiskNeutProb();
//computing the stock price at node n,i
double S(int n, int i);
//inputing, displaying and checking model data
int GetInputData();
double GetR();
};
Header file 2: Options06.h
#pragma once
#include "BinModel02.h"
class EurOption {
private:
int N; //steps to expiry
public:
void SetN(int N_) { N = N_; }
//Payoff defined to return 0.0
//for pedagogical purposes.
//To use a pure virtual function replace by
//virtual double Payoff(double z)=0;
virtual double Payoff(double z) { return 0.0; }
//pricing European option
double PriceByCRR(BinModel Model);
};
class Call : public EurOption {
private:
double K; //strike price
public:
void SetK(double K_) { K = K_; }
int GetInputData();
double Payoff(double z);
};
class Put : public EurOption {
private:
double K; //strike price
public:
void SetK(double K_) { K = K_; }
int GetInputData();
double Payoff(double z);
};
class BullSpread : public EurOption {
private:
double K1;
double K2;
public:
void SetK1(double K1_) { K1 = K1_; }
void SetK2(double K2_) { K2 = K2_; }
int GetInputData();
double Payoff(double z);
};
class BearSpread : public EurOption {
private:
double K1;
double K2;
public:
void SetK1(double K1_) { K1 = K1_; }
void SetK2(double K2_) { K2 = K2_; }
int GetInputData();
double Payoff(double z);
};
Cpp file 1: BinModel02.cpp
#include "BinModel02.h"
#include <iostream>
#include <cmath>
using namespace std;
int BinModel::GetInputData()
{
//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 || -1 >= D || -1 >= U || D >= U || -1 >= R)
{
cout << "illegal data ranges" << endl;
cout << "Terminating program" << endl;
return 1;
}
//check 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;
return 0;
}
double BinModel::RiskNeutProb()
{
return (R - D) / (U - D);
}
double BinModel::S(int n, int i)
{
return S0 * pow(1 + U, i) * pow(1 + D, n - i);
}
double BinModel::GetR()
{
return R;
}
Cpp file 2: Options06.cpp
#include "Options06.h"
#include "BinModel02.h"
#include <iostream>
#include <cmath>
using namespace std;
double EurOption::PriceByCRR(BinModel Model)
{
double q = Model.RiskNeutProb();
double Price[100];
for (int i = 0; i <= N; i++) { Price[i] = Payoff(Model.S(N, i)); }
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 + Model.GetR());
}
}
return Price[0];
}
int Call::GetInputData()
{
cout << "Enter Call option data:" << endl;
int N;
cout << "Enter steps to expiry N:"; cin >> N;
SetN(N);
cout << "Enter strike price K: "; cin >> K;
cout << endl;
return 0;
}
double Call::Payoff(double z)
{
if (z > K) return z - K;
return 0.0;
}
int Put::GetInputData()
{
cout << "Enter Call option data:" << endl;
int N;
cout << "Enter steps to expiry N:"; cin >> N;
SetN(N);
cout << "Enter strike price K: "; cin >> K;
cout << endl;
return 0;
}
double Put::Payoff(double z)
{
if (z < K) return K - z;
return 0.0;
}
int BullSpread::GetInputData()
{
cout << "Enter Call option data:" << endl;
int N;
cout << "Enter steps to expiry N:"; cin >> N;
SetN(N);
cout << "Enter strike price K1: "; cin >> K1;
cout << endl;
cout << "Enter strike price K2: "; cin >> K2;
cout << endl;
return 0;
}
double BullSpread::Payoff(double z) {
if (K2 <= z) {
return K2 - K1;
}
else if (K1 < z) {
return z - K1;
}
return 0.0;
}
int BearSpread::GetInputData()
{
cout << "Enter Call option data:" << endl;
int N;
cout << "Enter steps to expiry N:"; cin >> N;
SetN(N);
cout << "Enter strike price K1: "; cin >> K1;
cout << endl;
cout << "Enter strike price K2: "; cin >> K2;
cout << endl;
return 0;
}
double BearSpread::Payoff(double z) {
if (K2 <= z) {
return 0.0;
}
else if (K1 < z) {
return K2 - z;
}
return K2 - K1;
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
cpp file 3 : Main.cpp
#include "BinModel02.h"
#include "Options06.h"
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
BinModel Model;
if (Model.GetInputData() == 1) return 1;
Call Option1;
Option1.GetInputData();
cout << "European call option price = " << Option1.PriceByCRR(Model) << endl << endl;
Put Option2;
Option2.GetInputData();
cout << "European put option price = " << Option2.PriceByCRR(Model) << endl << endl;
BullSpread Bull;
Bull.GetInputData();
cout << "European BullSpread Price = " << Bull.PriceByCRR(Model) << endl << endl;
BearSpread Bear;
Bear.GetInputData();
cout << "European BearSpread Price = " << Bear.PriceByCRR(Model) << endl << endl;
return 0;
}
/*
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
Enter Call option data :
Enter steps to expiry N : 8
Enter strike price K : 100
European put option price = 11.4261
Enter Call option data :
Enter steps to expiry N : 8
Enter strike price K1 : 100
Enter strike price K2 : 110
European BullSpread Price = 4.71584
Enter Call option data :
Enter steps to expiry N : 8
Enter strike price K1 : 100
Enter strike price K2 : 110
European BearSpread Price = 4.85866
*/




Leave a comment