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)); }
public void EmitsDelistingEventsBasedOnCurrentTime() { var config = new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY_C_192_Feb19_2016, Resolution.Daily, TimeZones.NewYork, TimeZones.NewYork, true, true, false); var delistingDate = config.Symbol.GetDelistingDate(); var time = delistingDate.AddDays(-10); var cache = new SecurityCache(); cache.AddData(new Tick(DateTime.UtcNow, config.Symbol, 20, 10)); var timeProvider = new ManualTimeProvider(time); IEnumerator <BaseData> enumerator; Assert.IsTrue(LiveAuxiliaryDataEnumerator.TryCreate(config, timeProvider, null, cache, TestGlobals.MapFileProvider, TestGlobals.FactorFileProvider, config.Symbol.ID.Date, out enumerator)); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); // advance until delisting date, take into account 5 hour offset of NY + TradableDateOffset timeProvider.Advance(TimeSpan.FromDays(10)); timeProvider.Advance(TimeSpan.FromHours(5)); timeProvider.Advance(Time.LiveAuxiliaryDataOffset); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(DelistingType.Warning, (enumerator.Current as Delisting).Type); Assert.AreEqual(config.Symbol, (enumerator.Current as Delisting).Symbol); Assert.AreEqual(delistingDate, (enumerator.Current as Delisting).Time); Assert.AreEqual(15, (enumerator.Current as Delisting).Price); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); // when the day ends the delisted event will pass through, respecting the offset timeProvider.Advance(TimeSpan.FromDays(1)); cache.AddData(new Tick(DateTime.UtcNow, config.Symbol, 40, 20)); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual(DelistingType.Delisted, (enumerator.Current as Delisting).Type); Assert.AreEqual(config.Symbol, (enumerator.Current as Delisting).Symbol); Assert.AreEqual(delistingDate.AddDays(1), (enumerator.Current as Delisting).Time); Assert.AreEqual(30, (enumerator.Current as Delisting).Price); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); }
public void TickTypeDependencyTests() { // Arrange var time = DateTime.Now; var price = 100m; var bidPrice = 99m; var askPrice = 101m; var volume = 1m; var tick = new Tick(time, Symbols.AAPL, price, bidPrice, askPrice) { Quantity = volume };; var securityCache = new SecurityCache(); securityCache.AddData(tick); Assert.AreEqual(securityCache.Price, price); Assert.AreEqual(securityCache.BidPrice, bidPrice); Assert.AreEqual(securityCache.AskPrice, askPrice); Assert.AreEqual(securityCache.Volume, 0m); tick.TickType = TickType.Trade; securityCache = new SecurityCache(); securityCache.AddData(tick); Assert.AreEqual(securityCache.Price, price); Assert.AreEqual(securityCache.BidPrice, 0m); Assert.AreEqual(securityCache.AskPrice, 0m); Assert.AreEqual(securityCache.Volume, volume); }
private TestCaseData[] GetSecurityCacheInitialStates() { var defaultInstance = new SecurityCache(); var tradeTick = new SecurityCache(); tradeTick.AddData(new Tick { Value = 5, Quantity = 6, Time = ReferenceTime, TickType = TickType.Trade }); var quoteTick = new SecurityCache(); quoteTick.AddData(new Tick { AskPrice = 1, AskSize = 2, BidPrice = 3, BidSize = 4, Time = ReferenceTime, TickType = TickType.Quote }); var tradeBar = new SecurityCache(); tradeBar.AddData(new TradeBar { Open = 7, High = 8, Low = 9, Close = 10, Volume = 11, EndTime = ReferenceTime }); var quoteBar = new SecurityCache(); quoteBar.AddData(new QuoteBar { Ask = new Bar(12, 13, 14, 15), Bid = new Bar(16, 17, 18, 19), LastAskSize = 20, LastBidSize = 21, Value = 22, EndTime = ReferenceTime }); return(new[] { new TestCaseData(defaultInstance, SecuritySeedData.None).SetName("Default Instance"), new TestCaseData(tradeTick, SecuritySeedData.TradeTick).SetName("Seeded w/ Trade Tick"), new TestCaseData(quoteTick, SecuritySeedData.QuoteTick).SetName("Seeded w/ Quote Tick"), new TestCaseData(tradeBar, SecuritySeedData.TradeBar).SetName("Seeded w/ TradeBar"), new TestCaseData(quoteBar, SecuritySeedData.QuoteBar).SetName("Seeded w/ QuoteBar"), }); }
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())); }
public void TickQuantityUpdatedInSecurityCache() { var tick1 = new Tick(); tick1.Update(1, 1, 1, 10, 1, 1); var tick2 = new Tick(); tick2.Update(1, 1, 1, 20, 1, 1); var securityCache = new SecurityCache(); Assert.AreEqual(0, securityCache.Volume); securityCache.AddData(tick1); Assert.AreEqual(10, securityCache.Volume); securityCache.AddData(tick2); Assert.AreEqual(20, securityCache.Volume); }
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())); }
private void AddDataAndAssertChanges(SecurityCache cache, SecuritySeedData seedType, SecuritySeedData dataType, BaseData data, Dictionary <string, string> cacheToBaseDataPropertyMap = null) { var before = JObject.FromObject(cache); var dataSnapshot = JObject.FromObject(data); cache.AddData(data); var after = JObject.FromObject(cache); var updatedCacheProperties = GetPropertiesBy(dataType); if (seedType == SecuritySeedData.QuoteBar && (dataType == SecuritySeedData.QuoteBar || dataType == SecuritySeedData.TradeBar)) { // these properties aren't updated when previous data is quote bar at same time as a new IBar updatedCacheProperties = updatedCacheProperties.Where(p => p != "Open" && p != "High" && p != "Low" && p != "Close" && p != "Price" ).ToArray(); } foreach (var property in before.Properties()) { string dataPropertyName = null; if (updatedCacheProperties.Contains(property.Name)) { if (cacheToBaseDataPropertyMap?.TryGetValue(property.Name, out dataPropertyName) == true) { // avoiding failures due to decimal <> long in JToken.DeepEquals var e = dataSnapshot.SelectToken(dataPropertyName).ToString(); var a = after.SelectToken(property.Name).ToString(); Assert.AreEqual(e, a, $"{property.Name}: Expected {e}. Actual {a}"); } else { dataSnapshot.IsEqualTo(after, property.Name); } } else { before.IsEqualTo(after, property.Name); } } }
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 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); }
public void EmitsMappingEventsBasedOnCurrentMapFileAndTime(DataMappingMode dataMappingMode, string mappingDate, bool delayed) { var config = new SubscriptionDataConfig(typeof(TradeBar), Symbols.ES_Future_Chain, Resolution.Daily, TimeZones.NewYork, TimeZones.NewYork, true, true, false, dataMappingMode: dataMappingMode); var symbolMaps = new List <SubscriptionDataConfig.NewSymbolEventArgs>(); config.NewSymbol += (sender, args) => symbolMaps.Add(args); var time = new DateTime(2013, 05, 28); var cache = new SecurityCache(); cache.AddData(new Tick(time, config.Symbol, 20, 10)); var timeProvider = new ManualTimeProvider(time); var futureTicker1 = "es vhle2yxr5blt"; TestMapFileResolver.MapFile = new MapFile(Futures.Indices.SP500EMini, new [] { new MapFileRow(Time.BeginningOfTime, Futures.Indices.SP500EMini, Exchange.CME), new MapFileRow(new DateTime(2013, 06, 01), futureTicker1, Exchange.CME, DataMappingMode.FirstDayMonth), new MapFileRow(new DateTime(2013, 06, 15), futureTicker1, Exchange.CME, DataMappingMode.OpenInterest), new MapFileRow(new DateTime(2013, 06, 22), futureTicker1, Exchange.CME, DataMappingMode.LastTradingDay), }); IEnumerator <BaseData> enumerator; Assert.IsTrue(LiveAuxiliaryDataEnumerator.TryCreate(config, timeProvider, null, cache, new TestMapFileProvider(), TestGlobals.FactorFileProvider, time, out enumerator)); // get's mapped right away! Assert.AreEqual(futureTicker1.ToUpper(), config.MappedSymbol); Assert.AreEqual(1, symbolMaps.Count); Assert.AreEqual(Symbols.ES_Future_Chain, symbolMaps[0].Old); Assert.AreEqual(Futures.Indices.SP500EMini, symbolMaps[0].Old.ID.Symbol); Assert.AreEqual(Symbols.ES_Future_Chain, symbolMaps[0].New); Assert.AreEqual(futureTicker1.ToUpper(), symbolMaps[0].New.Underlying.ID.ToString()); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); var expectedMappingDate = DateTime.ParseExact(mappingDate, DateFormat.EightCharacter, CultureInfo.InvariantCulture); if (delayed) { // we advance to the mapping date, without any new mapFile! timeProvider.Advance(expectedMappingDate.ConvertToUtc(config.ExchangeTimeZone) - timeProvider.GetUtcNow() + Time.LiveAuxiliaryDataOffset); } else { // just advance a day to show nothing happens until mapping time timeProvider.Advance(TimeSpan.FromDays(1)); } Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); var futureTicker2 = "es vk2zrh843z7l"; TestMapFileResolver.MapFile = new MapFile(Futures.Indices.SP500EMini, TestMapFileResolver.MapFile.Concat( new [] { new MapFileRow(new DateTime(2013, 09, 01), futureTicker2, Exchange.CME, DataMappingMode.FirstDayMonth), new MapFileRow(new DateTime(2013, 09, 14), futureTicker2, Exchange.CME, DataMappingMode.OpenInterest), new MapFileRow(new DateTime(2013, 09, 21), futureTicker2, Exchange.CME, DataMappingMode.LastTradingDay), })); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); if (delayed) { // we got a new mapFile! advance the date and expect mapping to have happened timeProvider.Advance(TimeSpan.FromDays(1)); } else { // we advance to the mapping date timeProvider.Advance(expectedMappingDate.ConvertToUtc(config.ExchangeTimeZone) - timeProvider.GetUtcNow() + Time.LiveAuxiliaryDataOffset); } Assert.IsTrue(enumerator.MoveNext()); Assert.IsNotNull(enumerator.Current); Assert.AreEqual(2, symbolMaps.Count); Assert.AreEqual(Symbols.ES_Future_Chain, symbolMaps[1].Old); Assert.AreEqual(futureTicker1.ToUpper(), symbolMaps[1].Old.Underlying.ID.ToString()); Assert.AreEqual(Symbols.ES_Future_Chain, symbolMaps[1].New); Assert.AreEqual(futureTicker2.ToUpper(), symbolMaps[1].New.Underlying.ID.ToString()); Assert.AreEqual(futureTicker2.ToUpper(), config.MappedSymbol); Assert.AreEqual(futureTicker2.ToUpper(), (enumerator.Current as SymbolChangedEvent).NewSymbol); Assert.AreEqual(futureTicker1.ToUpper(), (enumerator.Current as SymbolChangedEvent).OldSymbol); Assert.AreEqual(config.Symbol, (enumerator.Current as SymbolChangedEvent).Symbol); Assert.AreEqual(timeProvider.GetUtcNow().Date, (enumerator.Current as SymbolChangedEvent).Time); Assert.IsTrue(enumerator.MoveNext()); Assert.IsNull(enumerator.Current); }