• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Free C++ Matlab VBA Python code for derivatives pricing

I came across this resource awhile ago but some tweets this morning remind me post the link here

A sample of the code in C++

Equity Options
Cox, Ross, Rubinstein Binomial Tree
Extrapolated Flexible Binomial Tree
Flexible binomial tree of Tian (1999)

Trinomial Tree for American and European options

Edgeworth Binomial Tree of Rubinstein (1998)
Black Scholes Price and Greeks
Black Scholes by Simulation
Leisen-Reimer Binomial Tree
Adaptative Mesh Method Trinomial Tree
Practitioner (Ad hoc) Black Scholes Model
Gram Charlier Option Price
Black Scholes Implied Volatility Using S&P500 Option Prices
Variance Gamma Model for European options with Madan and Milne
Duan (1995) GARCH Option Pricing Model on S&P 100 Index

Heston Model
Heston Model, Original Formulation Using Boole's Integration Rule
Heston Call Price by Simulation
Heston Model Parameter Estimation Using S&P500 Options
Heston and Nandi (2000) Using the Trapezoidal Rule
Heston and Nandi (2000) Parameter Estimation Using S&P500 Options

Exotic Options
Lattice for Floating Strike Lookback Option
Average Price Asian Option
Straddle Option Price

Mathematical and Statistical Functions
N(0,1) CDF by numerical integration and N(0,1) inverse values
N(0,1) CDF approximation
Matrix inversion, minors, and other operations
Natural Cubic Spline Interpolation
Nelder Mead minimization algorithm
GARCH(1,1) Parameter Estimation Using S&P500
RiskMetrics Volatility and Correlation
Numerical Integration Algorithms

Variance Swap Demeterfi, Derman, Kamal, and Zou (1999)
Hi, I'm new to MATLAB as well as to this forum. I'm currently doing a research on pricing convertible bonds using Longstaff and Schwartz (2011) model or better known as the LSM. I've managed to determine the model price for the convertible bond by minor adjustment to the Matlab code provided by this link http://www.volopta.com/About.html.(I'm really thankful for this).

Then, I calculate the pricing error by comparing the model price with the observed price of the convertible bond for that particular date.

Nonetheless, I can only determine the model price for one data point, for example at the issuance date. I would need to repeat the pricing process for multiple data points, say for example the model price one day after the issuance, two days after the issuance date and so on..For every data point, the Spot Price for the underlying equity (Spot) would be different and the number of time increments (T) would also be reduced by one. Briefly, I can only figure out these but I don't know how I should modify the Matlab code so that it can repeat the pricing process accordingly instead of changing the Spot Price and T manually.
Can anyone please help or provide some guide that would help me with the Matlab code.
Thanks, very much.

Yike Lu

Finder of biased coins.
The simplest solution is to put everything into a for loop which accesses spot and expiry lists that you initialize outside of the loop.
Hi there,

I would need some help here. I have tried out Yike's suggestion but still I failed to get the desired outputs. Followings are some of the code that I'm currently trying out. I would like to simulate the underlying stock price for a convertible bond for 12 different spot price that is a column vector.

6.35 6.38 6.40 6.42 6.41 6.44 6.43 6.45 6.38 6.37 6.36 6.36 (save under the name 'data')

function SPaths = GenPaths(S0, mu, sigma, T, NSteps, NRepl)
S0 = data(1,:); % Spot Price.
mu = -0.6883; % expected stock return.
sigma = 0.6569; % volatility.
T = 1; % Maturity in year.
NSteps = 12; % Number of time increments
NRepl = 50; % Number of simulated paths
dt = T/NSteps; % Time increments
for S0 = data(1,:)
nudt = (mu-0.5*sigma^2)*dt;
sidt = sigma*sqrt(dt);
RandMat = randn(round(NRepl/2), NSteps);
Increments = [nudt + sidt*RandMat ; nudt - sidt*RandMat];
LogPaths = cumsum([log(S0)*ones(NRepl,1) , Increments] , 2);
SPaths = exp(LogPaths);

When I ran the code, the output only show simulated price when the S0 = 6.35 (ie the first Spot Price). it did not repeat the simulation for different S0.

2nd question: I also have no idea how I should modify the NSteps. It should reduce by 1 each time I rerun the simulation with different spot price.

Kindly help.

I'm not sure I understand properly what you're trying to do, but you may wish to organize your Matlab code so that you have script GenPaths.m with GenPaths() function definition inside:
function SPaths = GenPaths(S0, mu, sigma, T, NSteps, NRepl)
  dt = T / NSteps; % Time increments
  nudt = (mu - 0.5 * sigma^2) * dt;
  sidt = sigma * sqrt(dt);
  RandMat = randn(round(NRepl / 2), NSteps);
  Increments = [nudt + sidt * RandMat; nudt - sidt * RandMat];
  LogPaths = cumsum([log(S0) * ones(NRepl,1) , Increments], 2);
  SPaths = exp(LogPaths);
and then another file, say GenPathsTest.m containing test script, with double loop over spot prices and number of times increment:
data = [6.35 6.38 6.40 6.42 6.41 6.44 6.43 6.45 6.38 6.37 6.36 6.36];

mu = -0.6883; % expected stock return.
sigma = 0.6569; % volatility.
T = 1; % Maturity in year.
NSteps = 12; % Number of time increments
NRepl = 50; % Number of simulated paths

for k = 1:length(data)
  S0 = data(k); % Spot Price.
  for l = NSteps:-1:1
    SPaths = GenPaths(S0, mu, sigma, T, l, NRepl);
    % here, do something with SPaths

But overall, I think you really should learn a bit about Matlab before proceeding with this work...
Hey Andy, table of contents looks great but apparently the site is off. Does anyone has a copy of the code or comparable code? That would be great! Thanks a lot in advance!
The site is backup but Heston code has been removed and will be included in the author's upcoming book.
The Heston Model and its Extensions in Matlab and C#
Seems the page is not hosted anymore - would you know where I could get the code? Thanks in advance!!

Daniel Duffy

C++ author, trainer
Thanks Daniel - Did you mean the memorial page has a link to the files? I see "Rouah links" on the memorial page but link does not work. Would you happen to have the zipped files?
I was more mentoning RIP.

Maybe the publisher has code on its site?