MQL4 - automated forex trading   /  

Documentation

MQL4 Reference  Array functions  ArrayBsearch

 
int ArrayBsearch( double array[], double value, int count=WHOLE_ARRAY, int start=0, int direction=MODE_ASCEND)
If the element with the specified value doesn't exist in the array, the function returns the index of the nearest smallest value of the elements between which the searched value is located.
The function cannot be used with string arrays and series arrays (with the exception of the series array of the bar open time).
Note: Binary search processes only sorted arrays. To sort numeric arrays use the ArraySort() function.
Parameters:
array[]   -   The numeric array to search for.
value   -   The value to search for.
count   -   Count of elements to search for. By default, it searches in the whole array.
start   -   Starting index to search for. By default, the search starts at the first element.
direction   -   Search direction. It can be any of the following values:
MODE_ASCEND searching in forward direction,
MODE_DESCEND searching in backward direction.
Sample:
   datetime daytimes[];
   int      shift=10,dayshift;
   // All the Time[] series are sorted in descendant mode
   ArrayCopySeries(daytimes,MODE_TIME,Symbol(),PERIOD_D1);
   if(Time[shift]>=daytimes[0]) dayshift=0;
   else
     {
      dayshift=ArrayBsearch(daytimes,Time[shift],WHOLE_ARRAY,0,MODE_DESCEND);
      if(Period()<PERIOD_D1) dayshift++;
     }
   Print(TimeToStr(Time[shift])," corresponds to ",dayshift," day bar opened at ",
         TimeToStr(daytimes[dayshift]));