private void ConsumeBridge(IDataFeed feed, TimeSpan timeout, bool alwaysInvoke, Action <TimeSlice> handler, bool noOutput = true) { var endTime = DateTime.UtcNow.Add(timeout); bool startedReceivingata = false; foreach (var timeSlice in feed) { if (!noOutput) { ConsoleWriteLine("\r\n" + $"Now (EDT): {DateTime.UtcNow.ConvertFromUtc(TimeZones.NewYork):o}" + $". TimeSlice.Time (EDT): {timeSlice.Time.ConvertFromUtc(TimeZones.NewYork):o}"); } if (!startedReceivingata && timeSlice.Slice.Count != 0) { startedReceivingata = true; } if (startedReceivingata || alwaysInvoke) { handler(timeSlice); } _algorithm.OnEndOfTimeStep(); _manualTimeProvider.AdvanceSeconds(1); if (endTime <= DateTime.UtcNow) { feed.Exit(); return; } } }
public void RemovalFromUniverseAndDataFeedMakesSecurityNotTradable() { SymbolCache.Clear(); var algorithm = new AlgorithmStub(new MockDataFeedWithSubscription()); var orderProcessorMock = new Mock <IOrderProcessor>(); orderProcessorMock.Setup(m => m.GetOpenOrders(It.IsAny <Func <Order, bool> >())).Returns(new List <Order>()); algorithm.Transactions.SetOrderProcessor(orderProcessorMock.Object); algorithm.SetStartDate(2012, 3, 27); algorithm.SetEndDate(2012, 3, 30); algorithm.AddUniverse("my-custom-universe", dt => dt.Day < 30 ? new List <string> { "CPRT" } : Enumerable.Empty <string>()); // OnEndOfTimeStep will add all pending universe additions algorithm.OnEndOfTimeStep(); var universe = algorithm.UniverseManager.Values.First(); var securityChanges = algorithm.DataManager.UniverseSelection.ApplyUniverseSelection( universe, algorithm.EndDate.ConvertToUtc(algorithm.TimeZone).Subtract(TimeSpan.FromDays(2)), new BaseDataCollection( algorithm.UtcTime, Symbol.Create("CPRT", SecurityType.Equity, Market.USA), new List <BaseData>() ) ); Assert.AreEqual(1, securityChanges.AddedSecurities.Count); Assert.AreEqual(0, securityChanges.RemovedSecurities.Count); var security = securityChanges.AddedSecurities.First(); Assert.IsTrue(security.IsTradable); securityChanges = algorithm.DataManager.UniverseSelection.ApplyUniverseSelection( universe, algorithm.EndDate.ConvertToUtc(algorithm.TimeZone), new BaseDataCollection( algorithm.UtcTime, Symbol.Create("CPRT", SecurityType.Equity, Market.USA), new List <BaseData>() ) ); Assert.AreEqual(0, securityChanges.AddedSecurities.Count); Assert.AreEqual(1, securityChanges.RemovedSecurities.Count); Assert.AreEqual(security.Symbol, securityChanges.RemovedSecurities.First().Symbol); Assert.IsFalse(security.IsTradable); }
private void ConsumeBridge(IDataFeed feed, TimeSpan timeout, bool alwaysInvoke, Action <TimeSlice> handler, bool noOutput = true, bool sendUniverseData = false) { var endTime = DateTime.UtcNow.Add(timeout); bool startedReceivingata = false; var cancellationTokenSource = new CancellationTokenSource(); foreach (var timeSlice in _synchronizer.StreamData(cancellationTokenSource.Token)) { if (!noOutput) { ConsoleWriteLine("\r\n" + $"Now (EDT): {DateTime.UtcNow.ConvertFromUtc(TimeZones.NewYork):o}" + $". TimeSlice.Time (EDT): {timeSlice.Time.ConvertFromUtc(TimeZones.NewYork):o}"); } if (!startedReceivingata && (timeSlice.Slice.Count != 0 || sendUniverseData && timeSlice.UniverseData.Count > 0)) { startedReceivingata = true; } if (startedReceivingata || alwaysInvoke) { handler(timeSlice); } _algorithm.OnEndOfTimeStep(); _manualTimeProvider.AdvanceSeconds(1); Thread.Sleep(1); if (endTime <= DateTime.UtcNow) { feed.Exit(); cancellationTokenSource.Cancel(); return; } } }
public void CoarseFundamentalHasFundamentalDataFalseExcludedInFineUniverseSelection() { var algorithm = new AlgorithmStub(new MockDataFeed()); algorithm.SetEndDate(Time.EndOfTime); algorithm.SetStartDate(DateTime.UtcNow.Subtract(TimeSpan.FromDays(10))); algorithm.AddUniverse( coarse => coarse.Select(c => c.Symbol), fine => fine.Select(f => f.Symbol) ); // OnEndOfTimeStep will add all pending universe additions algorithm.OnEndOfTimeStep(); var universe = algorithm.UniverseManager.Values.First(); var securityChanges = algorithm.DataManager.UniverseSelection.ApplyUniverseSelection( universe, algorithm.EndDate.ConvertToUtc(algorithm.TimeZone).Subtract(TimeSpan.FromDays(1)), new BaseDataCollection( DateTime.UtcNow, Symbols.AAPL, new[] { new CoarseFundamental { Symbol = Symbols.AAPL, HasFundamentalData = true }, new CoarseFundamental { Symbol = Symbols.SPY, HasFundamentalData = false } } ) ); Assert.AreEqual(1, securityChanges.Count); Assert.AreEqual(Symbols.AAPL, securityChanges.AddedSecurities.First().Symbol); }
public void CreatedEquityIsNotAddedToSymbolCache() { SymbolCache.Clear(); var algorithm = new AlgorithmStub(new MockDataFeed()); algorithm.SetEndDate(Time.EndOfTime); algorithm.SetStartDate(DateTime.UtcNow.Subtract(TimeSpan.FromDays(10))); algorithm.AddUniverse(CoarseSelectionFunction, FineSelectionFunction); // OnEndOfTimeStep will add all pending universe additions algorithm.OnEndOfTimeStep(); var universe = algorithm.UniverseManager.Values.First(); var securityChanges = algorithm.DataManager.UniverseSelection.ApplyUniverseSelection( universe, algorithm.EndDate.ConvertToUtc(algorithm.TimeZone).Subtract(TimeSpan.FromDays(1)), new BaseDataCollection( DateTime.UtcNow, Symbols.AAPL, new[] { new CoarseFundamental { Symbol = Symbols.AAPL, HasFundamentalData = true }, new CoarseFundamental { Symbol = Symbols.SPY, HasFundamentalData = false } } ) ); Symbol symbol; Assert.AreEqual(1, securityChanges.AddedSecurities.Count); Assert.AreEqual(Symbols.AAPL, securityChanges.AddedSecurities.First().Symbol); Assert.IsFalse(SymbolCache.TryGetSymbol("AAPL", out symbol)); Assert.IsFalse(SymbolCache.TryGetSymbol("SPY", out symbol)); }