Example #1
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);
        }
Example #2
0
 /// <summary>
 /// Creates a new object that is a copy of the current instance.
 /// </summary>
 /// <returns>
 /// A new object that is a copy of this instance.
 /// </returns>
 public object Clone()
 {
     var clone = new RealTimeDataRequest(Instrument, Frequency, RTHOnly, SaveToLocalStorage);
     clone.FallBack = FallBack;
     clone.RequestID = RequestID;
     clone.AssignedID = AssignedID;
     return clone;
 }
        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            var clone = new RealTimeDataRequest(Instrument, Frequency, RTHOnly, SaveToLocalStorage);

            clone.FallBack   = FallBack;
            clone.RequestID  = RequestID;
            clone.AssignedID = AssignedID;
            return(clone);
        }
Example #4
0
        public void ServerCorrectlyForwardsRealTimeDataRequestsToBroker()
        {
            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);
            _client.RequestRealTimeData(req);

            _brokerMock.Verify(x => x.RequestRealTimeData(It.Is<RealTimeDataRequest>(
                y => y.Frequency == BarSize.FiveSeconds &&
                y.RTHOnly == false &&
                y.SaveToLocalStorage == false &&
                y.Instrument.ID == 15 &&
                y.Instrument.Symbol == "SPY" &&
                y.Instrument.Datasource.Name == "TestDS")));
        }
Example #5
0
        public void DoesNotRepeatRequestToSourceWhenARealTimeStreamAlreadyExists()
        {
            var inst = new Instrument
            {
                ID = 1,
                Symbol = "SPY",
                Datasource = new Datasource { ID = 999, Name = "MockSource" }
            };

            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds);

            _broker.RequestRealTimeData(req);
            Thread.Sleep(100);
            _broker.RequestRealTimeData(req);

            _dataSourceMock.Verify(x => x.RequestRealTimeData(
                It.IsAny<RealTimeDataRequest>()),
                Times.Once);
        }
Example #6
0
        public void RealTimeDataRequestsForwardedToDataSource()
        {
            var inst = new Instrument
            {
                ID = 1,
                Symbol = "SPY",
                Datasource = new Datasource { ID = 999, Name = "MockSource" }
            };

            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds);

            _broker.RequestRealTimeData(req);

            _dataSourceMock.Verify(x => x.RequestRealTimeData(
                It.Is<RealTimeDataRequest>(
                    r => r.Instrument.ID == 1 &&
                    r.Instrument.Symbol == "SPY" &&
                    r.Frequency == BarSize.FiveSeconds)),
                Times.Once);
        }
Example #7
0
        public void ServerReturnsErrorToClientIfNoInstrumentIdIsSet()
        {
            var ds = new Datasource { ID = 1, Name = "TestDS" };
            var inst = new Instrument { ID = null, Datasource = ds, DatasourceID = 1, Symbol = "SPY", Type = InstrumentType.Stock };
            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds, false);

            string error = null;
            int? requestId = null;

            _client.Error += (s, e) =>
            {
                error = e.ErrorMessage;
                requestId = e.RequestID;
            };

            _client.RequestRealTimeData(req);

            Thread.Sleep(DefaultDelayInMilliseconds);

            Assert.IsTrue(!string.IsNullOrEmpty(error));
            Assert.IsTrue(requestId.HasValue);
        }
Example #8
0
        public void DoesNotRepeatRequestToSourceWhenARealTimeContinuousFuturesStreamAlreadyExists()
        {
            var inst = new Instrument
            {
                ID = 1,
                Symbol = "ContinuousVIXFutures",
                IsContinuousFuture = true,
                Type = InstrumentType.Future,
                Datasource = new Datasource { ID = 999, Name = "MockSource" }
            };

            var frontContract = new Instrument
            {
                ID = 2,
                Symbol = "VXF3",
                Type = InstrumentType.Future,
                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(1);
            _broker.RequestRealTimeData(req);

            _cfBrokerMock.Raise(x => x.FoundFrontContract += null, new FoundFrontContractEventArgs(1, frontContract, DateTime.Now));

            _broker.RequestRealTimeData(req);

            _dataSourceMock.Verify(x => x.RequestRealTimeData(
                It.IsAny<RealTimeDataRequest>()),
                Times.Once);
            _cfBrokerMock.Verify(x => x.RequestFrontContract(
                It.IsAny<Instrument>(),
                It.IsAny<DateTime?>()),
                Times.Once);
        }
Example #9
0
        private void SendIntradayBarRequest(Session session, RealTimeDataRequest req)
        {
            session.OpenService("//blp/refdata");
            Service refDataService = session.GetService("//blp/refdata");
            Request request = refDataService.CreateRequest("IntradayBarRequest");

            // only one security/eventType per request
            request.Set("security", req.Instrument.DatasourceSymbol);
            request.Set("eventType", "TRADE");
            request.Set("interval", req.Frequency.ToTimeSpan().TotalSeconds);

            DateTime prevTradedDate = DateTime.Now.AddDays(-1); //todo fix
            var startDateTime = string.Format("{0}-{1}-{2}T13:30:00", prevTradedDate.Year, prevTradedDate.Month, prevTradedDate.Day);
            prevTradedDate = prevTradedDate.AddDays(1); // next day for end date
            var endDateTime = string.Format("{0}-{1}-{2}T13:30:00", prevTradedDate.Year, prevTradedDate.Month, prevTradedDate.Day);

            request.Set("startDateTime", startDateTime);
            request.Set("endDateTime", endDateTime);


            request.Set("gapFillInitialBar", true);

            session.SendRequest(request, null);
        }
Example #10
0
        static void Main()
        {
            //create the client, assuming the default port settings
            QDMSClient.QDMSClient client = new QDMSClient.QDMSClient(
                "SampleClient",
                "127.0.0.1",
                5556,
                5557,
                5558,
                5555);

            //hook up the events needed to receive data & error messages
            client.HistoricalDataReceived += client_HistoricalDataReceived;
            client.RealTimeDataReceived += client_RealTimeDataReceived;
            client.LocallyAvailableDataInfoReceived += client_LocallyAvailableDataInfoReceived;
            client.Error += client_Error;

            //connect to the server
            client.Connect();
            
            //make sure the connection was succesful before we continue
            if (!client.Connected)
            {
                Console.WriteLine("Could not connect.");
                Console.WriteLine("Press enter to exit.");
                Console.ReadLine();
                return;
            }

            //request the list of available instruments
            List<Instrument> instruments = client.FindInstruments();
            foreach (Instrument i in instruments)
            {
                Console.WriteLine("Instrument ID {0}: {1} ({2}), Datasource: {3}",
                    i.ID,
                    i.Symbol,
                    i.Type,
                    i.Datasource.Name);
            }

            Thread.Sleep(3000);
            
            //then we grab some historical data from Yahoo
            //start by finding the SPY instrument
            var spy = client.FindInstruments(x => x.Symbol == "SPY" && x.Datasource.Name == "Yahoo").FirstOrDefault();
            if (spy != null)
            {
                var req = new HistoricalDataRequest(
                    spy,
                    BarSize.OneDay,
                    new DateTime(2013, 1, 1),
                    new DateTime(2013, 1, 15),
                    dataLocation: DataLocation.Both,
                    saveToLocalStorage: true,
                    rthOnly: true);

                client.RequestHistoricalData(req);


                Thread.Sleep(3000);

                //now that we downloaded the data, let's make a request to see what is stored locally
                client.GetLocallyAvailableDataInfo(spy);

                Thread.Sleep(3000);

                //finally send a real time data request (from the simulated data datasource)
                spy.Datasource.Name = "SIM";
                var rtReq = new RealTimeDataRequest(spy, BarSize.OneSecond);
                client.RequestRealTimeData(rtReq);

                Thread.Sleep(3000);

                //And then cancel the real time data stream
                client.CancelRealTimeData(spy);
            }

            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
            client.Disconnect();
            client.Dispose();
        }
Example #11
0
        public void ArrivedRealTimeDataCorrentlyRaisesEvent()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new RealTimeDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", UnderlyingSymbol = "SPY", Exchange = exchange, Currency = "USD", Type = InstrumentType.Stock },
                Frequency = QDMS.BarSize.FiveSeconds,
                RTHOnly = true
            };

            int requestID = -1;
            _ibClientMock
                .Setup(x => x.RequestRealTimeBars(It.IsAny<int>(), It.IsAny<Contract>(), It.IsAny<int>(), It.IsAny<RealTimeBarType>(), It.IsAny<bool>()))
                .Callback<int, Contract, Int32, RealTimeBarType, Boolean>((y, a, b, c, d) => requestID = y);

            _ibDatasource.RequestRealTimeData(req);

            bool received = false;
            _ibDatasource.DataReceived += (sender, e) => received = true;

            _ibClientMock.Raise(x => x.RealTimeBar += null, new RealTimeBarEventArgs(requestID, 10000000, 1, 2, 3, 4, 5, 3, 5));

            Assert.IsTrue(received);
        }
Example #12
0
        public void WhenDataSourceSymbolIsSetThatIsTheValueSentInTheRealTimeRequest()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new RealTimeDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", DatasourceSymbol = "TestMe!", UnderlyingSymbol = "SPY", Exchange = exchange, Currency = "USD", Type = InstrumentType.Stock },
                Frequency = QDMS.BarSize.FiveSeconds,
                RTHOnly = true
            };

            _ibDatasource.RequestRealTimeData(req);

            _ibClientMock.Verify(x => x.RequestRealTimeBars(
                It.IsAny<int>(),
                It.Is<Contract>(y => y.Symbol == "TestMe!"),
                It.IsAny<int>(),
                It.IsAny<RealTimeBarType>(),
                It.IsAny<bool>()));
        }
Example #13
0
        public void RealTimeRequestsAreCorrectlyForwardedToTheIBClient()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new RealTimeDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", UnderlyingSymbol = "SPY", Exchange = exchange, Currency = "USD", Type = InstrumentType.Stock },
                Frequency = QDMS.BarSize.FiveSeconds,
                RTHOnly = true
            };

            _ibDatasource.RequestRealTimeData(req);

            _ibClientMock.Verify(x => x.RequestRealTimeBars(
                It.IsAny<int>(),
                It.Is<Contract>(y => y.Symbol == "SPY" && y.Exchange == "Ex" && y.SecurityType == SecurityType.Stock),
                It.Is<int>(y => y == (int)Krs.Ats.IBNet.BarSize.FiveSeconds),
                It.Is<RealTimeBarType>(y => y == RealTimeBarType.Trades),
                It.Is<bool>(y => y == true)));
        }
Example #14
0
        public void RealTimeRequestsAreReSentAfterARealTimeDataPacingViolation()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new RealTimeDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", Exchange = exchange },
                Frequency = QDMS.BarSize.FiveSeconds,
                RTHOnly = true
            };

            int requestID = 0;

            _ibClientMock
                .Setup(x => x.RequestRealTimeBars(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<int>(),
                    It.IsAny<RealTimeBarType>(),
                    It.IsAny<bool>()))
                .Callback<int, Contract, int, RealTimeBarType, bool>((y, a, b, c, d) => requestID = y);


            _ibDatasource.RequestRealTimeData(req);

            _ibClientMock.Raise(x => x.Error += null, new ErrorEventArgs(requestID, (ErrorMessage)420, ""));

            Thread.Sleep(25000);

            _ibClientMock.Verify(x => x.RequestRealTimeBars(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<int>(),
                    It.IsAny<RealTimeBarType>(),
                    It.IsAny<bool>()),
                    Times.Exactly(2));
        }
Example #15
0
        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);
        }
Example #16
0
        public void ServerReturnsErrorToClientIfNoInstrumentIdIsSet()
        {
            var ds = new Datasource() { ID = 1, Name = "TestDS" };
            var inst = new Instrument() { ID = null, Datasource = ds, DatasourceID = 1, Symbol = "SPY", Type = InstrumentType.Stock };
            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds, rthOnly: false, savetoLocalStorage: false);

            string error = null;
            int? requestID = -1;
            _client.Error += (s, e) =>
            {
                error = e.ErrorMessage;
                requestID = e.RequestID;
            };

            _client.RequestRealTimeData(req);

            Thread.Sleep(100);

            Assert.AreEqual("Real time data request error: Instrument had no ID set.", error);
        }
Example #17
0
        public void RequestsFrontContractFromCFBrokerForContinuousFuturesRequests()
        {
            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);

            _broker.RequestRealTimeData(req);

            _cfBrokerMock.Verify(x => x.RequestFrontContract(It.IsAny<Instrument>(), It.IsAny<DateTime?>()));
        }
Example #18
0
        public void RequestsCorrectFuturesContractForContinuousFutures()
        {
            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);

            _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.Verify(x => x.RequestRealTimeData(
                It.Is<RealTimeDataRequest>(y =>
                    y.Instrument.ID == 2)), Times.Once);
        }
Example #19
0
        public void RealTimeDataIsSavedToLocalStorageIfFlagIsSet()
        {
            var inst = new Instrument
            {
                ID = 1,
                Symbol = "SPY",
                Datasource = new Datasource { ID = 999, Name = "MockSource" }
            };

            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds, savetoLocalStorage: true);

            int assignedID = 0;
            _dataSourceMock.Setup(x => x.RequestRealTimeData(It.IsAny<RealTimeDataRequest>())).Callback<RealTimeDataRequest>(r => assignedID = r.AssignedID);

            _broker.RequestRealTimeData(req);
            Thread.Sleep(100);

            _dataSourceMock.Raise(x => x.DataReceived += null, new RealTimeDataEventArgs(1, 1389906576, 100, 100, 100, 100, 1000, 100, 5, assignedID));

            _localStorageMock.Verify(x => x.AddDataAsync(
                It.Is<OHLCBar>(y => y.Open == 100 && y.Volume == 1000),
                It.Is<Instrument>(y => y.ID == 1 && y.Symbol == "SPY"),
                It.Is<BarSize>(y => y == BarSize.FiveSeconds),
                It.Is<bool>(y => y == false)));
        }
Example #20
0
        public void ServerForwardsErrorToClientIfExceptionIsThrownInRequestRealTimeData()
        {
            var ds = new Datasource { ID = 1, Name = "TestDS" };
            var inst = new Instrument { ID = 1, Datasource = ds, DatasourceID = 1, Symbol = "SPY", Type = InstrumentType.Stock };
            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds, false);

            string error = null;
            int? requestId = null;

            _client.Error += (s, e) =>
            {
                error = e.ErrorMessage;
                requestId = e.RequestID;
            };

            _brokerMock.Setup(x => x.RequestRealTimeData(It.IsAny<RealTimeDataRequest>())).Throws(new Exception("testerror"));

            _client.RequestRealTimeData(req);

            Thread.Sleep(DefaultDelayInMilliseconds);

            Assert.IsTrue(!string.IsNullOrEmpty(error));
            Assert.IsTrue(requestId.HasValue);
        }
Example #21
0
        public void ServerForwardsErrorToClientIfExceptionIsThrownInRequestRealTimeData()
        {
            var ds = new Datasource() { ID = 1, Name = "TestDS" };
            var inst = new Instrument() { ID = 1, Datasource = ds, DatasourceID = 1, Symbol = "SPY", Type = InstrumentType.Stock };
            var req = new RealTimeDataRequest(inst, BarSize.FiveSeconds, rthOnly: false, savetoLocalStorage: false);

            string error = null;
            int? requestID = -1;
            _client.Error += (s, e) =>
            {
                error = e.ErrorMessage;
                requestID = e.RequestID;
            };

            _brokerMock.Setup(x => x.RequestRealTimeData(It.IsAny<RealTimeDataRequest>())).Throws(new Exception("testerror"));

            _client.RequestRealTimeData(req);

            Thread.Sleep(100);

            Assert.AreEqual("Real time data request error: testerror", error);
        }
Example #22
0
 /// <summary>
 /// Request real time data.
 /// </summary>
 /// <param name="request"></param>
 /// <returns>The ID associated with this real time data request.</returns>
 public int RequestRealTimeData(RealTimeDataRequest request)
 {
     throw new NotImplementedException();
 }
Example #23
0
        public void RequestRealTimelDataRaisesErrorEventAndReturnsMinusOneWhenInstrumentIsNull()
        {
            var req = new RealTimeDataRequest
            {
                Instrument = null
            };

            bool errorTriggered = false;
            _client.Error += (sender, e) => errorTriggered = true;

            Assert.AreEqual(-1, _client.RequestRealTimeData(req));

            Assert.IsTrue(errorTriggered);
        }
Example #24
0
        public void RequestRealTimelDataRaisesErrorEventAndReturnsMinusOneWhenNotConnected()
        {
            var req = new RealTimeDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY" }
            };

            bool errorTriggered = false;
            _client.Error += (sender, e) => errorTriggered = true;

            Assert.AreEqual(-1, _client.RequestRealTimeData(req));

            Assert.IsTrue(errorTriggered);
        }