Skip to content

Instantly share code, notes, and snippets.

@mick001
Created August 27, 2015 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mick001/a4ff384d7e93d6b788f7 to your computer and use it in GitHub Desktop.
Save mick001/a4ff384d7e93d6b788f7 to your computer and use it in GitHub Desktop.
Call option pricing C++ implementation. (Returns are assumed to be normally distributed)
// Call option Monte Carlo evaluation
#include <iostream>
#include <random>
#include <math.h>
#include <chrono>
using namespace std;
/* double stoc_walk: returns simulated price after periods
p = price at t=t0
dr = drift
vol = volatility
periods (days)
*/
std::default_random_engine generator;
double stoc_walk(double p,double dr,double vol,int periods)
{
double mean = 0.0;
double stdv = 1.0;
std::normal_distribution<double> distribution(mean,stdv);
for(int i=0; i < periods; i++)
{
double w = distribution(generator);
p += dr*p + w*vol*p;
}
return p;
}
int main()
{
//Initialize variables
double s0 = 10.0; //Initial price
double drift = 0.001502; //daily drift
double volatility = 0.026; //volatility (daily)
double r = 0.02; //Risk free yearly rate
int days = 255; //Days
int N = 100000; //Number of Monte Carlo trials
double zero_trials = 0.0;
double k = 12.0; //Strike price
//Initialize random number generator
//Calculate N payoffs
double avg = 0.0;
for(int j=0; j < N; j++)
{
double temp = stoc_walk(s0,drift,volatility,days);
if(temp > k)
{
double payoff = temp - k;
payoff = payoff * exp(-r);
avg += payoff;
}
else
{
zero_trials += 1;
}
}
//Average the results
double price_ = avg/(double)N;
//Print results
cout << "MONTE CARLO PLAIN VANILLA CALL OPTION PRICING" << endl;
cout << "Option price: " << price_ << endl;
cout << "Initial price: " << s0 << endl;
cout << "Strike price: " << k << endl;
cout << "Daily expected drift: " << drift*100 << "%" << endl;
cout << "Daily volatility: " << volatility*100 << "%" << endl;
cout << "Total trials: " << N << endl;
cout << "Zero trials: " << zero_trials << endl;
cout << "Percentage of total trials: " << zero_trials/N*100 << "%";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment