Esempio 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));
            }
        }
Esempio 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);
        }
Esempio n. 3
0
File: IB.cs Progetto: herbota/QDMS
        /// <summary>
        /// real time data request
        /// </summary>
        public void RequestRealTimeData(RealTimeDataRequest request)
        {
            var id = _requestCounter++;

            lock (_requestIDMapLock)
            {
                _realTimeDataRequests.Add(id, request);
                _requestIDMap.Add(id, request.AssignedID);
                if (_reverseRequestIDMap.ContainsKey(request.AssignedID))
                {
                    _reverseRequestIDMap[request.AssignedID] = id;
                }
                else
                {
                    _reverseRequestIDMap.Add(request.AssignedID, id);
                }
            }

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

                if (_ibUseNewRealTimeDataSystem)
                {
                    //the new system uses the historical data update endpoint instead of realtime data
                    _client.RequestHistoricalData(id, contract, "",
                                                  "60 S", QDMSIBClient.BarSize.FiveSeconds, HistoricalDataType.Trades, request.RTHOnly, true);

                    //todo: write test
                }
                else
                {
                    _client.RequestRealTimeBars(
                        id,
                        contract,
                        "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));
            }
        }