Exemple #1
0
        public void GetHistoricalDataTest()
        {
            StartHistoricalReaderTest();

            int actual1 = s_utilities.GetHistoricalData("AAPL", QuoteType.Stock);
            int actual2 = s_utilities.GetHistoricalData("IBM", QuoteType.Stock, null, new DateTime(2018, 10, 1));
            int actual3 = s_utilities.GetHistoricalData("SPY", QuoteType.Stock, null, new DateTime(2018, 10, 2));
            int actual4 = s_utilities.GetHistoricalData("NDX", QuoteType.Index, null);
            int actual5 = s_utilities.GetHistoricalData("BXM", QuoteType.Index, null, new DateTime(2018, 10, 3));
            int actual6 = s_utilities.GetHistoricalData("BadTicker", QuoteType.Stock, null, new DateTime(2018, 10, 3));
            int actual7 = s_utilities.GetHistoricalData("SPX   181116C02900000", QuoteType.Option, null);
            int actual8 = s_utilities.GetHistoricalData("ES/18Z", QuoteType.Future, null);

            Assert.AreEqual(0, actual1, "GetHistoricalData test failed for AAPL");
            Assert.AreEqual(1, actual2, "GetHistoricalData test failed for IBM");
            Assert.AreEqual(2, actual3, "GetHistoricalData test failed for SPY");
            Assert.AreEqual(3, actual4, "GetHistoricalData test failed for NDX");
            Assert.AreEqual(4, actual5, "GetHistoricalData test failed for BXM");
            Assert.AreEqual(5, actual6, "GetHistoricalData test failed for BadTicker");
            Assert.AreEqual(-1, actual7, "GetHistoricalData test failed for SPX   181116C02900000");
            Assert.AreEqual(6, actual8, "GetHistoricalData test failed for ES/18Z");
            System.Diagnostics.Debug.WriteLine("Waiting 10 seconds to get data");
            System.Threading.Thread.Sleep(10000);

            s_utilities.StopHistoricalReader();
        }
        public HistoricalQuote ProvideQuote(string ticker, string quoteType, DateTime date)
        {
            HistoricalQuote quote = new HistoricalQuote()
            {
                Ticker = ticker,
                Date   = date
            };
            TWSQuoteObject quoteObject = null;

            try
            {
                if (!Connect())
                {
                    quote.ErrorMessage = "Unable to connect to TWS";
                }
                else
                {
                    lock (m_quoteMapLock)
                    {
                        QuoteType quoteTypeEnum = (QuoteType)Enum.Parse(typeof(QuoteType), quoteType);
                        int       requestId     = m_twsUtilities.GetHistoricalData(ticker, quoteTypeEnum, null, date);
                        if (requestId < 0)
                        {
                            quote.ErrorMessage = "Unable to get historical quote";
                        }
                        else
                        {
                            quoteObject = new TWSQuoteObject(requestId);
                            m_quoteMap.Add(requestId, quoteObject);
                        }
                    }
                }

                if (quoteObject != null)
                {
                    if (!WaitAny(null, quoteObject.WaitHandle))
                    {
                        quote.ErrorMessage = "Request timed out";
                    }
                    else
                    {
                        quoteObject.FillQuote(quote);
                    }
                }
            }
            catch (Exception ex)
            {
                quote.ErrorMessage = ex.Message;
            }

            return(quote);
        }