function CEL_OnInstrumentSubscribed(Source, EventID, symbl, Instrument, varargin)

global m_CEL;

global m_CurFunc;
global m_OptionSymbol;

global m_AccountID;
global m_Status;
global m_OrderID;

try     
    if strcmp(m_CurFunc, 'realtimedata')
        
        % Informing about subscription of given contract and presenting its fullname.
        disp(['"', symbl, '" is subscribed at ', m_CEL.Environment.get('LineTime'),  '. Fullname: "', Instrument.get('FullName'), '"']);
        
        % Placing market buy order.
        GWConnectionStatus = m_CEL.Environment.get('GWConnectionStatus');
        if strcmp(GWConnectionStatus, 'csConnectionUp')
           if ~isempty(m_AccountID)
               order = m_CEL.CreateOrder(1, Instrument,  m_CEL.Accounts.Item(m_AccountID), 1, 1);    
               order.Place;

               m_OrderID = order.get('GUID');
               disp(['Placing an order with "', m_OrderID, '" GUID.']);
           else
               disp('Unknown Account. Order is not placed.');
           end

        else
           disp(['The order has not been placed, because GW connection status is ', GWConnectionStatus]);
        end
        
    elseif strcmp(m_CurFunc, 'timedbars')
        
        if m_Status == 0
            m_OptionSymbol = Instrument.get('FullName');
    
            % Informing about subscription of given contract and presenting its fullname.
            disp(['"', symbl, '" is subscribed at ', m_CEL.Environment.get('LineTime'),  '. Fullname: "', m_OptionSymbol, '"']);
            if ~(strcmp(Instrument.get('InstrumentType'), 'itOptionCall') || strcmp(Instrument.get('InstrumentType'), 'itOptionPut'))
                disp('You must subscribe to an option!');
                m_CEL.Shutdown;
            else
                %     Subscribing to underlying instrument
                sUnderlyingInstrument = Instrument.get('UnderlyingInstrumentName');
                disp(['Subscribing to underlying instrument: ', sUnderlyingInstrument]);
                m_CEL.NewInstrument(sUnderlyingInstrument);
                m_Status = 1;
                m_CurFunc = 'timedbars';
            end
        elseif m_Status == 1
            % Informing about subscription of underlying instrument
            disp([symbl, ' is subscribed.']);
            % Checking if underlying instrument is future
            if (~strcmp(Instrument.get('InstrumentType'), 'itFuture'))
                disp('Black model is supported only for option on future.');
                m_CEL.Shutdown;
            end
            % Getting contract name to subscribe for calculating interest rate.
            Instr = m_CEL.get('Instruments').Item(m_OptionSymbol);
            switch Instr.get('Currency')
                case 'AUD'
                    sCurrencyInstrument = 'HB';
                case 'CAD'
                    sCurrencyInstrument = 'KJ';
                case 'CHF'
                    sCurrencyInstrument = 'QNA';
                case 'GBP'
                    sCurrencyInstrument = 'QSA';
                case 'JPY'
                    sCurrencyInstrument = 'EY';
                case 'SGD'
                    sCurrencyInstrument = 'ZSD';
                case 'USD'
                    sCurrencyInstrument = 'EDAA';
                otherwise
                    sCurrencyInstrument = 'QEA';
            end

            MonthChar = Instr.get('MonthChar');
            YearString = Instr.get('YearString');
            sCurrencyInstrument = [sCurrencyInstrument, MonthChar, YearString(3:4)];
            disp(['Subscribing to ', sCurrencyInstrument, ' for calculating interest rate.']);
            m_CEL.NewInstrument(sCurrencyInstrument);
            m_Status = 2;
            m_CurFunc = 'timedbars';
        else
            % Collecting data for calculating IV and Greeks
            disp([symbl, ' is subscribed.']);
            qt = Instrument.get('Quotes').Item('qtSettlement');
            if ~qt.get('IsValid')
                disp(['Settlement of ', Instrument.get('FullName'), ' is not valid.']);
            else    
                disp('-------------------------------------------');
                r = (100 - qt.get('Price')) / 100;
                disp(['Interest Rate = ', num2str(r)]);

                Instr = m_CEL.get('Instruments').Item(m_OptionSymbol);
                qt = Instr.get('Quotes').Item('qtSettlement');
                if ~qt.get('IsValid')
                    disp(['Settlement of ', m_OptionSymbol, ' is not valid.']);
                else     
                    P = qt.get('Price');
                    disp(['Option''s settlement = ', Instr.ToDisplayPrice(P)]);
                    X = Instr.get('Strike');
                    X = num2str(X);
                    disp(['Option''s strike = ', X]);
                    X = Instr.FromDisplayPrice(X);
                    sExpirationDate = Instr.get('ExpirationDate');
                    T = round(StringToDate(sExpirationDate) - StringToDate(qt.get('ServerTimeStamp')));
                    disp(['Option''s DTE = ', num2str(T)]);
                    T = T / 365.25;
                    if (strcmp(Instr.get('InstrumentType'), 'itOptionCall'))
                        PC = 1; 
                    else
                        PC = -1;
                    end

                    Instr = Instr.get('UnderlyingInstrument');
                    qt = Instr.get('Quotes').Item('qtSettlement');
                    if ~qt.get('IsValid')
                        disp(['Settlement of ', Instr.get('FullName'), ' is not valid.']);
                    else    
                        S = qt.get('Price');                
                        disp(['Underlying instrument''s settlement = ', Instr.ToDisplayPrice(S)]);
                        disp('------------- Please wait... --------------');
                        % Calculating IV and Greeks
                        v = BS_Delta(PC, S, X, P, T, r);
                        disp(['IV = ', num2str(v)]);
                        disp(['Delta = ', num2str(BS_Delta(PC, S, X, T, v, r))]);
                        disp(['Gamma = ', num2str(BS_Gamma(S, X, T, v, r))]);
                        disp(['Theta = ', num2str(BS_Theta(PC, S, X, T, v, r))]);
                        disp(['Vega = ', num2str(BS_Vega(S, X, T, v, r))]);
                        disp(['Rho = ', num2str(BS_Rho(PC, S, X, T, v, r))]);
                        disp('-------------------------------------------');
                    end
                end
            end
            m_Status = 3;
        end
    
    end
catch
    CEL_ShutDown;
    disp('An error occurred in CEL_OnInstrumentSubscribed.m file.');
end