Exemplo n.º 1
0
        private void SendHistoricalRequest(int id, HistoricalDataRequest request)
        {
            Log(LogLevel.Info, string.Format("Sent historical data request to TWS. ID: {0}, Symbol: {1}, {2} back from {3}",
                                             id,
                                             request.Instrument.Symbol,
                                             TWSUtils.TimespanToDurationString((request.EndingDate - request.StartingDate), request.Frequency),
                                             request.EndingDate.ToString("yyyy-MM-dd hh:mm:ss")));

            TimeZoneInfo exchangeTZ = request.Instrument.GetTZInfo();
            //we need to convert time from the exchange TZ to Local...the ib client then converts it to UTC
            var startingDate = TimeZoneInfo.ConvertTime(request.StartingDate, exchangeTZ, TimeZoneInfo.Local);
            var endingDate   = TimeZoneInfo.ConvertTime(request.EndingDate, exchangeTZ, TimeZoneInfo.Local);

            try
            {
                _client.RequestHistoricalData
                (
                    id,
                    TWSUtils.InstrumentToContract(request.Instrument),
                    endingDate,
                    TWSUtils.TimespanToDurationString((endingDate - startingDate), request.Frequency),
                    TWSUtils.BarSizeConverter(request.Frequency),
                    GetDataType(request.Instrument),
                    request.RTHOnly ? 1 : 0
                );
            }
            catch (Exception ex)
            {
                Log(LogLevel.Error, "IB: Could not send historical data request: " + ex.Message);
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not send historical data request: " + ex.Message, id));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// real time data request
        /// </summary>
        public int RequestRealTimeData(RealTimeDataRequest request)
        {
            lock (_requestIDMapLock)
            {
                _requestCounter++;
                _realTimeDataRequests.Add(_requestCounter, request);
                _requestIDMap.Add(_requestCounter, request.AssignedID);
            }

            try
            {
                Contract contract = TWSUtils.InstrumentToContract(request.Instrument);

                _client.RequestRealTimeBars(
                    _requestCounter,
                    contract,
                    (int)TWSUtils.BarSizeConverter(request.Frequency),
                    RealTimeBarType.Trades, request.RTHOnly);
            }
            catch (Exception ex)
            {
                Log(LogLevel.Error, "IB: Could not send real time data request: " + ex.Message);
                RaiseEvent(Error, this, new ErrorArgs(-1, "Could not send real time data request: " + ex.Message));
            }
            return(_requestCounter);
        }
Exemplo n.º 3
0
        public void HistoricalRequestsAreNotSplitIfNotNecessary()
        {
            int[] requestCount = { 0 };

            _ibClientMock.Setup(
                x => x.RequestHistoricalData(
                    It.IsAny <int>(),
                    It.IsAny <Contract>(),
                    It.IsAny <DateTime>(),
                    It.IsAny <string>(),
                    It.IsAny <BarSize>(),
                    It.IsAny <HistoricalDataType>(),
                    It.IsAny <int>(),
                    It.IsAny <List <TagValue> >()))
            .Callback(() => requestCount[0]++);

            var requests = new Dictionary <KeyValuePair <BarSize, int>, int> //left side is barsize/seconds, right side is expected splits
            {
                { new KeyValuePair <BarSize, int>(BarSize.OneDay, 300 * 24 * 3600), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.OneHour, 25 * 24 * 3600), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.ThirtyMinutes, 6 * 24 * 3600), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.OneMinute, 1 * 24 * 3600), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.ThirtySeconds, 21 * 3600), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.FifteenSeconds, 13400), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.FiveSeconds, 6900), 1 },
                { new KeyValuePair <BarSize, int>(BarSize.OneSecond, 1500), 1 }
            };

            var inst = new Instrument();

            foreach (var kvp in requests)
            {
                _ibDatasource.RequestHistoricalData(new HistoricalDataRequest(
                                                        inst,
                                                        TWSUtils.BarSizeConverter(kvp.Key.Key),
                                                        DateTime.Now.AddSeconds(-kvp.Key.Value),
                                                        DateTime.Now,
                                                        dataLocation: DataLocation.ExternalOnly));

                Assert.AreEqual(kvp.Value, requestCount[0], kvp.Key.Key.ToString());
                requestCount[0] = 0;
            }
        }
Exemplo n.º 4
0
        public void HistoricalRequestsAreSplitToRespectRequestLimits()
        {
            int[] requestCount = { 0 };

            _ibClientMock.Setup(x => x.RequestHistoricalData(
                                    It.IsAny <int>(),
                                    It.IsAny <Contract>(),
                                    It.IsAny <string>(),
                                    It.IsAny <string>(),
                                    It.IsAny <QDMSIBClient.BarSize>(),
                                    It.IsAny <HistoricalDataType>(),
                                    It.IsAny <bool>(),
                                    It.IsAny <bool>(),
                                    It.IsAny <List <TagValue> >()))
            .Callback(() => requestCount[0]++);

            var requests = new Dictionary <KeyValuePair <BarSize, int>, int> //left side is barsize/seconds, right side is expected splits
            {
                { new KeyValuePair <BarSize, int>(BarSize.OneDay, 500 * 24 * 3600), 2 },
                { new KeyValuePair <BarSize, int>(BarSize.OneHour, 75 * 24 * 3600), 3 },
                { new KeyValuePair <BarSize, int>(BarSize.ThirtyMinutes, 22 * 24 * 3600), 4 },
                { new KeyValuePair <BarSize, int>(BarSize.OneMinute, 9 * 24 * 3600), 5 },
                { new KeyValuePair <BarSize, int>(BarSize.ThirtySeconds, 40 * 3600), 2 },
                { new KeyValuePair <BarSize, int>(BarSize.FifteenSeconds, 4 * 14400), 5 },
                { new KeyValuePair <BarSize, int>(BarSize.FiveSeconds, 2 * 7200), 3 },
                { new KeyValuePair <BarSize, int>(BarSize.OneSecond, 10 * 1800), 11 }
            };

            var inst = new Instrument();

            foreach (var kvp in requests)
            {
                _ibDatasource.RequestHistoricalData(new HistoricalDataRequest(
                                                        inst,
                                                        TWSUtils.BarSizeConverter(kvp.Key.Key),
                                                        DateTime.Now.AddSeconds(-kvp.Key.Value),
                                                        DateTime.Now,
                                                        dataLocation: DataLocation.ExternalOnly));

                Assert.AreEqual(kvp.Value, requestCount[0], kvp.Key.Key.ToString());
                requestCount[0] = 0;
            }
        }