Esempio n. 1
0
        public void EvaluateTest(DateTime startDate, DateTime endDate, int folds, string[] expectedDates, double[] expectedValues)
        {
            var config = new OptimizerConfiguration
            {
                StartDate           = startDate,
                EndDate             = endDate,
                FitnessTypeName     = "Optimization.OptimizerFitness",
                EnableFitnessFilter = true,
                Genes   = SetupGenes(),
                Fitness = new FitnessConfiguration {
                    Folds = folds
                }
            };

            var unit = new Wrapper(config);

            GeneFactory.Initialize(config.Genes);

            var actual = unit.Evaluate(new Chromosome(true, config.Genes));

            Assert.AreEqual(1, actual);

            var actualDates  = new List <DateTime>();
            var actualValues = new List <double>();


            for (int i = 0; i < unit.ActualInSampleConfig.Count(); i++)
            {
                actualDates.Add(unit.ActualInSampleConfig[i].StartDate.Value);
                actualDates.Add(unit.ActualInSampleConfig[i].EndDate.Value);
                actualDates.Add(unit.ActualOutSampleConfig[i].StartDate.Value);
                actualDates.Add(unit.ActualOutSampleConfig[i].EndDate.Value);

                actualValues.Add((double)unit.ActualInSampleChromosome[i].ToDictionary()["Key"]);
                actualValues.Add((double)unit.ActualInSampleChromosome[i].ToDictionary()["Key2"]);
                actualValues.Add((double)unit.ActualOutSampleList[i]["Key"]);
                actualValues.Add((double)unit.ActualOutSampleList[i]["Key2"]);
            }

            expectedDates.Select(s => DateTime.Parse(s)).ShouldDeepEqual(actualDates);
            expectedValues.ShouldDeepEqual(actualValues);
        }
        public void GenerateTest()
        {
            var config = new[] { new GeneConfiguration {
                                     Key = "slow", Actual = 200
                                 }, new GeneConfiguration {
                                     Key = "take", Precision = 2, Max = 0.06,
                                     Min = 0.04, Actual = 0.05
                                 } };

            RandomizationProvider.Current = new BasicRandomization();
            GeneFactory.Initialize(config);

            var actual = GeneFactory.Generate(config[0], true);

            Assert.AreEqual(200, (double)((KeyValuePair <string, object>)actual.Value).Value);

            RandomizationProvider.Current = new BasicRandomization();
            actual = GeneFactory.Generate(config[1], false);

            Assert.IsTrue(double.TryParse(((KeyValuePair <string, object>)actual.Value).Value.ToString(), out var parsed));
            Assert.AreEqual(2, GeneFactory.GetPrecision(parsed));
        }
        public void MinimizeTest()
        {
            var genes = new[]
            {
                new GeneConfiguration
                {
                    Key    = "1",
                    Actual = 1.23
                },
                new GeneConfiguration
                {
                    Key    = "2",
                    Actual = 123,
                    Min    = 1,
                    Max    = 123
                },
                new GeneConfiguration
                {
                    Key = "3",
                    Min = 1,
                    Max = 2
                }
            };

            var config = new OptimizerConfiguration
            {
                Genes               = genes,
                FitnessTypeName     = "Optimization.OptimizerFitness",
                EnableFitnessFilter = true,
            };

            var unit = new Mock <SharpeMaximizer>(config, new FitnessFilter())
            {
                CallBase = true
            };

            unit.Setup(x => x.RunAlgorithm(It.IsAny <Dictionary <string, object> >(), It.IsAny <IOptimizerConfiguration>())).Returns <Dictionary <string, object>, IOptimizerConfiguration>((l, c) =>
            {
                return(new Dictionary <string, decimal> {
                    { "SharpeRatio", 1 }, { "CompoundingAnnualReturn", 1 }, { "TotalNumberOfTrades", 1 },
                    { "LossRate", 0.1m }
                });
            });

            GeneFactory.Initialize(config.Genes);
            RandomizationProvider.Current = new BasicRandomization();
            var chromosome = new Chromosome(true, genes, false);

            var actual = unit.Object.Minimize(new[] { 3.21, 321, 456 }, chromosome);

            Assert.AreEqual(0.999, actual.Error);
            Assert.AreEqual(1.23, actual.ParameterSet[0]);
            Assert.AreEqual(123, actual.ParameterSet[1]);
            Assert.AreEqual(456, actual.ParameterSet[2]);

            var nextActual = unit.Object.Minimize(new[] { 3.21, 321, 789 }, chromosome);

            Assert.AreEqual(0.999, nextActual.Error);
            Assert.AreEqual(3.21, nextActual.ParameterSet[0]);
            Assert.AreEqual(321, nextActual.ParameterSet[1]);
            Assert.AreEqual(789, nextActual.ParameterSet[2]);
        }
Esempio n. 4
0
 public void InitializeTest()
 {
     GeneFactory.Initialize(new GeneConfiguration[0]);
     Assert.IsNotNull(GeneFactory.Config);
 }