#property copyright "Copyright © FiatFap (http://www.forexfactory.com/fiatfap)"
#property link      "http://fxnewsalert.com/feedback.html" //Developer contact
#property indicator_chart_window

//+---------------Properties-----------------------------------------+
extern string DonateLink = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6H4FYBWQEZ8R2";
extern int LineStart = 150;
extern int Filter = 10;
extern bool UseCurrentTimeFrame = False;
extern int ClusterTimeFrame = 1;
//+------------------------------------------------------------------+

double arrayPrices[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init(){return(0);}
int deinit(){return(0);} 

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

int start()
{     
   static datetime candletemp;
      
   if(candletemp != iTime(NULL,ClusterTimeFrame,0))
   {   
      DeleteLines();
      PopulateCandleCloses();
      FindClusters(); 
      candletemp = iTime(NULL,ClusterTimeFrame,0);
   }
   
return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void DeleteLines()  
{
  int k=0;
  
  while (k<ObjectsTotal())   
  {
    string objname = ObjectName(k);
    if (StringSubstr(objname,0,5) == "tally")  
      ObjectDelete(objname);
    else
      k++;
  }    
  return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void PopulateCandleCloses()
{
   if (UseCurrentTimeFrame) {ArrayResize(arrayPrices,Bars); for (int i=0; i<=Bars; i++) arrayPrices[i] = Close[i];}  
   if (!UseCurrentTimeFrame) {ArrayResize(arrayPrices,iBars(NULL,ClusterTimeFrame)*3); for (int j=0; j<=iBars(NULL,ClusterTimeFrame)*3; j+=3) {arrayPrices[j] = iClose(NULL,ClusterTimeFrame,j); arrayPrices[j+1] = iHigh(NULL,ClusterTimeFrame,j); arrayPrices[j+2] = iLow(NULL,ClusterTimeFrame,j);}} 
   ArraySort(arrayPrices,WHOLE_ARRAY,0,MODE_DESCEND);

return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void FindClusters()
{
   int counter = 0;
   int numArrayElements = ArraySize(arrayPrices)-1;
   double startRange = arrayPrices[0];
   double endRange = arrayPrices[0]-(Filter * Point); 

   for (int i=0; i<numArrayElements; i++)
   {        
      if (endRange <= arrayPrices[numArrayElements]) break;
   
      if (arrayPrices[i] <= startRange && arrayPrices[i] > endRange) 
      {
         counter++;
      }
      else
      {
         DrawLine((startRange + endRange)/2,counter);
         //DrawBox(startRange, endRange, counter);
         startRange = arrayPrices[i];
         endRange = arrayPrices[i]-(Filter * Point); 
         counter=0;
      }
   } 


return(0);
}

//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

void DrawLine(double linePrice, int counter)
{
   int lineWidth = 1;
   int lineEnd = counter;
   color lineColour = White;
   string objID = "tally" + DoubleToStr(linePrice,5)+"_"+counter;
   
   if (counter > LineStart) {lineEnd = LineStart; lineWidth = 2;}
   if (counter > 150 && counter < 500) lineColour = Orange;
   if (counter >= 500) lineColour = Red;
   
   ObjectCreate(objID, OBJ_TREND, 0, Time[LineStart], linePrice, Time[LineStart-lineEnd], linePrice);
   ObjectSet(objID, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet(objID, OBJPROP_COLOR, lineColour);
   ObjectSet(objID, OBJPROP_WIDTH,lineWidth);
   ObjectSet(objID, OBJPROP_RAY,0);

return(0);
}

/*
void DrawBox(double price1, double price2, int counter)
{
   int lineEnd = counter;
   color boxColour = White;
   string objID = "tally" + DoubleToStr((price1 + price2)/2,5)+"_"+counter;
   
   if (counter > LineStart) lineEnd = LineStart;
   if (counter > 250 && counter < 500) boxColour = Orange;
   if (counter >= 500) boxColour = Red;
      
   ObjectCreate(objID,OBJ_RECTANGLE,0,Time[LineStart],price1,Time[LineStart-lineEnd],price2);
   ObjectSet(objID,OBJPROP_COLOR,boxColour);  
   ObjectSet(objID,OBJPROP_RAY,0); 

return(0);
}
*/
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+

