Download MetaTrader 5

Remove duplicates from array

To add comments, please log in or register
Take active part in the life of the community. Improve your rating!
Richard Read
322
Richard Read 2015.05.24 14:05 

Hi, how do I remove duplicate values from an array? I have tried to add a check to stop it adding duplicate values in the first place - but this doesn't seem to work.

 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property indicator_chart_window 

input int barstocount=100;
double levels[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   ObjectDelete(0,"SR1");
   ObjectDelete(0,"SR2");
   ObjectDelete(0,"SR3");
   ObjectDelete(0,"SR4");
   ObjectDelete(0,"SR5");
   ObjectDelete(0,"SR6");
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectDelete(0,"SR1");
   ObjectDelete(0,"SR2");
   ObjectDelete(0,"SR3");
   ObjectDelete(0,"SR4");
   ObjectDelete(0,"SR5");
   ObjectDelete(0,"SR6");
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   DoChecks();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DoChecks()
  {
   double PriceCheck;

   ArrayResize(levels,0);

   for(int i=1;i<barstocount-1;i++)
     {
      //Check Top Break
      if(High[i]>High[i+1])
        {
         PriceCheck=High[i];
         for(int j=i+1;j<100;j++) //Lets look backwards for a tail
           {
            if(PriceCheck>Low[j] && PriceCheck<High[j])
              {
               if((PriceCheck>Close[j] && PriceCheck>Open[j]) || 
                  (PriceCheck<Close[j] && PriceCheck<Open[j]))
                 {
                  for(int k=i-1;k>0;k--)//we found a tail backwards, now lets look for a body forwards
                    {
                     if(PriceCheck>Low[k] && PriceCheck<High[k])
                       {
                        if((PriceCheck>Open[k] && PriceCheck<Close[k]) || 
                           (PriceCheck<Open[k] && PriceCheck>Close[k]))
                          {
                           bool adds=true;
                           for(int v=0;v<ArraySize(levels);v++)
                             {
                              if(levels[v]==NormalizeDouble(PriceCheck,Digits))
                                 adds=false;
                                 break;
                             }
                           if(adds==true)
                             {
                              ArrayResize(levels,ArraySize(levels)+1);
                              levels[ArraySize(levels)-1]=NormalizeDouble(PriceCheck,Digits);
                             }
                          }
                        else
                          {
                           break;
                          }
                       }
                    }
                 }
               else
                 {
                  break;
                 }
              }
           }
        }
      //Check Bottom Break
      if(Low[i]<Low[i+1])
        {
         PriceCheck=Low[i];
         for(int o=i+1;o<Bars;o++) //Lets look backwards for a tail
           {
            if(PriceCheck>Low[o] && PriceCheck<High[o])
              {
               if((PriceCheck>Close[o] && PriceCheck>Open[o]) || 
                  (PriceCheck<Close[o] && PriceCheck<Open[o]))
                 {
                  for(int u=i-1;u>0;u--)//we found a tail backwards, now lets look for a body forwards
                    {
                     if(PriceCheck>Low[u] && PriceCheck<High[u])
                       {
                        if((PriceCheck>Open[u] && PriceCheck<Close[u]) || 
                           (PriceCheck<Open[u] && PriceCheck>Close[u]))
                          {
                           bool add=true;
                           for(int g=0;g<ArraySize(levels);g++)
                             {
                              if(levels[g]==NormalizeDouble(PriceCheck,Digits))
                                 add=false;
                                 break;
                             }
                           if(add==true)
                             {
                              ArrayResize(levels,ArraySize(levels)+1);
                              levels[ArraySize(levels)-1]=NormalizeDouble(PriceCheck,Digits);
                             }
                          }
                        else
                          {
                           break;
                          }
                       }
                    }
                 }
               else
                 {
                  break;
                 }
              }
           }
        }
     }
   DrawLines();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DrawLines()
  {
   int oneup;

   ArraySort(levels,WHOLE_ARRAY,0,MODE_ASCEND);

   for(int i=0;i<ArraySize(levels)-1;i++)
     {
      if(levels[i]>Bid)
        {
         oneup=i;
         break;
        }
     }
   if(oneup<=0)
      oneup=ArraySize(levels)-1;

   ObjectCreate(0,"SR1",OBJ_HLINE,0,iTime(Symbol(),0,0),levels[oneup+2]);
   ObjectCreate(0,"SR2",OBJ_HLINE,0,iTime(Symbol(),0,0),levels[oneup+1]);
   ObjectCreate(0,"SR3",OBJ_HLINE,0,iTime(Symbol(),0,0),levels[oneup]);
   ObjectCreate(0,"SR4",OBJ_HLINE,0,iTime(Symbol(),0,0),levels[oneup-1]);
   ObjectCreate(0,"SR5",OBJ_HLINE,0,iTime(Symbol(),0,0),levels[oneup-2]);
   ObjectCreate(0,"SR6",OBJ_HLINE,0,iTime(Symbol(),0,0),levels[oneup-3]);
  }
//+------------------------------------------------------------------+

 Thanks!

 

Richard 

Keith Watford
Moderator
9619
Keith Watford 2015.05.24 18:27  
                           for(int v=0;v<ArraySize(levels);v++)
                             {
                              if(levels[v]==NormalizeDouble(PriceCheck,Digits))
                                 adds=false;
                                 break;
                             }
                           for(int g=0;g<ArraySize(levels);g++)
                             {
                              if(levels[g]==NormalizeDouble(PriceCheck,Digits))
                                 add=false;
                                 break;
                             }

No matter what the size of the loop, the break will stop it after the first pass

                           for(int v=0;v<ArraySize(levels);v++)
                             {
                              if(levels[v]==NormalizeDouble(PriceCheck,Digits))
                                 {
                                 adds=false;
                                 break;
                                 }
                             }
                           for(int g=0;g<ArraySize(levels);g++)
                             {
                              if(levels[g]==NormalizeDouble(PriceCheck,Digits))
                                 {
                                 add=false;
                                 break;
                                 }
                             }

 

 add the curly braces so that the loop completes

Richard Read
322
Richard Read 2015.05.24 18:41  

Can't believe I missed that! Thanks!

 

Richard 

To add comments, please log in or register