/// <summary>
 /// ctor
 /// </summary>
 /// <param name="responseListener"></param>
 public TableListener(ResponseListener responseListener)
 {
     mResponseListener = responseListener;
     mRequestID = string.Empty;
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            O2GSession session = null;

            try
            {
                LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings);
                SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings);

                PrintSampleParams("NetStopLimit", loginParams, sampleParams);

                session = O2GTransport.createSession();
                session.useTableManager(O2GTableManagerMode.Yes, null);
                SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                session.subscribeSessionStatus(statusListener);
                statusListener.Reset();
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    ResponseListener responseListener = new ResponseListener();
                    TableListener tableListener = new TableListener(responseListener);
                    session.subscribeResponse(responseListener);

                    O2GTableManager tableManager = session.getTableManager();
                    O2GTableManagerStatus managerStatus = tableManager.getStatus();
                    while (managerStatus == O2GTableManagerStatus.TablesLoading)
                    {
                        Thread.Sleep(50);
                        managerStatus = tableManager.getStatus();
                    }

                    if (managerStatus == O2GTableManagerStatus.TablesLoadFailed)
                    {
                        throw new Exception("Cannot refresh all tables of table manager");
                    }
                    O2GAccountRow account = GetAccount(tableManager, sampleParams.AccountID);
                    if (account == null)
                    {
                        if (string.IsNullOrEmpty(sampleParams.AccountID))
                        {
                            throw new Exception("No valid accounts");
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID));
                        }
                    }
                    sampleParams.AccountID = account.AccountID;

                    O2GOfferRow offer = GetOffer(tableManager, sampleParams.Instrument);
                    if (offer == null)
                    {
                        throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument));
                    }

                    O2GTradeRow trade = GetTrade(tableManager, sampleParams.AccountID, offer.OfferID);
                    if (trade == null)
                    {
                        throw new Exception(string.Format("There are no opened positions for instrument '{0}'", sampleParams.Instrument));
                    }

                    O2GRequest request;
                    string sBuySell = trade.BuySell.Equals(Constants.Buy) ? Constants.Sell : Constants.Buy;

                    tableListener.SubscribeEvents(tableManager);

                    request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateStop, sBuySell, Constants.Orders.StopEntry);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    tableListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateLimit, sBuySell, Constants.Orders.LimitEntry);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    tableListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    Console.WriteLine("Done!");

                    tableListener.UnsubscribeEvents(tableManager);

                    statusListener.Reset();
                    session.logout();
                    statusListener.WaitEvents();
                    session.unsubscribeResponse(responseListener);
                }
                session.unsubscribeSessionStatus(statusListener);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
 /// <summary>
 /// ctor
 /// </summary>
 /// <param name="responseListener"></param>
 public TableListener(ResponseListener responseListener)
 {
     mResponseListener = responseListener;
     mRequestID        = string.Empty;
 }
Esempio n. 4
0
        static void Main(string[] args)
        {
            O2GSession session = null;

            try
            {
                LoginParams  loginParams  = new LoginParams(ConfigurationManager.AppSettings);
                SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings);

                PrintSampleParams("NetStopLimit", loginParams, sampleParams);

                session = O2GTransport.createSession();
                session.useTableManager(O2GTableManagerMode.Yes, null);
                SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                session.subscribeSessionStatus(statusListener);
                statusListener.Reset();
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    ResponseListener responseListener = new ResponseListener();
                    TableListener    tableListener    = new TableListener(responseListener);
                    session.subscribeResponse(responseListener);

                    O2GTableManager       tableManager  = session.getTableManager();
                    O2GTableManagerStatus managerStatus = tableManager.getStatus();
                    while (managerStatus == O2GTableManagerStatus.TablesLoading)
                    {
                        Thread.Sleep(50);
                        managerStatus = tableManager.getStatus();
                    }

                    if (managerStatus == O2GTableManagerStatus.TablesLoadFailed)
                    {
                        throw new Exception("Cannot refresh all tables of table manager");
                    }
                    O2GAccountRow account = GetAccount(tableManager, sampleParams.AccountID);
                    if (account == null)
                    {
                        if (string.IsNullOrEmpty(sampleParams.AccountID))
                        {
                            throw new Exception("No valid accounts");
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID));
                        }
                    }
                    sampleParams.AccountID = account.AccountID;

                    O2GOfferRow offer = GetOffer(tableManager, sampleParams.Instrument);
                    if (offer == null)
                    {
                        throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument));
                    }

                    O2GTradeRow trade = GetTrade(tableManager, sampleParams.AccountID, offer.OfferID);
                    if (trade == null)
                    {
                        throw new Exception(string.Format("There are no opened positions for instrument '{0}'", sampleParams.Instrument));
                    }

                    O2GRequest request;
                    string     sBuySell = trade.BuySell.Equals(Constants.Buy) ? Constants.Sell : Constants.Buy;

                    tableListener.SubscribeEvents(tableManager);

                    request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateStop, sBuySell, Constants.Orders.StopEntry);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    tableListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateLimit, sBuySell, Constants.Orders.LimitEntry);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    tableListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    Console.WriteLine("Done!");

                    tableListener.UnsubscribeEvents(tableManager);

                    statusListener.Reset();
                    session.logout();
                    statusListener.WaitEvents();
                    session.unsubscribeResponse(responseListener);
                }
                session.unsubscribeSessionStatus(statusListener);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (session != null)
                {
                    session.Dispose();
                }
            }
        }
        static void Main(string[] args)
        {
            O2GSession            session          = null;
            SessionStatusListener statusListener   = null;
            ResponseListener      responseListener = null;

            try
            {
                Console.WriteLine("NetStopLimit sample\n");

                ArgumentParser argParser = new ArgumentParser(args, "NetStopLimit");

                argParser.AddArguments(ParserArgument.Login,
                                       ParserArgument.Password,
                                       ParserArgument.Url,
                                       ParserArgument.Connection,
                                       ParserArgument.SessionID,
                                       ParserArgument.Pin,
                                       ParserArgument.Instrument,
                                       ParserArgument.RateStop,
                                       ParserArgument.RateLimit,
                                       ParserArgument.AccountID);

                argParser.ParseArguments();

                if (!argParser.AreArgumentsValid)
                {
                    argParser.PrintUsage();
                    return;
                }

                argParser.PrintArguments();

                LoginParams  loginParams  = argParser.LoginParams;
                SampleParams sampleParams = argParser.SampleParams;

                session        = O2GTransport.createSession();
                statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                session.subscribeSessionStatus(statusListener);
                statusListener.Reset();
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    responseListener = new ResponseListener(session);
                    session.subscribeResponse(responseListener);

                    O2GAccountRow account = GetAccount(session, sampleParams.AccountID);
                    if (account == null)
                    {
                        if (string.IsNullOrEmpty(sampleParams.AccountID))
                        {
                            throw new Exception("No valid accounts");
                        }
                        else
                        {
                            throw new Exception(string.Format("The account '{0}' is not valid", sampleParams.AccountID));
                        }
                    }
                    sampleParams.AccountID = account.AccountID;

                    O2GOfferRow offer = GetOffer(session, sampleParams.Instrument);
                    if (offer == null)
                    {
                        throw new Exception(string.Format("The instrument '{0}' is not valid", sampleParams.Instrument));
                    }

                    O2GTradeRow trade = GetTrade(session, sampleParams.AccountID, offer.OfferID, responseListener);
                    if (trade == null)
                    {
                        throw new Exception(string.Format("There are no opened positions for instrument '{0}'", sampleParams.Instrument));
                    }

                    O2GRequest request;
                    string     sBuySell = trade.BuySell.Equals(Constants.Buy) ? Constants.Sell : Constants.Buy;

                    request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateStop, sBuySell, Constants.Orders.StopEntry);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    session.sendRequest(request);
                    if (!responseListener.WaitEvents())
                    {
                        throw new Exception("Response waiting timeout expired");
                    }

                    request = CreateNetEntryOrderRequest(session, offer.OfferID, sampleParams.AccountID, sampleParams.RateLimit, sBuySell, Constants.Orders.LimitEntry);
                    if (request == null)
                    {
                        throw new Exception("Cannot create request");
                    }
                    responseListener.SetRequestID(request.RequestID);
                    session.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 (session != null)
                {
                    if (statusListener.Connected)
                    {
                        statusListener.Reset();
                        session.logout();
                        statusListener.WaitEvents();
                        if (responseListener != null)
                        {
                            session.unsubscribeResponse(responseListener);
                        }
                    }
                    session.unsubscribeSessionStatus(statusListener);
                    session.Dispose();
                }
            }
        }
        /// <summary>
        /// Find the first opened position by AccountID and OfferID
        /// </summary>
        /// <param name="session"></param>
        /// <param name="sAccountID"></param>
        /// <param name="sOfferID"></param>
        /// <param name="responseListener"></param>
        /// <returns></returns>
        private static O2GTradeRow GetTrade(O2GSession session, string sAccountID, string sOfferID, ResponseListener responseListener)
        {
            O2GTradeRow       trade          = null;
            bool              bHasTrade      = false;
            O2GRequestFactory requestFactory = session.getRequestFactory();

            if (requestFactory == null)
            {
                throw new Exception("Cannot create request factory");
            }
            O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);

            responseListener.SetRequestID(request.RequestID);
            session.sendRequest(request);
            if (!responseListener.WaitEvents())
            {
                throw new Exception("Response waiting timeout expired");
            }
            O2GResponse response = responseListener.GetResponse();

            if (response != null)
            {
                O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
                if (readerFactory != null)
                {
                    O2GTradesTableResponseReader tradesResponseReader = readerFactory.createTradesTableReader(response);
                    for (int i = 0; i < tradesResponseReader.Count; i++)
                    {
                        trade = tradesResponseReader.getRow(i);
                        if (sOfferID.Equals(trade.OfferID))
                        {
                            bHasTrade = true;
                            break;
                        }
                    }
                }
            }
            if (!bHasTrade)
            {
                return(null);
            }
            else
            {
                return(trade);
            }
        }
Esempio n. 7
0
 /// <summary>
 /// Find the first opened position by AccountID and OfferID
 /// </summary>
 /// <param name="session"></param>
 /// <param name="sAccountID"></param>
 /// <param name="sOfferID"></param>
 /// <param name="responseListener"></param>
 /// <returns></returns>
 private static O2GTradeRow GetTrade(O2GSession session, string sAccountID, string sOfferID, ResponseListener responseListener)
 {
     O2GTradeRow trade = null;
     bool bHasTrade = false;
     O2GRequestFactory requestFactory = session.getRequestFactory();
     if (requestFactory == null)
     {
         throw new Exception("Cannot create request factory");
     }
     O2GRequest request = requestFactory.createRefreshTableRequestByAccount(O2GTableType.Trades, sAccountID);
     responseListener.SetRequestID(request.RequestID);
     session.sendRequest(request);
     if (!responseListener.WaitEvents())
     {
         throw new Exception("Response waiting timeout expired");
     }
     O2GResponse response = responseListener.GetResponse();
     if (response != null)
     {
         O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory();
         if (readerFactory != null)
         {
             O2GTradesTableResponseReader tradesResponseReader = readerFactory.createTradesTableReader(response);
             for (int i = 0; i < tradesResponseReader.Count; i++)
             {
                 trade = tradesResponseReader.getRow(i);
                 if (sOfferID.Equals(trade.OfferID))
                 {
                     bHasTrade = true;
                     break;
                 }
             }
         }
     }
     if (!bHasTrade)
     {
         return null;
     }
     else
     {
         return trade;
     }
 }