Esempio n. 1
0
        /// <summary>
        ///     When one of the data sources receives new real time data, it raises an event which is handled by this method,
        ///     which then forwards the data over the PUB socket after serializing it.
        /// </summary>
        public void RealTimeData(object sender, RealTimeDataEventArgs e)
        {
            RaiseEvent(RealTimeDataArrived, this, e);

            //save to local storage
            //perhaps just add it to a queue and then process in a different thread
            lock (requestsLock)
            {
                if (requests[e.RequestID].SaveToLocalStorage)
                {
                    localStorage.AddData(
                        new OHLCBar
                    {
                        Open          = e.Open,
                        High          = e.High,
                        Low           = e.Low,
                        Close         = e.Close,
                        Volume        = e.Volume,
                        DateTimeClose = MyUtils.TimestampToDateTime(e.Time)
                    },
                        requests[e.RequestID].Instrument,
                        requests[e.RequestID].Frequency);
                }
            }

#if DEBUG
            Log(LogLevel.Trace,
                $"RTD Received Instrument ID: {e.InstrumentID} O:{e.Open} H:{e.High} L:{e.Low} C:{e.Close} V:{e.Volume} T:{e.Time}");
#endif
        }
Esempio n. 2
0
 static void client_RealTimeDataReceived(object sender, RealTimeDataEventArgs e)
 {
     Console.WriteLine("Real Time Data Received: O: {0}  H: {1}  L: {2}  C: {3}",
                       e.Open,
                       e.High,
                       e.Low,
                       e.Close);
 }
Esempio n. 3
0
        //This event is raised when real time data arrives
        //We convert them and pass them on downstream
        private void _client_RealTimeBar(object sender, RealTimeBarEventArgs e)
        {
            RealTimeDataEventArgs args = TWSUtils.RealTimeDataEventArgsConverter(e);
            var originalRequest        = _realTimeDataRequests[e.RequestId];

            args.InstrumentID = originalRequest.Instrument.ID.Value;
            args.RequestID    = _requestIDMap[e.RequestId];
            RaiseEvent(DataReceived, this, args);
        }
Esempio n. 4
0
 /// <summary>
 ///     When data arrives from an external data source to the broker, this event is fired.
 /// </summary>
 private void BrokerRealTimeDataArrived(object sender, RealTimeDataEventArgs e)
 {
     lock (_publisherSocketLock) {
         if (_publisherSocket != null)
         {
             using (var ms = new MemoryStream()) {
                 Serializer.Serialize(ms, e);
                 _publisherSocket.SendMoreFrame(BitConverter.GetBytes(e.InstrumentID)); // Start by sending the ticker before the data
                 _publisherSocket.SendFrame(ms.ToArray());                              // Then send the serialized bar
             }
         }
     }
 }
Esempio n. 5
0
 /// <summary>
 /// When data arrives from an external data source to the broker, this event is fired.
 /// </summary>
 void _broker_RealTimeDataArrived(object sender, RealTimeDataEventArgs e)
 {
     using (var ms = new MemoryStream())
     {
         Serializer.Serialize(ms, e);
         //this lock is needed because this method will be called from
         //the thread of each DataSource in the broker
         lock (_pubSocketLock)
         {
             _pubSocket.SendMore(BitConverter.GetBytes(e.InstrumentID)); //start by sending the ticker before the data
             _pubSocket.Send(ms.ToArray());                              //then send the serialized bar
         }
     }
 }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        /// <summary>
        /// When one of the data sources receives new real time data, it raises an event which is handled by this method,
        /// which then forwards the data over the PUB socket after serializing it.
        /// </summary>
        public void RealTimeData(object sender, RealTimeDataEventArgs e)
        {
            RaiseEvent(RealTimeDataArrived, this, e);

            //continuous futures aliases
            lock (_aliasLock)
            {
                int instrumentID = e.InstrumentID;
                if (_aliases.ContainsKey(instrumentID))
                {
                    for (int i = 0; i < _aliases[instrumentID].Count; i++)
                    {
                        e.InstrumentID = _aliases[instrumentID][i];
                        RaiseEvent(RealTimeDataArrived, this, e);
                    }
                }
            }

            //save to local storage
            //perhaps just add it to a queue and then process in a different thread
            lock (_requestsLock)
            {
                if (_requests[e.RequestID].SaveToLocalStorage)
                {
                    _localStorage.AddDataAsync(
                        new OHLCBar {
                        Open = e.Open, High = e.High, Low = e.Low, Close = e.Close, Volume = e.Volume, DT = MyUtils.TimestampToDateTime(e.Time)
                    },
                        _requests[e.RequestID].Instrument,
                        _requests[e.RequestID].Frequency,
                        overwrite: false);
                }
            }

#if DEBUG
            Log(LogLevel.Trace,
                string.Format("RTD Received Instrument ID: {0} O:{1} H:{2} L:{3} C:{4} V:{5} T:{6}",
                              e.InstrumentID,
                              e.Open,
                              e.High,
                              e.Low,
                              e.Close,
                              e.Volume,
                              e.Time));
#endif
        }
Esempio n. 8
0
        /// <summary>
        ///     When data arrives from an external data source to the broker, this event is fired.
        /// </summary>
        private void BrokerRealTimeDataArrived(object sender, RealTimeDataEventArgs e)
        {
            lock (_publisherSocketLock)
            {
                if (_publisherSocket == null)
                {
                    return;
                }

                using (var ms = new MemoryStream())
                {
                    Serializer.Serialize(ms, e);
                    _publisherSocket.SendMoreFrame(Encoding.UTF8.GetBytes($"{e.InstrumentID}~{(int)e.Frequency}")); // Start by sending the id+freq before the data
                    _publisherSocket.SendMoreFrame(MessageType.RealTimeBars);
                    _publisherSocket.SendFrame(ms.ToArray());                                                       // Then send the serialized bar
                }
            }
        }
Esempio n. 9
0
        private void _client_OnPriceTick(Contract contract, Price tick)
        {
            int     instrumentID = 0; //todo fix, keep id -> id map
            int     requestID    = 0; //todo fix, keep id -> id map
            decimal price        = (decimal)tick.LastPrice;

            var args = new RealTimeDataEventArgs(
                instrumentID,
                tick.LastDateTime.ToBinary(),
                price,
                price,
                price,
                price,
                tick.LastVol,
                tick.LastPrice,
                1,
                requestID);

            RaiseEvent(DataReceived, this, args);
        }
Esempio n. 10
0
        public void ServerCorrectlyForwardsRealTimeData()
        {
            var ds = new Datasource()
            {
                ID = 1, Name = "TestDS"
            };
            var inst = new Instrument()
            {
                ID = 15, Datasource = ds, DatasourceID = 1, Symbol = "SPY", Type = InstrumentType.Stock
            };
            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds, rthOnly: false, savetoLocalStorage: false);

            _brokerMock.Setup(x => x.RequestRealTimeData(It.IsAny <RealTimeDataRequest>())).Returns(true);

            _client.RequestRealTimeData(req);

            Thread.Sleep(50);

            RealTimeDataEventArgs receivedData = null;

            _client.RealTimeDataReceived += (s, e) => receivedData = e;

            long dt = DateTime.Now.ToBinary();

            _brokerMock.Raise(x => x.RealTimeDataArrived += null, new RealTimeDataEventArgs(15, dt, 100m, 105m, 95m, 99m, 10000000, 101, 500, 1));

            Thread.Sleep(50);

            Assert.IsNotNull(receivedData);
            Assert.AreEqual(15, receivedData.InstrumentID);
            Assert.AreEqual(dt, receivedData.Time);
            Assert.AreEqual(100m, receivedData.Open);
            Assert.AreEqual(105m, receivedData.High);
            Assert.AreEqual(95m, receivedData.Low);
            Assert.AreEqual(99m, receivedData.Close);
            Assert.AreEqual(10000000, receivedData.Volume);
            Assert.AreEqual(500, receivedData.Count);
            Assert.AreEqual(101, receivedData.Wap);
        }
Esempio n. 11
0
        private void RealTimeDataSocketReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            lock (realTimeDataSocketLock)
            {
                if (realTimeDataSocket == null)
                {
                    return;
                }

                realTimeDataSocket.ReceiveFrameBytes(out bool hasMore);

                if (hasMore)
                {
                    byte[] buffer = realTimeDataSocket.ReceiveFrameBytes();

                    using (MemoryStream ms = new MemoryStream(buffer))
                    {
                        RealTimeDataEventArgs bar = MyUtils.ProtoBufDeserialize <RealTimeDataEventArgs>(ms);

                        RaiseEvent(RealTimeDataReceived, null, bar);
                    }
                }
            }
        }
Esempio n. 12
0
 private static void client_RealTimeDataReceived(object sender, RealTimeDataEventArgs e)
 {
     Console.WriteLine($"Real Time Data Received: O: {e.Open}  H: {e.High}  L: {e.Low}  C: {e.Close}");
 }
 void OnUpdateFailed(object sender, RealTimeDataEventArgs e)
 {
 }