コード例 #1
0
ファイル: LeanOptimizerTests.cs プロジェクト: yzhao20/Lean
        public void MaximizeEulerWithConstraints()
        {
            var resetEvent = new ManualResetEvent(false);
            var packet     = new OptimizationNodePacket
            {
                OptimizationStrategy   = "QuantConnect.Optimizer.Strategies.EulerSearchOptimizationStrategy",
                Criterion              = new Target("Profit", new Maximization(), null),
                OptimizationParameters = new HashSet <OptimizationParameter>
                {
                    new OptimizationStepParameter("ema-slow", 1, 10, 1),
                    new OptimizationStepParameter("ema-fast", 10, 100, 10m, 0.1m)
                },
                Constraints = new List <Constraint>
                {
                    new Constraint("Drawdown", ComparisonOperatorTypes.LessOrEqual, 0.15m)
                },
                MaximumConcurrentBacktests   = 20,
                OptimizationStrategySettings = new StepBaseOptimizationStrategySettings {
                    DefaultSegmentAmount = 10
                }
            };
            var optimizer = new FakeLeanOptimizer(packet);

            OptimizationResult result = null;

            optimizer.Ended += (s, solution) =>
            {
                result = solution;
                optimizer.DisposeSafely();
                resetEvent.Set();
            };

            optimizer.Start();

            resetEvent.WaitOne();
            Assert.NotNull(result);
            Assert.AreEqual(
                15,
                JsonConvert.DeserializeObject <BacktestResult>(result.JsonBacktestResult).Statistics.Profit);
            Assert.AreEqual(
                0.15m,
                JsonConvert.DeserializeObject <BacktestResult>(result.JsonBacktestResult).Statistics.Drawdown);
        }
コード例 #2
0
ファイル: LeanOptimizerTests.cs プロジェクト: yzhao20/Lean
        public void MaximizeNoTarget(string strategyName)
        {
            var resetEvent = new ManualResetEvent(false);
            var packet     = new OptimizationNodePacket
            {
                OptimizationStrategy = strategyName,
                Criterion            = new Target("Profit",
                                                  new Maximization(),
                                                  null),
                OptimizationParameters = new HashSet <OptimizationParameter>
                {
                    new OptimizationStepParameter("ema-slow", 1, 10, 1),
                    new OptimizationStepParameter("ema-fast", 10, 100, 3)
                },
                MaximumConcurrentBacktests   = 20,
                OptimizationStrategySettings = new StepBaseOptimizationStrategySettings {
                    DefaultSegmentAmount = 10
                }
            };
            var optimizer = new FakeLeanOptimizer(packet);

            OptimizationResult result = null;

            optimizer.Ended += (s, solution) =>
            {
                result = solution;
                optimizer.DisposeSafely();
                resetEvent.Set();
            };

            optimizer.Start();

            resetEvent.WaitOne();
            Assert.NotNull(result);
            Assert.AreEqual(
                110,
                JsonConvert.DeserializeObject <BacktestResult>(result.JsonBacktestResult).Statistics.Profit);

            Assert.AreEqual(10, result.ParameterSet.Value["ema-slow"].ToDecimal());
            Assert.AreEqual(100, result.ParameterSet.Value["ema-fast"].ToDecimal());
        }
コード例 #3
0
ファイル: LeanOptimizerTests.cs プロジェクト: yzhao20/Lean
        public void MinimizeWithTargetAndConstraints()
        {
            var resetEvent = new ManualResetEvent(false);
            var packet     = new OptimizationNodePacket
            {
                Criterion = new Target("Profit", new Minimization(), 20),
                OptimizationParameters = new HashSet <OptimizationParameter>
                {
                    new OptimizationStepParameter("ema-slow", 1, 10, 1),
                    new OptimizationStepParameter("ema-fast", 10, 100, 3)
                },
                Constraints = new List <Constraint>
                {
                    new Constraint("Drawdown", ComparisonOperatorTypes.LessOrEqual, 0.15m)
                },
                MaximumConcurrentBacktests = 20
            };
            var optimizer = new FakeLeanOptimizer(packet);

            OptimizationResult result = null;

            optimizer.Ended += (s, solution) =>
            {
                result = solution;
                optimizer.DisposeSafely();
                resetEvent.Set();
            };

            optimizer.Start();

            resetEvent.WaitOne();
            Assert.NotNull(result);
            Assert.GreaterOrEqual(
                20,
                JsonConvert.DeserializeObject <BacktestResult>(result.JsonBacktestResult).Statistics.Profit);
            Assert.GreaterOrEqual(
                0.15m,
                JsonConvert.DeserializeObject <BacktestResult>(result.JsonBacktestResult).Statistics.Drawdown);
        }
コード例 #4
0
ファイル: LeanOptimizerTests.cs プロジェクト: yzhao20/Lean
        public void TrackEstimation()
        {
            Config.Set("optimization-update-interval", 1);
            OptimizationResult result = null;
            var resetEvent            = new ManualResetEvent(false);
            var packet = new OptimizationNodePacket
            {
                Criterion = new Target("Profit", new Minimization(), null),
                OptimizationParameters = new HashSet <OptimizationParameter>
                {
                    new OptimizationStepParameter("ema-slow", 1, 10, 1),
                    new OptimizationStepParameter("ema-fast", 10, 100, 10)
                },
                Constraints = new List <Constraint>
                {
                    new Constraint("Drawdown", ComparisonOperatorTypes.LessOrEqual, 0.15m)
                },
                MaximumConcurrentBacktests = 5
            };
            var optimizer = new FakeLeanOptimizer(packet);
            // keep stats up-to-date
            int totalBacktest  = optimizer.GetCurrentEstimate();
            int totalUpdates   = 0;
            int completedTests = 0;
            int failed         = 0;

            optimizer.Update += (s, e) =>
            {
                var runtimeStats = optimizer.GetRuntimeStatistics();
                Assert.LessOrEqual(int.Parse(runtimeStats["Running"], CultureInfo.InvariantCulture), packet.MaximumConcurrentBacktests);
                Assert.LessOrEqual(completedTests, int.Parse(runtimeStats["Completed"], CultureInfo.InvariantCulture));
                Assert.LessOrEqual(failed, int.Parse(runtimeStats["Failed"], CultureInfo.InvariantCulture));

                Assert.AreEqual(totalBacktest, optimizer.GetCurrentEstimate());

                completedTests = int.Parse(runtimeStats["Completed"], CultureInfo.InvariantCulture);
                failed         = int.Parse(runtimeStats["Failed"], CultureInfo.InvariantCulture);

                if (completedTests > 0)
                {
                    // 'ms' aren't stored so might be 0
                    Assert.GreaterOrEqual(TimeSpan.Parse(runtimeStats["Average Length"], CultureInfo.InvariantCulture), TimeSpan.Zero);
                }

                totalUpdates++;
            };
            optimizer.Ended += (s, solution) =>
            {
                result = solution;
                optimizer.DisposeSafely();
                resetEvent.Set();
            };

            optimizer.Start();

            resetEvent.WaitOne();

            var runtimeStatistics = optimizer.GetRuntimeStatistics();

            Assert.NotZero(int.Parse(runtimeStatistics["Completed"], CultureInfo.InvariantCulture));
            Assert.NotZero(int.Parse(runtimeStatistics["Failed"], CultureInfo.InvariantCulture));
            // we have 2 force updates at least, expect a few more over it.
            Assert.Greater(totalUpdates, 2);
            Assert.AreEqual(int.Parse(runtimeStatistics["Completed"], CultureInfo.InvariantCulture)
                            + int.Parse(runtimeStatistics["Failed"], CultureInfo.InvariantCulture)
                            + int.Parse(runtimeStatistics["Running"], CultureInfo.InvariantCulture), totalBacktest);
        }
コード例 #5
0
ファイル: LeanOptimizerTests.cs プロジェクト: lennon33/Lean
        public void TrackEstimation()
        {
            Config.Set("optimization-update-interval", 1);
            OptimizationEstimate estimate = null;
            OptimizationResult   result   = null;
            var resetEvent = new ManualResetEvent(false);
            var packet     = new OptimizationNodePacket
            {
                Criterion = new Target("Profit", new Minimization(), null),
                OptimizationParameters = new HashSet <OptimizationParameter>
                {
                    new OptimizationStepParameter("ema-slow", 1, 10, 1),
                    new OptimizationStepParameter("ema-fast", 10, 100, 3)
                },
                Constraints = new List <Constraint>
                {
                    new Constraint("Drawdown", ComparisonOperatorTypes.LessOrEqual, 0.15m)
                },
                MaximumConcurrentBacktests = 5
            };
            var optimizer = new FakeLeanOptimizer(packet);
            // keep stats up-to-date
            int totalBacktest  = optimizer.GetCurrentEstimate().TotalBacktest;
            int totalUpdates   = 0;
            int completedTests = 0;
            int failed         = 0;

            optimizer.Update += (s, e) =>
            {
                estimate = optimizer.GetCurrentEstimate();
                Assert.LessOrEqual(estimate.RunningBacktest, packet.MaximumConcurrentBacktests);
                Assert.LessOrEqual(completedTests, estimate.CompletedBacktest);
                Assert.LessOrEqual(failed, estimate.FailedBacktest);

                Assert.AreEqual(totalBacktest, estimate.TotalBacktest);

                completedTests = estimate.CompletedBacktest;
                failed         = estimate.FailedBacktest;

                if (completedTests > 0)
                {
                    Assert.Greater(estimate.AverageBacktest, TimeSpan.Zero);
                }

                totalUpdates++;
            };
            optimizer.Ended += (s, solution) =>
            {
                result   = solution;
                estimate = optimizer.GetCurrentEstimate();
                optimizer.DisposeSafely();
                resetEvent.Set();
            };

            optimizer.Start();

            resetEvent.WaitOne();

            Assert.NotZero(estimate.CompletedBacktest);
            Assert.NotZero(estimate.FailedBacktest);
            // we have 2 force updates at least, expect a few more over it.
            Assert.Greater(totalUpdates, 2);
            Assert.AreEqual(estimate.CompletedBacktest + estimate.FailedBacktest + estimate.RunningBacktest, totalBacktest);
        }