public virtual double Evaluate(IChromosome chromosome)
        {
            try
            {
                string output = "";
                var    list   = ((Chromosome)chromosome).ToDictionary();

                foreach (var item in list)
                {
                    output += item.Key + ": " + item.Value.ToString() + ", ";
                }

                var result = OptimizerAppDomainManager.RunAlgorithm(list);

                var fitness = CalculateFitness(result);

                output += string.Format("{0}: {1}", this.Name, fitness.Value);
                Program.Logger.Info(output);

                return(fitness.Fitness);
            }
            catch (Exception ex)
            {
                Program.Logger.Error(ex);
                return(0);
            }
        }
Exemple #2
0
        public void Initialize(string[] args)
        {
            _config = LoadConfig(args);
            _file.File.Copy(_config.ConfigPath, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"), true);

            string path = _config.AlgorithmLocation;

            if (!string.IsNullOrEmpty(path) && (path.Contains('\\') || path.Contains('/')))
            {
                _file.File.Copy(path, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFileName(path)), true);
                string pdb = path.Replace(System.IO.Path.GetExtension(path), ".pdb");

                //due to locking issues, need to manually clean to update pdb
                if (!_file.File.Exists(pdb))
                {
                    _file.File.Copy(pdb, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFileName(pdb)), true);
                }
            }

            OptimizerAppDomainManager.Initialize();

            OptimizerFitness fitness = (OptimizerFitness)Assembly.GetExecutingAssembly().CreateInstance(_config.FitnessTypeName, false, BindingFlags.Default, null,
                                                                                                        new object[] { _config, new FitnessFilter() }, null, null);

            _manager.Initialize(_config, fitness);
            _manager.Start();
        }
Exemple #3
0
        public Dictionary <string, decimal> Run(Dictionary <string, object> items, IOptimizerConfiguration config)
        {
            Dictionary <string, Dictionary <string, decimal> > results = OptimizerAppDomainManager.GetResults(AppDomain.CurrentDomain);

            _config = config;

            _id = (items.ContainsKey("Id") ? items["Id"] : Guid.NewGuid()).ToString();

            if (_config.StartDate.HasValue && _config.EndDate.HasValue)
            {
                if (!items.ContainsKey("startDate"))
                {
                    items.Add("startDate", _config.StartDate);
                }
                if (!items.ContainsKey("endDate"))
                {
                    items.Add("endDate", _config.EndDate);
                }
            }

            string jsonKey = JsonConvert.SerializeObject(items);

            if (results.ContainsKey(jsonKey))
            {
                return(results[jsonKey]);
            }

            //just ignore id gene
            foreach (var pair in items.Where(i => i.Key != "Id"))
            {
                if (pair.Value is DateTime?)
                {
                    var cast = ((DateTime?)pair.Value);
                    if (cast.HasValue)
                    {
                        Config.Set(pair.Key, cast.Value.ToString("O"));
                    }
                }
                else
                {
                    Config.Set(pair.Key, pair.Value.ToString());
                }
            }

            if (EngineContext.SystemHandlers == null || EngineContext.Engine == null)
            {
                LaunchLean();
            }

            RunJob();

            if (_resultsHandler.FullResults != null && _resultsHandler.FullResults.Any())
            {
                results.Add(jsonKey, _resultsHandler.FullResults);
                OptimizerAppDomainManager.SetResults(AppDomain.CurrentDomain, results);
            }

            return(_resultsHandler.FullResults);
        }
 public virtual Dictionary <string, decimal> RunAlgorithm(Dictionary <string, object> list, IOptimizerConfiguration config)
 {
     if (config.UseSharedAppDomain)
     {
         return(SingleAppDomainManager.RunAlgorithm(list, config));
     }
     else
     {
         return(OptimizerAppDomainManager.RunAlgorithm(list, config));
     }
 }
        public void Initialize(string[] args)
        {
            _config = LoadConfig(args);
            _file.File.Copy(_config.ConfigPath, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json"), true);

            string path = _config.AlgorithmLocation;

            if (!string.IsNullOrEmpty(path) && (path.Contains('\\') || path.Contains('/')))
            {
                _file.File.Copy(path, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFileName(path)), true);
                string pdb = path.Replace(System.IO.Path.GetExtension(path), ".pdb");

                //due to locking issues, need to manually clean to update pdb
                if (!_file.File.Exists(pdb))
                {
                    _file.File.Copy(pdb, System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.IO.Path.GetFileName(pdb)), true);
                }
            }

            if (_config.UseSharedAppDomain)
            {
                SingleAppDomainManager.Initialize();
            }
            else
            {
                OptimizerAppDomainManager.Initialize();
            }

            OptimizerFitness fitness = (OptimizerFitness)Assembly.GetExecutingAssembly().CreateInstance(_config.FitnessTypeName, false, BindingFlags.Default, null,
                                                                                                        new object[] { _config, new FitnessFilter() }, null, null);

            if (_manager == null)
            {
                if (new[] { typeof(SharpeMaximizer), typeof(NFoldCrossReturnMaximizer), typeof(NestedCrossSharpeMaximizer), typeof(NFoldCrossSharpeMaximizer) }.Contains(fitness.GetType()))
                {
                    _manager = new MaximizerManager();
                    if (fitness.GetType() == typeof(OptimizerFitness))
                    {
                        LogProvider.ErrorLogger.Info("Fitness for shared app domain was switched to the default: SharpeMaximizer.");
                        fitness = new SharpeMaximizer(_config, new FitnessFilter());
                    }
                }
                else
                {
                    _manager = new GeneManager();
                }
            }

            _manager.Initialize(_config, fitness);
            _manager.Start();
        }
Exemple #6
0
        public Dictionary <string, decimal> Run(Dictionary <string, object> items)
        {
            Dictionary <string, Dictionary <string, decimal> > results = OptimizerAppDomainManager.GetResults(AppDomain.CurrentDomain);

            _config = OptimizerAppDomainManager.GetConfig(AppDomain.CurrentDomain);

            _id = (items.ContainsKey("Id") ? items["Id"] : Guid.NewGuid()).ToString();
            items.Remove("Id");

            if (_config.StartDate.HasValue && _config.EndDate.HasValue)
            {
                if (!items.ContainsKey("startDate"))
                {
                    items.Add("startDate", _config.StartDate);
                }
                if (!items.ContainsKey("endDate"))
                {
                    items.Add("endDate", _config.EndDate);
                }
            }

            string plain = string.Join(",", items.Select(s => s.Value));

            if (results.ContainsKey(plain))
            {
                return(results[plain]);
            }

            foreach (var pair in items)
            {
                if (pair.Value is DateTime?)
                {
                    var cast = ((DateTime?)pair.Value);
                    if (cast.HasValue)
                    {
                        Config.Set(pair.Key, cast.Value.ToString("O"));
                    }
                }
                else
                {
                    Config.Set(pair.Key, pair.Value.ToString());
                }
            }

            LaunchLean();

            results.Add(plain, _resultsHandler.FullResults);
            OptimizerAppDomainManager.SetResults(AppDomain.CurrentDomain, results);

            return(_resultsHandler.FullResults);
        }
        public virtual double Evaluate(IChromosome chromosome)
        {
            Name = "Sharpe";

            try
            {
                string output = "";
                var    list   = ((Chromosome)chromosome).ToDictionary();

                list.Add("Id", ((Chromosome)chromosome).Id);

                foreach (var item in list)
                {
                    output += item.Key + ": " + item.Value.ToString() + ", ";
                }

                if (Config.StartDate.HasValue && Config.EndDate.HasValue)
                {
                    output += string.Format("Start: {0}, End: {1}, ", Config.StartDate, Config.EndDate);
                }

                Dictionary <string, decimal> result = null;

                if (Config.UseSharedAppDomain)
                {
                    result = SingleAppDomainManager.RunAlgorithm(list, Config);
                }
                else
                {
                    result = OptimizerAppDomainManager.RunAlgorithm(list, Config);
                }

                if (result == null)
                {
                    return(0);
                }

                var fitness = CalculateFitness(result);

                output += string.Format("{0}: {1}", Name, fitness.Value);
                Program.Logger.Info(output);

                return(fitness.Fitness);
            }
            catch (Exception ex)
            {
                Program.Logger.Error(ex);
                return(0);
            }
        }
Exemple #8
0
        public virtual void Initialize()
        {
            var fullResults = OptimizerAppDomainManager.GetResults();
            //let's exclude non-trading tests and any marked as error/failure with -10 Sharpe
            var hasTraded = fullResults.Where(d => d.Value["TotalNumberOfTrades"] != 0 && d.Value["SharpeRatio"] > -10);

            SharpeData  = hasTraded.ToDictionary(k => k.Key, v => (double)v.Value["SharpeRatio"]);
            ReturnsData = hasTraded.ToDictionary(k => k.Key, v => (double)v.Value["CompoundingAnnualReturn"]);

            N = SharpeData.Count();
            var statistics = new DescriptiveStatistics(ReturnsData.Select(d => d.Value));

            V = new DescriptiveStatistics(SharpeData.Select(s => s.Value)).Variance;
            //measure only trading days
            T        = ((Config.EndDate - Config.StartDate).Value.TotalDays / 365) * days;
            Skewness = statistics.Skewness;
            Kurtosis = statistics.Kurtosis;
        }
Exemple #9
0
        //todo: configurable k-folds
        public override Dictionary <string, decimal> GetScore(Dictionary <string, object> list, IOptimizerConfiguration config)
        {
            //todo: threads
            var score = base.GetScore(list, config);

            var period   = Config.StartDate.Value - Config.EndDate.Value.Date.AddDays(1).AddMilliseconds(-1);
            var oneThird = period.Ticks / 3;

            var oneThirdConfig = Clone((OptimizerConfiguration)Config);

            oneThirdConfig.EndDate = Config.StartDate.Value.AddTicks(oneThird);
            OptimizerAppDomainManager.RunAlgorithm(list, oneThirdConfig).Select(s => score[s.Key] += s.Value);

            var twoThirdsConfig = Clone((OptimizerConfiguration)Config);

            twoThirdsConfig.EndDate = Config.StartDate.Value.AddTicks(oneThird * 2);
            OptimizerAppDomainManager.RunAlgorithm(list, oneThirdConfig).Select(s => score[s.Key] += s.Value);
            return(score.ToDictionary(d => d.Key, d => d.Value / 3));
        }
Exemple #10
0
        protected void ExtendFailureKeys(DateTime extending)
        {
            var failures = OptimizerAppDomainManager.GetResults().Where(r => r.Value["SharpeRatio"] == -10m);

            var previousKey  = JsonConvert.SerializeObject(Config.StartDate);
            var extendingKey = JsonConvert.SerializeObject(extending);

            var switching = new List <Tuple <string, string> >();

            foreach (var item in failures)
            {
                var after = item.Key.Replace(previousKey, extendingKey);
                switching.Add(Tuple.Create(item.Key, after));
            }

            foreach (var item in switching)
            {
                var before = OptimizerAppDomainManager.GetResults()[item.Item1];
                OptimizerAppDomainManager.GetResults().Remove(item.Item1);
                OptimizerAppDomainManager.GetResults().Add(item.Item2, before);
            }
        }
 public virtual Dictionary <string, decimal> RunAlgorithm(Dictionary <string, object> list, IOptimizerConfiguration config)
 {
     return(OptimizerAppDomainManager.RunAlgorithm(list, config));
 }
        public Dictionary <string, decimal> Run(Dictionary <string, object> items, IOptimizerConfiguration config)
        {
            Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);

            Dictionary <string, Dictionary <string, decimal> > results = OptimizerAppDomainManager.GetResults(AppDomain.CurrentDomain);

            _config = config;

            _id = (items.ContainsKey("Id") ? items["Id"] : Guid.NewGuid().ToString("N")).ToString();

            if (_config.StartDate.HasValue && _config.EndDate.HasValue)
            {
                if (!items.ContainsKey("startDate"))
                {
                    items.Add("startDate", _config.StartDate);
                }
                if (!items.ContainsKey("endDate"))
                {
                    items.Add("endDate", _config.EndDate);
                }
            }

            string jsonKey = JsonConvert.SerializeObject(items.Where(i => i.Key != "Id"));

            if (!config.EnableRunningDuplicateParameters && results.ContainsKey(jsonKey))
            {
                return(results[jsonKey]);
            }

            //just ignore id gene
            foreach (var pair in items.Where(i => i.Key != "Id"))
            {
                if (pair.Value is DateTime?)
                {
                    var cast = ((DateTime?)pair.Value);
                    if (cast.HasValue)
                    {
                        Config.Set(pair.Key, cast.Value.ToString("O"));
                    }
                }
                else
                {
                    Config.Set(pair.Key, pair.Value.ToString());
                }
            }

            LogProvider.TraceLogger.Trace($"id: {_id} started.");
            LaunchLean();
            LogProvider.TraceLogger.Trace($"id: {_id} finished.");

            if (_resultsHandler.FullResults != null && _resultsHandler.FullResults.Any())
            {
                if (config.EnableRunningDuplicateParameters && results.ContainsKey(jsonKey))
                {
                    results.Remove(jsonKey);
                }
                results.Add(jsonKey, _resultsHandler.FullResults);
                OptimizerAppDomainManager.SetResults(AppDomain.CurrentDomain, results);
            }

            return(_resultsHandler.FullResults);
        }