function varargout = OPtionPricing(varargin)
% OPTIONPRICING M-file for OPtionPricing.fig
%      OPTIONPRICING, by itself, creates a new OPTIONPRICING or raises the existing
%      singleton*.
%
%      H = OPTIONPRICING returns the handle to a new OPTIONPRICING or the handle to
%      the existing singleton*.
%
%      OPTIONPRICING('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in OPTIONPRICING.M with the given input arguments.
%
%      OPTIONPRICING('Property','Value',...) creates a new OPTIONPRICING or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before OPtionPricing_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to OPtionPricing_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 OPtionPricing

% Last Modified by GUIDE v2.5 10-Oct-2011 12:11:53

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @OPtionPricing_OpeningFcn, ...
                   'gui_OutputFcn',  @OPtionPricing_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 OPtionPricing is made visible.
function OPtionPricing_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 OPtionPricing (see VARARGIN)

% Choose default command line output for OPtionPricing
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = OPtionPricing_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;



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
stock_price=str2double(get(handles.edit1,'String'));
if(isempty(stock_price))
set(hObject,'String','0');
elseif (isnan(stock_price))||(stock_price< 0)
errordlg('Invalid Stock Price','Invalid input','modal');
end
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
k=str2double(get(handles.edit2,'String'));
if(isempty(k))
set(hObject,'String','0');
elseif (isnan(k))||(k< 0)
errordlg('Invalid Strike Price','Invalid Input','modal');
end
guidata(hObject, handles) %return old value of Initial interest rate


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
v=str2double(get(handles.edit3,'String'));
if(isempty(v))
set(hObject,'String','0');
elseif (isnan(v))||(v< 0)
errordlg('Invalid Volatility','Invalid input','modal');
end
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double
r=str2double(get(handles.edit4,'String'));
if(isempty(r))
set(hObject,'String','0');
elseif (isnan(r))||(r< 0)
errordlg('Invalid Interest Rate','Invalid Input','modal');
end
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double
step=str2double(get(handles.edit5,'String'));
if(isempty(step))
set(hObject,'String','0');
elseif (isnan(step))||(step< 0)
errordlg('Invalid Number of Steps','Invalid Input','modal');
end
guidata(hObject, handles)

% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit6_Callback(hObject, eventdata, handles)
% hObject    handle to edit6 (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 edit6 as text
%        str2double(get(hObject,'String')) returns contents of edit6 as a double


% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit6 (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 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)

% 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



function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (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 edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double
try
entry = datestr(datenum(get(handles.edit1,'dd-mm-yyyy')));

catch
    errordlg('Invalid Date Format','Error Message','modal')

    uicontrol(hObject)

end



% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (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 selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

guidata(hObject, handles)

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


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
stock_price=str2double(get(handles.edit1,'String'));
k=str2double(get(handles.edit2,'String'));
v=str2double(get(handles.edit3,'String'));
r=str2double(get(handles.edit4,'String'));
step=str2double(get(handles.edit5,'String'));
n1=datenum(get(handles.edit8,'String'));
n2=datenum(get(handles.edit11,'String'));
switch get(handles.popupmenu2,'Value')
    case 1
        bt=1/12;
    case 2
        bt=3/12;
    case 3
        bt=0.5;
    case 4
        bt=1;
end

d=datevec(n2-n1);
if isnan(k)||k<0 ||isnan(v)||v<0 ||isnan(r)||r<0 ||isnan(step)||step<0 ||isnan(stock_price)||stock_price<0    
 errordlg('Input Parameters Should Be Positive Number','Invalid Input','modal'); 
else 
if d(1)<0 || d(2)<0 || d(3)<0  
    errordlg('Maturity Is Earlier Than Settlement','Invalid Input','modal');
else
    

d1=(d(1)*12)+d(2)+(d(3)/30);
T=round(d1);


dt=(T/12)*(1/step);
u=exp(v*sqrt(dt));
d=1/u;
p =(exp(r*dt)-d)/(u-d);
q=1-p;

%time vector used for bermudan options
time=zeros(2,step+1);
for i=1:step
    time(1,i+1)=i*dt;
end
if mod(bt,dt)==0
   j=bt/dt;
   for i=j:j:step
       time(2,i+1)=time(1,i+1);
   end
end

if sum(time(2,:))==0
    errordlg('According to the duration and number of steps bermudan option can not be calculated','Invalid Input','modal'); 
else

%constructing the binomial tree with price at each node
ptree=zeros(step+1,step+1);
ptree(1,1)=stock_price;
for i=2:step+1
    ptree(i,i)=ptree(i-1,i-1)*d;
    ptree(1,i)=ptree(1,i-1)*u;
end

for i=3:step+1
    for j=3:step+1
    ptree(i-1,j)=ptree(i-2,j-1)*d;
    end
end
%--------------------------------------------------------------------------
%Bermudan options
%call
%calculating option price at the final nodes
bctree=zeros(step+1,step+1);
for i=1:step+1
    bctree(i,step+1)=max(ptree(i,step+1)-k,0);
end

%calculating option price at other nodes
for j=step:-1:1
    for i=1:step+1
       if time(1,j)==time(2,j)&& ptree(i,j)~=0
          bctree(i,j)=(p*bctree(i,j+1)+q*bctree(i+1,j+1))*exp(-r*dt);
       elseif time(1,j)~=time(2,j)&& ptree(i,j)~=0
          bctree(i,j)=max(max(ptree(i,j)-k,0),(p*bctree(i,j+1)+q*bctree(i+1,j+1))*exp(-r*dt));
       end
    end
end
%put
%calculating option price at the final nodes
bptree=zeros(step+1,step+1);
for i=1:step+1
    bptree(i,step+1)=max(k-ptree(i,step+1),0);
end

%calculating option price at other nodes
for j=step:-1:1
    for i=1:step+1
       if time(1,j)==time(2,j)&& ptree(i,j)~=0
          bptree(i,j)=(p*bptree(i,j+1)+q*bptree(i+1,j+1))*exp(-r*dt);
       elseif time(1,j)~=time(2,j)&& ptree(i,j)~=0
          bptree(i,j)=max(max(k-ptree(i,j),0),(p*bptree(i,j+1)+q*bptree(i+1,j+1))*exp(-r*dt));
       end
    end
end
%-------------------------------------------------------------------------
%American
%call
%calculating option price at the final nodes
actree=zeros(step+1,step+1);
for i=1:step+1
    actree(i,step+1)=max(ptree(i,step+1)-k,0);
end

%calculating option price at other nodes
for j=step:-1:1
   for i=1:step
       if ptree(i,j)~=0
          actree(i,j)=max(max(ptree(i,j)-k,0),(p*actree(i,j+1)+q*actree(i+1,j+1))*exp(-r*dt));
       end  
   end
end

%put
%calculating option price at the final nodes
aptree=zeros(step+1,step+1);
for i=1:step+1
    aptree(i,step+1)=max(k-ptree(i,step+1),0);
end

%calculating option price at other nodes
for j=step:-1:1
   for i=1:step
       if ptree(i,j)~=0
          aptree(i,j)=max(max(k-ptree(i,j),0),(p*aptree(i,j+1)+q*aptree(i+1,j+1))*exp(-r*dt));
       end  
   end
end

%-------------------------------------------------------------------------
%European
%call
%calculating option price at the final nodes
ectree=zeros(step+1,step+1);
for i=1:step+1
    ectree(i,step+1)=max(ptree(i,step+1)-k,0);
end

%calculating option price at other nodes
for j=step:-1:1
   for i=1:step
       if ptree(i,j)~=0
          ectree(i,j)=(p*ectree(i,j+1)+q*ectree(i+1,j+1))*exp(-r*dt);
       end  
   end
end
%put
%calculating option price at the final nodes
eptree=zeros(step+1,step+1);
for i=1:step+1
    eptree(i,step+1)=max(k-ptree(i,step+1),0);
end

%calculating option price at other nodes
for j=step:-1:1
   for i=1:step
       if ptree(i,j)~=0
          eptree(i,j)=(p*eptree(i,j+1)+q*eptree(i+1,j+1))*exp(-r*dt);
       end  
   end
end
%-------------------------------------------------------------------------
%result
%call
if get(handles.radiobutton1,'Value')
   set(handles.text17,'String', ectree(1,1));
   set(handles.text18,'String', bctree(1,1));
   set(handles.text19,'String', actree(1,1));
else
%put
    set(handles.text17,'String', eptree(1,1));
    set(handles.text18,'String', bptree(1,1));
    set(handles.text19,'String', aptree(1,1));
end
%-------------------------------------------------------------------------
%plotting profit diagram

x=[0:0.1:k+30];
z=length(x);
if get(handles.radiobutton1,'Value')
%call option

for i=1:z
    if x(i)<k
        y1(i)=-ectree(1,1);
    else
        y1(i)=x(i)-k-ectree(1,1);
    end
end
for i=1:z
    if x(i)<k
        y2(i)=-bctree(1,1);
    else
        y2(i)=x(i)-k-bctree(1,1);
    end
end
for i=1:z
    if x(i)<k
        y3(i)=-actree(1,1);
    else
        y3(i)=x(i)-k-actree(1,1);
    end
end
 
 plot(handles.axes3,x,y1,'r',x,y2,'b',x,y3,'g');
 title('Profit Diagram');
 xlabel('Price');
 ylabel('Profit or Loss');
 legend('European','Bermudan','American');
 grid on;

%Put option
else
 for i=1:z
    if x(i)<k
        y1(i)=k-x(i)-eptree(1,1);
    else
        y1(i)=-eptree(1,1);
    end
end
for i=1:z
    if x(i)<k
        y2(i)=k-x(i)-bptree(1,1);
    else
        y2(i)=-bptree(1,1);
    end
end
for i=1:z
    if x(i)<k
        y3(i)=k-x(i)-aptree(1,1);
    else
       y3(i)=-aptree(1,1); 
    end
end
 plot(handles.axes3,x,y1,'r',x,y2,'b',x,y3,'g');
 title('Profit Diagram');
 xlabel('Price');
 ylabel('Profit or Loss');
 legend('European','Bermudan','American');
 legend('European','Bermudan','American');
 grid on

end
%-------------------------------------------------------------------------
end
end
 
  
end
guidata(hObject, handles)



function edit11_Callback(hObject, eventdata, handles)
% hObject    handle to edit11 (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 edit11 as text
%        str2double(get(hObject,'String')) returns contents of edit11 as a double
try
entry = datestr(datenum(get(handles.edit1,'dd-mm-yyyy')));

catch
    errordlg('Invalid Date Format','Error Message','modal')

    uicontrol(hObject)

end


% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit11 (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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
cla(handles.axes3,'reset');
set(handles.text17,'String', ' ');
set(handles.text18,'String', ' ');
set(handles.text19,'String', ' ');
guidata(hObject, handles)
