示例#1
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));
            }
        }
示例#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
            {
                var 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);
        }