示例#1
0
        internal void GivenInstrument_WithSubscriber_PublishesMessage()
        {
            // Arrange
            this.publisher.Start();
            Task.Delay(100).Wait(); // Allow publisher to start

            var instrument = StubInstrumentProvider.AUDUSD();

            var subscriber = new SubscriberSocket(TestAddress);

            subscriber.Connect(TestAddress);
            subscriber.Subscribe(nameof(Instrument) + ":" + instrument.Symbol.Value);
            Task.Delay(100).Wait();

            // Act
            this.publisher.Endpoint.SendAsync(instrument);

            var topic   = subscriber.ReceiveFrameBytes();
            var message = subscriber.ReceiveFrameBytes();

            // Assert
            Assert.Equal("Instrument:AUD/USD.FXCM", Encoding.UTF8.GetString(topic));
            Assert.Equal(instrument, this.instrumentSerializer.Deserialize(message));

            // Tear Down
            subscriber.Disconnect(TestAddress);
            subscriber.Dispose();
            this.publisher.Stop().Wait();
            this.publisher.Dispose();
        }
        internal void GivenInstrumentRequest_WithInstrument_ReturnsValidInstrumentResponse()
        {
            // Arrange
            var provider = new InstrumentProvider(
                this.container,
                this.messagingAdapter,
                this.repository,
                this.dataSerializer);

            provider.Start().Wait();

            var instrument = StubInstrumentProvider.AUDUSD();

            this.repository.Add(instrument);

            var query = new Dictionary <string, string>
            {
                { "DataType", "Instrument[]" },
                { "Symbol", instrument.Symbol.Value },
            };

            var request = new DataRequest(
                query,
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            // Act
            var response = (DataResponse)provider.FindData(request);
            var data     = this.dataSerializer.DeserializeBlob(response.Data);

            // Assert
            Assert.Equal(typeof(DataResponse), response.Type);
            Assert.Equal(instrument, data[0]);
        }
示例#3
0
        internal void Ingest_MultipleTicksDifferentSymbols_CorrectlyAddsTicksToRepository()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();
            var eurusd = StubInstrumentProvider.EURUSD();

            this.repository.Update(audusd);
            this.repository.Update(eurusd);

            var tick1 = StubQuoteTickProvider.Create(audusd.Symbol);
            var tick2 = StubQuoteTickProvider.Create(audusd.Symbol, StubZonedDateTime.UnixEpoch() + Duration.FromDays(1));
            var tick3 = StubQuoteTickProvider.Create(audusd.Symbol, StubZonedDateTime.UnixEpoch() + Duration.FromDays(2));
            var tick4 = StubQuoteTickProvider.Create(eurusd.Symbol, StubZonedDateTime.UnixEpoch());
            var tick5 = StubQuoteTickProvider.Create(eurusd.Symbol, StubZonedDateTime.UnixEpoch() + Duration.FromDays(1));

            // Act
            this.repository.Ingest(tick1);
            this.repository.Ingest(tick2);
            this.repository.Ingest(tick3);
            this.repository.Ingest(tick4);
            this.repository.Ingest(tick5);

            // Assert
            // TODO: System.InvalidCastException : Specified cast is not valid. (inside StackExchange.Redis)
            // Assert.Equal(3, this.repository.TicksCount(audusd.Symbol));
            // Assert.Equal(2, this.repository.TicksCount(eurusd.Symbol));
        }
        internal void CanSerializeAndDeserialize_InstrumentResponses()
        {
            // Arrange
            var dataSerializer = new InstrumentSerializer();
            var instrument     = StubInstrumentProvider.AUDUSD();
            var correlationId  = Guid.NewGuid();

            Instrument[] instruments           = { instrument };
            var          serializedInstruments = dataSerializer.Serialize(instruments);

            var metadata = new Dictionary <string, string> {
                { "Symbol", instrument.Symbol.ToString() }
            };
            var data = dataSerializer.SerializeBlob(serializedInstruments, metadata);

            var response = new DataResponse(
                data,
                typeof(Instrument[]).Name,
                dataSerializer.BlobEncoding,
                correlationId,
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            // Act
            var serializedResponse   = this.serializer.Serialize(response);
            var deserializedResponse = (DataResponse)this.serializer.Deserialize(serializedResponse);
            var deserializedData     = dataSerializer.DeserializeBlob(deserializedResponse.Data);

            // Assert
            Assert.Equal(response, deserializedResponse);
            Assert.Equal(instrument, deserializedData[0]);
            Assert.Equal(correlationId, deserializedResponse.CorrelationId);
            this.Output.WriteLine(Convert.ToBase64String(serializedResponse));
            this.Output.WriteLine(Encoding.UTF8.GetString(serializedResponse));
        }
        internal void GivenInstrumentRequest_WithNoInstruments_ReturnsQueryFailedMessage()
        {
            // Arrange
            var provider = new InstrumentProvider(
                this.container,
                this.messagingAdapter,
                this.repository,
                this.dataSerializer);

            provider.Start().Wait();

            var instrument = StubInstrumentProvider.AUDUSD();

            var query = new Dictionary <string, string>
            {
                { "DataType", "Instrument" },
                { "Symbol", instrument.Symbol.Value },
            };

            var request = new DataRequest(
                query,
                Guid.NewGuid(),
                StubZonedDateTime.UnixEpoch());

            // Act
            var response = (QueryFailure)provider.FindData(request);

            // Assert
            Assert.Equal(typeof(QueryFailure), response.Type);
        }
示例#6
0
        internal void Build_WithValidInstrument_ReturnsExpectedInstrument()
        {
            // Arrange
            var audusd            = StubInstrumentProvider.AUDUSD();
            var instrumentBuilder = new InstrumentBuilder(audusd);

            // Act
            var result = instrumentBuilder.Build(StubZonedDateTime.UnixEpoch());

            // Assert
            Assert.Equal(audusd, result);
        }
        internal void Add_WithOneInstruments_AddsToRepository()
        {
            // Arrange
            var instrument = StubInstrumentProvider.AUDUSD();

            // Act
            this.repository.Add(instrument);
            var count = this.repository.GetAllKeys().Count;

            // Assert
            Assert.Equal(1, count);
        }
        internal void Delete_WithOneSymbol_DeletesFromRepository()
        {
            // Arrange
            var instrument = StubInstrumentProvider.AUDUSD();

            this.repository.Add(instrument);

            // Act
            this.repository.Delete(instrument.Symbol);
            var result = this.repository.GetAllKeys();

            // Assert
            Assert.Equal(0, result.Count);
        }
        internal void GetInstrument_WithInstrumentInRepository_ReturnsInstrument()
        {
            // Arrange
            var instrument = StubInstrumentProvider.AUDUSD();

            this.repository.Add(instrument);
            this.repository.CacheAll();

            // Act
            var result = this.repository.GetInstrument(instrument.Symbol);

            // Assert
            Assert.Equal(instrument, result.Value);
        }
        internal void CanSerializeAndDeserializeInstrument()
        {
            // Arrange
            var serializer = new InstrumentSerializer();
            var instrument = StubInstrumentProvider.AUDUSD();

            // Act
            var packed   = serializer.Serialize(instrument);
            var unpacked = serializer.Deserialize(packed);

            // Assert
            Assert.Equal(instrument, unpacked);
            this.Output.WriteLine(Convert.ToBase64String(packed));
            this.Output.WriteLine(Encoding.UTF8.GetString(packed));
        }
示例#11
0
        internal void GetTicks_WithNoTicks_ReturnsEmptyArray()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();

            this.repository.Update(audusd);

            // Act
            var result = this.repository.GetTicks(audusd.Symbol, null, null, null);

            // Assert
            Assert.False(this.repository.TicksExist(audusd.Symbol));
            Assert.Equal(0, this.repository.TicksCount(audusd.Symbol));
            Assert.Empty(result);
        }
        internal void Add_WithCollectionOfInstruments_AddsAllToRepository()
        {
            // Arrange
            var instrument1 = StubInstrumentProvider.AUDUSD();
            var instrument2 = StubInstrumentProvider.EURUSD();
            var instrument3 = StubInstrumentProvider.USDJPY();

            // Act
            this.repository.Add(instrument1);
            this.repository.Add(instrument2);
            this.repository.Add(instrument3);
            var count = this.repository.GetAllKeys().Count;

            // Assert
            Assert.Equal(3, count);
        }
示例#13
0
        [Fact] internal void Ingest_WithValidTick_AddsTickToRepository()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();
            var tick   = StubQuoteTickProvider.Create(audusd.Symbol);

            this.repository.Update(audusd);

            // Act
            this.repository.Ingest(tick);

            // Assert
            // TODO: System.InvalidCastException : Specified cast is not valid. (inside StackExchange.Redis)
            // Assert.True(this.repository.TicksExist(audusd.Symbol));
            // Assert.Equal(1, this.repository.TicksCount(audusd.Symbol));
        }
示例#14
0
        internal void GetBars_WithNoTicksIngested_ReturnsEmptyArray()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();

            // var barType = StubBarType.AUDUSD_OneMinuteAsk();
            this.repository.Update(audusd);

            // Act
            // var result = this.repository.GetBars(StubBarType.AUDUSD_OneMinuteAsk(), null, null, null);

            // Assert
            // TODO: System.InvalidCastException : Specified cast is not valid. (inside StackExchange.Redis)
            // Assert.False(this.repository.BarsExist(barType));
            // Assert.Equal(0, this.repository.BarsCount(barType));
            // Assert.Empty(result.Bars);
        }
        internal void DeleteAll_WithThreeInstrumentsInRepository_DeletesAllFromRepository()
        {
            // Arrange
            var instrument1 = StubInstrumentProvider.AUDUSD();
            var instrument2 = StubInstrumentProvider.EURUSD();
            var instrument3 = StubInstrumentProvider.USDJPY();

            this.repository.Add(instrument1);
            this.repository.Add(instrument2);
            this.repository.Add(instrument3);

            // Act
            this.repository.DeleteAll();
            var result = this.repository.GetAllKeys();

            // Assert
            Assert.Equal(0, result.Count);
        }
示例#16
0
        internal void GetTicks_WithOneTick_ReturnsCorrectTick()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();

            this.repository.Update(audusd);

            var tick1 = StubQuoteTickProvider.Create(audusd.Symbol);

            this.repository.Ingest(tick1);

            // Act
            // var result = this.repository.GetTicks(audusd.Symbol, null, null, null);

            // Assert
            // TODO: System.InvalidCastException : Specified cast is not valid. (inside StackExchange.Redis)
            // Assert.Equal(1, this.repository.TicksCount(audusd.Symbol));
            // Assert.Single(result);
            // Assert.Equal(tick1, result[0]);
        }
示例#17
0
        internal void Update_WithChanges_ReturnsNewInstrument()
        {
            // Arrange
            var audusd            = StubInstrumentProvider.AUDUSD();
            var instrumentBuilder = new InstrumentBuilder(audusd);

            var instrument = new Instrument(
                new Symbol("SPX500", new Venue("FXCM")),
                Currency.USD,
                SecurityType.CFD,
                2,
                0,
                1,
                1,
                1,
                1,
                Price.Create(0.01m, 2),
                Quantity.Create(1m),
                Quantity.Create(10m),
                Quantity.Create(10000m),
                45,
                45,
                StubZonedDateTime.UnixEpoch());

            // Act
            var result = instrumentBuilder
                         .Update(instrument)
                         .Build(StubZonedDateTime.UnixEpoch());

            // Assert
            Assert.Equal(8, instrumentBuilder.Changes.Count);
            Assert.Equal(Quantity.Create(10000), result.MaxTradeSize);
            Assert.Equal(45, result.RolloverInterestBuy);
            Assert.Equal(45, result.RolloverInterestSell);

            foreach (var change in instrumentBuilder.Changes)
            {
                this.Output.WriteLine(change);
            }
        }
示例#18
0
        internal void GetTicks_WithToDateTime_ReturnsTickInRange()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();

            this.repository.Update(audusd);

            var tick1 = StubQuoteTickProvider.Create(audusd.Symbol);
            var tick2 = StubQuoteTickProvider.Create(audusd.Symbol, StubZonedDateTime.UnixEpoch() + Duration.FromMilliseconds(1000));

            this.repository.Ingest(tick1);
            this.repository.Ingest(tick2);

            // Act
            // var result = this.repository.GetTicks(audusd.Symbol, null, StubZonedDateTime.UnixEpoch(), null);

            // Assert
            // TODO: System.InvalidCastException : Specified cast is not valid. (inside StackExchange.Redis)
            // Assert.Equal(2, this.repository.TicksCount(audusd.Symbol));
            // Assert.Single(result);
            // Assert.Equal(tick1, result[0]);
        }
        internal void Add_WithMultipleInstruments_AddsToRepository()
        {
            // Arrange
            var instrument1 = StubInstrumentProvider.AUDUSD();
            var instrument2 = StubInstrumentProvider.EURUSD();
            var instrument3 = StubInstrumentProvider.USDJPY();

            this.repository.Add(instrument1);
            this.repository.Add(instrument2);
            this.repository.Add(instrument3);

            // Act
            this.repository.CacheAll();
            var result1 = this.repository.GetInstrument(instrument1.Symbol);
            var result2 = this.repository.GetInstrument(instrument2.Symbol);
            var result3 = this.repository.GetInstrument(instrument3.Symbol);

            // Assert
            Assert.Equal(instrument1, result1.Value);
            Assert.Equal(instrument2, result2.Value);
            Assert.Equal(instrument3, result3.Value);
        }
示例#20
0
        internal void GetMidBars_WithTicksCreatingBar_ReturnsExpectedBar()
        {
            // Arrange
            var audusd = StubInstrumentProvider.AUDUSD();

            // var barType = StubBarType.AUDUSD_OneMinuteMid();
            this.repository.Update(audusd);

            var tick0 = new QuoteTick(
                audusd.Symbol,
                Price.Create(1.00001, 5),
                Price.Create(1.00010, 5),
                Quantity.One(),
                Quantity.One(),
                StubZonedDateTime.UnixEpoch());

            var tick1 = new QuoteTick(
                audusd.Symbol,
                Price.Create(1.00000, 5),
                Price.Create(1.00010, 5),
                Quantity.One(),
                Quantity.One(),
                StubZonedDateTime.UnixEpoch() + Duration.FromSeconds(60));

            var tick2 = new QuoteTick(
                audusd.Symbol,
                Price.Create(1.00030, 5),
                Price.Create(1.00040, 5),
                Quantity.One(),
                Quantity.One(),
                StubZonedDateTime.UnixEpoch() + Duration.FromSeconds(62));

            var tick3 = new QuoteTick(
                audusd.Symbol,
                Price.Create(0.99980, 5),
                Price.Create(0.99990, 5),
                Quantity.One(),
                Quantity.One(),
                StubZonedDateTime.UnixEpoch() + Duration.FromSeconds(63));

            var tick4 = new QuoteTick(
                audusd.Symbol,
                Price.Create(1.00001, 5),
                Price.Create(1.00004, 5),
                Quantity.One(),
                Quantity.One(),
                StubZonedDateTime.UnixEpoch() + Duration.FromSeconds(119));

            var tick5 = new QuoteTick(
                audusd.Symbol,
                Price.Create(1.00001, 5),
                Price.Create(1.00011, 5),
                Quantity.One(),
                Quantity.One(),
                StubZonedDateTime.UnixEpoch() + Duration.FromSeconds(121));

            this.repository.Ingest(tick0);
            this.repository.Ingest(tick1);
            this.repository.Ingest(tick2);
            this.repository.Ingest(tick3);
            this.repository.Ingest(tick4);
            this.repository.Ingest(tick5);

            // var expected = new Bar(
            //     Price.Create(1.000050, 6),
            //     Price.Create(1.000350, 6),
            //     Price.Create(0.999850, 6),
            //     Price.Create(1.000025, 6),
            //     Quantity.Create(8),
            //     StubZonedDateTime.UnixEpoch() + Duration.FromSeconds(120));

            // Act
            // var result = this.repository.GetBars(barType, null, null, 1);

            // Assert
            // TODO: System.InvalidCastException : Specified cast is not valid. (inside StackExchange.Redis)
            // Assert.Equal(6, this.repository.TicksCount(audusd.Symbol));
            // Assert.True(this.repository.BarsExist(barType));
            // Assert.Equal(2, this.repository.BarsCount(barType));
            // Assert.Single(result.Bars);
            // Assert.Equal(expected, result.Bars[0]);
        }