/// <summary> /// This event is raised when historical data arrives from TWS /// </summary> private void _client_HistoricalData(object sender, Krs.Ats.IBNet.HistoricalDataEventArgs e) { //convert the bar and add it to the Dictionary of arrived data var bar = TwsUtils.HistoricalDataEventArgsToOHLCBar(e); int id; lock (subReqMapLock) { //if the data is arriving for a sub-request, we must get the id of the original request first //otherwise it's just the same id id = subRequestIDMap.ContainsKey(e.RequestId) ? subRequestIDMap[e.RequestId] : e.RequestId; } //stocks need to have their volumes multiplied by 100, I think all other instrument types do not if (historicalDataRequests[id].Instrument.Type == InstrumentType.Stock) { bar.Volume *= 100; } arrivedHistoricalData[id].Add(bar); if (e.RecordNumber >= e.RecordTotal - 1) //this was the last item to receive for this request, send it to the broker { var requestComplete = true; lock (subReqMapLock) { if (subRequestIDMap.ContainsKey(e.RequestId)) { //If there are sub-requests, here we check if this is the last one requestComplete = ControlSubRequest(e.RequestId); if (requestComplete) { //If it was the last one, we need to order the data because sub-requests can arrive out of order arrivedHistoricalData[id] = arrivedHistoricalData[id].OrderBy(x => x.DateTimeOpen).ToList(); } } } if (requestComplete) { HistoricalDataRequestComplete(id); } } }