Need advice on how to understand C++ code for Monte Carlo Pricing Barrier Call Option

56 views Asked by At

I was inspired by the article Monte Carlo Pricing in NVIDIA CUDA — Barrier Call Option.

I created the C++ program in Microsoft Visual Studio 2022 and corrected the missing definition for the Payoff vector.

Although the program is running well, there is no output except when I insert a break in the code while debugging the program to see value in the Payoff vector.

I need advice and show a practical example on real stock data how the program can calculate values for Barrier Call Option.

#include <iostream>
#include <vector>

// Setting parameter values
const int NumberOfPaths = 1'000'000;
const int NumberOfSteps = 500;
const float Maturity = 1.0;
const float Strike = 90.0;
const float Barrier = 95.0;
const float InitialStockPrice = 100.0;
const float Volatility = 0.2f;
const float InterestRate = 0.05f;
float Dt = float(Maturity) / float(NumberOfSteps);
float SqrtDt = sqrt(Dt);

// Creating 2d-vector for paths
std::vector<std::vector<float>> Paths(NumberOfPaths, std::vector<float>(NumberOfSteps, InitialStockPrice));

#include <random>
const int seed = 748392047;
std::mt19937 generator(seed);
std::normal_distribution<float> StdNormal(0, 1);
std::vector<float> Normals(NumberOfPaths* NumberOfSteps);
std::vector<float> Payoff(NumberOfPaths* NumberOfSteps);

int main()
{
    for (int NormalIdx = 0; NormalIdx < NumberOfPaths * NumberOfSteps; NormalIdx++)
    {
        Normals.at(NormalIdx) = StdNormal(generator);
    }
    
    for (int PathIdx = 0; PathIdx < NumberOfPaths; PathIdx++)
    {
        float StockPrice = InitialStockPrice;
        int NormalIdx = PathIdx * NumberOfSteps;
        for (int StepIdx = 1; StepIdx < NumberOfSteps; StepIdx++)
        {
            StockPrice += StockPrice * InterestRate * Dt + StockPrice * Volatility * SqrtDt * Normals.at(NormalIdx);
            Paths.at(PathIdx).at(StepIdx) = StockPrice;
            NormalIdx++;
        }
    }
    
    for (int PathIdx = 0; PathIdx < NumberOfPaths; PathIdx++)
    {
        int StepIdx = 0;
        float StockPrice = InitialStockPrice;
        bool Expired = false;
        while (!Expired && StepIdx < NumberOfSteps)
        {
            StockPrice = Paths.at(PathIdx).at(StepIdx);
            if (StockPrice < Barrier)
                Expired = true;
            StepIdx++;
        }
        if (!Expired && StockPrice > Strike)
            Payoff.at(PathIdx) = StockPrice - Strike;
    }
    
    std::cout << "Hello World!\n";
}

The code above. I expect to know for what I can use this program on real example of stock data.

0

There are 0 answers