function varargout = Analytical(varargin)
% ANALYTICAL M-file for Analytical.fig
%      ANALYTICAL, by itself, creates a new ANALYTICAL or raises the existing
%      singleton*.
%
%      H = ANALYTICAL returns the handle to a new ANALYTICAL or the handle to
%      the existing singleton*.
%
%      ANALYTICAL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ANALYTICAL.M with the given input arguments.
%
%      ANALYTICAL('Property','Value',...) creates a new ANALYTICAL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Analytical_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Analytical_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Analytical

% Last Modified by GUIDE v2.5 08-Oct-2008 17:50:27

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Analytical_OpeningFcn, ...
                   'gui_OutputFcn',  @Analytical_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Analytical is made visible.
function Analytical_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Analytical (see VARARGIN)

% Choose default command line output for Analytical
handles.output = hObject;
handles.bCall = true;


% Update handles structure
guidata(hObject, handles);



% UIWAIT makes Analytical wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Analytical_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Determine the selected data set.
str = get(hObject,'String');
val = get(hObject,'Value');
switch str{val};
    case 'Call'%user selects call
        handles.bCall = true;
    case 'Put' %user selects put
        handles.bCall = false;
end
guidata(hObject,handles)



% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.



function edit1strikeprice_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'String'))
% hObject    handle to edit1strikeprice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1strikeprice as text
%        str2double(get(hObject,'String')) returns contents of edit1strikeprice as a double
New1=get(hObject,'String');
New2=str2double(New1);
handles.edit1strikeprice=New2;
guidata(hObject,handles);

function edit1strikeprice_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1strikeprice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function edit2spotprice_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'string'));
% hObject    handle to edit2spotprice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2spotprice as text
%        str2double(get(hObject,'String')) returns contents of edit2spotprice as a double
New1=get(hObject,'String');
New2=str2double(New1);
handles.edit2spotprice=New2;
guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function edit2spotprice_CreateFcn(hObject, eventdata, handles);
% hObject    handle to edit2spotprice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function edit3interestrate_Callback(hObject, eventdata, handles) 
user_entry = str2double(get(hObject,'string'));  %#ok<NASGU>
%#ok<NASGU>
% hObject    handle to edit3interestrate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3interestrate as text
%        str2double(get(hObject,'String')) returns contents of edit3interestrate as a double
New1=get(hObject,'String');
New2=str2double(New1);
handles.edit3interestrate=New2;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function edit3interestrate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3interestrate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit4volatility_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'String'));
% hObject    handle to edit4volatility (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4volatility as text
%        str2double(get(hObject,'String')) returns contents of edit4volatility as a double
New1=get(hObject,'String');
New2=str2double(New1);
handles.edit4volatility=New2;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function edit4volatility_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4volatility (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function edit5startdate_Callback(hObject, eventdata, handles)
% hObject    handle to edit5startdate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit5startdate as text
%        str2double(get(hObject,'String')) returns contents of edit5startdate as a double


% --- Executes during object creation, after setting all properties.
function edit5startdate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5startdate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




function edit7iterations_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'String'));
% hObject    handle to edit7iterations (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7iterations as text
%        str2double(get(hObject,'String')) returns contents of edit7iterations as a double
New1=get(hObject,'String');
New2=str2double(New1);
handles.edit7iterations=New2;
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function edit7iterations_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7iterations (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
K = handles.edit1strikeprice;
S = handles.edit2spotprice;
r = handles.edit3interestrate;
SIG = handles.edit4volatility;
iter = handles.edit7iterations
startDate = get(handles.edit5startdate, 'String');
endDate = get(handles.edit6enddate, 'String');
try
        T = yearfrac(startDate, endDate);
    catch 
        error('optionPriceGui:InvalidDates', ...
            'Dates must be in a valid format')
end
[call,put] = blsprice(S,K,r,T,SIG);
if handles.bCall == true
    set(handles.optionprice,'string',call);
else
    set(handles.optionprice,'string',put);
end
guidata(hObject,handles);
%now run the Monte Carlo simulation
numDays = daysact(startDate, endDate);
TT = sqrt(T/numDays) * ones(numDays, iter);
rNum = randn(numDays, iter);
BT = TT.*rNum;
TT = [zeros(1, iter); TT];
BT = [zeros(1, iter); BT];
ST = S* exp(cumsum((r - 0.5 * SIG^2) * TT + SIG * BT));
% Plot the Monte Carlo simulation
% Create line/bar object for histogram of monte-carlo price distributions
set(handles.axesHist,'yaxislocation','right','xaxislocation','top',...
    'xtick',[],'xlimmode','auto')
if size(ST,2)>1000
    ind = randperm(size(ST,2))';
    STplot = ST(:,ind(1:1000));
else
    STplot = ST;
end
% Plot the monte-carlo underlying price paths
handles.pricePaths = plot(handles.axesMonte, (0:numDays), STplot,...
    'color', [.7,.7,.7]);
%plot the mean underlying Monte carlo price path
STmean = mean(ST,2);
handles.meanLine = line('XData', (0:numDays), 'YData', STmean, ...
    'Parent', handles.axesMonte, 'Marker', '.' ,'MarkerSize', 10, 'Color', 'r');


%Monte Carlo Plots
xlabel(handles.axesMonte, 'Time (Days)','fontweight','bold')
ylabel(handles.axesMonte, 'Underlying ($)','fontweight','bold')
axis(handles.axesMonte, 'tight')
grid(handles.axesMonte, 'on')
legend(handles.axesMonte, ...
    {'$$S_{t+dt} = S_te^{(r-\frac{{\sigma^2}}{2})dt+\sigma\epsilon\sqrt{dt}}$$'}, ...
'fontsize',14,'interpreter','latex', 'Location', 'NorthWest');
 
       
x = handles.meanLine;
y = [zeros(1, iter); BT];
hist(y,x)

 
 
 







       








% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


function edit6enddate_Callback(hObject, eventdata, handles)

% hObject    handle to edit6enddate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit6enddate as text
%        str2double(get(hObject,'String')) returns contents of edit6enddate as a double


% --- Executes during object creation, after setting all properties.
function edit6enddate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6enddate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function optionprice_Callback(hObject, eventdata, handles)
% hObject    handle to optionprice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of optionprice as text
%        str2double(get(hObject,'String')) returns contents of optionprice as a double


% --- Executes during object creation, after setting all properties.
function optionprice_CreateFcn(hObject, eventdata, handles)
% hObject    handle to optionprice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object deletion, before destroying properties.


% --- Executes during object deletion, before destroying properties.

