Ejemplo n.º 1
0
        public EditExchangeWindow(Exchange exchange)
        {
            InitializeComponent();
            DataContext = this;

            if (exchange == null)
            {
                TheExchange = new Exchange {ID = -1};
                _addingNew = true;

                ModifyBtn.Content = "Add";
            }
            else
            {
                exchange.Sessions = exchange.Sessions.OrderBy(x => x.OpeningDay).ThenBy(x => x.OpeningTime).ToList();
                _originalExchange = exchange;
                TheExchange = (Exchange)exchange.Clone();
                ModifyBtn.Content = "Modify";
            }

            var timezones = TimeZoneInfo.GetSystemTimeZones();
            foreach (TimeZoneInfo tz in timezones)
            {
                TimeZoneComboBox.Items.Add(tz);
            }
        }
Ejemplo n.º 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 Exchange();
     clone.ID = ID;
     clone.Name = Name;
     clone.Timezone = Timezone;
     clone.Sessions = Sessions == null ? null : new List<ExchangeSession>(Sessions.Select(x => (ExchangeSession)x.Clone()));
     clone.LongName = LongName;
     return clone;
 }
Ejemplo n.º 3
0
        public void BarTimeAdjustmentsHappenCorrectly()
        {
            string json = @"{
	""status"": {
		""code"": 200,
		""message"": ""Success.""
	},
	""results"": [{
		""symbol"": ""IBM"",
		""timestamp"": ""2016-08-15T09:00:00-04:00"",
		""tradingDay"": ""2016-08-15"",
		""open"": 162.4,
		""high"": 162.97,
		""low"": 162.38,
		""close"": 162.77,
		""volume"": 215371
	},
	{
		""symbol"": ""IBM"",
		""timestamp"": ""2016-08-15T10:00:00-04:00"",
		""tradingDay"": ""2016-08-15"",
		""open"": 162.8,
		""high"": 162.95,
		""low"": 162.61,
		""close"": 162.63,
		""volume"": 222815
	}]
}";
            
            var exchange = new Exchange
            {
                Timezone = "Eastern Standard Time"
            };

            var instrument = new Instrument()
            {
                Symbol = "IBM",
                Exchange = exchange,
                Sessions = new List<InstrumentSession>{
                new InstrumentSession{ OpeningDay = DayOfTheWeek.Monday, OpeningTime = new TimeSpan(9, 30, 0) } }
            };

            var request = new HistoricalDataRequest(instrument, BarSize.OneHour, DateTime.Now, DateTime.Now);
            List<OHLCBar> bars = BarChartUtils.ParseJson(JObject.Parse(json), request);

            //opening time set to the session opening instead of earlier
            Assert.AreEqual(new DateTime(2016, 8, 15, 9, 30, 0), bars[0].DTOpen.Value);

            //Bar closing time set correctly
            Assert.AreEqual(new DateTime(2016, 8, 15, 10, 0, 0), bars[0].DT);


            //timezone conversion has happened properly
            Assert.AreEqual(new DateTime(2016, 8, 15, 10, 0, 0), bars[1].DTOpen.Value);
        }
Ejemplo n.º 4
0
        public void InstrumentAdditionRequestsAreSentCorrectly()
        {
            var instrumentSourceMock = new Mock<IInstrumentSource>();
            var instrumentsServer = new InstrumentsServer(5555, instrumentSourceMock.Object);
            instrumentsServer.StartServer();

            var rtdBrokerMock = new Mock<IRealTimeDataBroker>();
            var rtdServer = new RealTimeDataServer(5554, 5553, rtdBrokerMock.Object);
            rtdServer.StartServer();

            

            _client.Connect();

            var exchange = new Exchange() { ID = 1, Name = "NYSE", Sessions = new List<ExchangeSession>(), Timezone = "Eastern Standard Time" };
            var datasource = new Datasource() { ID = 1, Name = "Yahoo" };
            var instrument = new Instrument() { Symbol = "SPY", UnderlyingSymbol = "SPY", Type = InstrumentType.Stock, Currency = "USD", Exchange = exchange, Datasource = datasource, Multiplier = 1 };

            instrumentSourceMock.Setup(x => x.AddInstrument(It.IsAny<Instrument>(), It.IsAny<bool>(), It.IsAny<bool>())).Returns(instrument);
            
            Instrument result = _client.AddInstrument(instrument);

            Thread.Sleep(50);

            Assert.IsTrue(result != null);

            instrumentSourceMock.Verify(x => x.AddInstrument(
                It.Is<Instrument>(y =>
                    y.Symbol == "SPY" &&
                    y.Exchange != null &&
                    y.Exchange.Name == "NYSE" &&
                    y.Datasource != null &&
                    y.Datasource.Name == "Yahoo" &&
                    y.Type == InstrumentType.Stock &&
                    y.Currency == "USD" &&
                    y.Multiplier == 1),
                It.Is<bool>(y => y == false),
                It.Is<bool>(y => y == true)));

            rtdServer.StopServer();
            rtdServer.Dispose();

            instrumentsServer.StopServer();
            instrumentsServer.Dispose();
        }
Ejemplo n.º 5
0
        public void ArrivedHistoricalDataCorrectlyRaisesEvent()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", UnderlyingSymbol = "SPY", Exchange = exchange, Currency = "USD", Type = InstrumentType.Stock },
                Frequency = QDMS.BarSize.OneDay,
                StartingDate = new DateTime(2014, 1, 14),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true
            };

            int requestID = -1;
            _ibClientMock
                .Setup(x => x.RequestHistoricalData(It.IsAny<int>(), It.IsAny<Contract>(), It.IsAny<DateTime>(), It.IsAny<string>(), It.IsAny<BarSize>(), It.IsAny<HistoricalDataType>(), It.IsAny<int>()))
                .Callback<Int32, Contract, DateTime, String, BarSize, HistoricalDataType, Int32>((y, a, b, c, d, e, f) => requestID = y);

            _ibDatasource.RequestHistoricalData(req);

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

            _ibClientMock.Raise(x => x.HistoricalData += null,
                new HistoricalDataEventArgs(
                    requestID,
                    new DateTime(2014, 1, 15),
                    1,
                    2,
                    3,
                    4,
                    5,
                    5,
                    3,
                    false,
                    1,
                    1));

            Assert.IsTrue(received);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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>()));
        }
Ejemplo n.º 8
0
        public void WhenDataSourceSymbolIsSetThatIsTheValueSentInTheHistoricalRequest()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", UnderlyingSymbol = "SPY", DatasourceSymbol = "TestMe!", Exchange = exchange, Currency = "USD", Type = InstrumentType.Stock },
                Frequency = QDMS.BarSize.OneDay,
                StartingDate = new DateTime(2014, 1, 14),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true
            };

            _ibDatasource.RequestHistoricalData(req);

            _ibClientMock.Verify(x => x.RequestHistoricalData(
                It.IsAny<int>(), 
                It.Is<Contract>(y => y.Symbol == "TestMe!"),
                It.IsAny<DateTime>(),
                It.IsAny<string>(),
                It.IsAny<BarSize>(),
                It.IsAny<HistoricalDataType>(),
                It.IsAny<int>()));
        }
Ejemplo n.º 9
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)));
        }
Ejemplo n.º 10
0
        public void HistoricalRequestsAreCorrectlyForwardedToTheIBClient()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", Exchange = exchange },
                Frequency = QDMS.BarSize.OneDay,
                StartingDate = new DateTime(2014, 1, 14),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true
            };

            _ibDatasource.RequestHistoricalData(req);

            _ibClientMock.Verify(
                x => x.RequestHistoricalData(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<DateTime>(),
                    It.Is<string>(y => y == "1 D"),
                    It.Is<Krs.Ats.IBNet.BarSize>(y => y == Krs.Ats.IBNet.BarSize.OneDay),
                    It.Is<HistoricalDataType>(y => y == HistoricalDataType.Trades),
                    It.Is<int>(y => y == 1))
                , Times.Once);
        }
Ejemplo n.º 11
0
        public void HistoricalRequestsAreReSentAfterARealTimeDataPacingViolation()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", Exchange = exchange },
                Frequency = QDMS.BarSize.OneDay,
                StartingDate = new DateTime(2014, 1, 14),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true
            };

            int requestID = 0;

            _ibClientMock
                .Setup(x => x.RequestHistoricalData(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<DateTime>(),
                    It.IsAny<string>(),
                    It.IsAny<Krs.Ats.IBNet.BarSize>(),
                    It.IsAny<HistoricalDataType>(),
                    It.IsAny<int>()))
                .Callback<Int32, Contract, DateTime, String, BarSize, HistoricalDataType, Int32>((y, a, b, c, d, e, f) => requestID = y);


            _ibDatasource.RequestHistoricalData(req);

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

            Thread.Sleep(25000);

            _ibClientMock.Verify(x => x.RequestHistoricalData(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<DateTime>(),
                    It.IsAny<string>(),
                    It.IsAny<Krs.Ats.IBNet.BarSize>(),
                    It.IsAny<HistoricalDataType>(),
                    It.IsAny<int>()), 
                    Times.Exactly(2));
        }
Ejemplo n.º 12
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));
        }
Ejemplo n.º 13
0
        public void HistoricalRequestsWithSubRequestDataPacingViolationResendWorksCorrectly()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", Exchange = exchange },
                Frequency = QDMS.BarSize.OneMinute,
                StartingDate = new DateTime(2014, 1, 13),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true,
                RequestID = 123
            };

            List<int> requestIds = new List<int>();

            _ibClientMock
                .Setup(x => x.RequestHistoricalData(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<DateTime>(),
                    It.IsAny<string>(),
                    It.IsAny<Krs.Ats.IBNet.BarSize>(),
                    It.IsAny<HistoricalDataType>(),
                    It.IsAny<int>(),
                    It.IsAny<List<TagValue>>()))
                .Callback<Int32, Contract, DateTime, String, BarSize, HistoricalDataType, Int32, List<TagValue>>(
                    (y, a, b, c, d, e, f, g) => requestIds.Add(y));

            int histDataArrivalCounter = 0;
            int errorReqId = -1;
            List<OHLCBar> data = null;

            _ibDatasource.Error += (e, s) => errorReqId = s.RequestID.Value;
            _ibDatasource.HistoricalDataArrived += (e, s) =>
            {
                data = s.Data;
                histDataArrivalCounter++;
            };

            _ibDatasource.RequestHistoricalData(req);

            _ibClientMock.Raise(x => x.Error += null, new ErrorEventArgs(requestIds[0], (ErrorMessage)162, "Historical Market Data Service error message:Historical data request pacing violation"));

            Thread.Sleep(25000);

            //So at this point the sub-request has been given a data pacing violation, and the DS should have tried to re-send it

            Assert.AreEqual(3, requestIds.Count);

            //return the data
            _ibClientMock.Raise(x => x.HistoricalData += null, new HistoricalDataEventArgs(requestIds[1], new DateTime(2014, 1, 14, 1, 0, 0), 1, 2, 0, 1, 0, 0, 0, false, 1, 1));
            _ibClientMock.Raise(x => x.HistoricalData += null, new HistoricalDataEventArgs(requestIds[2], new DateTime(2014, 1, 14, 2, 0, 0), 1, 2, 0, 1, 0, 0, 0, false, 1, 1));

            //and make sure it's sent up in the HistoricalDataArrived event properly
            Assert.AreEqual(1, histDataArrivalCounter);
            Assert.AreEqual(2, data.Count);
        }
Ejemplo n.º 14
0
        public void HistoricalRequestsWithSubRequestReceivesErrorNoMarketPermissions()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", Exchange = exchange },
                Frequency = QDMS.BarSize.OneMinute,
                StartingDate = new DateTime(2014, 1, 13),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true,
                RequestID = 123
            };

            List<int> requestIds = new List<int>();

            _ibClientMock
                .Setup(x => x.RequestHistoricalData(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<DateTime>(),
                    It.IsAny<string>(),
                    It.IsAny<Krs.Ats.IBNet.BarSize>(),
                    It.IsAny<HistoricalDataType>(),
                    It.IsAny<int>(),
                    It.IsAny<List<TagValue>>()))
                .Callback<Int32, Contract, DateTime, String, BarSize, HistoricalDataType, Int32, List<TagValue>>(
                    (y, a, b, c, d, e, f, g) => requestIds.Add(y));

            int histDataArrivalCounter = 0;
            int errorReqId = -1;

            _ibDatasource.Error += (e, s) => errorReqId = s.RequestID.Value;
            _ibDatasource.HistoricalDataArrived += (e, s) => histDataArrivalCounter++;

            _ibDatasource.RequestHistoricalData(req);

            _ibClientMock.Raise(x => x.Error += null, new ErrorEventArgs(requestIds[0], (ErrorMessage)162, "Historical Market Data Service error message:No market data permissions"));

            //At this point only one of the subrequests has been given an error, so we get no reply yet
            Assert.AreEqual(0, histDataArrivalCounter);

            _ibClientMock.Raise(x => x.Error += null, new ErrorEventArgs(requestIds[1], (ErrorMessage)162, "Historical Market Data Service error message:No market data permissions"));

            //Now both subrequests have been given an error, we expect to get an empty data set in return
            Assert.AreEqual(1, histDataArrivalCounter);

            //The error gives the id of the parent request, not any of the subrequests
            Assert.AreEqual(123, errorReqId);
        }
Ejemplo n.º 15
0
        public void HistoricalRequestsOneRequestReceivesErrorNoData()
        {
            var exchange = new Exchange { ID = 1, Name = "Ex", Timezone = "Pacific Standard Time" };
            var req = new HistoricalDataRequest
            {
                Instrument = new Instrument { ID = 1, Symbol = "SPY", Exchange = exchange },
                Frequency = QDMS.BarSize.OneDay,
                StartingDate = new DateTime(2014, 1, 13),
                EndingDate = new DateTime(2014, 1, 15),
                RTHOnly = true
            };

            int requestID = 0;

            _ibClientMock
                .Setup(x => x.RequestHistoricalData(
                    It.IsAny<int>(),
                    It.IsAny<Contract>(),
                    It.IsAny<DateTime>(),
                    It.IsAny<string>(),
                    It.IsAny<Krs.Ats.IBNet.BarSize>(),
                    It.IsAny<HistoricalDataType>(),
                    It.IsAny<int>(),
                    It.IsAny<List<TagValue>>()))
                .Callback<Int32, Contract, DateTime, String, BarSize, HistoricalDataType, Int32, List<TagValue>>((y, a, b, c, d, e, f, g) => requestID = y);


            _ibDatasource.RequestHistoricalData(req);

            _ibClientMock.Raise(x => x.Error += null, new ErrorEventArgs(requestID, (ErrorMessage)162, "Historical Market Data Service error message:HMDS query returned no data"));

            _ibClientMock.Verify(x => x.RequestHistoricalData(
                It.IsAny<int>(),
                It.IsAny<Contract>(),
                It.IsAny<DateTime>(),
                It.IsAny<string>(),
                It.IsAny<Krs.Ats.IBNet.BarSize>(),
                It.IsAny<HistoricalDataType>(),
                It.IsAny<int>(),
                It.IsAny<List<TagValue>>()),
                Times.Exactly(1));
        }