Code covered by the BSD License  

Highlights from
MATLAB for R Users in Computational Finance

image thumbnail

MATLAB for R Users in Computational Finance

by

 

23 Jul 2013 (Updated )

Learn how to use MATLAB and R together to tackle your computational needs

blsapp()
function blsapp()
%BLSAPP Option pricing application
%
%  In addition to pricing individual call, put, straddle, and butterfly
%  options using the Black Scholes option pricing model, this application 
%  also allows for visualization each option's value surface over a range of
%  spot prices and times to expiry.

%Author: C. Bassignani, 01-11-99

% Copyright 2013 MathWorks, Inc.

%-----------------------------------------------------------------------------
%This GUI code was written by hand. MATLAB also provides a facility for
%visually building GUI's called GUIDE. The MATLAB code generated by GUIDE 
%will differ in organization and appearance from the code below.

%-----------------------------------------------------------------------------
%Set all default parameters

% Important: this pragma ensures that the compiler includes the helper
% function blsmaincall2.
%#function blsmaincall2

%Size parameters and starting points for the main GUI panel
HorizStart = 200;
TotalWidth = 400;
TotalHeight = 345;

%Default background color
DefBGColor = 'white';

%Vertical position from which to start drawing the text labels and edit fields
%(as measured from the bottom of the main GUI panel)
VertStart = 300;

%Left margin for drawing the text labels
LeftEdge = 25;

%Height and width for the text labels and fields
TextWidth = 120;
TextHeight = 25;
EditHeight = 25;
EditWidth = 75;

%Default input parameters
DefSpotPrice = '98';
DefStrikePrice = '98';
DefRiskFreeRate = '0.07';
DefTimeExpiry = '2';
DefVolatility = '0.20';
DefVizRange = '0.10';
DefButterflyRange = '0.10';

%Position and size the radion buttons
SecondEdge = LeftEdge + (TotalWidth .* 0.55)+10;
RadioWidth = 120;
RadioHeight = 25;

%Button size
ButtonWidth = 100;
ButtonHeight = 25;

%-----------------------------------------------------------------------------
%Build the main GUI panel

%Main panel
h0 = figure('Numbertitle', 'off', ...
     'Menubar', 'none', ...
     'Name', 'Option Pricing Tool', ...
     'Position',[HorizStart VertStart TotalWidth TotalHeight], ...
     'DeleteFcn', 'blsmaincall2 closeall');

%Define a structure in the UserData of the main GUI to hold application
%variables
DataStruct.OptionType = [];
set(h0, 'UserData', DataStruct);

%-----------------------------------------------------------------------------
%Build the text labels and edit fields for the input arguments

%Frame
uicontrol('Parent', h0, ...
     'Position', [10 5 TotalWidth-(TotalWidth*0.45)+5 TotalHeight-10], ...
     'Style', 'frame', ...
     'Tag', 'Frame');

%Spot price
uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart TextWidth TextHeight], ...
     'String', 'Spot Price');

uicontrol('Parent', h0, ...
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart EditWidth EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditSpotPrice', ...
     'String', DefSpotPrice);

%Strike price
uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart-35 TextWidth TextHeight], ...
     'String', 'Strike Price');

uicontrol('Parent', h0, ...
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart-35 EditWidth EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditStrikePrice', ...
     'String', DefStrikePrice);

%Risk free rate
uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart-70 TextWidth TextHeight], ...
     'String', 'Risk Free Rate');

uicontrol('Parent', h0, ...
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart-70 EditWidth EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditRiskFreeRate', ...
     'String', DefRiskFreeRate);

%Time to Expiry
uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart-105 TextWidth TextHeight], ...
     'String', 'Months to Expiry');

uicontrol('Parent', h0, ...
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart-105 EditWidth EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditTimeExpiry', ...
     'String', DefTimeExpiry);

%Volatility
uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart-140 TextWidth TextHeight], ...
     'String', 'Volatility');

uicontrol('Parent', h0, ...
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart-140 EditWidth EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditVolatility', ...
     'String', DefVolatility);

%Visualization range
uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart-185 TextWidth TextHeight+10], ...
     'String', 'Visualization Range');

uicontrol('Parent', h0, ...
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart-175 EditWidth EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditVisualizationRange', ...
     'String', DefVizRange);

%Butterfly range
uicontrol('Parent', h0, ...
     'Enable', 'off', ... % Initially disable
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [LeftEdge VertStart-220 TextWidth TextHeight+10], ...
     'String', 'Butterfly Range', ...
     'Tag', 'TextButterflyRange');

uicontrol('Parent', h0, ...
     'Enable', 'off', ... % Initially disable
     'BackgroundColor', 'white', ...
     'Style', 'edit', ...
     'Position', [LeftEdge+TextWidth+5 VertStart-210 EditWidth EditHeight], ...
     'BackgroundColor', 'default', ...
     'Tag', 'EditButterflyRange', ...
     'String', DefButterflyRange);


%-----------------------------------------------------------------------------
%Build the radio buttons for selecting the type of option

%Frame
uicontrol('Parent', h0, ...
     'Position', [SecondEdge-10 VertStart-85 ...
          TotalWidth*0.45-30 ...
          TotalHeight-(TotalHeight-2*RadioHeight)+75], ...
     'Style', 'frame', ...
     'Tag', 'Frame');

%Call option
uicontrol('Parent', h0, ...
     'Style', 'radio', ...
     'Position', [SecondEdge VertStart RadioWidth RadioHeight], ...
     'String', 'Call Option', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'RadioButton', ...
     'Callback', 'blsmaincall2 radiocall');

%Put option
uicontrol('Parent', h0, ...
     'Style', 'radio', ...
     'Position', [SecondEdge VertStart-25 RadioWidth RadioHeight], ...
     'String', 'Put Option', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'RadioButton', ...
     'Callback', 'blsmaincall2 radioput');

%Straddle
uicontrol('Parent', h0, ...
     'Style', 'radio', ...
     'Position', [SecondEdge VertStart-50 RadioWidth RadioHeight], ...
     'String', 'Straddle', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'RadioButton', ...
     'Callback', 'blsmaincall2 radiostraddle');

%Butterfly
uicontrol('Parent', h0, ...
     'Style', 'radio', ...
     'Position', [SecondEdge VertStart-75 RadioWidth RadioHeight], ...
     'String', 'Butterfly', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'RadioButton', ...
     'Callback', 'blsmaincall2 radiobutterfly');

%-----------------------------------------------------------------------------
%Build the edit box to contain the output value

%Frame
uicontrol('Parent', h0, ...
     'Position', [SecondEdge-10 5 TotalWidth*0.45-30 200], ...
     'Style', 'frame', ...
     'Tag', 'Frame');

%Raised panel for the field
uicontrol('Parent', h0, ...
     'Style', 'pushbutton', ...
     'Position', [SecondEdge VertStart-185 ButtonWidth+30 ...
          2*EditHeight+30], ...
     'enable', 'inactive');

uicontrol('Parent', h0, ...
     'Style', 'text', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Position', [SecondEdge+15 VertStart-140 TextWidth-20 TextHeight], ...
     'String', 'Option Value');

uicontrol('Parent', h0, ...
     'Style', 'edit', ...
     'Position', [SecondEdge+15 VertStart-175 EditWidth+25 EditHeight], ...
     'BackgroundColor', DefBGColor, ...
     'Tag', 'EditAnswer');

%-----------------------------------------------------------------------------
%Build the main buttons for the GUI

%Raised panel for the buttons
uicontrol('Parent', h0, ...
     'Style', 'pushbutton', ...
     'Position', [SecondEdge VertStart-290 ButtonWidth+30 ...
          3*ButtonHeight+25], ...
     'enable', 'inactive');


%Calculate button
uicontrol('Parent', h0, ...
     'Style', 'pushbutton', ...
     'Position', [SecondEdge+15 VertStart-222.5 ButtonWidth ButtonHeight], ...
     'String', 'Calculate', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'ButtonCalculate', ...
     'Callback', 'blsmaincall2 calculate');

%Visualize button
uicontrol('Parent', h0, ...
     'Style', 'pushbutton', ...
     'Position', [SecondEdge+15 VertStart-252.5 ButtonWidth ButtonHeight], ...
     'String', 'Visualize', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'ButtonVisualize', ...
     'Callback', 'blsmaincall2 visualize');

%Done button
uicontrol('Parent', h0, ...
     'Style', 'pushbutton', ...
     'Position', [SecondEdge+15 VertStart-282.5 ButtonWidth ButtonHeight], ...
     'String', 'Done', ...
     'FontSize', 10, ...
     'FontWeight', 'bold', ...
     'Tag', 'ButtonDone', ...
     'Callback', 'blsmaincall2 closeall');


%Normalize the units of all the controls
AllUICtrlHandles = findobj(gcf, 'Type', 'uicontrol');
set(AllUICtrlHandles, 'Units', 'normal');

%end of BLSAPP GUI

Contact us