Exemplo n.º 1
0
        //
        public ActionResult _SortBenchMarkDemo()
        {
            //-------------------------------------------------
            // LLAMADO A LA PÁGINA "_SortBenchMarkDemo"
            //-------------------------------------------------

            try
            {
                //
                string unsortedListRaw = AlgorithmManager.GetUnsortedList
                                         (
                    ARRAY_SIZE
                                         );
                //
                string unsortedListEncoded = HttpUtility.HtmlEncode(unsortedListRaw).Replace("|", "<br/>");
                //
                ViewBag.Message = unsortedListEncoded;
                //
                Session["unsortedList"] = unsortedListRaw;

                //
                LogModel.Log("PAGE_SORT_BENCHAMRK_DEMO", this.GetIpValue(), LogModel.LogType.Info);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(View());
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        public static void RunLocalBacktest(string algorithm, Dictionary <string, string> expectedStatistics, Language language)
        {
            var statistics = new Dictionary <string, string>();

            Composer.Instance.Reset();

            try
            {
                // set the configuration up
                Config.Set("algorithm-type-name", algorithm);
                Config.Set("live-mode", "false");
                Config.Set("environment", "");
                Config.Set("messaging-handler", "QuantConnect.Messaging.Messaging");
                Config.Set("job-queue-handler", "QuantConnect.Queues.JobQueue");
                Config.Set("api-handler", "QuantConnect.Api.Api");
                Config.Set("result-handler", "QuantConnect.Lean.Engine.Results.BacktestingResultHandler");
                Config.Set("algorithm-language", language.ToString());
                Config.Set("algorithm-location", "QuantConnect.Algorithm." + language + ".dll");

                var debugEnabled = Log.DebuggingEnabled;

                var logHandlers = new ILogHandler[] { new ConsoleLogHandler(), new FileLogHandler("regression.log", false) };
                using (Log.LogHandler = new CompositeLogHandler(logHandlers))
                    using (var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance))
                        using (var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance))
                        {
                            Log.DebuggingEnabled = true;

                            Log.LogHandler.Trace("");
                            Log.LogHandler.Trace("{0}: Running " + algorithm + "...", DateTime.UtcNow);
                            Log.LogHandler.Trace("");

                            // run the algorithm in its own thread

                            var engine = new Lean.Engine.Engine(systemHandlers, algorithmHandlers, false);
                            Task.Factory.StartNew(() =>
                            {
                                string algorithmPath;
                                var job = systemHandlers.JobQueue.NextJob(out algorithmPath);
                                var algorithmManager = new AlgorithmManager(false);
                                engine.Run(job, algorithmManager, algorithmPath);
                            }).Wait();

                            var backtestingResultHandler = (BacktestingResultHandler)algorithmHandlers.Results;
                            statistics = backtestingResultHandler.FinalStatistics;

                            Log.DebuggingEnabled = debugEnabled;
                        }
            }
            catch (Exception ex)
            {
                Log.LogHandler.Error("{0} {1}", ex.Message, ex.StackTrace);
            }

            foreach (var stat in expectedStatistics)
            {
                Assert.AreEqual(true, statistics.ContainsKey(stat.Key), "Missing key: " + stat.Key);
                Assert.AreEqual(stat.Value, statistics[stat.Key], "Failed on " + stat.Key);
            }
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            var manager = new AlgorithmManager(9876);

            manager.Init();
            Console.ReadLine();
        }
Exemplo n.º 5
0
        public ActionResult _UnitTesting02()
        {
            //
            string htmlNewLine = @"<br/>";
            //
            StringBuilder stringArray = new StringBuilder();
            //
            int    ARRAY_SIZE = 25;
            Random rand       = new Random();

            //
            int[] arr = new int[ARRAY_SIZE];
            //
            int[] randomArray = AlgorithmManager.FisherYates(ARRAY_SIZE, rand);
            //
            for (int i = 0; i < ARRAY_SIZE; i++)
            {
                stringArray.Append(string.Format("{0}{1}", ((i == 0) ? "" : @"|"), randomArray[i].ToString()));
            }
            //
            string unsortedList = stringArray.ToString();
            //
            AlgorithmManager am = new AlgorithmManager
                                  (
                unsortedList
                , ARRAY_SIZE
                                  );
            //
            string sortedList = am.BubbleSort();

            //
            ViewBag.Message = HttpUtility.HtmlEncode(unsortedList).Replace(@"|", htmlNewLine);
            //
            return(View());
        }
 // Use this for initialization
 void Start()
 {
     characterManager = GameObject.Find("/Managers/CharacterManager")
                        .GetComponent <CharacterManager> ();
     algorithmManager = GameObject.Find("/Managers/AlgorithmManager")
                        .GetComponent <AlgorithmManager> ();
     proceedList = new ArrayList();
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
    static Manager()
    {
        GameObject g = SafeFind("_managers");

        Sound         = (SoundManager)SafeComponent(g, "SoundManager");
        Experiment    = (ExperimentManager)SafeComponent(g, "ExperimentManager");
        SceneSwitcher = (SceneSwitcher)SafeComponent(g, "SceneSwitcher");
        Spawn         = (SpawnManager)SafeComponent(g, "SpawnManager");
        Algorithm     = (AlgorithmManager)SafeComponent(g, "AlgorithmManager");
    }
Exemplo n.º 9
0
 public AlgorithmRunnerResults(
     string algorithm,
     Language language,
     AlgorithmManager manager,
     BacktestingResultHandler results)
 {
     Algorithm        = algorithm;
     Language         = language;
     AlgorithmManager = manager;
     Results          = results;
 }
Exemplo n.º 10
0
        public void StartSecondAssignment_AOCPasswordFile_346ValidPasswords()
        {
            //Arrange:
            pwdManager  = new PasswordManager();
            algoManager = new AlgorithmManager();

            //Act:
            int result = algoManager.StartSecondAssignment(pwdManager);

            //Assert:
            Assert.AreEqual(346, result);
        }
Exemplo n.º 11
0
        public string GenerateRandomVertex(ushort p_vertexSize, ushort p_sourcePoint)
        {
            //
            ushort p_sampleSize = 23;
            string status       = string.Empty;

            //
            status = AlgorithmManager.GenerateRandomPoints(p_vertexSize, p_sampleSize, p_sourcePoint);

            //
            return(status);
        }
Exemplo n.º 12
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();
                }
            }
        }
Exemplo n.º 13
0
        public void StartFirstAssigment_AOCPasswordFile_569ValidPasswords()
        {
            //Arrange:
            pwdManager  = new PasswordManager();
            algoManager = new AlgorithmManager();

            //Act:
            int result = algoManager.StartFirstAssignment(pwdManager);

            //Assert:
            Assert.AreEqual(569, result);
        }
Exemplo n.º 14
0
        public void AppliesDividendsOnce()
        {
            // init algorithm
            var algorithm = new AlgorithmStub();

            algorithm.SetLiveMode(true);
            var dividend    = new Dividend(Symbols.SPY, DateTime.UtcNow, 10m, 100m);
            var feed        = new TestDividendDataFeed(algorithm, dividend);
            var dataManager = new DataManager(feed, new UniverseSelection(feed, algorithm), algorithm.Settings, algorithm.TimeKeeper);

            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["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(), feed, new BrokerageSetupHandler(), transactions);
            results.SetAlgorithm(algorithm);
            transactions.Initialize(algorithm, brokerage, results);

            // run algorithm manager
            manager.Run(job,
                        algorithm,
                        dataManager,
                        transactions,
                        results,
                        new BacktestingRealTimeHandler(),
                        new AlgorithmManagerTests.NullLeanManager(),
                        new AlgorithmManagerTests.NullAlphaHandler(),
                        new CancellationToken()
                        );

            var postDividendCash = algorithm.Portfolio.CashBook["USD"].Amount;

            Assert.AreEqual(initializedCash + dividend.Distribution, postDividendCash);
        }
Exemplo n.º 15
0
        public void Run(Dictionary <string, string> parameters)
        {
            const string mode = "DEBUG";

            Console.OutputEncoding = System.Text.Encoding.UTF8;

            var liveMode = Config.GetBool("live-mode");

            Log.DebuggingEnabled = Config.GetBool("debug-mode");
            Log.FilePath         = Path.Combine(Config.Get("results-destination-folder"), "log.txt");
            Log.LogHandler       = Composer.Instance.GetExportedValueByTypeName <ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));

            Log.Trace($"Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v{Globals.Version} Mode: {mode} ({(Environment.Is64BitProcess ? "64" : "32")}bit)");
            Log.Trace($"Engine.Main(): Started {DateTime.Now.ToShortTimeString()}");

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

            systemHandler.Initialize();

            var job = systemHandler.JobQueue.NextJob(out var assemblyPath);

            foreach (var kvp in parameters)
            {
                job.Parameters[kvp.Key] = kvp.Value;
            }

            var algorithmHandler = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);

            try
            {
                var algorithmManager = new AlgorithmManager(liveMode, job);

                systemHandler.LeanManager.Initialize(systemHandler, algorithmHandler, job, algorithmManager);

                var engine = new Engine(systemHandler, algorithmHandler, liveMode);
                engine.Run(job, algorithmManager, assemblyPath, WorkerThread.Instance);
            }
            finally
            {
                Console.WriteLine("Engine.Main(): Analysis Complete. Press any key to continue.");
                Console.ReadKey(true);

                systemHandler.Dispose();
                algorithmHandler.Dispose();
                Log.LogHandler.Dispose();

                Log.Trace("Program.Main(): Exiting Lean...");

                Thread.Sleep(2000);
                Environment.Exit(0);
            }
        }
Exemplo n.º 16
0
        //
        public string GenerateRandomVertex()
        {
            //
            string status = string.Empty;
            //
            const ushort sampleSize  = 23;
            const ushort vertexSize  = 6;
            const ushort sourcePoint = 5;

            status = AlgorithmManager.GenerateRandomPoints(vertexSize, sampleSize, sourcePoint);

            return(status);
        }
Exemplo n.º 17
0
        private void simpleButton_GO_Click(object sender, EventArgs e)
        {
            ChildModuleOf计算结果 mo = new ChildModuleOf计算结果();
            //选择数据源

            //执行选中的算法
            string header = "==========" + comboBoxEdit_公式选择.Text + "==========\n";
            string res    = AlgorithmManager.Go(comboBoxEdit_公式选择.Text, new List <TestResult测试结果信息>(WaitProcessTestResults));

            //打印执行结果
            mo.richTextBox1.AppendText(header + res);
            mo.ShowDialog();
        }
Exemplo n.º 18
0
        public TrackModel Run(TrackModel model, AccountModel account, SettingModel settings, HostDomainLogger logger)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            Log.LogHandler = logger;
            if (!SetConfig(model, account, settings))
            {
                return(model);
            }

            // Register all data providers
            ProviderFactory.RegisterProviders(settings);

            Log.Trace("LeanLanucher: Memory " + OS.ApplicationMemoryUsed + "Mb-App " + OS.TotalPhysicalMemoryUsed + "Mb-Used");
            try
            {
                var liveMode = Config.GetBool("live-mode");
                using var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
                using var systemHandlers    = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);
                systemHandlers.Initialize();
                var engine              = new Engine(systemHandlers, algorithmHandlers, liveMode);
                var algorithmManager    = new AlgorithmManager(liveMode);
                AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out string assemblyPath);
                job.UserPlan = UserPlan.Professional;
                job.Language = model.AlgorithmLanguage;
                if (job is BacktestNodePacket backtest)
                {
                    backtest.PeriodStart  = model.StartDate;
                    backtest.PeriodFinish = model.EndDate;
                    backtest.CashAmount   = new CashAmount(model.InitialCapital, Currencies.USD);
                }
                systemHandlers.LeanManager.Initialize(systemHandlers, algorithmHandlers, job, algorithmManager);
                engine.Run(job, algorithmManager, assemblyPath, WorkerThread.Instance);
                BacktestResultHandler resultHandler = algorithmHandlers.Results as BacktestResultHandler;
                model.Result = resultHandler?.JsonResult ?? string.Empty;
                model.Logs   = resultHandler?.Logs ?? string.Empty;
            }
            catch (Exception ex)
            {
                Log.Error("{0}: {1}", ex.GetType(), ex.Message);
            }

            NetMQConfig.Cleanup(false);
            Log.LogHandler.Dispose();
            return(model);
        }
Exemplo n.º 19
0
        public void Works()
        {
            var algorithm = new AlgorithmStub();

            algorithm.HistoryProvider = new TestHistoryProvider();
            var security = algorithm.AddEquity(Symbols.SPY);
            var model    = new TestVolatilityModel();

            security.VolatilityModel = model;

            AlgorithmManager.ProcessVolatilityHistoryRequirements(algorithm);

            Assert.AreEqual(1, model.dataUpdate.Count);
            Assert.AreEqual(Symbols.SPY, model.dataUpdate.First().Symbol);
            Assert.AreEqual(4, model.dataUpdate.First().Price);
        }
Exemplo n.º 20
0
        public TrackModel Run(TrackModel model, AccountModel account, SettingService settings, HostDomainLogger logger)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            Log.LogHandler = logger;
            if (!SetConfig(model, account, settings))
            {
                return(model);
            }

            // Register all data providers
            ProviderFactory.RegisterProviders();

            Log.Trace("LeanLanucher: Memory " + OS.ApplicationMemoryUsed + "Mb-App " + OS.TotalPhysicalMemoryUsed + "Mb-Used");
            try
            {
                var liveMode = Config.GetBool("live-mode");
                using (var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance))
                    using (var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance))
                    {
                        string assemblyPath;
                        systemHandlers.Initialize();
                        var engine              = new Engine(systemHandlers, algorithmHandlers, liveMode);
                        var algorithmManager    = new AlgorithmManager(liveMode);
                        AlgorithmNodePacket job = systemHandlers.JobQueue.NextJob(out assemblyPath);
                        job.UserPlan = UserPlan.Professional;
                        systemHandlers.LeanManager.Initialize(systemHandlers, algorithmHandlers, job, algorithmManager);
                        engine.Run(job, algorithmManager, assemblyPath);
                        systemHandlers.JobQueue.AcknowledgeJob(job);
                        BacktestResultHandler resultHandler = algorithmHandlers.Results as BacktestResultHandler;
                        model.Result = resultHandler?.JsonResult ?? string.Empty;
                        model.Logs   = resultHandler?.Logs ?? string.Empty;
                    }
            }
            catch (Exception ex)
            {
                Log.Error("{0}: {1}", ex.GetType(), ex.Message);
            }

            NetMQConfig.Cleanup(false);
            Log.LogHandler.Dispose();
            return(model);
        }
Exemplo n.º 21
0
        //
        public string _GetGraph()
        {
            //--------------------------------------------------
            // DECLARACION DE VARIABLES
            //--------------------------------------------------
            string status = "ok";

            try
            {
                //--------------------------------------------------
                // LOG
                //--------------------------------------------------
#if DEBUG
                LogModel.Log("DIJKSTRA DEMO. GET_GRAPH");
#endif
                //--------------------------------------------------
                // CREAR GRAFO
                //--------------------------------------------------
                string vertexList = AlgorithmManager.Dijkstra();

                //--------------------------------------------------
                // CONVERTIR LISTADO A CADENA VISIBLE EN HTML
                //--------------------------------------------------
                string sortedListEncoded = string.Empty;
                sortedListEncoded = HttpUtility.HtmlEncode(vertexList);
                sortedListEncoded = sortedListEncoded.Replace(@",", @"<br/>");
                sortedListEncoded = sortedListEncoded.Replace(@"\t", @"&nbsp;");
                //
                status = sortedListEncoded;
                //
            }
            catch (Exception ex)
            {
                LogModel.Log(string.Format("REGEX_ÈRROR. ='{0}'-'{1}'"
                                           , ex.Message
                                           , ex.StackTrace)
                             , string.Empty
                             , LogModel.LogType.Error
                             );
            }
            //
            return(status);
        }
Exemplo n.º 22
0
        //
        public string _NewSort()
        {
            //
            string status = "OK";

            try
            {
                //
                string unsortedListRaw = AlgorithmManager.GetUnsortedList
                                         (
                    ARRAY_SIZE
                                         );
                //
                string unsortedListEncoded = HttpUtility.HtmlEncode(unsortedListRaw).Replace("|", "<br/>");
                //
                Session["unsortedList"] = unsortedListRaw;
                //
                status = unsortedListEncoded;
                //
            }
            catch (Exception ex)
            {
                LogModel.Log(string.Format("REGEX_ÈRROR. ='{0}'-'{1}'"
                                           , ex.Message
                                           , ex.StackTrace)
                             , string.Empty
                             , LogModel.LogType.Error
                             );
            }
            //--------------------------------------------------
            // LOG
            //--------------------------------------------------
#if DEBUG
            LogModel.Log(string.Format("SORT_BENCHMARK . NEW SORT : {0}", status));
#endif
            //
            return(status);
        }
Exemplo n.º 23
0
        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);
        }
Exemplo n.º 24
0
 /// <summary>
 /// Empty implementation of the ILeanManager interface
 /// </summary>
 /// <param name="systemHandlers">Exposes lean engine system handlers running LEAN</param>
 /// <param name="algorithmHandlers">Exposes the lean algorithm handlers running lean</param>
 /// <param name="job">The job packet representing either a live or backtest Lean instance</param>
 /// <param name="algorithmManager">The Algorithm manager</param>
 public void Initialize(LeanEngineSystemHandlers systemHandlers, LeanEngineAlgorithmHandlers algorithmHandlers, AlgorithmNodePacket job, AlgorithmManager algorithmManager)
 {
     // NOP
 }
Exemplo n.º 25
0
        static void Main(string[] args)
        {
            //Initialize:
            var mode = "RELEASE";

            #if DEBUG
            mode = "DEBUG";
            #endif

            if (OS.IsWindows)
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
            }

            // expect first argument to be config file name
            if (args.Length > 0)
            {
                Config.MergeCommandLineArgumentsWithConfiguration(LeanArgumentParser.ParseArguments(args));
            }

            var liveMode = Config.GetBool("live-mode");
            Log.DebuggingEnabled = Config.GetBool("debug-mode");
            Log.FilePath         = Path.Combine(Config.Get("results-destination-folder"), "log.txt");
            Log.LogHandler       = Composer.Instance.GetExportedValueByTypeName <ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));

            //Name thread for the profiler:
            Thread.CurrentThread.Name = "Algorithm Analysis Thread";
            Log.Trace($"Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v{Globals.Version} Mode: {mode} ({(Environment.Is64BitProcess ? "64" : "32")}bit) Host: {Environment.MachineName}");
            Log.Trace("Engine.Main(): Started " + DateTime.Now.ToShortTimeString());

            //Import external libraries specific to physical server location (cloud/local)

            try
            {
                leanEngineSystemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);
            }
            catch (CompositionException compositionException)
            {
                Log.Error("Engine.Main(): Failed to load library: " + compositionException);
                throw;
            }

            //Setup packeting, queue and controls system: These don't do much locally.
            leanEngineSystemHandlers.Initialize();

            //-> Pull job from QuantConnect job queue, or, pull local build:
            string assemblyPath;
            job = leanEngineSystemHandlers.JobQueue.NextJob(out assemblyPath);

            if (job == null)
            {
                const string jobNullMessage = "Engine.Main(): Sorry we could not process this algorithm request.";
                Log.Error(jobNullMessage);
                throw new ArgumentException(jobNullMessage);
            }

            try
            {
                leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
            }
            catch (CompositionException compositionException)
            {
                Log.Error("Engine.Main(): Failed to load library: " + compositionException);
                throw;
            }

            // if the job version doesn't match this instance version then we can't process it
            // we also don't want to reprocess redelivered jobs
            if (job.Redelivered)
            {
                Log.Error("Engine.Run(): Job Version: " + job.Version + "  Deployed Version: " + Globals.Version + " Redelivered: " + job.Redelivered);
                //Tiny chance there was an uncontrolled collapse of a server, resulting in an old user task circulating.
                //In this event kill the old algorithm and leave a message so the user can later review.
                leanEngineSystemHandlers.Api.SetAlgorithmStatus(job.AlgorithmId, AlgorithmStatus.RuntimeError, _collapseMessage);
                leanEngineSystemHandlers.Notify.SetAuthentication(job);
                leanEngineSystemHandlers.Notify.Send(new RuntimeErrorPacket(job.UserId, job.AlgorithmId, _collapseMessage));
                leanEngineSystemHandlers.JobQueue.AcknowledgeJob(job);
                Exit(1);
            }

            try
            {
                // Set our exit handler for the algorithm
                Console.CancelKeyPress += new ConsoleCancelEventHandler(ExitKeyPress);

                // Create the algorithm manager and start our engine
                algorithmManager = new AlgorithmManager(liveMode, job);

                leanEngineSystemHandlers.LeanManager.Initialize(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);

                var engine = new Engine.Engine(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, liveMode);
                engine.Run(job, algorithmManager, assemblyPath, WorkerThread.Instance);
            }
            finally
            {
                var algorithmStatus = algorithmManager?.State ?? AlgorithmStatus.DeployError;

                Exit(algorithmStatus != AlgorithmStatus.Completed ? 1 : 0);
            }
        }
Exemplo n.º 26
0
        public static void RunLocalBacktest(string algorithm, Dictionary <string, string> expectedStatistics, AlphaRuntimeStatistics expectedAlphaStatistics, Language language)
        {
            var statistics      = new Dictionary <string, string>();
            var alphaStatistics = new AlphaRuntimeStatistics();

            Composer.Instance.Reset();
            var ordersLogFile = string.Empty;
            var logFile       = $"./regression/{algorithm}.{language.ToLower()}.log";

            Directory.CreateDirectory(Path.GetDirectoryName(logFile));
            File.Delete(logFile);

            try
            {
                // set the configuration up
                Config.Set("algorithm-type-name", algorithm);
                Config.Set("live-mode", "false");
                Config.Set("environment", "");
                Config.Set("messaging-handler", "QuantConnect.Messaging.Messaging");
                Config.Set("job-queue-handler", "QuantConnect.Queues.JobQueue");
                Config.Set("setup-handler", "RegressionSetupHandlerWrapper");
                Config.Set("history-provider", "RegressionHistoryProviderWrapper");
                Config.Set("api-handler", "QuantConnect.Api.Api");
                Config.Set("result-handler", "QuantConnect.Lean.Engine.Results.RegressionResultHandler");
                Config.Set("algorithm-language", language.ToString());
                Config.Set("algorithm-location",
                           language == Language.Python
                        ? "../../../Algorithm.Python/" + algorithm + ".py"
                        : "QuantConnect.Algorithm." + language + ".dll");


                var debugEnabled = Log.DebuggingEnabled;


                var logHandlers = new ILogHandler[] { new ConsoleLogHandler(), new FileLogHandler(logFile, false) };
                using (Log.LogHandler = new CompositeLogHandler(logHandlers))
                    using (var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance))
                        using (var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance))
                        {
                            Log.DebuggingEnabled = true;

                            Log.LogHandler.Trace("");
                            Log.LogHandler.Trace("{0}: Running " + algorithm + "...", DateTime.UtcNow);
                            Log.LogHandler.Trace("");

                            // run the algorithm in its own thread

                            var engine = new Lean.Engine.Engine(systemHandlers, algorithmHandlers, false);
                            Task.Factory.StartNew(() =>
                            {
                                string algorithmPath;
                                var job = systemHandlers.JobQueue.NextJob(out algorithmPath);
                                var algorithmManager = new AlgorithmManager(false);
                                engine.Run(job, algorithmManager, algorithmPath);
                                ordersLogFile = ((RegressionResultHandler)algorithmHandlers.Results).OrdersLogFilePath;
                            }).Wait();

                            var backtestingResultHandler = (BacktestingResultHandler)algorithmHandlers.Results;
                            statistics = backtestingResultHandler.FinalStatistics;

                            var defaultAlphaHandler = (DefaultAlphaHandler)algorithmHandlers.Alphas;
                            alphaStatistics = defaultAlphaHandler.RuntimeStatistics;

                            Log.DebuggingEnabled = debugEnabled;
                        }
            }
            catch (Exception ex)
            {
                Log.LogHandler.Error("{0} {1}", ex.Message, ex.StackTrace);
            }

            foreach (var stat in expectedStatistics)
            {
                Assert.AreEqual(true, statistics.ContainsKey(stat.Key), "Missing key: " + stat.Key);
                Assert.AreEqual(stat.Value, statistics[stat.Key], "Failed on " + stat.Key);
            }

            if (expectedAlphaStatistics != null)
            {
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.MeanPopulationScore.Direction);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.MeanPopulationScore.Magnitude);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.RollingAveragedPopulationScore.Direction);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.RollingAveragedPopulationScore.Magnitude);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.LongShortRatio);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalInsightsClosed);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalInsightsGenerated);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalAccumulatedEstimatedAlphaValue);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalInsightsAnalysisCompleted);
            }

            // we successfully passed the regression test, copy the log file so we don't have to continually
            // re-run master in order to compare against a passing run
            var passedFile = logFile.Replace("./regression/", "./passed/");

            Directory.CreateDirectory(Path.GetDirectoryName(passedFile));
            File.Delete(passedFile);
            File.Copy(logFile, passedFile);

            var passedOrderLogFile = ordersLogFile.Replace("./regression/", "./passed/");

            Directory.CreateDirectory(Path.GetDirectoryName(passedFile));
            File.Delete(passedOrderLogFile);
            if (File.Exists(ordersLogFile))
            {
                File.Copy(ordersLogFile, passedOrderLogFile);
            }
        }
Exemplo n.º 27
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();
                }
            }
        }
Exemplo n.º 28
0
        public static AlgorithmRunnerResults RunLocalBacktest(
            string algorithm,
            Dictionary <string, string> expectedStatistics,
            AlphaRuntimeStatistics expectedAlphaStatistics,
            Language language,
            AlgorithmStatus expectedFinalStatus,
            DateTime?startDate  = null,
            DateTime?endDate    = null,
            string setupHandler = "RegressionSetupHandlerWrapper",
            decimal?initialCash = null)
        {
            AlgorithmManager algorithmManager = null;
            var statistics      = new Dictionary <string, string>();
            var alphaStatistics = new AlphaRuntimeStatistics(new TestAccountCurrencyProvider());
            BacktestingResultHandler results = null;

            Composer.Instance.Reset();
            SymbolCache.Clear();

            var ordersLogFile = string.Empty;
            var logFile       = $"./regression/{algorithm}.{language.ToLower()}.log";

            Directory.CreateDirectory(Path.GetDirectoryName(logFile));
            File.Delete(logFile);

            try
            {
                // set the configuration up
                Config.Set("algorithm-type-name", algorithm);
                Config.Set("live-mode", "false");
                Config.Set("environment", "");
                Config.Set("messaging-handler", "QuantConnect.Messaging.Messaging");
                Config.Set("job-queue-handler", "QuantConnect.Queues.JobQueue");
                Config.Set("setup-handler", setupHandler);
                Config.Set("history-provider", "RegressionHistoryProviderWrapper");
                Config.Set("api-handler", "QuantConnect.Api.Api");
                Config.Set("result-handler", "QuantConnect.Lean.Engine.Results.RegressionResultHandler");
                Config.Set("algorithm-language", language.ToString());
                Config.Set("algorithm-location",
                           language == Language.Python
                        ? "../../../Algorithm.Python/" + algorithm + ".py"
                        : "QuantConnect.Algorithm." + language + ".dll");

                // Store initial log variables
                var initialLogHandler   = Log.LogHandler;
                var initialDebugEnabled = Log.DebuggingEnabled;

                // Log handlers specific to this test function
                var newLogHandlers = new ILogHandler[] { new ConsoleErrorLogHandler(), new FileLogHandler(logFile, false) };

                using (Log.LogHandler = new CompositeLogHandler(newLogHandlers))
                    using (var algorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance))
                        using (var systemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance))
                            using (var workerThread = new TestWorkerThread())
                            {
                                Log.DebuggingEnabled = true;

                                Log.Trace("");
                                Log.Trace("{0}: Running " + algorithm + "...", DateTime.UtcNow);
                                Log.Trace("");

                                // run the algorithm in its own thread
                                var engine = new Lean.Engine.Engine(systemHandlers, algorithmHandlers, false);
                                Task.Factory.StartNew(() =>
                                {
                                    try
                                    {
                                        string algorithmPath;
                                        var job          = (BacktestNodePacket)systemHandlers.JobQueue.NextJob(out algorithmPath);
                                        job.BacktestId   = algorithm;
                                        job.PeriodStart  = startDate;
                                        job.PeriodFinish = endDate;
                                        if (initialCash.HasValue)
                                        {
                                            job.CashAmount = new CashAmount(initialCash.Value, Currencies.USD);
                                        }
                                        algorithmManager = new AlgorithmManager(false, job);

                                        systemHandlers.LeanManager.Initialize(systemHandlers, algorithmHandlers, job, algorithmManager);

                                        engine.Run(job, algorithmManager, algorithmPath, workerThread);
                                        ordersLogFile = ((RegressionResultHandler)algorithmHandlers.Results).LogFilePath;
                                    }
                                    catch (Exception e)
                                    {
                                        Log.Trace($"Error in AlgorithmRunner task: {e}");
                                    }
                                }).Wait();

                                var backtestingResultHandler = (BacktestingResultHandler)algorithmHandlers.Results;
                                results    = backtestingResultHandler;
                                statistics = backtestingResultHandler.FinalStatistics;

                                var defaultAlphaHandler = (DefaultAlphaHandler)algorithmHandlers.Alphas;
                                alphaStatistics = defaultAlphaHandler.RuntimeStatistics;
                            }

                // Reset settings to initial values
                Log.LogHandler       = initialLogHandler;
                Log.DebuggingEnabled = initialDebugEnabled;
            }
            catch (Exception ex)
            {
                if (expectedFinalStatus != AlgorithmStatus.RuntimeError)
                {
                    Log.Error("{0} {1}", ex.Message, ex.StackTrace);
                }
            }

            if (algorithmManager?.State != expectedFinalStatus)
            {
                Assert.Fail($"Algorithm state should be {expectedFinalStatus} and is: {algorithmManager?.State}");
            }

            foreach (var stat in expectedStatistics)
            {
                Assert.AreEqual(true, statistics.ContainsKey(stat.Key), "Missing key: " + stat.Key);
                Assert.AreEqual(stat.Value, statistics[stat.Key], "Failed on " + stat.Key);
            }

            if (expectedAlphaStatistics != null)
            {
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.MeanPopulationScore.Direction);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.MeanPopulationScore.Magnitude);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.RollingAveragedPopulationScore.Direction);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.RollingAveragedPopulationScore.Magnitude);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.LongShortRatio);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalInsightsClosed);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalInsightsGenerated);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalAccumulatedEstimatedAlphaValue);
                AssertAlphaStatistics(expectedAlphaStatistics, alphaStatistics, s => s.TotalInsightsAnalysisCompleted);
            }

            // we successfully passed the regression test, copy the log file so we don't have to continually
            // re-run master in order to compare against a passing run
            var passedFile = logFile.Replace("./regression/", "./passed/");

            Directory.CreateDirectory(Path.GetDirectoryName(passedFile));
            File.Delete(passedFile);
            File.Copy(logFile, passedFile);

            var passedOrderLogFile = ordersLogFile.Replace("./regression/", "./passed/");

            Directory.CreateDirectory(Path.GetDirectoryName(passedFile));
            File.Delete(passedOrderLogFile);
            if (File.Exists(ordersLogFile))
            {
                File.Copy(ordersLogFile, passedOrderLogFile);
            }

            return(new AlgorithmRunnerResults(algorithm, language, algorithmManager, results));
        }
Exemplo n.º 29
0
        static void Main(string[] args)
        {
            if (OS.IsWindows)
            {
                Console.OutputEncoding = System.Text.Encoding.UTF8;
            }

            // expect first argument to be config file name
            if (args.Length > 0)
            {
                Config.MergeCommandLineArgumentsWithConfiguration(LeanArgumentParser.ParseArguments(args));
            }

            var liveMode = Config.GetBool("live-mode");

            //Name thread for the profiler:
            Thread.CurrentThread.Name = "Algorithm Analysis Thread";

            Initializer.Start();
            leanEngineSystemHandlers = Initializer.GetSystemHandlers();

            //-> Pull job from QuantConnect job queue, or, pull local build:
            string assemblyPath;

            job = leanEngineSystemHandlers.JobQueue.NextJob(out assemblyPath);

            leanEngineAlgorithmHandlers = Initializer.GetAlgorithmHandlers();

            if (job == null)
            {
                const string jobNullMessage = "Engine.Main(): Sorry we could not process this algorithm request.";
                Log.Error(jobNullMessage);
                throw new ArgumentException(jobNullMessage);
            }

            // if the job version doesn't match this instance version then we can't process it
            // we also don't want to reprocess redelivered jobs
            if (job.Redelivered)
            {
                Log.Error("Engine.Run(): Job Version: " + job.Version + "  Deployed Version: " + Globals.Version + " Redelivered: " + job.Redelivered);
                //Tiny chance there was an uncontrolled collapse of a server, resulting in an old user task circulating.
                //In this event kill the old algorithm and leave a message so the user can later review.
                leanEngineSystemHandlers.Api.SetAlgorithmStatus(job.AlgorithmId, AlgorithmStatus.RuntimeError, _collapseMessage);
                leanEngineSystemHandlers.Notify.SetAuthentication(job);
                leanEngineSystemHandlers.Notify.Send(new RuntimeErrorPacket(job.UserId, job.AlgorithmId, _collapseMessage));
                leanEngineSystemHandlers.JobQueue.AcknowledgeJob(job);
                Exit(1);
            }

            try
            {
                // Set our exit handler for the algorithm
                Console.CancelKeyPress += new ConsoleCancelEventHandler(ExitKeyPress);

                // Create the algorithm manager and start our engine
                algorithmManager = new AlgorithmManager(liveMode, job);

                leanEngineSystemHandlers.LeanManager.Initialize(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);

                var engine = new Engine.Engine(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, liveMode);
                engine.Run(job, algorithmManager, assemblyPath, WorkerThread.Instance);
            }
            finally
            {
                var algorithmStatus = algorithmManager?.State ?? AlgorithmStatus.DeployError;

                Exit(algorithmStatus != AlgorithmStatus.Completed ? 1 : 0);
            }
        }
Exemplo n.º 30
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();
                }
            }
        }