-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
85 lines (67 loc) · 2.03 KB
/
main.cpp
File metadata and controls
85 lines (67 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// SimpleMCMain1.cpp
// Created by carlos on 26/10/2018.
//
// Simple routine for pricing European call options (non-dividend) using Monte Carlo simulation
//
// requires Random1.cpp
//#include "SimpleMCMain1.hpp"
#include "Random1.hpp"
#include <iostream>
#include <cmath>
using namespace std;
double SimpleMonteCarlo1(double Expiry,
double Strike,
double Spot,
double Vol,
double r,
unsigned long NumberOfPaths)
{
double variance = Vol*Vol*Expiry;
double rootVariance = sqrt(variance);
double itoCorrection = -0.5*variance;
double movedSpot = Spot*exp(r*Expiry + itoCorrection);
double thisSpot;
double runningSum=0;
for (unsigned long i=0; i < NumberOfPaths; i++)
{
double thisGaussian = GetOneGaussianByBoxMuller();
thisSpot = movedSpot*exp(rootVariance*thisGaussian);
double thisPayoff = thisSpot - Strike;
thisPayoff = thisPayoff >0 ? thisPayoff : 0;
runningSum += thisPayoff;
}
double mean = runningSum / NumberOfPaths;
mean *= exp(-r*Expiry);
return mean;
}
int main()
{
double Expiry;
double Strike;
double Spot;
double Vol;
double r;
unsigned long NumberOfPaths;
cout << "Enter expiry T: ";
cin >> Expiry;
cout << "Enter strike K: ";
cin >> Strike;
cout << "Enter spot S_0: ";
cin >> Spot;
cout << "Enter volatility: ";
cin >> Vol;
cout << "Enter risk-free interest rate r: ";
cin >> r;
cout << "Number of paths (iterations): ";
cin >> NumberOfPaths;
double result = SimpleMonteCarlo1(Expiry,
Strike,
Spot,
Vol,
r,
NumberOfPaths);
cout << "\nThe price is: " << result << endl;
cout << endl;
return 0;
}