示例#1
0
        private void LaunchLean()
        {
            ConfigMerger.Merge(_config, _id, this.GetType());

            var systemHandlers = new LeanEngineSystemHandlers(
                new JobQueue(),
                new EmptyApiHandler(),
                new Messaging(),
                new LocalLeanManager());

            systemHandlers.Initialize();

            //separate log uniquely named
            var logFileName = "log" + DateTime.Now.ToString("yyyyMMddssfffffff") + "_" + _id + ".txt";

            using (Log.LogHandler = new FileLogHandler(logFileName, true))
            {
                var map = new LocalDiskMapFileProvider();
                var leanEngineAlgorithmHandlers = new LeanEngineAlgorithmHandlers(
                    new OptimizerResultHandler(),
                    new ConsoleSetupHandler(),
                    new FileSystemDataFeed(),
                    new BacktestingTransactionHandler(),
                    new BacktestingRealTimeHandler(),
                    map,
                    new LocalDiskFactorFileProvider(map),
                    new DefaultDataProvider(),
                    new OptimizerAlphaHandler(),
                    new EmptyObjectStore(),
                    new DataPermissionManager());

                _resultsHandler = (OptimizerResultHandler)leanEngineAlgorithmHandlers.Results;

                var job = (BacktestNodePacket)systemHandlers.JobQueue.NextJob(out var algorithmPath);
                //todo: pass period through job
                //job.PeriodStart = _config.StartDate;
                //job.PeriodFinish = _config.EndDate;

                try
                {
                    var algorithmManager = new AlgorithmManager(false);
                    systemHandlers.LeanManager.Initialize(systemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);
                    var engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, false);
                    engine.Run(job, algorithmManager, algorithmPath, WorkerThread.Instance);
                }
                finally
                {
                    // clean up resources
                    systemHandlers.Dispose();
                    leanEngineAlgorithmHandlers.Dispose();
                }
            }
        }
示例#2
0
        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;
            }
        }