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 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;
                }
            }
        }
        private void TuneAllIndicators(StockSerie stockSerie)
        {
            float indicatorSARAccelerationStepStep = float.MaxValue;
            float indicatorSARFastSwingFactorStep = float.MaxValue;

            StockPortofolio bestPortofolio = null;
            StockPersonality bestPersonality = null;

            List<FloatPropertyRange> sarexParamRangeList = null;
            FloatPropertyRange accelerationStepRange = null;
            FloatPropertyRange fastSwingFactorRange = null;

            #region Tune only margin with no indicator
            // Generate report header
            if (this.generateReportCheckBox.Checked)
            {
                this.simulationParameterControl.GenerateReportHeader("TuningReport_" + stockSerie.StockName + ".csv", false);
            }
            sarexParamRangeList = SarexParamRangeListFromDialog(ref indicatorSARAccelerationStepStep, ref indicatorSARFastSwingFactorStep);
            accelerationStepRange = sarexParamRangeList.First(r => r.Name == "IndicatorSARAccelerationStep");
            fastSwingFactorRange = sarexParamRangeList.First(r => r.Name == "IndicatorSARFastSwingFactor");

            stockSerie.StockAnalysis.StockPersonality = new StockPersonality();

            TuneSarexParams(stockSerie, sarexParamRangeList, ref bestPortofolio, ref bestPersonality);

            // Update GUI with new parameters
            if (bestPersonality != null)
            {
                stockSerie.StockAnalysis.StockPersonality = bestPersonality.Clone();
                stockSerie.ResetSAREX();
                this.simulationParameterControl.Personality = bestPersonality.Clone();
                this.simulationParameterControl.Refresh();

                this.Portofolio = bestPortofolio;
                if (SimulationCompleted != null)
                {
                    SimulationCompleted();
                }

                // Generate report header
                if (this.generateReportCheckBox.Checked)
                {
                    this.simulationParameterControl.GenerateReportHeader("TuningReport_" + stockSerie.StockName + ".csv", true);
                    this.simulationParameterControl.GenerateReportLine("TuningReport_" + stockSerie.StockName + ".csv", stockSerie, bestPortofolio);
                }
            }
            #endregion

            this.stockPortofolioList.Remove(bestPortofolio.Name);
            this.stockPortofolioList.Add(bestPortofolio);
            this.Portofolio = bestPortofolio;
            stockSerie.StockAnalysis.StockPersonality = bestPersonality;
        }
        private void TuneAllIndicators(StockSerie stockSerie)
        {
            float indicatorSARAccelerationStepStep = float.MaxValue;
            float indicatorSARFastSwingFactorStep = float.MaxValue;

            float bestReturn = float.MinValue;
            StockPersonality bestPersonality = null;

            List<FloatPropertyRange> sarexParamRangeList = null;
            FloatPropertyRange accelerationStepRange = null;
            FloatPropertyRange fastSwingFactorRange = null;

            #region Tune only margin with no indicator

            sarexParamRangeList = SarexParamRangeListFromDialog(ref indicatorSARAccelerationStepStep, ref indicatorSARFastSwingFactorStep);
            accelerationStepRange = sarexParamRangeList.First(r => r.Name == "IndicatorSARAccelerationStep");
            fastSwingFactorRange = sarexParamRangeList.First(r => r.Name == "IndicatorSARFastSwingFactor");

            stockSerie.StockAnalysis.StockPersonality = new StockPersonality();

            TuneSarexParams(stockSerie, sarexParamRangeList, ref bestReturn, ref bestPersonality);

            // Update GUI with new parameters
            if (bestPersonality != null)
            {
                stockSerie.StockAnalysis.StockPersonality = bestPersonality.Clone();
                stockSerie.ResetSAREX();
                this.simulationParameterControl.Personality = bestPersonality.Clone();
                this.simulationParameterControl.Refresh();

                if (!this.allStocksCheckBox.Checked)
                {
                    MessageBox.Show("Best return is " + bestReturn.ToString("P2"));
                }
                if (this.generateReportCheckBox.Checked)
                {
                    this.GenerateReportLine(group + "_SARTuningReport.csv", stockSerie, bestReturn);
                }
            }
            #endregion

            stockSerie.StockAnalysis.StockPersonality = bestPersonality;
        }