//+------------------------------------------------------------------+
//|                                                         MVC-Highest.mq4 |
//|                                                Copyright 2015, pije76. |
//|                              http://www.forexfactory.com/pije76 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, pije76"
#property link      "http://www.forexfactory.com/pije76"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 1
#property indicator_color1  Blue
#property indicator_width1  2
extern int NumberOfBars = 100;
extern int ControlOfBars = 25;
extern int NumberOfHighs = 3;


//1st step: Define a two dimensional array
// double ArrayVolume[];

 //-----//define as 2 dimensional
double ArrayVolume[][2];


//double ArrayVolume[][2]; //[i][0] contains the volume index & [i][1] contains the candle index
double iVolume, jVolume, kVolume;
double HighestColor[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  SetIndexBuffer(0, HighestColor);
  SetIndexStyle(0, DRAW_HISTOGRAM);
  SetIndexLabel(0, NULL);
  SetIndexEmptyValue(0, 0.0);
  IndicatorShortName("MVC-Highest");
  IndicatorDigits(0);
  //----
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
  //----
  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  double iVolume, jVolume, kVolume;
  int kIndex;
//  int bulan = 0;
//  int hari = 0;
//  int tahun = 0;
//  string tanggal = "";
//  datetime thisbartime = 0;
  datetime thisbartime[];
//  int shift = 0;
//  int time_frame = 1440;
//  int min = 0;
//  int jam = 0;
//  int thour=0, tmin=0;


 //-----//define 2 counters
int cnt1=0;
int indxcnt=0;

  int Limit;
  int counted_bars=IndicatorCounted();
  //---- last counted bar will be recounted
  if(counted_bars>0) counted_bars--; //always count the previous bar
  if ( NumberOfBars == 0 )
  NumberOfBars = Bars-counted_bars;
  
  
   //-----//for now use  100
  Limit=100;
//  bulan=TimeMonth(Time[NumberOfBars]);
//  hari=TimeDay(Time[NumberOfBars]);
//  tahun=TimeYear(Time[NumberOfBars]);
//  min = TimeMinute(Time[NumberOfBars]);
//  jam = TimeHour(Time[NumberOfBars]);
//  tanggal = tahun + "." + bulan + "." + hari + " " + jam + ":" + min;
//  thisbartime = StrToTime(tanggal);
//  shift = iBarShift(NULL,time_frame,thisbartime); //returns the correct shift of the current daily bar
//  thour = TimeHour(iTime(NULL,60,iBarShift(NULL,60,thisbartime)));
//  tmin = TimeMinute(iTime(NULL,Period(),NumberOfBars));
  ArrayCopySeries(thisbartime,MODE_TIME,Symbol(),1440);
  for(int i=0, t=0; i<Limit; i++)
//  for(int i=0; i<Limit; i++)
  {
    //2nd step: Inside main loop, add condition here to check for Hour == 0
//     if (Time[i]<thisbartime[t]) t++;
     if(TimeHour(Time[i]) == 0)
 //if(thour == 0 && tmin == 0)
    {
//      Print("thisbartime[t]:"+thisbartime[t]);
      ArrayResize(ArrayVolume, Limit, 0);
      //3rd step: Set up another "for loop" length = 24 as there are 24 candles in a day for H1
      for(int j=0; j<ControlOfBars; j++)
      {
      
       //-----//keep track of where you are in the main loop
        indxcnt=i+j;
        
       // 4th step: Populate the Array with volume index
      // ArrayVolume[j] = Volume[j];
        
        //-----// 4th step: Populate the Array with volume and index
        ArrayVolume[cnt1][0] = Volume[indxcnt];
        ArrayVolume[cnt1][1] = indxcnt;
        
         //-----//increment counter for array elements
        cnt1++;
        //ArrayVolume[j] = Volume[j];
        //j++;
      } // end of "j for" loop
    
 //-----// j and hour loop ends here but main loop does not
//}// end of "i for" loop


    //5th step: Sort the Array
    ArraySort(ArrayVolume, WHOLE_ARRAY, 0, MODE_DESCEND);
    //jVolume = ArrayVolume[j];
    
 
  //6th step: Define a function to draw a trend line
  //7th step: Set up a second "for loop" with the length equal to the number of highs to display on the chart with lines
    for (int k=0; k<NumberOfHighs; k++)
    {
      //if (ArrayResize(ArrayVolume) > 0) ArraySort(ArrayVolume, WHOLE_ARRAY, 0, MODE_DESCEND);
      //{
     //-----//read both elements then plot on chart  
        kVolume = ArrayVolume[k][0];
        kIndex = ArrayVolume[k][1];
        //8th step: call the draw trend line function using price and candle index as parameters to draw the trend line
          Print("BarIndex: ", kIndex, ", Value: ", kVolume);
          HighestColor[kIndex]=kVolume;
      //} //end of "if" condition
    }//end of "k for" loop
    
    
     //-----//end hour here
        } // end of "if" condition for hour == 0

      ArrayResize(ArrayVolume,0,0);

      }// end of "i for" loop

  return(0);
}
