private static double RunAlgorithm(Chromosome chromosome)
        {
            var sum_sharpe = 0.0;
            var i          = 0;

            foreach (var gene in chromosome.Genes)
            {
                Console.WriteLine("Running gene number {0}", i);
                var       val = (ConfigVars)gene.ObjectValue;
                AppDomain ad  = null;
                RunClass  rc  = CreateRunClassInAppDomain(ref ad);
                foreach (KeyValuePair <string, object> kvp in val.vars)
                {
                    Console.WriteLine("Running algorithm with variable {0}:, value {1}", kvp.Key, kvp.Value.ToString());
                }

                var res = (double)rc.Run(val);
                Console.WriteLine("Sharpe ratio: {0}", res);
                sum_sharpe += res;
                AppDomain.Unload(ad);
                Console.WriteLine("Sum Sharpe ratio: {0}", sum_sharpe);

                i++;
            }

            return(sum_sharpe);
        }
Exemple #2
0
        private static double RunAlgorithm(List <string> algos, Dictionary <string, DateRange> daysDictionary)
        {
            var sum_sharpe = 0.0;

            foreach (string s in algos)
            {
                foreach (string key in daysDictionary.Keys)
                {
                    var       val       = s;
                    var       startDate = daysDictionary[key].startDate;
                    var       endDate   = daysDictionary[key].endDate;
                    AppDomain ad        = null;
                    RunClass  rc        = CreateRunClassInAppDomain(ref ad);
                    Console.WriteLine("Running algorithm {0} for: {1} to {2}", val, startDate, endDate);

                    try
                    {
                        sum_sharpe += (double)rc.Run(val, startDate, endDate);
                    }
                    catch (Exception e)
                    {
                        Log.Error(e.Message + e.StackTrace);
                    }
                    AppDomain.Unload(ad);

                    // After the Lean Engine has run and is deallocated,
                    // rename my custom mylog.csv file to include the algorithm name.
                    //  mylog.csv is written in the algorithm.  Replace with your custom logs.
                    try
                    {
                        string f          = AssemblyLocator.ExecutingDirectory();
                        string sourcefile = f + @"mylog.csv";
                        if (File.Exists(sourcefile))
                        {
                            string destfile = f + string.Format(@"mylog{0}.csv", s);
                            if (File.Exists(destfile))
                            {
                                File.Delete(destfile);
                            }
                            File.Move(sourcefile, destfile);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    runnumber++;
                }
            }

            return(sum_sharpe);
        }
        static RunClass CreateRunClassInAppDomain(ref AppDomain ad)
        {
            // Create the second AppDomain.
            var name = Guid.NewGuid().ToString("x");

            ad = AppDomain.CreateDomain(name, null, _ads);

            // Create an instance of MarshalbyRefType in the second AppDomain.
            // A proxy to the object is returned.
            RunClass rc = (RunClass)ad.CreateInstanceAndUnwrap(_exeAssembly, typeof(RunClass).FullName);

            return(rc);
        }