After watching several webinars and revisiting Options, Futures, and Other Derivatives, I came across a Quant Insider session featuring Dr. Paul Bilokon. His insights on low-latency systems and quantitative finance were especially valuable and gave me the push to dive back into C++.
With a bit more time available, I’ve decided to focus on deepening my coding skills, using implementation to better understand the theory. I started by setting up a clean GitHub repository to track my progress:
https://github.com/JohannesMeyerYC/RoadToQuant
The first thing I tackled was a prime number generator using the Sieve of Eratosthenes. It took a few rounds of trial and error, but I eventually got it working:
#include <iostream>
#include <vector>
using namespace std;
vector<int> sieve(int n) {
vector<bool> prime(n + 1, true);
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
vector<int> res;
for (int p = 2; p <= n; p++){
if (prime[p]){
res.push_back(p);
}
}
return res;
}
int main(){
int n = 35;
vector<int> res = sieve(n);
for(auto ele : res){
cout << ele << ' ';
}
return 0;
}
Next, I moved on to the Black-Scholes model. I’m still learning the underlying concepts, but writing the code helped a lot with intuition and structure. I utilized a couple of free resources for the structure of it. Here’s the full C++ implementation:
#include <iostream>
#include <cmath>
double norm_pdf(const double& x) {
return (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x);
}
double norm_cdf(const double& x) {
double k = 1.0/(1.0 + 0.2316419*x);
double k_sum = k*(0.319381530 + k*(-0.356563782 + k*(1.781477937 + k*(-1.821255978 + 1.330274429*k))));
if (x >= 0.0) {
return (1.0 - (1.0/(pow(2*M_PI,0.5)))*exp(-0.5*x*x) * k_sum);
}
else {
return 1.0 - norm_cdf(-x);
}
}
double d_j(const int& j, const double& S, const double& K, const double& r, const double& v, const double& T) {
return (log(S/K) + (r + (pow(-1,j-1))*0.5*v*v)*T)/(v*(pow(T,0.5)));
}
double call_price(const double& S, const double& K, const double& r, const double& v, const double& T) {
return S * norm_cdf(d_j(1, S, K, r, v, T))-K*exp(-r*T) * norm_cdf(d_j(2, S, K, r, v, T));
}
double put_price(const double& S, const double& K, const double& r, const double& v, const double& T) {
return -S*norm_cdf(-d_j(1, S, K, r, v, T))+K*exp(-r*T) * norm_cdf(-d_j(2, S, K, r, v, T));
}
int main(int argc, char **argv) {
double S, K, r, v, T;
std::cout << "Enter current asset price (S): ";
std::cin >> S;
std::cout << "Enter strike price (K): ";
std::cin >> K;
std::cout << "Enter risk-free rate (r) [e.g., 0.05 for 5%]: ";
std::cin >> r;
std::cout << "Enter volatility (v) [e.g., 0.2 for 20%]: ";
std::cin >> v;
std::cout << "Enter time to expiry (T) in years: ";
std::cin >> T;
if (S <= 0 || K <= 0 || v <= 0 || T <= 0) {
std::cerr << "Error: Input parameters must be positive." << std::endl;
return 1;
}
double call = call_price(S, K, r, v, T);
double put = put_price(S, K, r, v, T);
std::cout << "Underlying: " << S << std::endl;
std::cout << "Strike: " << K << std::endl;
std::cout << "Risk-Free Rate: " << r << std::endl;
std::cout << "Volatility: " << v << std::endl;
std::cout << "Maturity: " << T << std::endl;
std::cout << "Call Price: " << call << std::endl;
std::cout << "Put Price: " << put << std::endl;
return 0;
}
That wraps up what I’ve been working on today. Writing code like this makes the theory more tangible and forces you to really understand what each variable and equation is doing.
Next, I’ll be exploring performance optimization, Monte Carlo simulation, and possibly implementing implied volatility estimation. If you’re learning C++ again or exploring quant finance, I also recommend checking out C++ Masterclass. It’s a solid way to rebuild your foundation.
Feel free to check out my repo or reach out with ideas. See you in the next update.
Leave a Reply