public StatusEventArgs(GeneticResult geneticResult) { this.GeneticResult = geneticResult; }
public void Init(TradeSystem tradeSystem, bool seedSystem, double stopOutValue) { Initiated(); this.Proceed = true; this.workToDo = this.GenerationSize * this.NumberOfIteration; this.workDone = 0; this.CurrentIteration = 0; this.CurrentGeneration = 0; this.BestResult = null; List<Chromosome> currentChromosomes; List<TradeRule> mixedTradeRules; Chromosome seedChromosome = null; if (seedSystem) { seedChromosome = new Chromosome() { BuyCondition = tradeSystem.BuyCondition, SellCondition = tradeSystem.SellCondition }; } //if (this.MixedTradeRules == null) //{ // this.MixedTradeRules = this.MachineLearnerTradeRule.GenerateTradeRuleForGroups(this.PopulationSize); // this.BuyRules = this.BuyRules.Concat(this.MixedTradeRules).ToList(); // this.SellRules = this.SellRules.Concat(this.MixedTradeRules).ToList(); //} TradeType? tradeType = null; if (tradeSystem.Longs && !tradeSystem.Shorts) // long only { tradeType = TradeType.longTrade; } else if (!tradeSystem.Longs && tradeSystem.Shorts) // short only { tradeType = TradeType.shortTrade; } for (int i = 1; i <= this.NumberOfIteration; i++) { if (!Proceed) break; mixedTradeRules = this.MachineLearnerTradeRule.GenerateTradeRuleForGroups(this.PopulationSize); this.BuyRules = this.BuyRulesCache.Concat(mixedTradeRules).ToList(); this.SellRules = this.SellRulesCache.Concat(mixedTradeRules).ToList(); if (i == 1 && !seedSystem) { this.BestResult = null; } else if (!this.Seed) { this.BestResult = null; } this.CurrentIteration = i; currentChromosomes = new List<Chromosome>(); UpdateProgress(string.Format("Processing Iteration: {0}", i)); if (!Proceed) break; for (int g = 1; g <= this.GenerationSize; g++) { if (!currentChromosomes.Any()) { currentChromosomes = GeneratePopulation(); if(seedChromosome != null) { currentChromosomes[0] = seedChromosome; } } if (!Proceed) break; currentChromosomes = EvaluateFitness(tradeSystem, currentChromosomes, i, g, stopOutValue, tradeType).Select(d => d.Chromosome).Cast<Chromosome>().ToList(); if (this.BestResult != null) { ChromosomeProcessed(this.BestResult); currentChromosomes = Breed(currentChromosomes); } this.workDone++; this.CurrentGeneration = g; UpdateProgress(string.Format("Completed - Iteration {0}, Generation {1}", i, g)); } } Completed(); }
private void ChromosomeProcessed(GeneticResult geneticResult) { if (OnChromosomeProcessed == null) return; StatusEventArgs args; args = new StatusEventArgs(geneticResult); OnChromosomeProcessed(this, args); }
public List<dynamic> EvaluateFitness(TradeSystem tradeSystem, List<Chromosome> chromosomes, int iteration, int generation, double stopOutValue, TradeType? tradeType = null) { List<dynamic> list = new List<dynamic>(); if (chromosomes == null) return list; Chromosome bestChromosome = null; double currentFitnessValue = 0D; double bestFitnessValue = double.MinValue; this.Evaluate.SetTradeRules(chromosomes.SelectMany(c=>c.BuyCondition.TradeRules.Concat(c.SellCondition.TradeRules))); Trades trades = null; TradeCondition buyCond; TradeCondition sellCond; int totalTrades = 0; int winners = 0; int losers = 0; int stopout = 0; Chromosome chr = null; for (int i = 0; i < chromosomes.Count; i++) { chr = chromosomes[i]; buyCond = chr.BuyCondition; sellCond = chr.SellCondition; trades = this.Evaluate.EvalTrades(tradeSystem, buyCond, sellCond, stopOutValue); double endingCapital = Math.Round((tradeSystem.FinancialSettings.InitialCapital + trades.TradeTable.Select().Sum(r => (double)r["Profit"])), 2); currentFitnessValue = endingCapital > 0 ? endingCapital : 0; if (!Proceed) { return list; } if (bestFitnessValue < currentFitnessValue) { bestChromosome = chr; bestFitnessValue = currentFitnessValue; winners = this.Evaluate.Winners; losers = this.Evaluate.Losers; stopout = this.Evaluate.StopOuts; totalTrades = trades.Count(); } dynamic d = new System.Dynamic.ExpandoObject(); d.FitnessValue = currentFitnessValue; d.Chromosome = chr; list.Add(d); } TradeSystem ts = tradeSystem.CloneWithOutConditions(); ts.BuyCondition = bestChromosome.BuyCondition; ts.SellCondition = bestChromosome.SellCondition; GeneticResult result = new GeneticResult(ts, bestFitnessValue, winners, losers, totalTrades, stopout, iteration, generation); if (this.BestResult == null || this.BestResult.FitnessValue < result.FitnessValue) { this.BestResult = result; } this.BestResult.Iteration = iteration; this.BestResult.Generation = generation; return list.OrderByDescending(d => d.FitnessValue).ToList(); }