Binary Tree Option Pricing App in MATLAB
function app = binaryTreeOptionPricing
% Create the app interface
app = uifigure;
% Add an input box
inputBox = uieditfield(app, 'numeric');
inputBox.Position = [100 200 100 22];
% Add a button
calculateButton = uibutton(app, 'push');
calculateButton.Text = 'Calculate';
calculateButton.Position = [100 150 100 22];
calculateButton.ButtonPushedFcn = @(src, event) calculateOptionPrice(src, event, inputBox);
end
function calculateOptionPrice(src, event, inputBox)
% Get the value from the input box
optionPrice = inputBox.Value;
% Call the binomial option pricing function with the appropriate inputs
S0 = 100; % Replace with actual current price
K = 100; % Replace with actual strike price
r = 0.05; % Replace with actual risk-free rate
T = 1; % Replace with actual time to expiration
sigma = 0.2; % Replace with actual volatility
N = 100; % Replace with actual number of steps
[price, delta, gamma] = binomialOptionPricing(S0, K, r, T, sigma, N);
% Display the calculated option price
resultBox = uieditfield(src.Parent, 'numeric');
resultBox.Position = [100 100 100 22];
resultBox.Value = price;
end
app = binaryTreeOptionPricing;
function [price, delta, gamma] = binomialOptionPricing(S0, K, r, T, sigma, N)
% 使用二叉法进行期权定价
% 输入参数:
% S0 - 标的资产当前价格
% K - 期权行权价格
% r - 无风险利率
% T - 期权到期时间
% sigma - 标的资产的波动率
% N - 二叉树的步数
% 输出参数:
% price - 期权价格
% delta - 期权的Delta
% gamma - 期权的Gamma
% 计算二叉树的参数
dt = T/N; % 每一步的时间间隔
u = exp(sigma*sqrt(dt)); % 上涨因子
d = 1/u; % 下跌因子
p = (exp(r*dt)-d)/(u-d); % 上涨概率
% 构建二叉树
stockPrices = zeros(N+1, N+1);
stockPrices(1,1) = S0;
for i = 2:N+1
stockPrices(1:i, i) = stockPrices(1:i-1, i-1)*u;
stockPrices(i+1:end, i) = stockPrices(i, i)*d;
end
% 计算期权价值
optionValues = zeros(N+1, N+1);
optionValues(:, end) = max(stockPrices(:, end)-K, 0); % 终止条件
% 逐步回溯计算期权价值
for j = N:-1:1
for i = 1:j
optionValues(i, j) = exp(-r*dt)*(p*optionValues(i, j+1) + (1-p)*optionValues(i+1, j+1));
end
end
% 计算期权价格
price = optionValues(1, 1);
% 计算Delta和Gamma
delta = (optionValues(1, 2) - optionValues(2, 2))/(stockPrices(1, 2) - stockPrices(2, 2));
gamma = ((optionValues(1, 2) - optionValues(2, 2))/(stockPrices(1, 2) - stockPrices(2, 2)) - ...
(optionValues(2, 2) - optionValues(3, 2))/(stockPrices(2, 2) - stockPrices(3, 2))) / ...
((stockPrices(1, 2) - stockPrices(3, 2))/2);
end
This MATLAB app implements the binomial option pricing model to calculate the price of an option. It allows users to input the necessary parameters and obtain the calculated option price, delta, and gamma. This app is useful for understanding the basics of option pricing and for experimenting with different parameters. Please note that this is a simplified model and may not reflect the actual market conditions.
Please note that I have replaced the inputs for the binomial option pricing function with some example values. You will need to replace these values with your own inputs.
原文地址: https://www.cveoy.top/t/topic/f3Fh 著作权归作者所有。请勿转载和采集!