-1

I am trying to write a C++ program that runs a Monte Carlo simulation to approximate the theoretical price of a down-and-in call option with a barrier between the moment of pricing and the option expiry. I implemented a BarrOption constructed but I don't know if I implemented this correctly. Please if anyone has an idea about what should be corrected leave a comment. Code below:

using namespace std;

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include "random.h"
#include "function.h"

        //definition of constructor
BarrOption::BarrOption(
    int nInt_,
    double strike_,
    double spot_,
    double vol_,
    double r_,
    double expiry_,
    double barr_){
        nInt = nInt_;
        strike = strike_;
        spot = spot_;
        vol = vol_;
        r = r_;
        expiry = expiry_;
        barr = barr_;
        generatePath();

}

void BarrOption::generatePath (){
    double thisDrift = (r * expiry - 0.5 * vol * vol * expiry) / double(nInt);
    double cumShocks = 0;
    thisPath.clear();

    for(int i = 0; i < nInt; i++){
        cumShocks += (thisDrift + vol * sqrt(expiry / double(nInt)) * GetOneGaussianByBoxMuller());
        thisPath.push_back(spot * exp(cumShocks));
    }
}

// definition of getBarrOptionPrice(int nReps) method:
double BarrOption::getBarrOptionPrice(int nReps){

    double rollingSum = 0.0;

    for(int i = 0; i < nReps; i++){
        generatePath();
        std::vector<double>::iterator minPrice;
        minPrice = std::min_element(thisPath.begin(), thisPath.end());

        if (thisPath[std::distance(thisPath.end(), minPrice)] <= barr & thisPath.back() > strike) {

        rollingSum += (thisPath.back() - strike);

    }

}

    return exp(-r*expiry)*rollingSum/double(nReps);
}



// definition of printPath() method:
void BarrOption::printPath(){

    for(int i = 0;  i < nInt; i++){


        std::cout << thisPath[i] << "\n";

    }

}
erindj
  • 7
  • 2

1 Answers1

0

Hi some comments from a first read, I am pretty sure you did some other mistakes, but below some:

  1. You are using push_back instead of reserve + assignment.
  2. Your diffusion is better is in log-space.
  3. You are recalculating (thisDrift + vol * sqrt(expiry / double(nInt)) each time.
  4. Just use auto instead of std::vector::iterator minPrice.
  5. std::distance(thisPath.end(), minPrice) this is negative no? Should be std::distance(minPrice, thisPath.end())??
  6. You are checking the min first, which takes expensive time, first start with if the call is triggered (S>strike), and then after check the barrier, otherwise you are just wasting time.
  7. You are finding the min, while all you need is a number less than the barrier, so better if you do a for loop with a break than finding the min.

Really this is bad code :) however, it's a good start.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Maths 4Us
  • 21
  • 7