function B = CRRboo(T,S0,L,H,r,v,N) 
% Octave/MATLAB function to price booster options
% using the Cox-Ross-Rubinstein (CRR) model.
% INPUTS:                       (Example)
%    T =  expiration time       (1 year)
%    S =  stock price           ($100)
%    L =  low barrier price     ($85)
%    H =  high barrier price    ($115)
%    r =  risk-free yield       (0.05)
%    v =  volatility, >0        (0.20)
%    N =  tree height           (4)
% OUTPUT:
%   B =  price of the option at all (n,j).
% EXAMPLE:
%   boo = CRRboo(1,100,85,115,0.05,0.20,4);
%
  [pu,up,R] = CRRparams(T,r,v,N); % Use CRR values 
  B=zeros(N+1,N+1); % allocate the output matrix
  for j=0:N  % Set the values at expiry step N
    SNj=S0*up^(2*j-N); % asset price S(N,j)
    if(SNj < H)          % less than high barrier
      if(L < SNj )
        B(N+1,j+1) = N; % time spent between L,H
      end
    end
  end % Now test for barrier crossing before n=N
  for n=1:N-1            % n=0 case is unneeded 
    for j=0:n % test barriers at (n,0),...,(n,n):
      Snj=S0*up^(2*j-n); % asset price S(n,j)
      if(Snj >= H || Snj <= L )
        B(n+1,j+1) = n;
      end
    end
  end
  % Backward induction prices any zero grid values:
  for n=(N-1):(-1):0  % grid times n={N-1,..., 0}
    for j=0:n    % states j={0,1,...,n} at time n
      if (B(n+1,j+1)==0) % then use backwarding
        B(n+1,j+1)=(pu*B(n+2,j+2)+(1-pu)*B(n+2,j+1))/R;   
      end
    end
  end
  return;   %  Prices in B are now defined.
end
