示例#1
0
        public void RunJob()
        {
            try
            {
                EngineContext.SystemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);
                EngineContext.SystemHandlers.Initialize();
                EngineContext.AlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
                _resultsHandler      = (OptimizerResultHandler)EngineContext.AlgorithmHandlers.Results;
                EngineContext.Engine = new Engine(EngineContext.SystemHandlers, EngineContext.AlgorithmHandlers, false);
            }
            catch (CompositionException compositionException)
            {
                LogProvider.ErrorLogger.Error("Engine.Main(): Failed to load library", compositionException);
                throw;
            }

            string algorithmPath;
            AlgorithmNodePacket job = EngineContext.SystemHandlers.JobQueue.NextJob(out algorithmPath);

            EngineContext.Engine.Run(job, new AlgorithmManager(false), algorithmPath, WorkerThread.Instance);
        }
示例#2
0
        private void LaunchLean()
        {
            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);
            }

            //transaction-log

            Config.Set("api-handler", nameof(EmptyApiHandler));
            var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);

            systemHandlers.Initialize();

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

            using (Log.LogHandler = new FileLogHandler(logFileName, true))
            {
                LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers;
                try
                {
                    //override config to use custom result handler
                    Config.Set("backtesting.result-handler", nameof(OptimizerResultHandler));
                    leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
                    _resultsHandler             = (OptimizerResultHandler)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 algorithmManager = new AlgorithmManager(false);
                    systemHandlers.LeanManager.Initialize(systemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);
                    var engine = new Engine(systemHandlers, leanEngineAlgorithmHandlers, false);
                    engine.Run(job, algorithmManager, algorithmPath);
                }
                finally
                {
                    Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);

                    // clean up resources
                    systemHandlers.Dispose();
                    leanEngineAlgorithmHandlers.Dispose();
                }
            }
        }
        private void LaunchLean()
        {
            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);
            }

            var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);

            systemHandlers.Initialize();

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

            if (File.Exists(logFileName + ".txt"))
            {
                logFileName += "_" + Guid.NewGuid().ToString();
            }
            logFileName += ".txt";

            var logHandlers = new ILogHandler[] { new FileLogHandler(logFileName, true) };

            using (Log.LogHandler = new CompositeLogHandler(logHandlers))
            {
                LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers;
                try
                {
                    //override config to use custom result handler
                    Config.Set("backtesting.result-handler", nameof(OptimizerResultHandler));
                    leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
                    _resultsHandler             = (OptimizerResultHandler)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
                {
                    Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);

                    // clean up resources
                    systemHandlers.Dispose();
                    leanEngineAlgorithmHandlers.Dispose();
                }
            }
        }
        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 systemHandlers = new LeanEngineSystemHandlers(
                new JobQueue(),
                new EmptyApiHandler(),
                new QuantConnect.Messaging.Messaging(),
                new LocalLeanManager());

            systemHandlers.Initialize();

            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());
            _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;

            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, new MultipleWorkerThread());
            }
            finally
            {
                // clean up resources
                systemHandlers.Dispose();
                leanEngineAlgorithmHandlers.Dispose();
            }
        }