public static StockPersonality CreateDefaultPersonality() { StockPersonality newPersonality = new StockPersonality(); newPersonality.BuyMargin = 0.035f; newPersonality.SellMargin = 0.035f; return newPersonality; }
public StockPersonality Clone() { StockPersonality stockPersonality = new StockPersonality(); stockPersonality.BuyMargin = this.BuyMargin; stockPersonality.SellMargin = this.SellMargin; foreach (StockIndicator indicator in this.IndicatorDictionary.Values) { stockPersonality.IndicatorDictionary.Add(indicator.Type, indicator.Clone()); } return stockPersonality; }
private void TuneOneByOneIndicators(StockSerie stockSerie, List<StockIndicator> indicatorsToTune) { StockPersonality bestPersonality = null; float bestReturn = 0; StockPersonality newPersonality = new StockPersonality(); foreach (StockIndicator indicator in indicatorsToTune) { List<StockIndicator> indicatorList = new List<StockIndicator>(); indicatorList.Add(indicator); this.GAIndicatorList(indicatorList, stockSerie, 3, 10, ref bestPersonality, ref bestReturn); Console.WriteLine("Best return = " + bestReturn.ToString() + " best personality = " + bestPersonality.ToString()); newPersonality.IndicatorDictionary.Add(indicator.Type, indicator.Clone()); } stockSerie.StockAnalysis.StockPersonality = newPersonality; }
private void GAIndicatorList(List<StockIndicator> indicatorList, StockSerie stockSerie, int maxGeneration, int populationSize, ref StockPersonality bestPersonality, ref float bestReturn) { int nbFittest = 5; float impact, range, step, bestResult = float.MinValue; List<StockPersonality> personalityList = new List<StockPersonality>(); List<StockPersonality> tmpPersonalityList = null; List<StockPersonality> personalityNewGenList = new List<StockPersonality>(); StockPersonality personality = null; StockPersonality newPersonality = null; StockIndicator newIndicator = null; int nbTryPerIndicator = 10; // Generate initial population foreach (StockIndicator indicator in indicatorList) { if (personalityList.Count == 0) { impact = indicator.Range.Min; step = (indicator.Range.Max - indicator.Range.Min) / (float)(nbTryPerIndicator -1); for (int i = 0; i < nbTryPerIndicator; i++) { newIndicator = new StockIndicator(indicator.Type, true, impact, indicator.SmoothingType, impact - step, impact + step, step / 10.0f); newPersonality = new StockPersonality(); newPersonality.IndicatorDictionary.Add(indicator.Type, newIndicator); personalityList.Add(newPersonality); impact += step; } } else { tmpPersonalityList = new List<StockPersonality>(); foreach (StockPersonality currentPersonality in personalityList) { impact = indicator.Range.Min; step = (indicator.Range.Max - indicator.Range.Min) / (float)(nbTryPerIndicator-1); for (int i = 0; i < nbTryPerIndicator; i++) { newIndicator = new StockIndicator(indicator.Type, true, impact, indicator.SmoothingType, impact - step, impact + step, step / 10.0f); newPersonality = currentPersonality.Clone(); newPersonality.IndicatorDictionary.Add(indicator.Type, newIndicator); tmpPersonalityList.Add(newPersonality); impact += step; } } personalityList = tmpPersonalityList; } } // Start GA SortedDictionary<float, StockPersonality> returnDico = null; for (int n = 0; n < maxGeneration; n++) { // Calculate fittest personalities returnDico = this.EvalPersonalityList(stockSerie, personalityList); // Select the best personalities personalityList.Clear(); for (int i = 0; i < Math.Min(nbFittest, returnDico.Count); i++) { personality = returnDico.Values.ElementAt(returnDico.Count - i - 1); // Calculate new indicator min/max values foreach (StockIndicator stockIndicator in personality.IndicatorDictionary.Values) { range = (stockIndicator.Range.Max - stockIndicator.Range.Min) / 4.0f; stockIndicator.Range.Max = stockIndicator.Impact + range; stockIndicator.Range.Min = stockIndicator.Impact - range; } personalityList.Add(personality); } // at this point the nbFittest are stored in the array. // Generate new generation personalityNewGenList = GenerateNewGeneration(personalityList, populationSize); personalityList.AddRange(personalityNewGenList); bestPersonality = returnDico.Values.Last(); bestReturn = returnDico.Keys.Last(); Console.WriteLine("Best return = " + bestReturn.ToString() + " best personality = " + bestPersonality.ToString()); if (bestReturn <= bestResult) { break; } else { bestResult = bestReturn; } } }
public int CompareIndicators(StockPersonality other) { if (this.IndicatorDictionary.Values.Count(i => i.IsActive) == other.IndicatorDictionary.Values.Count(i => i.IsActive)) { foreach (StockIndicator indicator in this.IndicatorDictionary.Values) { if (other.IndicatorDictionary.Keys.Contains(indicator.Type)) { if (indicator.CompareTo(other.IndicatorDictionary[indicator.Type]) != 0) { return -1; } } else { return -1; } } } else { return -1; } return 0; }
private void UpdateBestForValue(StockSerie stockSerie, FloatPropertyRange range, bool refreshGUI, ref StockPortofolio bestPortofolio, ref StockPersonality bestPersonality, float currentValue) { stockSerie.StockAnalysis.StockPersonality.SetFloatPropertyValue(range, currentValue); stockSerie.ResetSAREX(); //Console.WriteLine("Fixed Fee:" + this.simulationParameterControl.fixedFee + " Tax Rate:" + this.simulationParameterControl.taxRate); StockPortofolio currentPortofolio = GenerateTradingSimulation(stockSerie, this.simulationParameterControl.StartDate, this.simulationParameterControl.EndDate.AddHours(18), this.simulationParameterControl.amount, this.simulationParameterControl.reinvest, this.simulationParameterControl.amendOrders, this.simulationParameterControl.supportShortSelling, this.simulationParameterControl.fixedFee, this.simulationParameterControl.taxRate); //if (generateReportCheckBox.Checked) //{ // this.simulationParameterControl.Personality = currentPersonality; // this.simulationParameterControl.GenerateReportLine("TuningReport_"+stockSerie.StockName+".csv", stockSerie, currentPortofolio); //} if ((bestPortofolio == null || bestPortofolio.TotalAddedValue < currentPortofolio.TotalAddedValue) && currentPortofolio.OrderList.Count != 0) { bestPersonality = stockSerie.StockAnalysis.StockPersonality.Clone(); //Console.WriteLine("new best" + bestPersonality.ToString()); //Console.WriteLine("new best" + currentPortofolio.ToString()); // Update new best portofolio bestPortofolio = currentPortofolio; // Refresh GUI if (refreshGUI) { this.simulationParameterControl.Personality = bestPersonality; } } if (!this.allStocksCheckBox.Checked) { if (this.progressBar.Value < this.progressBar.Maximum) { this.progressBar.Value++; } else { this.progressBar.Value = 0; } } }
private void TuneSarexParams(StockSerie stockSerie, List<FloatPropertyRange> propertyRangeList, ref StockPortofolio bestPortofolio, ref StockPersonality bestPersonality) { try { int nbIterations = 1; if (this.allStocksCheckBox.Checked) { nbIterations = this.stockComboBox.Items.Count; } else { foreach (FloatPropertyRange propRange in propertyRangeList) { nbIterations *= propRange.NbStep(); } } this.progressBar.Maximum = nbIterations; this.progressBar.Value = 0; TuneIndicatorList(stockSerie, propertyRangeList, true, ref bestPortofolio, ref bestPersonality); } catch (Exception exception) { System.Windows.Forms.MessageBox.Show(exception.Message, "Check input parameters"); } }
private void TuneProperty(StockSerie stockSerie, FloatPropertyRange range, bool refreshGUI, ref StockPortofolio bestPortofolio, ref StockPersonality bestPersonality) { float mediumValue = (range.Max + range.Min) / 2.0f; float maxWidth = (range.Max - range.Min) / 2.0f; UpdateBestForValue(stockSerie, range, refreshGUI, ref bestPortofolio, ref bestPersonality, mediumValue); float precision = range.Step / -100.0f; for (float currentWidth = range.Step; (maxWidth - currentWidth) > precision; currentWidth += range.Step) { UpdateBestForValue(stockSerie, range, refreshGUI, ref bestPortofolio, ref bestPersonality, mediumValue + currentWidth); UpdateBestForValue(stockSerie, range, refreshGUI, ref bestPortofolio, ref bestPersonality, mediumValue - currentWidth); } }
private void TuneIndicatorList(StockSerie stockSerie, List<FloatPropertyRange> rangeList, bool refreshGUI, ref StockPortofolio bestPortofolio, ref StockPersonality bestPersonality) { int rangeCount = rangeList.Count; if (rangeCount == 0) { return; } if (rangeCount == 1) { TuneProperty(stockSerie, rangeList[0], refreshGUI, ref bestPortofolio, ref bestPersonality); } else { FloatPropertyRange range = rangeList[0]; List<FloatPropertyRange> subList = rangeList.GetRange(1, rangeCount - 1); float mediumValue = (range.Max + range.Min) / 2.0f; float maxWidth = (range.Max - range.Min) / 2.0f; stockSerie.StockAnalysis.StockPersonality.SetFloatPropertyValue(range, mediumValue); TuneIndicatorList(stockSerie, subList, refreshGUI, ref bestPortofolio, ref bestPersonality); float precision = range.Step / -100.0f; for (float currentWidth = range.Step; (maxWidth - currentWidth) > precision; currentWidth += range.Step) { stockSerie.StockAnalysis.StockPersonality.SetFloatPropertyValue(range, mediumValue + currentWidth); TuneIndicatorList(stockSerie, subList, refreshGUI, ref bestPortofolio, ref bestPersonality); stockSerie.StockAnalysis.StockPersonality.SetFloatPropertyValue(range, mediumValue - currentWidth); TuneIndicatorList(stockSerie, subList, refreshGUI, ref bestPortofolio, ref bestPersonality); } } }
private void UpdateBestForValue(StockSerie stockSerie, FloatPropertyRange range, bool refreshGUI, ref float bestReturn, ref StockPersonality bestPersonality, float currentValue) { stockSerie.StockAnalysis.StockPersonality.SetFloatPropertyValue(range, currentValue); stockSerie.ResetSAREX(); float currentReturn = stockSerie.CalculateSarReturn(StockDataType.SAREX_FOLLOWER_CUSTOM); if ( currentReturn > bestReturn) { bestPersonality = stockSerie.StockAnalysis.StockPersonality.Clone(); bestReturn = currentReturn; // Refresh GUI if (refreshGUI) { this.simulationParameterControl.Personality = bestPersonality; } } if (!this.allStocksCheckBox.Checked) { if (this.progressBar.Value < this.progressBar.Maximum) { this.progressBar.Value++; } else { this.progressBar.Value = 0; } } }