private void WorkerOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            dispatcher.Invoke((Action)delegate
             {
            trends.Clear();
             });

             // Calculate duration
             this.CurrentStock = 0;
             this.NbSelectedStocks = 0;
             this.ProgressVisible = true;
             foreach (StockSerie stockSerie in stockSeries)
             {
            if (stockSerie.Initialise())
            {
               MTFTrend trend = new MTFTrend(stockSerie.StockName);
               try
               {
                  stockSerie.BarDuration = barDuration1;
                  IStockUpDownState upDownState = stockSerie.GetTrailStop(indicatorName);
                  trend.Trend1 = upDownState.UpDownState.Last();
                  if (trend.Trend1 == StockSerie.Trend.UpTrend && this.SelectedView == SelectedTrend.DownTrendOnly)
                     continue;
                  if (trend.Trend1 == StockSerie.Trend.DownTrend && this.SelectedView == SelectedTrend.UpTrendOnly)
                     continue;

                  float close = stockSerie.Values.Last().CLOSE;
                  float distToStop = 0;
                  int nbBars = 0;

                  if (trend.Trend1 == StockSerie.Trend.UpTrend)
                  {
                     distToStop = ((upDownState as IStockTrailStop).Series[0].Last - close)/close;
                  }
                  else
                  {
                     distToStop = ((upDownState as IStockTrailStop).Series[1].Last - close) / close;
                  }
                  for (int i = upDownState.UpDownState.Length - 2;
                        i > 1 &&
                        upDownState.UpDownState[i] ==
                        trend.Trend1;
                        i-- ,nbBars++) ;
                     trend.ToolTip1 = "Close: " + close + Environment.NewLine +
                                      "Stop %: " + distToStop.ToString("P2") + Environment.NewLine +
                                      "Nb Bars: " + nbBars;

                  stockSerie.BarDuration = barDuration2;
                  upDownState = stockSerie.GetTrailStop(indicatorName);
                  trend.Trend2 = upDownState.UpDownState.Last();
                  if (trend.Trend2 == StockSerie.Trend.UpTrend && this.SelectedView == SelectedTrend.DownTrendOnly)
                     continue;
                  if (trend.Trend2 == StockSerie.Trend.DownTrend && this.SelectedView == SelectedTrend.UpTrendOnly)
                     continue;

                  close = stockSerie.Values.Last().CLOSE;
                  distToStop = 0;
                  nbBars = 0;

                  if (trend.Trend2 == StockSerie.Trend.UpTrend)
                  {
                     distToStop = ((upDownState as IStockTrailStop).Series[0].Last - close) / close;
                  }
                  else
                  {
                     distToStop = ((upDownState as IStockTrailStop).Series[1].Last - close) / close;
                  }
                  for (int i = upDownState.UpDownState.Length - 2;
                        i > 1 &&
                        upDownState.UpDownState[i] ==
                        trend.Trend2;
                        i--, nbBars++) ;
                  trend.ToolTip2 = "Close: " + close + Environment.NewLine +
                                   "Stop %: " + distToStop.ToString("P2") + Environment.NewLine +
                                   "Nb Bars: " + nbBars;

                  stockSerie.BarDuration = barDuration3;
                  upDownState = stockSerie.GetTrailStop(indicatorName);
                  trend.Trend3 = upDownState.UpDownState.Last();
                  if (trend.Trend3 == StockSerie.Trend.UpTrend && this.SelectedView == SelectedTrend.DownTrendOnly)
                     continue;
                  if (trend.Trend3 == StockSerie.Trend.DownTrend && this.SelectedView == SelectedTrend.UpTrendOnly)
                     continue;

                  close = stockSerie.Values.Last().CLOSE;
                  distToStop = 0;
                  nbBars = 0;

                  if (trend.Trend3 == StockSerie.Trend.UpTrend)
                  {
                     distToStop = ((upDownState as IStockTrailStop).Series[0].Last - close) / close;
                  }
                  else
                  {
                     distToStop = ((upDownState as IStockTrailStop).Series[1].Last - close) / close;
                  }
                  for (int i = upDownState.UpDownState.Length - 2;
                        i > 1 &&
                        upDownState.UpDownState[i] ==
                        trend.Trend3;
                        i--, nbBars++) ;
                  trend.ToolTip3 = "Close: " + close + Environment.NewLine +
                                   "Stop %: " + distToStop.ToString("P2") + Environment.NewLine +
                                   "Nb Bars: " + nbBars;
               }
               catch (Exception ex)
               {
                  StockLog.Write(ex);
               }
               switch (SelectedView)
               {
                  case SelectedTrend.All:
                     AddTrend(trend);
                     break;
                  case SelectedTrend.DownTrendOnly:
                     if (trend.Trend1 == StockSerie.Trend.DownTrend
                         && trend.Trend2 == StockSerie.Trend.DownTrend
                         && trend.Trend3 == StockSerie.Trend.DownTrend)
                     {
                        AddTrend(trend);
                     }
                     break;
                  case SelectedTrend.UpTrendOnly:
                     if (trend.Trend1 == StockSerie.Trend.UpTrend
                        && trend.Trend2 == StockSerie.Trend.UpTrend
                        && trend.Trend3 == StockSerie.Trend.UpTrend)
                     {
                        AddTrend(trend);
                     }
                     break;
               }
            }
            this.CurrentStock++;
             }
             this.ProgressVisible = false;
        }
 private void AddTrend(MTFTrend trend)
 {
     this.NbSelectedStocks++;
      dispatcher.Invoke((Action)delegate
      {
     trends.Add(trend);
      });
 }