示例#1
0
 /// <summary>
 /// Initializes the controller.
 ///
 /// Please note that controller doesn't become available immediatelly. Watch for OnStateChange event.
 /// </summary>
 /// <param name="userName">Trader's user name</param>
 /// <param name="password">Trader's password</param>
 /// <param name="url">Connection URL</param>
 /// <param name="terminal">The terminal name (e.g. Demo or Real)</param>
 /// <param name="cachePath">Absolute or relative path where the cache data shall be located</param>
 public void initialize(string userName, string password, string url, string terminal, string cachePath)
 {
     // create the trading session and subscribe for it's events
     mTradingSession = O2GTransport.createSession();
     mTradingSession.subscribeSessionStatus(this);
     mTradingSession.subscribeResponse(this);
     // create a price history communicator and subscribe for its events
     cachePath = Path.Combine(new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName, cachePath);
     mPriceHistoryCommunicator = PriceHistoryCommunicatorFactory.createCommunicator(mTradingSession, cachePath);
     mPriceHistoryCommunicator.addStatusListener(this);
     mPriceHistoryCommunicator.addListener(this);
     // Start login process
     mTradingSession.login(userName, password, url, terminal);
 }
示例#2
0
        static void Main(string[] args)
        {
            O2GSession session = null;
            IPriceHistoryCommunicator communicator   = null;
            SessionStatusListener     statusListener = null;

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

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

                argParser.AddArguments(ParserArgument.Login,
                                       ParserArgument.Password,
                                       ParserArgument.Url,
                                       ParserArgument.Connection,
                                       ParserArgument.SessionID,
                                       ParserArgument.Pin,
                                       ParserArgument.Instrument,
                                       ParserArgument.TimeFrame,
                                       ParserArgument.DateFrom,
                                       ParserArgument.DateTo,
                                       ParserArgument.QuotesCount,
                                       ParserArgument.OpenPriceCandlesMode);

                argParser.ParseArguments();

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

                argParser.PrintArguments();

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

                // use the application module path as a base path for quotes storage
                string storagePath = System.IO.Path.Combine(AppContext.BaseDirectory, "History");

                // create the ForexConnect trading session
                session        = O2GTransport.createSession();
                statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                // subscribe IO2GSessionStatus interface implementation for the status events
                session.subscribeSessionStatus(statusListener);
                statusListener.Reset();

                // create an instance of IPriceHistoryCommunicator
                communicator = PriceHistoryCommunicatorFactory.createCommunicator(session, storagePath);

                // log in to ForexConnect
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    CommunicatorStatusListener communicatorStatusListener = new CommunicatorStatusListener();
                    communicator.addStatusListener(communicatorStatusListener);

                    // wait until the communicator signals that it is ready
                    if (communicator.isReady() ||
                        (communicatorStatusListener.WaitEvents() && communicatorStatusListener.Ready))
                    {
                        // set open price candles mode, it must be called after login
                        QuotesManager quotesManager = communicator.getQuotesManager();
                        quotesManager.openPriceCandlesMode = sampleParams.OpenPriceCandlesMode;

                        // attach the instance of the class that implements the IPriceHistoryCommunicatorListener
                        // interface to the communicator
                        ResponseListener responseListener = new ResponseListener();
                        communicator.addListener(responseListener);

                        GetHistoryPrices(communicator, sampleParams.Instrument, sampleParams.Timeframe,
                                         sampleParams.DateFrom, sampleParams.DateTo, sampleParams.QuotesCount, responseListener);
                        Console.WriteLine("Done!");

                        communicator.removeListener(responseListener);
                    }

                    communicator.removeStatusListener(communicatorStatusListener);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (communicator != null)
                {
                    communicator.Dispose();
                }
                if (session != null)
                {
                    try
                    {
                        statusListener.Reset();
                        session.logout();
                        statusListener.WaitEvents();
                    }
                    catch (Exception)
                    {
                    }

                    session.unsubscribeSessionStatus(statusListener);
                    session.Dispose();
                }
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            O2GSession session = null;
            IPriceHistoryCommunicator communicator   = null;
            SessionStatusListener     statusListener = null;
            bool loggedIn = false;

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

                PrintSampleParams("GetHistPrices", loginParams, sampleParams);

                // use the application module path as a base path for quotes storage
                string storagePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "History");

                // create the ForexConnect trading session
                session        = O2GTransport.createSession();
                statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin);
                // subscribe IO2GSessionStatus interface implementation for the status events
                session.subscribeSessionStatus(statusListener);
                statusListener.Reset();

                // create an instance of IPriceHistoryCommunicator
                communicator = PriceHistoryCommunicatorFactory.createCommunicator(session, storagePath);

                // log in to ForexConnect
                session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection);
                if (statusListener.WaitEvents() && statusListener.Connected)
                {
                    loggedIn = true;

                    CommunicatorStatusListener communicatorStatusListener = new CommunicatorStatusListener();
                    communicator.addStatusListener(communicatorStatusListener);

                    // wait until the communicator signals that it is ready
                    if (communicator.isReady() ||
                        (communicatorStatusListener.WaitEvents() && communicatorStatusListener.Ready))
                    {
                        // set open price candles mode, it must be called after login
                        QuotesManager quotesManager = communicator.getQuotesManager();
                        quotesManager.openPriceCandlesMode = sampleParams.OpenPriceCandlesMode;

                        // attach the instance of the class that implements the IPriceHistoryCommunicatorListener
                        // interface to the communicator
                        ResponseListener responseListener = new ResponseListener();
                        communicator.addListener(responseListener);

                        GetHistoryPrices(communicator, sampleParams.Instrument, sampleParams.Timeframe,
                                         sampleParams.DateFrom, sampleParams.DateTo, sampleParams.QuotesCount, responseListener);
                        Console.WriteLine("Done!");

                        communicator.removeListener(responseListener);
                    }

                    communicator.removeStatusListener(communicatorStatusListener);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: {0}", e.ToString());
            }
            finally
            {
                if (communicator != null)
                {
                    communicator.Dispose();
                }
                if (session != null)
                {
                    try
                    {
                        statusListener.Reset();
                        session.logout();
                        statusListener.WaitEvents();
                    }
                    catch (Exception ee)
                    {
                    }

                    session.unsubscribeSessionStatus(statusListener);
                    session.Dispose();
                }
            }
        }