public void DuplicateScheduledEventsAreBothFired() { var algorithm = new QCAlgorithm(); var handler = new BacktestingRealTimeHandler(); var timeLimitManager = new AlgorithmTimeLimitManager(TokenBucket.Null, TimeSpan.MaxValue); handler.Setup(algorithm, new AlgorithmNodePacket(PacketType.BacktestNode), null, null, timeLimitManager); algorithm.Schedule.SetEventSchedule(handler); var time = new DateTime(2018, 1, 1); algorithm.SetDateTime(time); var count1 = 0; var count2 = 0; algorithm.Schedule.On(algorithm.Schedule.DateRules.EveryDay(), algorithm.Schedule.TimeRules.Every(TimeSpan.FromHours(1)), () => { count1++; }); algorithm.Schedule.On(algorithm.Schedule.DateRules.EveryDay(), algorithm.Schedule.TimeRules.Every(TimeSpan.FromHours(1)), () => { count2++; }); const int timeSteps = 12; for (var i = 0; i < timeSteps; i++) { handler.SetTime(time); time = time.AddHours(1); } Assert.AreEqual(timeSteps, count1); Assert.AreEqual(timeSteps, count2); }
public void TestAlgorithmManagerSpeed() { var algorithmManager = new AlgorithmManager(false); var algorithm = PerformanceBenchmarkAlgorithms.SingleSecurity_Second; var job = new BacktestNodePacket(1, 2, "3", null, 9m, $"{nameof(AlgorithmManagerTests)}.{nameof(TestAlgorithmManagerSpeed)}"); var feed = new MockDataFeed(); var transactions = new BacktestingTransactionHandler(); var results = new BacktestingResultHandler(); var realtime = new BacktestingRealTimeHandler(); var leanManager = new NullLeanManager(); var alphas = new NullAlphaHandler(); var token = new CancellationToken(); algorithm.Initialize(); results.Initialize(job, new QuantConnect.Messaging.Messaging(), new Api.Api(), feed, new BacktestingSetupHandler(), transactions); results.SetAlgorithm(algorithm); transactions.Initialize(algorithm, new BacktestingBrokerage(algorithm), results); feed.Initialize(algorithm, job, results, null, null, null); Log.Trace("Starting algorithm manager loop to process " + feed.Count + " time slices"); var sw = Stopwatch.StartNew(); algorithmManager.Run(job, algorithm, feed, transactions, results, realtime, leanManager, alphas, token); sw.Stop(); var thousands = feed.Count / 1000d; var seconds = sw.Elapsed.TotalSeconds; Log.Trace("COUNT: " + feed.Count + " KPS: " + thousands / seconds); }
public void TriggersScheduledEventsSameTimeInOrder() { var realTimeHandler = new BacktestingRealTimeHandler(); realTimeHandler.Setup(new AlgorithmStub(new NullDataFeed()), new AlgorithmNodePacket(PacketType.AlgorithmNode), new BacktestingResultHandler(), null, null); var eventTime = DateTime.UtcNow; var count = 0; for (var i = 0; i < 100; i++) { var id = i; realTimeHandler.Add(new ScheduledEvent($"{id}", eventTime, (s, time) => { Assert.AreEqual(id, count); Assert.AreEqual(s, $"{id}"); count++; })); } realTimeHandler.SetTime(DateTime.UtcNow); realTimeHandler.Exit(); Assert.AreEqual(100, count); }
/// <summary> /// Launches a Lean Engine using a parameter /// </summary> /// <param name="val">The paramater to use when launching lean. </param> private void LaunchLean(string val) { Config.Set("environment", "backtesting"); string algorithm = val; // Set the algorithm in Config. Here is where you can customize Config settings Config.Set("algorithm-type-name", algorithm); _jobQueue = new JobQueue(); _notify = new Messaging(); _api = new Api(); /************ Comment one of the two following lines to select which ResultHandler to use ***********/ _resultshandler = new OptimizationResultHandler(); //_resultshandler = new ConsoleResultHandler(); _dataFeed = new FileSystemDataFeed(); _setup = new ConsoleSetupHandler(); _realTime = new BacktestingRealTimeHandler(); _historyProvider = new SubscriptionDataReaderHistoryProvider(); _transactions = new BacktestingTransactionHandler(); // Set the Log.LogHandler to only write to the log.txt file. // This setting avoids writing Log messages to the console. Log.LogHandler = (ILogHandler) new FileLogHandler(); Log.DebuggingEnabled = false; // Set this property to true for lots of messages Log.DebuggingLevel = 1; // A reminder that the default level for Log.Debug message is 1 var systemHandlers = new LeanEngineSystemHandlers(_jobQueue, _api, _notify); systemHandlers.Initialize(); var algorithmHandlers = new LeanEngineAlgorithmHandlers(_resultshandler, _setup, _dataFeed, _transactions, _realTime, _historyProvider); string algorithmPath; AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath); try { var _engine = new Engine(systemHandlers, algorithmHandlers, Config.GetBool("live-mode")); _engine.Run(job, algorithmPath); } finally { /* The JobQueue.AcknowledgeJob only asks for any key to close the window. * We do not want that behavior, so we comment out this line so that multiple Leans will run * * The alternative is to comment out Console.Read(); the line in JobQueue class. */ //systemHandlers.JobQueue.AcknowledgeJob(job); Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources systemHandlers.Dispose(); algorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }
public void TestAlgorithmManagerSpeed() { var algorithm = PerformanceBenchmarkAlgorithms.SingleSecurity_Second; var algorithmManager = new AlgorithmManager(false); var job = new BacktestNodePacket(1, 2, "3", null, 9m, $"{nameof(AlgorithmManagerTests)}.{nameof(TestAlgorithmManagerSpeed)}"); var feed = new MockDataFeed(); var marketHoursDatabase = MarketHoursDatabase.FromDataFolder(); var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder(); var dataPermissionManager = new DataPermissionManager(); var dataManager = new DataManager(feed, new UniverseSelection( algorithm, new SecurityService(algorithm.Portfolio.CashBook, marketHoursDatabase, symbolPropertiesDataBase, algorithm, RegisteredSecurityDataTypesProvider.Null, new SecurityCacheProvider(algorithm.Portfolio)), dataPermissionManager, new DefaultDataProvider()), algorithm, algorithm.TimeKeeper, marketHoursDatabase, false, RegisteredSecurityDataTypesProvider.Null, dataPermissionManager); algorithm.SubscriptionManager.SetDataManager(dataManager); var transactions = new BacktestingTransactionHandler(); var results = new BacktestingResultHandler(); var realtime = new BacktestingRealTimeHandler(); var leanManager = new NullLeanManager(); var alphas = new NullAlphaHandler(); var token = new CancellationToken(); var nullSynchronizer = new NullSynchronizer(algorithm); algorithm.Initialize(); algorithm.PostInitialize(); results.Initialize(job, new QuantConnect.Messaging.Messaging(), new Api.Api(), transactions); results.SetAlgorithm(algorithm, algorithm.Portfolio.TotalPortfolioValue); transactions.Initialize(algorithm, new BacktestingBrokerage(algorithm), results); feed.Initialize(algorithm, job, results, null, null, null, dataManager, null, null); Log.Trace("Starting algorithm manager loop to process " + nullSynchronizer.Count + " time slices"); var sw = Stopwatch.StartNew(); algorithmManager.Run(job, algorithm, nullSynchronizer, transactions, results, realtime, leanManager, alphas, token); sw.Stop(); realtime.Exit(); results.Exit(); var thousands = nullSynchronizer.Count / 1000d; var seconds = sw.Elapsed.TotalSeconds; Log.Trace("COUNT: " + nullSynchronizer.Count + " KPS: " + thousands / seconds); }
private void LaunchLean() { Config.Set("environment", "backtesting"); string algorithm = "EMATest"; Config.Set("algorithm-type-name", algorithm); _jobQueue = new JobQueue(); _notify = new Messaging(); _api = new Api(); _resultshandler = new DesktopResultHandler(); _dataFeed = new FileSystemDataFeed(); _setup = new ConsoleSetupHandler(); _realTime = new BacktestingRealTimeHandler(); _historyProvider = new SubscriptionDataReaderHistoryProvider(); _transactions = new BacktestingTransactionHandler(); var systemHandlers = new LeanEngineSystemHandlers(_jobQueue, _api, _notify); systemHandlers.Initialize(); // var algorithmHandlers = new LeanEngineAlgorithmHandlers (_resultshandler, _setup, _dataFeed, _transactions, _realTime, _historyProvider); Log.LogHandler = Composer.Instance.GetExportedValueByTypeName <ILogHandler>(Config.Get("log-handler", "CompositeLogHandler")); LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers; try { leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance); _resultshandler = leanEngineAlgorithmHandlers.Results; } catch (CompositionException compositionException) { Log.Error("Engine.Main(): Failed to load library: " + compositionException); throw; } string algorithmPath; AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out algorithmPath); try { var _engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, Config.GetBool("live-mode")); _engine.Run(job, algorithmPath); } finally { //Delete the message from the job queue: //systemHandlers.JobQueue.AcknowledgeJob(job); Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId); // clean up resources systemHandlers.Dispose(); leanEngineAlgorithmHandlers.Dispose(); Log.LogHandler.Dispose(); } }
public void Sort() { var date = new DateTime(2020, 1, 1); var events = new List <ScheduledEvent> { new ScheduledEvent("1", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1) }, (s, time) => { }) }; BacktestingRealTimeHandler.SortFirstElement(events); Assert.AreEqual(date.AddMinutes(1), events[0].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(10), events[1].NextEventUtcTime); events = new List <ScheduledEvent> { new ScheduledEvent("1", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(3) }, (s, time) => { }) }; BacktestingRealTimeHandler.SortFirstElement(events); Assert.AreEqual(date.AddMinutes(1), events[0].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(3), events[1].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(10), events[2].NextEventUtcTime); events = new List <ScheduledEvent> { new ScheduledEvent("1", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(3) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(50) }, (s, time) => { }) }; BacktestingRealTimeHandler.SortFirstElement(events); Assert.AreEqual(date.AddMinutes(1), events[0].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(3), events[1].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(10), events[2].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(50), events[3].NextEventUtcTime); }
public void SortRespectsOriginalOrderSameTime() { var date = new DateTime(2020, 1, 1); var events = new List <ScheduledEvent> { new ScheduledEvent("1", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1) }, (s, time) => { }), new ScheduledEvent("3", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }) }; BacktestingRealTimeHandler.SortFirstElement(events); Assert.AreEqual(date.AddMinutes(1), events[0].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(10), events[1].NextEventUtcTime); // scheduled event 3 and 1 have the same time, 3 should still be next else it would mean 1 executed twice when 3 once Assert.AreEqual("3", events[1].Name); Assert.AreEqual(date.AddMinutes(10), events[2].NextEventUtcTime); Assert.AreEqual("1", events[2].Name); events = new List <ScheduledEvent> { new ScheduledEvent("1", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }), new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1) }, (s, time) => { }), new ScheduledEvent("3", new List <DateTime> { date.AddMinutes(3) }, (s, time) => { }), new ScheduledEvent("4", new List <DateTime> { date.AddMinutes(10) }, (s, time) => { }), new ScheduledEvent("5", new List <DateTime> { date.AddMinutes(50) }, (s, time) => { }) }; BacktestingRealTimeHandler.SortFirstElement(events); Assert.AreEqual(date.AddMinutes(1), events[0].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(3), events[1].NextEventUtcTime); Assert.AreEqual(date.AddMinutes(10), events[2].NextEventUtcTime); // scheduled event 4 and 1 have the same time, 4 should still be next else it would mean 1 executed twice when 4 once Assert.AreEqual("4", events[2].Name); Assert.AreEqual(date.AddMinutes(10), events[3].NextEventUtcTime); Assert.AreEqual("1", events[3].Name); Assert.AreEqual(date.AddMinutes(50), events[4].NextEventUtcTime); }
public void SortsEventsAfterSetup() { var realTimeHandler = new BacktestingRealTimeHandler(); var algo = new TestAlgorithm(); algo.SubscriptionManager.SetDataManager(new DataManagerStub(algo)); algo.AddEquity("SPY"); var startDate = new DateTime(2019, 1, 1); algo.SetStartDate(startDate); algo.SetDateTime(startDate); algo.SetEndDate(2020, 1, 1); var firstCalled = false; var secondCalled = false; var events = new List <ScheduledEvent> { new ScheduledEvent("1", new List <DateTime> { startDate.AddMinutes(-10), startDate.AddMinutes(5) }, (s, time) => { firstCalled = true; }), new ScheduledEvent("2", new List <DateTime> { startDate.AddMinutes(1) }, (s, time) => { secondCalled = true; }), new ScheduledEvent("3", new List <DateTime> { startDate.AddMinutes(10) }, (s, time) => { }) }; foreach (var scheduledEvent in events) { realTimeHandler.Add(scheduledEvent); } realTimeHandler.Setup(algo, new AlgorithmNodePacket(PacketType.AlgorithmNode), new BacktestingResultHandler(), null, null); realTimeHandler.SetTime(startDate.AddMinutes(1)); realTimeHandler.Exit(); Assert.IsTrue(secondCalled); // 'first' should of been called and should be moved behind 'second' after setup Assert.IsFalse(firstCalled); }
public void SingleScheduledEventFires_SetTime() { var realTimeHandler = new BacktestingRealTimeHandler(); var algo = new TestAlgorithm(); algo.SubscriptionManager.SetDataManager(new DataManagerStub(algo)); algo.AddEquity("SPY"); algo.SetStartDate(2019, 1, 1); algo.SetDateTime(new DateTime(2019, 1, 1)); algo.SetEndDate(2020, 1, 1); realTimeHandler.Setup(algo, new AlgorithmNodePacket(PacketType.AlgorithmNode), new BacktestingResultHandler(), null, null); realTimeHandler.SetTime(DateTime.UtcNow); Assert.IsTrue(algo.OnEndOfDayFired); }
/// <summary> /// Select the realtime event handler set in the job. /// </summary> private static IRealTimeHandler GetRealTimeHandler(IAlgorithm algorithm, IBrokerage brokerage, IDataFeed feed, IResultHandler results, AlgorithmNodePacket job) { var rth = default(IRealTimeHandler); switch (job.RealTimeEndpoint) { //Don't fire based on system time but virtualized backtesting time. case RealTimeEndpoint.Backtesting: Log.Trace("Engine.GetRealTimeHandler(): Selected Backtesting RealTimeEvent Handler"); rth = new BacktestingRealTimeHandler(algorithm, job); break; // Fire events based on real system clock time. case RealTimeEndpoint.LiveTrading: Log.Trace("Engine.GetRealTimeHandler(): Selected LiveTrading RealTimeEvent Handler"); rth = new LiveTradingRealTimeHandler(algorithm, feed, results, brokerage, job); break; } return(rth); }
public void TriggersScheduledEventsSameTimeInOrder() { var realTimeHandler = new BacktestingRealTimeHandler(); var eventTime = DateTime.UtcNow; var count = 0; for (var i = 0; i < 100; i++) { var id = i; realTimeHandler.Add(new ScheduledEvent($"{id}", eventTime, (s, time) => { Assert.AreEqual(id, count); count++; })); } realTimeHandler.SetTime(DateTime.UtcNow); Assert.AreEqual(100, count); }
public void AppliesDividendsOnce() { // init algorithm var algorithm = new AlgorithmStub(new MockDataFeed()); algorithm.SetLiveMode(true); var dividend = new Dividend(Symbols.SPY, DateTime.UtcNow, 10m, 100m); var feed = new MockDataFeed(); var marketHoursDatabase = MarketHoursDatabase.FromDataFolder(); var symbolPropertiesDataBase = SymbolPropertiesDatabase.FromDataFolder(); var dataPermissionManager = new DataPermissionManager(); var dataManager = new DataManager(feed, new UniverseSelection( algorithm, new SecurityService(algorithm.Portfolio.CashBook, marketHoursDatabase, symbolPropertiesDataBase, algorithm, RegisteredSecurityDataTypesProvider.Null, new SecurityCacheProvider(algorithm.Portfolio)), dataPermissionManager, new DefaultDataProvider()), algorithm, algorithm.TimeKeeper, marketHoursDatabase, true, RegisteredSecurityDataTypesProvider.Null, dataPermissionManager); var synchronizer = new NullSynchronizer(algorithm, dividend); algorithm.SubscriptionManager.SetDataManager(dataManager); algorithm.AddSecurities(equities: new List <string> { "SPY" }); algorithm.Securities[Symbols.SPY].Holdings.SetHoldings(100m, 1); algorithm.PostInitialize(); var initializedCash = algorithm.Portfolio.CashBook[Currencies.USD].Amount; // init algorithm manager var manager = new AlgorithmManager(true); var job = new LiveNodePacket { UserId = 1, ProjectId = 2, DeployId = $"{nameof(PaperBrokerageTests)}.{nameof(AppliesDividendsOnce)}" }; var results = new LiveTradingResultHandler(); var transactions = new BacktestingTransactionHandler(); var brokerage = new PaperBrokerage(algorithm, job); // initialize results and transactions results.Initialize(job, new EventMessagingHandler(), new Api.Api(), transactions); results.SetAlgorithm(algorithm, algorithm.Portfolio.TotalPortfolioValue); transactions.Initialize(algorithm, brokerage, results); var realTime = new BacktestingRealTimeHandler(); // run algorithm manager manager.Run(job, algorithm, synchronizer, transactions, results, realTime, new AlgorithmManagerTests.NullLeanManager(), new AlgorithmManagerTests.NullAlphaHandler(), new CancellationToken() ); var postDividendCash = algorithm.Portfolio.CashBook[Currencies.USD].Amount; realTime.Exit(); results.Exit(); Assert.AreEqual(initializedCash + dividend.Distribution, postDividendCash); }
public void ScanPastEvents(bool oneStep) { var realTimeHandler = new BacktestingRealTimeHandler(); realTimeHandler.Setup(new AlgorithmStub(), new AlgorithmNodePacket(PacketType.AlgorithmNode) { Language = Language.CSharp }, _resultHandler, null, new TestTimeLimitManager()); var date = new DateTime(2020, 1, 1); var count = 0; var asserts = 0; realTimeHandler.Add(new ScheduledEvent("1", new List <DateTime> { date, date.AddMinutes(10) }, (s, time) => { count++; if (count == 1) { asserts++; Assert.AreEqual(date, time); } else if (count == 4) { asserts++; Assert.AreEqual(date.AddMinutes(10), time); } })); realTimeHandler.Add(new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1), date.AddMinutes(2) }, (s, time) => { count++; if (count == 2) { asserts++; Assert.AreEqual(date.AddMinutes(1), time); } else if (count == 3) { asserts++; Assert.AreEqual(date.AddMinutes(2), time); } })); if (oneStep) { realTimeHandler.ScanPastEvents(date.AddDays(1)); } else { realTimeHandler.ScanPastEvents(date.AddMilliseconds(1)); realTimeHandler.ScanPastEvents(date.AddMinutes(1).AddMilliseconds(1)); realTimeHandler.ScanPastEvents(date.AddMinutes(2).AddMilliseconds(1)); realTimeHandler.ScanPastEvents(date.AddMinutes(10).AddMilliseconds(1)); } realTimeHandler.Exit(); Assert.AreEqual(4, count); Assert.AreEqual(4, asserts); }
public void SetTime(bool oneStep) { var realTimeHandler = new BacktestingRealTimeHandler(); realTimeHandler.Setup(new AlgorithmStub(new NullDataFeed()), new AlgorithmNodePacket(PacketType.AlgorithmNode), new BacktestingResultHandler(), null, null); var date = new DateTime(2020, 1, 1); var count = 0; var asserts = 0; realTimeHandler.Add(new ScheduledEvent("1", new List <DateTime> { date, date.AddMinutes(10) }, (s, time) => { count++; if (count == 1) { asserts++; Assert.AreEqual(date, time); } else if (oneStep ? count == 2 : count == 4) { asserts++; Assert.AreEqual(date.AddMinutes(10), time); } })); realTimeHandler.Add(new ScheduledEvent("2", new List <DateTime> { date.AddMinutes(1), date.AddMinutes(2) }, (s, time) => { count++; if (oneStep ? count == 3 : count == 2) { asserts++; Assert.AreEqual(date.AddMinutes(1), time); } else if (oneStep ? count == 4 : count == 3) { asserts++; Assert.AreEqual(date.AddMinutes(2), time); } })); if (oneStep) { realTimeHandler.SetTime(date.AddDays(1)); } else { realTimeHandler.SetTime(date); realTimeHandler.SetTime(date.AddMinutes(1)); realTimeHandler.SetTime(date.AddMinutes(2)); realTimeHandler.SetTime(date.AddMinutes(10)); } realTimeHandler.Exit(); Assert.AreEqual(4, count); Assert.AreEqual(4, asserts); }
private void LaunchLean(string id) { ConfigMerger.Merge(_config, id); Config.Set("api-handler", nameof(EmptyApiHandler)); //todo: instance logging //var logFileName = "log" + DateTime.Now.ToString("yyyyMMddssfffffff") + "_" + id + ".txt"; Log.LogHandler = LogSingleton.Instance; var jobQueue = new JobQueue(); var manager = new LocalLeanManager(); var systemHandlers = new LeanEngineSystemHandlers( jobQueue, new EmptyApiHandler(), new QuantConnect.Messaging.Messaging(), manager); systemHandlers.Initialize(); var map = new LocalDiskMapFileProvider(); var results = new OptimizerResultHandler(); var transactions = new BacktestingTransactionHandler(); var dataFeed = new FileSystemDataFeed(); var realTime = new BacktestingRealTimeHandler(); var data = new DefaultDataProvider(); var leanEngineAlgorithmHandlers = new LeanEngineAlgorithmHandlers( results, new ConsoleSetupHandler(), dataFeed, transactions, realTime, map, new LocalDiskFactorFileProvider(map), data, new OptimizerAlphaHandler(), new EmptyObjectStore()); _resultsHandler = (OptimizerResultHandler)leanEngineAlgorithmHandlers.Results; var job = (BacktestNodePacket)systemHandlers.JobQueue.NextJob(out var algorithmPath); //mark job with id. Is set on algorithm in OptimizerAlphaHandler job.BacktestId = id; //todo: pass period through job //job.PeriodStart = _config.StartDate; //job.PeriodFinish = _config.EndDate; Engine engine; AlgorithmManager algorithmManager; try { algorithmManager = new AlgorithmManager(false); systemHandlers.LeanManager.Initialize(systemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager); engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, false); using (var workerThread = new MultipleWorkerThread()) { engine.Run(job, algorithmManager, algorithmPath, workerThread); } } finally { // clean up resources Composer.Instance.Reset(); results.Charts.Clear(); results.Messages.Clear(); if (results.Algorithm != null) { results.Algorithm.Transactions.TransactionRecord.Clear(); results.Algorithm.SubscriptionManager.Subscriptions.SelectMany(s => s.Consolidators)?.ToList().ForEach(f => { results.Algorithm.SubscriptionManager.RemoveConsolidator(f.WorkingData?.Symbol, f); UnregisterAllEvents(f); }); if (results.Algorithm is QCAlgorithm) { ((QCAlgorithm)results.Algorithm).SubscriptionManager.Subscriptions.ToList().Clear(); } if (_config.AlgorithmLanguage != "Python") { results.Algorithm.HistoryProvider = null; } var closedTrades = (List <Trade>) typeof(TradeBuilder).GetField("_closedTrades", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(results.Algorithm.TradeBuilder); closedTrades.Clear(); results.Algorithm = null; } transactions.Orders.Clear(); transactions.OrderTickets.Clear(); manager.Dispose(); systemHandlers.Dispose(); leanEngineAlgorithmHandlers.Dispose(); results = null; dataFeed = null; transactions = null; realTime = null; data = null; map = null; systemHandlers = null; leanEngineAlgorithmHandlers = null; algorithmManager = null; engine = null; job = null; jobQueue = null; manager = null; } }
private void LaunchLean(string id) { Config.Set("environment", "backtesting"); if (!string.IsNullOrEmpty(_config.AlgorithmTypeName)) { Config.Set("algorithm-type-name", _config.AlgorithmTypeName); } if (!string.IsNullOrEmpty(_config.AlgorithmLocation)) { Config.Set("algorithm-location", Path.GetFileName(_config.AlgorithmLocation)); } if (!string.IsNullOrEmpty(_config.DataFolder)) { Config.Set("data-folder", _config.DataFolder); } if (!string.IsNullOrEmpty(_config.TransactionLog)) { var filename = _config.TransactionLog; filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileNameWithoutExtension(filename) + id + Path.GetExtension(filename)); Config.Set("transaction-log", filename); } Config.Set("api-handler", nameof(EmptyApiHandler)); Config.Set("backtesting.result-handler", nameof(OptimizerResultHandler)); //Composer.Instance.Reset(); //todo: instance logging //var logFileName = "log" + DateTime.Now.ToString("yyyyMMddssfffffff") + "_" + id + ".txt"; Log.LogHandler = LogSingleton.Instance; var jobQueue = new JobQueue(); var manager = new LocalLeanManager(); var systemHandlers = new LeanEngineSystemHandlers( jobQueue, new EmptyApiHandler(), new QuantConnect.Messaging.Messaging(), manager); systemHandlers.Initialize(); var map = new LocalDiskMapFileProvider(); var results = new OptimizerResultHandler(); var transactions = new BacktestingTransactionHandler(); var dataFeed = new FileSystemDataFeed(); var realTime = new BacktestingRealTimeHandler(); var data = new DefaultDataProvider(); var leanEngineAlgorithmHandlers = new LeanEngineAlgorithmHandlers( results, new ConsoleSetupHandler(), dataFeed, transactions, realTime, map, new LocalDiskFactorFileProvider(map), data, new OptimizerAlphaHandler(), new EmptyObjectStore()); _resultsHandler = (OptimizerResultHandler)leanEngineAlgorithmHandlers.Results; var job = (BacktestNodePacket)systemHandlers.JobQueue.NextJob(out var algorithmPath); //mark job with id. Is set on algorithm in OptimizerAlphaHandler job.BacktestId = id; //todo: pass period through job //job.PeriodStart = _config.StartDate; //job.PeriodFinish = _config.EndDate; Engine engine; AlgorithmManager algorithmManager; try { algorithmManager = new AlgorithmManager(false); systemHandlers.LeanManager.Initialize(systemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager); engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, false); using (var workerThread = new MultipleWorkerThread()) { engine.Run(job, algorithmManager, algorithmPath, workerThread); } } finally { // clean up resources Composer.Instance.Reset(); results.Charts.Clear(); results.Messages.Clear(); results.Algorithm.Transactions.TransactionRecord.Clear(); var closedTrades = (List <Trade>) typeof(TradeBuilder).GetField("_closedTrades", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(results.Algorithm.TradeBuilder); closedTrades.Clear(); results.Algorithm.HistoryProvider = null; results.Algorithm = null; transactions.Orders.Clear(); transactions.OrderTickets.Clear(); manager.Dispose(); systemHandlers.Dispose(); leanEngineAlgorithmHandlers.Dispose(); results = null; dataFeed = null; transactions = null; realTime = null; data = null; map = null; systemHandlers = null; leanEngineAlgorithmHandlers = null; algorithmManager = null; engine = null; job = null; jobQueue = null; manager = null; } }