Exemplo n.º 1
0
        private void ListenForPricesAndTrades()
        {
            using (LogContext.PushProperty("InstanceId", InstanceId))
            {
                Log.Information("Subscribing to prices topic...");

                _subscriptions.Add(_broker.SubscribeToTopic <SpotPriceDto>("prices")
                                   .Subscribe(p => _service.OnPrice(p)));

                Log.Information("Subscribed to prices topic");

                Log.Information("Subscribing to trades...");

                _subscriptions.Add(_tradeCache.GetTrades()
                                   .SelectMany(t => t.Trades)
                                   .Where(t => t.Status == TradeStatusDto.Done)
                                   .Subscribe(t => _service.OnTrade(t)));

                _subscriptions.Add(_tradeCache.GetTrades()
                                   .SkipWhile(t => t.IsStale)
                                   .Where(t => t.IsStale && t.IsStateOfTheWorld && t.Trades.Count == 0)
                                   .Subscribe(t => _service.OnReset()));

                Log.Information("Subscribed to trades");
            }
        }
Exemplo n.º 2
0
        public void TradeCache_AddTrades_Resize()
        {
            // Arrange
            int incrementalSize = 5;
            var tradeCache      = new TradeCache <TestTradeCreator, TestTrade, TestTradeParameters>(incrementalSize);
            var trade1          = new TestTrade {
                Id = 1, Price = 0123m, Quantity = 1000m, Time = new DateTime(2000, 1, 1, 1, 0, 1)
            };
            var trade2 = new TestTrade {
                Id = 2, Price = 0121m, Quantity = 500m, Time = new DateTime(2000, 1, 1, 1, 0, 2)
            };
            var trade3 = new TestTrade {
                Id = 3, Price = 0124m, Quantity = 750m, Time = new DateTime(2000, 1, 1, 1, 0, 3)
            };
            var trade4 = new TestTrade {
                Id = 4, Price = 0122m, Quantity = 2000m, Time = new DateTime(2000, 1, 1, 1, 0, 4)
            };
            var trade5 = new TestTrade {
                Id = 5, Price = 0120m, Quantity = 1500m, Time = new DateTime(2000, 1, 1, 1, 0, 5)
            };
            var trade6 = new TestTrade {
                Id = 6, Price = 0119m, Quantity = 2750m, Time = new DateTime(2000, 1, 1, 1, 0, 6)
            };

            // Act
            tradeCache.Add(trade1);
            tradeCache.Add(trade2);
            tradeCache.Add(trade3);
            tradeCache.Add(trade4);
            tradeCache.Add(trade5);
            tradeCache.Add(trade6);

            var trades          = tradeCache.GetTrades();
            var lastTrade       = tradeCache.GetLastTrade();
            var lastTradesOver  = tradeCache.GetLastTrades(10);
            var lastTradesUnder = tradeCache.GetLastTrades(2);

            // Assert
            Assert.AreEqual(tradeCache.Position, 5);
            Assert.AreEqual(tradeCache.CacheSize, 10);
            Assert.AreEqual(tradeCache.TradeRange, 5);
            Assert.AreEqual(trades.Length, 6);

            Assert.AreEqual(lastTrade, trade6);

            Assert.AreEqual(lastTradesOver.Length, 6);
            Assert.AreEqual(lastTradesOver[0], trade1);
            Assert.AreEqual(lastTradesOver[1], trade2);
            Assert.AreEqual(lastTradesOver[2], trade3);
            Assert.AreEqual(lastTradesOver[3], trade4);
            Assert.AreEqual(lastTradesOver[4], trade5);
            Assert.AreEqual(lastTradesOver[5], trade6);

            Assert.AreEqual(lastTradesUnder.Length, 2);
            Assert.AreEqual(lastTradesUnder[0], trade5);
            Assert.AreEqual(lastTradesUnder[1], trade6);
        }
        public IDisposable Initialize(IObservable <IConnected <IBroker> > brokerStream, IObservable <IConnected <IEventStoreConnection> > eventStore)
        {
            _cache   = new TradeCache(eventStore);
            _service = new BlotterService(_cache.GetTrades());
            var disposable = brokerStream.LaunchOrKill(broker => new BlotterServiceHost(_service, broker)).Subscribe();

            _cleanup.Add(disposable);

            return(disposable);
        }
        public IDisposable Initialize(IObservable<IConnected<IBroker>> brokerStream, IObservable<IConnected<IEventStoreConnection>> eventStore)
        {
            _cache = new TradeCache(eventStore);
            _service = new BlotterService(_cache.GetTrades());
            var disposable = brokerStream.LaunchOrKill(broker => new BlotterServiceHost(_service, broker)).Subscribe();

            _cleanup.Add(disposable);

            return disposable;
        }
        private void ListenForPricesAndTrades()
        {
            Log.Information("Subscribing to prices topic...");

            _subscriptions.Add(_broker.SubscribeToTopic <SpotPriceDto>("prices")
                               .Subscribe(p => _service.OnPrice(p)));

            Log.Information("Subscribed to prices topic");

            Log.Information("Subscribing to trades...");

            _subscriptions.Add(_tradeCache.GetTrades()
                               .SelectMany(t => t.Trades)
                               .Where(t => t.Status == TradeStatusDto.Done)
                               .Subscribe(t => _service.OnTrade(t)));

            Log.Information("Subscribed to trades");
        }
Exemplo n.º 6
0
        public void TradeCache_Initialise()
        {
            // Arrange
            int incrementalSize = 5;
            TradeCache <TestTradeCreator, TestTrade, TestTradeParameters> tradeCache;

            // Act
            tradeCache = new TradeCache <TestTradeCreator, TestTrade, TestTradeParameters>(incrementalSize);

            // Assert
            Assert.AreEqual(tradeCache.Position, -1);
            Assert.AreEqual(tradeCache.TradeRange, 5);
            Assert.AreEqual(tradeCache.CacheSize, incrementalSize);
            Assert.IsNull(tradeCache.GetLastTrade());
            Assert.IsNull(tradeCache.GetTrades());
            Assert.IsNull(tradeCache.GetLastTrades(1));
            Assert.IsNull(tradeCache.GetLastTrades(5));
        }
Exemplo n.º 7
0
        public void TradeCache_AddTradeRange_NoResize()
        {
            // Arrange
            int incrementalSize = 5;
            var tradeCache      = new TradeCache <TestTradeCreator, TestTrade, TestTradeParameters>(incrementalSize);
            var trade1          = new TestTrade {
                Id = 1, Price = 0123m, Quantity = 1000m, Time = new DateTime(2000, 1, 1, 1, 0, 1)
            };
            var trade2 = new TestTrade {
                Id = 2, Price = 0121m, Quantity = 500m, Time = new DateTime(2000, 1, 1, 1, 0, 2)
            };
            var trade3 = new TestTrade {
                Id = 3, Price = 0124m, Quantity = 750m, Time = new DateTime(2000, 1, 1, 1, 0, 3)
            };

            var tradeRange = new[] { trade1, trade2, trade3 };

            // Act
            tradeCache.AddRange(tradeRange);

            var trades          = tradeCache.GetTrades();
            var lastTrade       = tradeCache.GetLastTrade();
            var lastTradesOver  = tradeCache.GetLastTrades(4);
            var lastTradesUnder = tradeCache.GetLastTrades(2);

            // Assert
            Assert.AreEqual(tradeCache.Position, 2);
            Assert.AreEqual(tradeCache.CacheSize, 5);
            Assert.AreEqual(tradeCache.TradeRange, 5);
            Assert.AreEqual(trades.Length, 3);

            Assert.AreEqual(lastTrade, trade3);

            Assert.AreEqual(lastTradesOver.Length, 3);
            Assert.AreEqual(lastTradesOver[0], trade1);
            Assert.AreEqual(lastTradesOver[1], trade2);
            Assert.AreEqual(lastTradesOver[2], trade3);

            Assert.AreEqual(lastTradesUnder.Length, 2);
            Assert.AreEqual(lastTradesUnder[0], trade2);
            Assert.AreEqual(lastTradesUnder[1], trade3);
        }