Exemplo n.º 1
0
        public void ResetTests(SecurityCache cache, SecuritySeedData seedType)
        {
            switch (seedType)
            {
            case SecuritySeedData.None:
            case SecuritySeedData.OpenInterest:
            case SecuritySeedData.OpenInterestTick:
                break;

            case SecuritySeedData.QuoteTick:
                Assert.IsNotNull(cache.GetData());
                Assert.Greater(cache.GetAll <Tick>().Count(x => x.TickType == TickType.Quote), 0);
                cache.Reset();
                Assert.IsFalse(cache.HasData(typeof(Tick)));
                Assert.AreEqual(cache.GetAll <Tick>().Count(x => x.TickType == TickType.Quote), 0);
                break;

            case SecuritySeedData.TradeTick:
                Assert.IsNotNull(cache.GetData());
                Assert.Greater(cache.GetAll <Tick>().Count(x => x.TickType == TickType.Trade), 0);
                cache.Reset();
                Assert.IsFalse(cache.HasData(typeof(Tick)));
                Assert.AreEqual(cache.GetAll <Tick>().Count(x => x.TickType == TickType.Trade), 0);
                break;

            default:
                Assert.IsNotNull(cache.GetData());
                cache.Reset();
                break;
            }
        }
Exemplo n.º 2
0
        public void StoreData_SecurityCacheHasTradeAndQuoteTick()
        {
            // Arrange
            var securityCache = new SecurityCache();
            var time          = DateTime.Now;

            var quote = new Tick(time, Symbol.Empty, 100, 102)
            {
                TickType = TickType.Quote
            };

            securityCache.StoreData(new[] { quote }, typeof(Tick));

            var trade = new Tick(time.AddMilliseconds(1), Symbol.Empty, 100, 100, 102)
            {
                TickType = TickType.Trade
            };

            securityCache.StoreData(new[] { trade }, typeof(Tick));

            // Adding OpenInterest as Tick or OpenInterest should not matter
            var openInterest = new OpenInterest(time, Symbol.Empty, 1000);

            securityCache.StoreData(new[] { openInterest }, typeof(Tick));           // Add as Tick
            securityCache.StoreData(new[] { openInterest }, typeof(OpenInterest));   // Add as OI

            // Assert
            Assert.IsTrue(securityCache.HasData(typeof(Tick)));
            Assert.True(securityCache.GetData <Tick>().Equals(trade));
            Assert.True(securityCache.GetData <OpenInterest>().Equals(openInterest));

            Assert.True(securityCache.GetAll <Tick>().LastOrDefault(x => x.TickType == TickType.Quote).Equals(quote));
            Assert.True(securityCache.GetAll <Tick>().LastOrDefault(x => x.TickType == TickType.Trade).Equals(trade));
        }
Exemplo n.º 3
0
        public void AddData_SecurityCacheHasTradeAndQuoteTick()
        {
            // Arrange
            var securityCache = new SecurityCache();
            var time          = DateTime.Now;

            var quote = new Tick(time, Symbol.Empty, 100, 102)
            {
                TickType = TickType.Quote
            };

            securityCache.AddData(quote);

            var trade = new Tick(time, Symbol.Empty, 100, 100, 102)
            {
                TickType = TickType.Trade
            };

            securityCache.AddData(trade);

            var openInterest = new OpenInterest(time, Symbol.Empty, 1000);

            securityCache.AddData(openInterest);

            // Assert
            Assert.True(securityCache.GetData().Equals(trade));
            Assert.True(securityCache.GetData <Tick>().Equals(trade));

            Assert.True(securityCache.GetAll <Tick>().LastOrDefault(x => x.TickType == TickType.Quote).Equals(quote));
            Assert.True(securityCache.GetAll <Tick>().LastOrDefault(x => x.TickType == TickType.Trade).Equals(trade));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Advances the enumerator to the next element of the collection.
        /// </summary>
        /// <returns> true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
        /// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created.</exception>
        public bool MoveNext()
        {
            var currentDate = _timeProvider.GetUtcNow().ConvertFromUtc(_dataConfig.ExchangeTimeZone).Date;

            if (currentDate != _lastTime)
            {
                // when the date changes for the security we trigger a new tradable date event
                var newDayEvent = new NewTradableDateEventArgs(currentDate, _securityCache.GetData(), _dataConfig.Symbol, null);
                foreach (var delistingEvent in _delistingEventProvider.GetEvents(newDayEvent))
                {
                    _dataToEmit.Enqueue(delistingEvent);
                }

                // update last time
                _lastTime = currentDate;
            }

            if (_dataToEmit.Count > 0)
            {
                // emit event if any
                Current = _dataToEmit.Dequeue();
                return(true);
            }

            Current = null;
            return(false);
        }
        public void GivenSameTimeStampForTradeBarAndQuoteQuotebarPrioritizeQuoteBar()
        {
            // Arrange
            var securityCache = new SecurityCache();
            var time          = DateTime.Now;
            var quotes        = GenerateData(MarketDataType.QuoteBar, 5, false, time);
            var trades        = GenerateData(MarketDataType.TradeBar, 5, false, time);
            var data          = quotes.Concat(trades);

            data = data.OrderBy(d => d.EndTime);
            // Act
            foreach (var baseData in data)
            {
                securityCache.AddData(baseData);
            }
            // Assert
            Assert.True(securityCache.GetData().Equals(quotes.Last()));
            Assert.True(securityCache.GetData <TradeBar>().Equals(trades.Last()));
        }
        public void UseLatestTradebarIfThereIsntAvailableQuotebar()
        {
            // Arrange
            var securityCache = new SecurityCache();
            var time          = DateTime.Now;
            var quotes        = GenerateData(MarketDataType.QuoteBar, 5, false, time);

            foreach (var baseData in quotes)
            {
                securityCache.AddData(baseData);
            }
            // Add one last tradebar with a later timestamp
            var laterTrade = GenerateData(MarketDataType.TradeBar, 1, true, quotes.Last().Time.AddSeconds(1)).First();

            // Act
            securityCache.AddData(laterTrade);
            // Assert
            Assert.True(securityCache.GetData().Equals(laterTrade));
            Assert.True(securityCache.GetData <QuoteBar>().Equals(quotes.Last()));
        }
Exemplo n.º 7
0
        public void StoreData_TargetToModify_SecurityCacheHasTradeAndQuoteTick()
        {
            // Arrange
            var sourceToShare = new SecurityCache();
            var time          = DateTime.Now;

            var quote = new Tick(time, Symbol.Empty, 100, 102)
            {
                TickType = TickType.Quote
            };

            sourceToShare.StoreData(new[] { quote }, typeof(Tick));

            var trade = new Tick(time, Symbol.Empty, 100, 100, 102)
            {
                TickType = TickType.Trade
            };

            sourceToShare.StoreData(new[] { trade }, typeof(Tick));

            // Adding OpenInterest as Tick or OpenInterest should not matter
            var openInterest = new OpenInterest(time, Symbol.Empty, 1000);

            sourceToShare.StoreData(new[] { openInterest }, typeof(Tick));           // Add as Tick
            sourceToShare.StoreData(new[] { openInterest }, typeof(OpenInterest));   // Add as OI

            var targetToModify = new SecurityCache();

            SecurityCache.ShareTypeCacheInstance(sourceToShare, targetToModify);

            // Assert
            Assert.IsTrue(targetToModify.HasData(typeof(Tick)));
            Assert.True(targetToModify.GetData <Tick>().Equals(trade));
            Assert.True(targetToModify.GetData <OpenInterest>().Equals(openInterest));

            Assert.True(targetToModify.GetAll <Tick>().LastOrDefault(x => x.TickType == TickType.Quote).Equals(quote));
            Assert.True(targetToModify.GetAll <Tick>().LastOrDefault(x => x.TickType == TickType.Trade).Equals(trade));
        }
Exemplo n.º 8
0
        public void AlwaysReturnTheLastData(MarketDataType marketDataType, int quantity, bool sameTime)
        {
            // Arrange
            var securityCache = new SecurityCache();
            var quotes        = GenerateData(marketDataType, quantity, sameTime);

            // Act
            foreach (var quoteBar in quotes)
            {
                securityCache.AddData(quoteBar);
            }
            // Assert
            Assert.True(securityCache.GetData().Equals(quotes.Last()));
        }
        public override bool MoveNext()
        {
            var currentDate = _timeProvider.GetUtcNow().ConvertFromUtc(Config.ExchangeTimeZone).Add(-Time.LiveAuxiliaryDataOffset).Date;

            if (currentDate != _lastTime)
            {
                // when the date changes for the security we trigger a new tradable date event
                var newDayEvent = new NewTradableDateEventArgs(currentDate, _securityCache.GetData(), Config.Symbol, null);

                NewTradableDate(this, newDayEvent);
                // update last time
                _lastTime = currentDate;
            }

            return(base.MoveNext());
        }
        public void AlwaysReturnTheLastData(MarketDataType marketDataType, int quantity, bool sameTime)
        {
            // Arrange
            var securityCache = new SecurityCache();
            var quotes        = GenerateData(marketDataType, quantity, sameTime);

            // Act
            foreach (var quoteBar in quotes)
            {
                quoteBar.Symbol = Symbols.SPY;
                securityCache.AddData(quoteBar);
            }

            var lastData = securityCache.GetData();

            if (marketDataType == MarketDataType.QuoteBar)
            {
                Assert.IsNull(lastData);
            }
            else
            {
                Assert.True(lastData.Equals(quotes.Last()));
            }
        }
        public void AddDataEquity_OHLC_IgnoresQuoteBar()
        {
            var securityCache = new SecurityCache();
            var quoteBar      = new QuoteBar
            {
                Bid         = new Bar(101, 102, 103, 104),
                Ask         = new Bar(105, 106, 107, 108),
                LastAskSize = 109,
                LastBidSize = 110,
                EndTime     = ReferenceTime,
                Symbol      = Symbols.SPY
            };

            securityCache.AddData(quoteBar);

            var last = securityCache.GetData();

            Assert.IsNull(last);

            Assert.AreEqual(0, securityCache.High);
            Assert.AreEqual(0, securityCache.Close);
            Assert.AreEqual(0, securityCache.Low);
            Assert.AreEqual(0, securityCache.Open);
            Assert.AreEqual(0, securityCache.Volume);

            var actualQuoteBar = securityCache.GetData <QuoteBar>();

            Assert.IsNotNull(actualQuoteBar);
            Assert.AreEqual(108, securityCache.AskPrice);
            Assert.AreEqual(109, securityCache.AskSize);
            Assert.AreEqual(104, securityCache.BidPrice);
            Assert.AreEqual(110, securityCache.BidSize);

            var tradeBar = new TradeBar
            {
                Open    = 101,
                High    = 102,
                Low     = 103,
                Close   = 104,
                Volume  = 105,
                EndTime = ReferenceTime,
                Symbol  = Symbols.SPY
            };

            securityCache.AddData(tradeBar);

            last = securityCache.GetData();
            Assert.IsNotNull(last);

            var actualTradeBar = securityCache.GetData <TradeBar>();

            Assert.IsNotNull(actualTradeBar);
            Assert.AreEqual(102, securityCache.High);
            Assert.AreEqual(104, securityCache.Close);
            Assert.AreEqual(103, securityCache.Low);
            Assert.AreEqual(101, securityCache.Open);
            Assert.AreEqual(105, securityCache.Volume);

            // quote bar data should still be the same
            Assert.AreEqual(108, securityCache.AskPrice);
            Assert.AreEqual(109, securityCache.AskSize);
            Assert.AreEqual(104, securityCache.BidPrice);
            Assert.AreEqual(110, securityCache.BidSize);
        }