% Script to generate Random Price Processes for N Underlying Stocks,l
% average the price of the last month of each Stock, to obtain the Payoff
% of Asian Digital Option

Y=0;            % Initial Value of Accumulated Payoff of one simulation

N = 10;          % Number of Underlying stocks for which the price processes 
                % will be generated. 
                                
T = 6;           % Maturity time of the Asian Digital Option in months.

n = 21;          % n is the number of prices per month.

t = T*n;         % Number of prices to generate until maturity
                
delta=(1/t)*ones(t,N);          % Times used in Stochastic Pricing Process (ones(t,N)                        
                             
mu=0.07;                        % Fixed risk-free interest rate.



sigma=[0.7963 0.10821 0.9598 0.8809 0.8319 0.8935 0.7085 0.12808 0.9837 0.8481];     % Volatility rates.

CurrentTime=1;

Initial_Price=[133.10 1712.91 0.55 1133.90 526.10 109.50 198.00 220.00 33.65 0.04];     % Initial Price


for s=1:(t-1)
    
    timetomaturity(s)=21*6-s;
    
end


for k=1:100
    
    epsilon(:,:,k) = randn(t,N);
    
for j=1:N
    
Price(:,j,k)=Initial_Price(j)*exp((((mu-(sigma(j)^2))/2)*delta(:,j))+sigma(j)*epsilon(:,j,k).*sqrt(delta(:,j)));


end


P_red(:,:,k) = Price(t-n+1:t,:,k);    % S_red is the Matrix that contains the prices for 
                                     % the last month of each stock.
                
M(k,:) = mean(P_red(:,:,k));     % The matrix M denotes the mean price of the last month
                                 % of each stock.
                

H(k) = 0;           % Counter that indicates the number of stocks whose mean of
                    % the last month is higher than their respective strike prices.

i = 1;           % Counter for the for-loop


K=Initial_Price;        % K is the strike price.

for i=1:N
    if M(k,i)>K(i) 
        H(k) = H(k)+1;
    end
end

P(k) = 0;          % This is the payoff of the Asian Digital Option. If there are
                % 5 or more stocks whose average price in the last month is
                % higher than their respective strike price then the payoff
                % is one cash unit, otherwise it is zero.

                

                
if H(k) > 4
        P(k) = 1;
else P(k) =0;

end
    
    Y=Y+P(k);
    
    expectedy=Y/k;
    
    
ExpectedPayoff=(exp(-mu*timetomaturity(CurrentTime)))*expectedy;    


end

plot(Price(:,:,1));
xlabel('Days')
ylabel('Price')
legend('Gazprom','Nornikel','Rusgidro','Lukoil','Poluszoloto','Rosneft','Rostelekom','Apotheke 36.6','Sberbank','VTB')
Title('Price Dynamics of one Simulation Stocks')


figure
plot(H)
xlabel('Iterations')
ylabel('Number of Stocks above Strike')
Title('Number of stocks above Strike')