Exemplo n.º 1
0
        public void ThrowOnReinitialization()
        {
            int nextId = 1;

            Strategy.NewParameterSet += (s, parameterSet) =>
            {
                Assert.AreEqual(nextId++, parameterSet.Id);
            };

            var set1 = new HashSet <OptimizationParameter>()
            {
                new OptimizationStepParameter("ema-fast", 10, 100, 1)
            };

            Strategy.Initialize(new Target("Profit", new Maximization(), null), new List <Constraint>(), set1, CreateSettings());

            Strategy.PushNewResults(OptimizationResult.Initial);
            Assert.Greater(nextId, 1);

            var set2 = new HashSet <OptimizationParameter>()
            {
                new OptimizationStepParameter("ema-fast", 10, 100, 1),
                new OptimizationStepParameter("ema-slow", 10, 100, 2)
            };

            Assert.Throws <InvalidOperationException>(() =>
            {
                Strategy.Initialize(new Target("Profit", new Minimization(), null), null, set2, CreateSettings());
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="nodePacket">The optimization node packet to handle</param>
        protected LeanOptimizer(OptimizationNodePacket nodePacket)
        {
            if (nodePacket.OptimizationParameters.IsNullOrEmpty())
            {
                throw new ArgumentException("Cannot start an optimization job with no parameter to optimize");
            }

            if (string.IsNullOrEmpty(nodePacket.Criterion?.Target))
            {
                throw new ArgumentException("Cannot start an optimization job with no target to optimize");
            }

            NodePacket                  = nodePacket;
            OptimizationTarget          = NodePacket.Criterion;
            OptimizationTarget.Reached += (s, e) =>
            {
                // we've reached the optimization target
                TriggerOnEndEvent();
            };

            Strategy = (IOptimizationStrategy)Activator.CreateInstance(Type.GetType(NodePacket.OptimizationStrategy));

            RunningParameterSetForBacktest = new ConcurrentDictionary <string, ParameterSet>();
            PendingParameterSet            = new ConcurrentQueue <ParameterSet>();

            Strategy.Initialize(OptimizationTarget, nodePacket.Constraints, NodePacket.OptimizationParameters, NodePacket.OptimizationStrategySettings);

            Strategy.NewParameterSet += (s, parameterSet) =>
            {
                if (parameterSet == null)
                {
                    // shouldn't happen
                    Log.Error($"Strategy.NewParameterSet({GetLogDetails()}): generated a null {nameof(ParameterSet)} instance");
                    return;
                }
                LaunchLeanForParameterSet(parameterSet);
            };
        }