-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtempCodeRunnerFile.cpp
More file actions
113 lines (90 loc) · 3.46 KB
/
Copy pathtempCodeRunnerFile.cpp
File metadata and controls
113 lines (90 loc) · 3.46 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream> // To display output
#include <vector> //To store
#include <cmath> // To use math functions
#include <random> // To use random device and mt19937
#include <algorithm> // To use max, min algo
#include <fstream> // To save data to files
#include <chrono> // To measure latency (Speed)
class Stock
{
public:
std::string symbol;
double price;
double volatility;
};
// Monte Carlo Simulator - Amazing tech
double MonteCarloSimulator(const Stock &stock, double strikePrice, double timeInYears, double riskFreeRate, int N)
{
// 1. Setup Random Engine and normal distribution
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double> dist(0.0, 1.0);
// 2. PRE-CALCULATION
double driftInDay = (riskFreeRate - 0.5 * stock.volatility * stock.volatility) * (timeInYears / 252);
double diffusionInDay = stock.volatility * std::sqrt(timeInYears / 252);
double totalPayoff = 0.0;
// File setup for Python later
std::ofstream file("simulation_data.csv");
file << "Simulation,Price\n"; // CSV Header
// 3. The Loop (100,000 simulations)
for (int i = 0; i < N; ++i)
{
double currentStockPrice = stock.price; // Start at $100 every path
double barrierPrice = stock.price * 0.8; // Barrier at $80
bool isKnockedOut = false;
// Loop 2: Walk through the 252 days
for (int day = 0; day < 252; ++day)
{
// A. Get new random shock for TODAY
double Z = dist(gen);
// B. Evolve the price (Note the *= operator)
// We use 'currentStockPrice' on both sides so it updates!
currentStockPrice *= std::exp(driftInDay + diffusionInDay * Z);
// C. Check Barrier
if (currentStockPrice <= barrierPrice)
{
isKnockedOut = true;
break;
}
}
// D. Calculate Payoff ONLY at the end, if not knocked out
double payoff = 0.0;
if (!isKnockedOut)
{
payoff = std::max(currentStockPrice - strikePrice, 0.0);
}
totalPayoff += payoff;
// Save data (Only need final price for histogram)
if (i < 100)
{
file << i << "," << currentStockPrice << "\n";
}
}
file.close();
// 4. Discount to Present Value
double averagePayoff = totalPayoff / N;
return averagePayoff * std::exp(-riskFreeRate * timeInYears);
}
int main()
{
Stock apple;
apple.symbol = "AAPL";
apple.price = 100.0;
apple.volatility = 0.2; // 20% Volatility
double strikePrice = 100.0;
double r = 0.05; // 5% Risk Free Rate
double T = 1.0; // 1 Year
int N = 100000; // 100k Simulations
std::cout << "Starting Simulation for " << apple.symbol << "..." << std::endl;
// MEASURING LATENCY
auto start = std::chrono::high_resolution_clock::now();
double optionPrice = MonteCarloSimulator(apple, strikePrice, T, r, N);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
std::cout << "--------------------------------" << std::endl;
std::cout << "Theoretical Option Price: " << optionPrice << std::endl;
std::cout << "Time Taken: " << elapsed.count() << " seconds" << std::endl;
std::cout << "Simulations per Second: " << (N / elapsed.count()) << std::endl;
std::cout << "Data saved to 'simulation_data.csv'" << std::endl;
return 0;
}