コード例 #1
0
        public void Stop()
        {
            try
            {
                if (_algorithmHandlers != null)
                {
                    _algorithmHandlers.Setup.Dispose();
                    _algorithmHandlers.Dispose();
                }
            }
            catch (Exception ex)
            {
            }

            try
            {
                if (_leanEngineThread != null)
                {
                    _leanEngineThread.Abort();
                }

                Dispose();
            }
            catch (Exception ex)
            {
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: sprgn/LeanITrend
        /// <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();
            }
        }
コード例 #3
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();
                }
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: jpm16011/Lean
        public static void Exit()
        {
            //Delete the message from the job queue:
            leanEngineSystemHandlers.JobQueue.AcknowledgeJob(job);
            Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);

            // clean up resources
            leanEngineSystemHandlers.Dispose();
            leanEngineAlgorithmHandlers.Dispose();
            Log.LogHandler.Dispose();

            Log.Trace("Program.Main(): Exiting Lean...");
            Environment.Exit(0);
        }
コード例 #5
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);
            }

            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))
            {
                //override config to use custom result handler
                Config.Set("backtesting.result-handler", nameof(OptimizerResultHandler));

                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);
                //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();
                }
            }
        }
コード例 #6
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;
            }
        }
コード例 #7
0
        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;
            }
        }
コード例 #8
0
        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("alpha-handler", nameof(OptimizerAlphaHandler));
            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());

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

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