public void Run()
        {
            try
            {
                string sLogin;
                string sPassword;
                string sSessionID;
                string sPin;
                if (mIsFirstAccount)
                {
                    sLogin     = mLoginParams.Login;
                    sPassword  = mLoginParams.Password;
                    sSessionID = mLoginParams.SessionID;
                    sPin       = mLoginParams.Pin;
                }
                else
                {
                    sLogin     = mLoginParams.Login2;
                    sPassword  = mLoginParams.Password2;
                    sSessionID = mLoginParams.SessionID2;
                    sPin       = mLoginParams.Pin2;
                }

                statusListener = new SessionStatusListener(mSession, sSessionID, sPin);
                mSession.subscribeSessionStatus(statusListener);
                statusListener.Reset();
                mSession.login(sLogin, sPassword, mLoginParams.URL, mLoginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    if (!mIsFirstAccount) // Disable receiving price updates for the second account
                    {
                        mSession.setPriceUpdateMode(O2GPriceUpdateMode.NoPrice);
                    }
                    responseListener = new ResponseListener(mSession);
                    mSession.subscribeResponse(responseListener);
                    O2GAccountRow account = null;
                    if (mIsFirstAccount)
                    {
                        bool bIsAccountEmpty = String.IsNullOrEmpty(mSampleParams.AccountID);
                        account = GetAccount(mSession, mSampleParams.AccountID);
                        if (account != null)
                        {
                            if (bIsAccountEmpty)
                            {
                                mSampleParams.AccountID = account.AccountID;
                                Console.WriteLine("Account: " + mSampleParams.AccountID);
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid",
                                                              mSampleParams.AccountID));
                        }
                    }
                    else
                    {
                        bool bIsAccountEmpty = String.IsNullOrEmpty(mSampleParams.AccountID2);
                        account = GetAccount(mSession, mSampleParams.AccountID2);
                        if (account != null)
                        {
                            if (bIsAccountEmpty)
                            {
                                mSampleParams.AccountID2 = account.AccountID;
                                Console.WriteLine("Account2: " + mSampleParams.AccountID2);
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("The account2 '{0}' is not valid",
                                                              mSampleParams.AccountID2));
                        }
                    }
                    O2GOfferRow offer = GetOffer(mSession, mSampleParams.Instrument);
                    if (offer == null)
                    {
                        throw new Exception(string.Format("The instrument '{0}' is not valid",
                                                          mSampleParams.Instrument));
                    }

                    O2GLoginRules loginRules = mSession.getLoginRules();
                    if (loginRules == null)
                    {
                        throw new Exception("Cannot get login rules");
                    }
                    O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider();
                    int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(mSampleParams.Instrument, account);
                    int iAmount       = iBaseUnitSize * mSampleParams.Lots;

                    O2GRequest request;
                    request = CreateTrueMarketOrderRequest(mSession, offer.OfferID, account.AccountID, iAmount, mSampleParams.BuySell);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    mSession.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }
                    Console.WriteLine("Done!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (statusListener.Connected)
                {
                    statusListener.Reset();
                    mSession.logout();
                    statusListener.WaitEvents();
                    mSession.unsubscribeResponse(responseListener);
                }
                mSession.subscribeSessionStatus(statusListener);
                mSession.Dispose();
            }
        }
Example #2
0
        public void Run()
        {
            try
            {
                string sLogin;
                string sPassword;
                string sSessionID;
                string sPin;
                if (mIsFirstAccount)
                {
                    sLogin = mLoginParams.Login;
                    sPassword = mLoginParams.Password;
                    sSessionID = mLoginParams.SessionID;
                    sPin = mLoginParams.Pin;
                }
                else
                {
                    sLogin = mLoginParams.Login2;
                    sPassword = mLoginParams.Password2;
                    sSessionID = mLoginParams.SessionID2;
                    sPin = mLoginParams.Pin2;
                }

                SessionStatusListener statusListener = new SessionStatusListener(mSession, sSessionID, sPin);
                mSession.subscribeSessionStatus(statusListener);
                statusListener.Reset();
                mSession.login(sLogin, sPassword, mLoginParams.URL, mLoginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    if (!mIsFirstAccount) // Disable receiving price updates for the second account
                    {
                        mSession.setPriceUpdateMode(O2GPriceUpdateMode.NoPrice);
                    }
                    ResponseListener responseListener = new ResponseListener(mSession);
                    mSession.subscribeResponse(responseListener);
                    O2GAccountRow account = null;
                    if (mIsFirstAccount)
                    {
                        bool bIsAccountEmpty = String.IsNullOrEmpty(mSampleParams.AccountID);
                        account = GetAccount(mSession, mSampleParams.AccountID);
                        if (account != null)
                        {
                            if (bIsAccountEmpty)
                            {
                                mSampleParams.AccountID = account.AccountID;
                                Console.WriteLine("Account: " + mSampleParams.AccountID);
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid",
                                    mSampleParams.AccountID));
                        }
                    }
                    else
                    {
                        bool bIsAccountEmpty = String.IsNullOrEmpty(mSampleParams.AccountID2);
                        account = GetAccount(mSession, mSampleParams.AccountID2);
                        if (account != null)
                        {
                            if (bIsAccountEmpty)
                            {
                                mSampleParams.AccountID2 = account.AccountID;
                                Console.WriteLine("Account2: " + mSampleParams.AccountID2);
                            }
                        }
                        else
                        {
                            throw new Exception(string.Format("The account2 '{0}' is not valid",
                                    mSampleParams.AccountID2));
                        }
                    }
                    O2GOfferRow offer = GetOffer(mSession, mSampleParams.Instrument);
                    if (offer == null)
                    {
                        throw new Exception(string.Format("The instrument '{0}' is not valid",
                                mSampleParams.Instrument));
                    }

                    O2GLoginRules loginRules = mSession.getLoginRules();
                    if (loginRules == null)
                    {
                        throw new Exception("Cannot get login rules");
                    }
                    O2GTradingSettingsProvider tradingSettingsProvider = loginRules.getTradingSettingsProvider();
                    int iBaseUnitSize = tradingSettingsProvider.getBaseUnitSize(mSampleParams.Instrument, account);
                    int iAmount = iBaseUnitSize * mSampleParams.Lots;

                    O2GRequest request;
                    request = CreateTrueMarketOrderRequest(mSession, offer.OfferID, account.AccountID, iAmount, mSampleParams.BuySell);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    mSession.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }
                    Console.WriteLine("Done!");

                    statusListener.Reset();
                    mSession.logout();
                    statusListener.WaitEvents();
                    mSession.unsubscribeResponse(responseListener);
                }
                mSession.unsubscribeSessionStatus(statusListener);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
        }