private void SimulateData(object sender, ElapsedEventArgs e) { foreach (int instrumentID in _requestedInstrumentIDs.Values) { _loopsPassed[instrumentID]++; if (_loopsPassed[instrumentID] < _loopLimit[instrumentID]) { continue; } _loopsPassed[instrumentID] = 0; double open = _rand.NextDouble() * 50 + 20; double high = open + _rand.NextDouble(); double low = open - _rand.NextDouble(); double close = low + (high - low) * _rand.NextDouble(); int id; bool success = _idMap.TryGetValue(instrumentID, out id); if (success) { RaiseEvent(DataReceived, this, new RealTimeDataEventArgs( instrumentID, _requests[id].Frequency, MyUtils.ConvertToTimestamp(DateTime.Now), (decimal)open, (decimal)high, (decimal)low, (decimal)close, 1000, 0, 0, id)); } } }
public void RaisesDataEventWithTheContinuousFuturesAlias() { var cf = new ContinuousFuture() { ID = 1, InstrumentID = 1, Month = 1, UnderlyingSymbol = new UnderlyingSymbol { ID = 1, Symbol = "VIX", Rule = new ExpirationRule() } }; var inst = new Instrument { ID = 1, Symbol = "VIXCONTFUT", IsContinuousFuture = true, ContinuousFuture = cf, Datasource = new Datasource { ID = 999, Name = "MockSource" } }; var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds); _cfBrokerMock.Setup(x => x.RequestFrontContract(It.IsAny <Instrument>(), It.IsAny <DateTime?>())).Returns(0); int assignedID = 0; _dataSourceMock.Setup(x => x.RequestRealTimeData(It.IsAny <RealTimeDataRequest>())).Callback <RealTimeDataRequest>(r => assignedID = r.AssignedID); bool raisedCorrectSymbol = false; _broker.RealTimeDataArrived += (sender, e) => raisedCorrectSymbol = raisedCorrectSymbol ? raisedCorrectSymbol : e.InstrumentID == 1; _broker.RequestRealTimeData(req); var frontFutureInstrument = new Instrument { Symbol = "VXF4", ID = 2, Datasource = new Datasource { ID = 999, Name = "MockSource" } }; _cfBrokerMock.Raise(x => x.FoundFrontContract += null, new FoundFrontContractEventArgs(0, frontFutureInstrument, DateTime.Now)); _dataSourceMock.Raise(x => x.DataReceived += null, new RealTimeDataEventArgs(2, MyUtils.ConvertToTimestamp(DateTime.Now), 100, 100, 100, 100, 50, 100, 2, assignedID)); Thread.Sleep(50); Assert.IsTrue(raisedCorrectSymbol); }
public static RealTimeDataEventArgs HistoricalDataEventArgsToRealTimeDataEventArgs(QDMSIBClient.HistoricalDataEventArgs e, int instrumentId, int reqId) { var rtdea = new RealTimeDataEventArgs(instrumentId, QDMS.BarSize.FiveSeconds, MyUtils.ConvertToTimestamp(e.Bar.Time), (decimal)e.Bar.Open, (decimal)e.Bar.High, (decimal)e.Bar.Low, (decimal)e.Bar.Close, e.Bar.Volume, e.Bar.WAP, e.Bar.Count, reqId); return(rtdea); }
void SendData(BarSize barSize) { foreach (KeyValuePair <RequestGrouping, ConcurrentDictionary <int, RealTimeDataRequest> > pair in _connectedRequests) { // @ToDo: Performance: if many entries exists in this list, this could be an performance issue. if (pair.Key.Frequency != barSize) { continue; } if (_connectedRequests.Count == 0) { continue; } int historicalRequestId; if (!_historicalRequests.TryGetValue(pair.Key, out historicalRequestId)) { continue; // the historical request is not made yet. } List <OHLCBar> historicalData; if (!_historicalData.TryGetValue(historicalRequestId, out historicalData)) { continue; // the historical data did not arrived yet. } List <int> requestsToDelete = new List <int>(); foreach (var pair2 in pair.Value) { // get the current position int indexPosition = 0; if (_requestPosition.TryGetValue(pair2.Key, out indexPosition)) { indexPosition++; } if (historicalData.Count <= indexPosition) { // end of historical data _logger.Log(LogLevel.Info, $"End of historical data for real time simulation - request ID: {pair2.Key}"); requestsToDelete.Add(pair2.Key); } else { var currentBar = historicalData[indexPosition]; RaiseEvent(DataReceived, this, new RealTimeDataEventArgs( pair2.Value.Instrument.ID.Value, barSize, MyUtils.ConvertToTimestamp(currentBar.DT), currentBar.Open, currentBar.High, currentBar.Low, currentBar.Close, currentBar.Volume ?? 0, 0, 0, pair2.Value.AssignedID )); if (indexPosition == 0) { _requestPosition.TryAdd(pair2.Key, indexPosition); } else { _requestPosition[pair2.Key] = indexPosition; } } } RealTimeDataRequest dummy; foreach (var i in requestsToDelete) { pair.Value.TryRemove(i, out dummy); } } }
public void ConvertToTimestampReturnsCorrectTimestamp() { Assert.AreEqual(1012907112, MyUtils.ConvertToTimestamp(new DateTime(2002, 2, 5, 11, 5, 12))); }