
function [euPrice, amPrice, brPrice] = binCalculator(S,K,r,sigma,T,N,exercise_Frequency,put_True)
% Course: MMA707 Analytical Finance I 
%Seminar in Analytical Finance I 
%Group: Jessica Radeschnic, Hossein Nohrouzian, Amir KazempourEsmati

%%%%%%%%%%%%% binCalculator%%%%%%%%%%%%%%%
%binCalculator implements the CRR binomial tree to calculate the fair price
%% for American,European, and Bermudan options. Please refer to the seminar
% report for the calculation details. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% Parameters Guide%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% S: Spot price, e.g., 50.
% K: Strike pricem, e.g., 50.
% r: Risk-free interest rate e.g., 0.1 for 10%.
% sigma: Volatility, e.g., 0.3 for 30%.
% T: Years to maturity, e.g., 1 for 1 year.
% N: Number of steps in the binomial tree.
% Exercise_Frequency: Number of times that the Bermudan option can be
% exercised in a year, e.g., 12 for the monthly exercise.
% put_True: 1 for put option, 0 for a call. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%Author: Amir Kazempour, akr10001@student.mdh.se%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dt= T/N;
d = exp(-sigma*sqrt(dt)); u = exp(sigma*sqrt(dt));
p = (exp(r*dt)-d)/(u-d);
discount_Factor = exp(-r*dt);
step_Length = T/N;
exercise_True= zeros(1,N); %This is used later, to check if the exercise is possible at a certain node(in Bermudan tree)
mCoeff = N / (T*exercise_Frequency); 

for i = mCoeff :mCoeff: N
    exercise_True(i) = 1;
end

%Creating the binomial tree from the last node to the first node
for j = N: -1:1
    for i = j+1: -1 :1
        binTree(i,j) = S * (u ^ (j-2*(i-1)));
       
    end
end

%binTreeEE calculates the payoff, taking into consideration only the sp
%prices at each node.
if put_True
    for j = N: -1:1
        for i = j+1: -1 :1
            binTreeEE(i,j) = max(0,K- binTree(i,j,1) );
        end
    end
else
    for j = N: -1:1
        for i = j+1: -1 :1
            binTreeEE(i,j) = max(0,binTree(i,j,1)- K );
        end
    end
end

    
%binTreeNE gives the pay-offs in case that the option can not be exercised
%until the last node. 
binTreeNE(:,N) = binTreeEE(:,N);
for j = N-1: -1:1
    for i = j+1: -1 :1
        binTreeNE(i,j) = discount_Factor * ( binTreeNE(i,j+1)*p +  binTreeNE(i+1,j+1)*(1-p) );
    end
end

binTreeAm(:,N) = binTreeEE(:,N);
%Code for the American tree
for j = N-1: -1:1
    for i = j+1: -1 :1
        binTreeAm(i,j) = max (binTreeEE(i,j),discount_Factor * ( binTreeAm(i,j+1)*p +  binTreeAm(i+1,j+1)*(1-p)));
     end
end

%Code for the Bermudan option,
binTreeBr(:,N) = binTreeEE(:,N);
for j = N-1: -1:1
    for i = j+1: -1 :1
        if exercise_True(j)
            binTreeBr(i,j) = max (binTreeEE(i,j),discount_Factor * ( binTreeBr(i,j+1)*p +  binTreeBr(i+1,j+1)*(1-p)));
        else
            binTreeBr(i,j) = discount_Factor * ( binTreeBr(i,j+1)*p +  binTreeBr(i+1,j+1)*(1-p));
        end
     end
end

%Options prices are stored in the cell(4,1) of the respective tree.
euPrice = discount_Factor * ( binTreeNE(1,1)*p +  binTreeNE(2,1)*(1-p) );

amPrice = discount_Factor * ( binTreeAm(1,1)*p +  binTreeAm(2,1)*(1-p) );

brPrice = discount_Factor * ( binTreeBr(1,1)*p +  binTreeBr(2,1)*(1-p) );
%Exporting all the prices to the Prices vector to be used for illustration.
% Prices = [binTreeNE(4,1); binTreeBr(4,1);binTreeAm(4,1)];
%     bar3(Prices);
end
    