This post is also available in: en, en and en
Introduction to C++ and Quantitative Finance¶
In [ ]:
#include <iostream>
#include <cmath>
double CallPrice( double sig)
{
// Test case Haug p. 172; student exercise to extend it
double S = 59.0;
double K = 60.0;
double r = 0.067;
double marketPrice = 2.82; // The call price
double b = r;
double T = 0.25; // Three months
double tmp = sig * std::sqrt(T);
double d1 = std::log(S/K) + (b + (sig*sig)*0.5)*T)/tmp;
double d2 = d1 - tmp;
double CalculatedValue = (S * std::exp((b - r)*T) * N(d1)) - (K* std::exp(-r*T)* N(d2));
// Function in the form f(x) = 0
return marketPrice - CalculatedValue;
}
double CallPrice()
{
double tmp = sig * std::sqrt(T);
double d1 = std::log(U/K) + (b + (sig*sig)*0.5)*T)/tmp;
double d2 = d1 - tmp;
return (U * std::exp((b - r)*T)* N(d1)) - (K*std::exp(-r*T)*N(d2));
}
double PutPrice()
{
double tmp = sig * std::sqrt(T);
double d1 = (std::log(U/K) + (b + (sig*sig)*0.5)*T)/tmp;
double d2 = d1 - tmp;
return (K * std::exp(-r*T) * N(-d2)) - (U * std::exp((b-r) * T)* N(-d1));
}
In [1]:
template <class Numeric> Numeric Max(const Numeric& x, const Numeric& y);
In [2]:
template <class Numeric>
Numeric Max(const Numeric& x, const Numeric& y)
{
if (x > y)
return x;
return y;
}
In [4]:
#include <iostream>
{
long dA = 1234; long dB = 2;
std::cout << "\n\nMax and min of two numbers: " << std::endl;
std::cout << "Max value is: " << Max<long>(dA, dB) << std::endl;
}