% Binomial for European or American Floating Strike Lookback Option
% by Fabrice Douglas Rouah
% written in Matlab Version 7.1

Spot = 90;       % Spot Price
T = 0.5;          % Years to Maturity
r = 0.04;         % Risk Free Rate
q = 0.07;         % Foreign Risk Free Rate
sigma = 0.2;      % Volatility
n = 100;          % Number of Steps
OpType = 'C';     % 'C'all or 'P'ut
ExType = 'A';     % 'E'uropean or 'A'merican

dt = T/n;
u = exp(sigma*sqrt(dt));
d = 1/u;
p = (exp((r-q)*dt) - d)/(u-d);

for i=1:n+1
    for j=i:n+1
        X(i,j) = u^(j-i) - 1;
        V(i,j) = 1 - d^(j-i);
        if j==n+1
            switch OpType
                case 'C'
                    Option(i,j) = V(i,j);
                case 'P'
                    Option(i,j) = X(i,j);
            end
        end
    end
end

for j=n:-1:1
    for i=1:j
        if (i~=j)
            switch ExType;
                case 'E'
                    if strcmp(OpType,'P')
                        Option(i,j) = exp(-r*dt)* ((1 - p)*Option(i,j+1)*d + p*Option(i+2,j+1)*u);
                    else
                        Option(i,j) = exp(-r*dt)* (p*Option(i,j+1)*u + (1 - p)*Option(i+2,j+1)*d);
                    end
                case 'A'
                    if strcmp(OpType,'P')
                        Option(i,j) = max(X(i,j), exp(-r*dt)* ((1 - p)*Option(i,j+1)*d + p*Option(i+2, j+1)*u));
                    else
                        Option(i, j) = max(V(i,j), exp(-r*dt)* (p*Option(i,j+1)*u + (1 - p)*Option(i+2,j+1)*d));
                    end
            end
        else
            switch ExType
                case 'E'
                    if strcmp(OpType,'P')
                        Option(i,j) = exp(-r*dt)* ((1 - p)*Option(i,j+1)*d + p*Option(i+1,j+1)*u);
                    else
                        Option(i,j) = exp(-r*dt)* (p*Option(i,j+1)*u + (1 - p)*Option(i+1,j+1)*d);
                    end
                case 'A'
                    if strcmp(OpType,'P')
                        Option(i,j) = max(X(i,j), exp(-r*dt)* ((1 - p)*Option(i,j+1)*d + p*Option(i+1,j+1)*u));
                    else
                        Option(i,j) = max(V(i,j), exp(-r*dt)* (p*Option(i,j+1)*u + (1 - p)*Option(i+1,j+1)*d));
                    end    
            end
        end
    end
end

LookBackFloat = Spot*Option(1,1)

