Learn MATLAB-FINANCIAL-TOOLBOX with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Calculate Portfolio Returns
prices = [100 102 101; 50 51 52]; % Example prices for 2 assets
weights = [0.6 0.4];
returns = diff(prices) ./ prices(1:end-1,:);
portfolioReturns = returns * weights';
disp(portfolioReturns);
Compute daily portfolio returns given asset prices and weights.
2
Plot Historical Stock Prices
dates = datetime(2025,1,1):days(1):datetime(2025,1,5);
prices = [100 102 101 103 104];
figure; plot(dates, prices, '-o');
datetick('x', 'yyyy-mm-dd'); xlabel('Date'); ylabel('Price'); title('Stock Price'); grid on;
Plot time series of historical stock prices using Financial Toolbox functions.
3
Compute Option Price Using Black-Scholes
S = 100; K = 100; r = 0.05; sigma = 0.2; T = 1;
[call, put] = blsprice(S, K, r, T, sigma);
disp(['Call Price: ', num2str(call)]);
Calculate the price of a European call option using the Black-Scholes model.
4
Calculate Portfolio Variance
covMatrix = [0.0004 0.0002; 0.0002 0.0003];
weights = [0.6 0.4];
portfolioVariance = weights * covMatrix * weights';
disp(portfolioVariance);
Compute the variance of a portfolio given asset covariances and weights.
5
Compute Covariance Matrix
prices = [100 102 101; 50 51 52; 30 31 32];
returns = diff(prices) ./ prices(1:end-1,:);
covMatrix = cov(returns);
disp(covMatrix);
Compute covariance matrix of multiple asset returns.
6
Compute Sharpe Ratio
rf = 0.01; % risk-free rate
portfolioReturns = [0.002 0.003 0.004];
sharpeRatio = (mean(portfolioReturns) - rf) / std(portfolioReturns);
disp(sharpeRatio);
Calculate the Sharpe ratio of a portfolio.
7
Price Bond Using Yield to Maturity
Face = 1000; CouponRate = 0.05; Maturity = 5; YTM = 0.04;
coupon = Face * CouponRate;
cashFlows = [repmat(coupon,1,Maturity-1) Face+coupon];
dates = 1:Maturity;
discFactors = (1+YTM).^(-dates);
price = sum(cashFlows .* discFactors);
disp(price);
Calculate the price of a fixed-rate bond given yield to maturity.
8
Plot Option Greeks
S = 80:2:120; K = 100; r = 0.05; sigma = 0.2; T = 1;
delta = blsdelta(S,K,r,T,sigma);
gamma = blsgamma(S,K,r,T,sigma);
theta = blstheta(S,K,r,T,sigma);
figure;
plot(S,delta,'-o',S,gamma,'-x',S,theta,'-s');
legend('Delta','Gamma','Theta'); xlabel('Stock Price'); grid on;
Calculate and plot Delta, Gamma, and Theta for a European call option.
9
Monte Carlo Simulation for Option Pricing
S0 = 100; K = 100; r = 0.05; sigma = 0.2; T = 1; N = 100000;
z = randn(N,1);
ST = S0 * exp((r-0.5*sigma^2)*T + sigma*sqrt(T)*z);
payoff = max(ST-K,0);
optionPrice = exp(-r*T) * mean(payoff);
disp(optionPrice);
Price a European option using Monte Carlo simulation.
10
Yield Curve Construction
maturities = [1 2 3 4 5];
prices = [0.99 0.975 0.96 0.945 0.93];
zeroRates = -log(prices) ./ maturities;
plot(maturities, zeroRates, '-o'); xlabel('Years'); ylabel('Zero Rate'); title('Zero-Coupon Yield Curve'); grid on;
Construct a zero-coupon yield curve from market bond prices.