Ejemplo n.º 1
0
        /// <summary>
        /// Creates and adds chromosomes to population
        /// </summary>
        /// <param name="size">Number of cromosomes in initial population</param>
        /// <param name="population">Population to fill up</param>
        private static void AddChromosomes(int size, ref Population population)
        {
            // checking for evenness
            if (size % 2 != 0)
            {
                throw new ArgumentException("Population size must be an even number.");
            }

            // create the chromosomes and add to collection
            for (var p = 0; p < size; p++)
            {
                var chromosome = new Chromosome();

                var v = new ConfigVars
                {
                    Vars =
                    {
                        ["bollinger-period"]     = RandomNumberBetweenInt(10, 30),
                        ["bollinger-multiplier"] = RandomNumberBetween(1.8, 2.9)
                    }
                };

                chromosome.Genes.Add(new Gene(v));

                population.Solutions.Add(chromosome);
            }
        }
Ejemplo n.º 2
0
 protected bool Equals(ConfigVars other)
 {
     foreach (KeyValuePair<string,object> kvp in vars) {
         if (kvp.Value.ToString () != other.vars [kvp.Key].ToString ())
             return false;
     }
     return true;
 }
Ejemplo n.º 3
0
 protected bool Equals(ConfigVars other)
 {
     foreach (KeyValuePair <string, object> kvp in vars)
     {
         if (kvp.Value.ToString() != other.vars [kvp.Key].ToString())
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 4
0
 protected bool Equals(ConfigVars other)
 {
     foreach (var kvp in Vars)
     {
         if (kvp.Value.ToString() != other.Vars [kvp.Key].ToString())
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 5
0
        static void ga_OnRunComplete(object sender, GaEventArgs e)
        {
            var fittest = e.Population.GetTop(1)[0];

            foreach (var gene in fittest.Genes)
            {
                ConfigVars v = (ConfigVars)gene.ObjectValue;
                foreach (KeyValuePair <string, object> kvp in v.vars)
                {
                    Console.WriteLine("Variable {0}:, value {1}", kvp.Key, kvp.Value.ToString());
                }
            }
        }
Ejemplo n.º 6
0
        public decimal Run(ConfigVars vars)
        {
            foreach (KeyValuePair <string, object> kvp in vars.vars)
            {
                Config.Set(kvp.Key, kvp.Value.ToString());
            }

            LaunchLean();
            ConsoleResultHandler resultshandler = (ConsoleResultHandler)_resultshandler;
            var sharpe_ratio = 0.0m;
            var ratio        = resultshandler.FinalStatistics ["Sharpe Ratio"];

            Decimal.TryParse(ratio, out sharpe_ratio);
            return(sharpe_ratio);
        }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            _ads = SetupAppDomain();


            const double crossoverProbability = 0.65;
            const double mutationProbability  = 0.08;
            const int    elitismPercentage    = 5;

            //create the population
            //var population = new Population(100, 44, false, false);

            var population = new Population();

            //create the chromosomes
            for (var p = 0; p < 10; p++)
            {
                var chromosome = new Chromosome();
                for (int i = 0; i < 2; i++)
                {
                    ConfigVars v = new ConfigVars();
                    v.vars ["EMA_VAR1"] = RandomNumberBetweenInt(0, 20);
                    v.vars ["EMA_VAR2"] = RandomNumberBetweenInt(0, 100);
                    //v.vars ["LTD3"] = RandomNumberBetweenInt (0, 100);
                    //v.vars ["LTD4"] = RandomNumberBetweenInt (2, 200);

                    chromosome.Genes.Add(new Gene(v));
                }
                chromosome.Genes.ShuffleFast();
                population.Solutions.Add(chromosome);
            }



            //create the genetic operators
            var elite = new Elite(elitismPercentage);

            var crossover = new Crossover(crossoverProbability, true)
            {
                CrossoverType = CrossoverType.SinglePoint
            };

            var mutation = new BinaryMutate(mutationProbability, true);

            //create the GA itself
            var ga = new GeneticAlgorithm(population, CalculateFitness);

            //subscribe to the GAs Generation Complete event
            ga.OnGenerationComplete += ga_OnGenerationComplete;

            //add the operators to the ga process pipeline
//			ga.Operators.Add(elite);
//			ga.Operators.Add(crossover);
//			ga.Operators.Add(mutation);

            var cv_operator = new ConfigVarsOperator();

            ga.Operators.Add(cv_operator);

            //run the GA
            ga.Run(Terminate);
        }
Ejemplo n.º 8
0
        public decimal Run(ConfigVars vars)
        {
            foreach (var kvp in vars.Vars)
            {
                Config.Set(kvp.Key, kvp.Value.ToString());
            }

            // settings
            Config.Set("environment", "backtesting");
            Config.Set("algorithm-type-name", "BitfinexSuperTrend");
            Config.Set("algorithm-language", "CSharp");
            Config.Set("algorithm-location", "Optimization.exe");
            Config.Set("data-folder", "C:/Users/stranger/Google Drive/Data/");

            // default value set in QuantConnect.Configuration.Config is invalid. specify explicitely.
            Config.Set("job-queue-handler", "QuantConnect.Queues.JobQueue");

            // log handler
            Log.LogHandler = Composer.Instance.GetExportedValueByTypeName <ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));

            // == LeanEngineSystemHandlers ==

            LeanEngineSystemHandlers leanEngineSystemHandlers;

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

            // can this be omitted?
            leanEngineSystemHandlers.Initialize();

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

            if (job == null)
            {
                throw new Exception("Engine.Main(): Job was null.");
            }

            // == LeanEngineAlgorithmHandlers ==

            LeanEngineAlgorithmHandlers leanEngineAlgorithmHandlers;

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

            // == Engine ==

            try
            {
                var liveMode         = Config.GetBool("live-mode");
                var algorithmManager = new AlgorithmManager(liveMode);
                // can this be omitted?
                leanEngineSystemHandlers.LeanManager.Initialize(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, job, algorithmManager);
                var engine = new Engine(leanEngineSystemHandlers, leanEngineAlgorithmHandlers, liveMode);
                engine.Run(job, algorithmManager, assemblyPath);
            }
            finally
            {
                // no Acknowledge Job, clean up resources
                Log.Trace("Engine.Main(): Packet removed from queue: " + job.AlgorithmId);
                leanEngineSystemHandlers.Dispose();
                leanEngineAlgorithmHandlers.Dispose();
                Log.LogHandler.Dispose();
            }

            // obtain results
            var sharpeRatio    = 0.0m;
            var resultshandler = leanEngineAlgorithmHandlers.Results as BacktestingResultHandler;

            if (resultshandler != null)
            {
                var ratio = resultshandler.FinalStatistics["Sharpe Ratio"];
                Decimal.TryParse(ratio, out sharpeRatio);
            }
            else
            {
                Log.Error("Unable to cast: BacktestingResultHandler");
            }

            return(sharpeRatio);
        }
Ejemplo n.º 9
0
        public static void Main(string[] args)
        {
            _ads = SetupAppDomain ();

            const double crossoverProbability = 0.65;
            const double mutationProbability = 0.08;
            const int elitismPercentage = 5;

            //create the population
            //var population = new Population(100, 44, false, false);

            var population = new Population();

            //create the chromosomes
            for (var p = 0; p < 10; p++)
            {

                var chromosome = new Chromosome();
                for (int i = 0; i < 2; i++) {
                    ConfigVars v = new ConfigVars ();
                    v.vars ["EMA_VAR1"] = RandomNumberBetweenInt (0, 20);
                    v.vars ["EMA_VAR2"] = RandomNumberBetweenInt (0, 100);
                    //v.vars ["LTD3"] = RandomNumberBetweenInt (0, 100);
                    //v.vars ["LTD4"] = RandomNumberBetweenInt (2, 200);

                    chromosome.Genes.Add (new Gene (v));
                }
                chromosome.Genes.ShuffleFast();
                population.Solutions.Add(chromosome);
            }

            //create the genetic operators
            var elite = new Elite(elitismPercentage);

            var crossover = new Crossover(crossoverProbability, true)
            {
                CrossoverType = CrossoverType.SinglePoint
            };

            var mutation = new BinaryMutate(mutationProbability, true);

            //create the GA itself
            var ga = new GeneticAlgorithm(population, CalculateFitness);

            //subscribe to the GAs Generation Complete event
            ga.OnGenerationComplete += ga_OnGenerationComplete;

            //add the operators to the ga process pipeline
            //			ga.Operators.Add(elite);
            //			ga.Operators.Add(crossover);
            //			ga.Operators.Add(mutation);

            var cv_operator = new ConfigVarsOperator ();
            ga.Operators.Add (cv_operator);

            //run the GA
            ga.Run(Terminate);
        }
Ejemplo n.º 10
0
        public decimal Run(ConfigVars vars)
        {
            foreach(KeyValuePair<string,object> kvp in vars.vars)
                Config.Set (kvp.Key, kvp.Value.ToString ());

            LaunchLean ();
            ConsoleResultHandler resultshandler = (ConsoleResultHandler)_resultshandler;
            var sharpe_ratio = 0.0m;
            var ratio = resultshandler.FinalStatistics ["Sharpe Ratio"];
            Decimal.TryParse(ratio,out sharpe_ratio);
            return sharpe_ratio;
        }