Example #1
0
        /// <summary>
        ///     historical data request
        /// </summary>
        public void RequestHistoricalData(HistoricalDataRequest request)
        {
            //Historical data limitations: https://www.interactivebrokers.com/en/software/api/apiguide/api/historical_data_limitations.htm
            //the issue here is that the request may not be fulfilled...so we need to keep track of the request
            //and if we get an error regarding its failure, send it again using a timer
            var originalReqID = ++requestCounter;

            historicalDataRequests.TryAdd(originalReqID, request);

            arrivedHistoricalData.TryAdd(originalReqID, new List <OHLCBar>());

            //if necessary, chop up the request into multiple chunks so as to abide
            //the historical data limitations
            if (TwsUtils.RequestObeysLimits(request))
            {
                //send the request, no need for subrequests
                SendHistoricalRequest(originalReqID, request);
            }
            else
            {
                //create subrequests, add them to the ID map, and send them to TWS
                var subRequests = SplitRequest(request);
                subRequestCount.Add(originalReqID, subRequests.Count);

                foreach (var subReq in subRequests)
                {
                    lock (subReqMapLock)
                    {
                        requestCounter++;
                        historicalDataRequests.TryAdd(requestCounter, subReq);
                        subRequestIDMap.Add(requestCounter, originalReqID);
                        SendHistoricalRequest(requestCounter, subReq);
                    }
                }
            }
        }
Example #2
0
        private void SendHistoricalRequest(int id, HistoricalDataRequest request)
        {
            Log(LogLevel.Info,
                $"Sent historical data request to TWS. ID: {id}, Symbol: {request.Instrument.Symbol}, {TwsUtils.TimespanToDurationString(request.EndingDate - request.StartingDate, request.Frequency)} back from {request.EndingDate:yyyy-MM-dd hh:mm:ss}");

            var exchangeTz = request.Instrument.GetTimeZoneInfo();
            //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.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));
            }
        }