Looking for help to calculate the lowest price within a time interval

 

Hello, everyone.

I coded this EA to determine the highest and lowest price during a specified time range. Unlike some similar EAs, my EA could calculate the highest Close price, which I believe provides a more realistic picture. The only issue I have is that my EA works OK for determining the highest price, but I don't know how to code the calculation of the lowest price within a given time range.


Any advice would be greatly appreciated.


Here's the code

input int TimeToCalculateStartHour = 11;
input int TimeToCalculateStartMin = 0;
input int TimeToCalculateEndHour = 14;
input int TimeToCalculateEndMin = 0;

input int TimeToSleepStartHour = 0;
input int TimeToSleepStartMin = 01;
input int TimeToSleepEndHour = 0;
input int TimeToSleepEndMin = 07;

double highestClosePrice = 0.0;

void OnTick()
  {
   
   
   MqlDateTime structTime;
   TimeCurrent(structTime);
   structTime.sec = 0;
   
   structTime.hour = TimeToCalculateStartHour;
   structTime.min = TimeToCalculateStartMin;
   datetime timeStartToCalculate = StructToTime(structTime);
   structTime.hour = TimeToCalculateEndHour;
   structTime.min = TimeToCalculateEndMin;
   datetime timeEndToCalculate = StructToTime(structTime);
   
   structTime.hour = TimeToSleepStartHour;
   structTime.min = TimeToSleepStartMin;
   datetime timeStartToSleep = StructToTime(structTime);
   structTime.hour = TimeToSleepEndHour;
   structTime.min = TimeToSleepEndMin;
   datetime timeEndToSleep = StructToTime(structTime);
   
  bool isTimeToCalculate = TimeCurrent() >=timeStartToCalculate && TimeCurrent() < timeEndToCalculate; 
    
     {
      // Calculate the highest close price for the M5 bars within the range
      int startPos = iBarShift(NULL, PERIOD_M5, timeStartToCalculate);
      int endPos = iBarShift(NULL, PERIOD_M5, timeEndToCalculate);

      for (int i = startPos; i >= endPos; i--)
        {
         double closePrice = iOpen(NULL, PERIOD_M5, i);
         if (isTimeToCalculate==true)
         if (closePrice > highestClosePrice)
       
            highestClosePrice = closePrice;
        }
   ObjectCreate(_Symbol,"Line1",OBJ_HLINE,0,0,highestClosePrice);
   ObjectSetInteger(0,"Line1",OBJPROP_COLOR,clrBeige);
   ObjectSetInteger(0,"Line1",OBJPROP_WIDTH,3);
   }
   
   bool isTimeToSleep = TimeCurrent() >=timeStartToSleep && TimeCurrent() < timeEndToSleep; 
    
     {
      // Resets the previous day's calculations (default setting: around midnight)
      int startPos = iBarShift(NULL, PERIOD_M5, timeStartToSleep);
      int endPos = iBarShift(NULL, PERIOD_M5, timeEndToSleep);

      for (int i = startPos; i >= endPos; i--)
        {
         double closePrice = iOpen(NULL, PERIOD_M5, i);
         if (isTimeToSleep==true)
       
            highestClosePrice = 0.0;
        }   
     
     }
   
   
      
      Comment("\nHighest M5 close price: ",highestClosePrice,
      "\nServer Time: ",TimeCurrent(),
           "\nTime To Calculate Start Dt: ",timeStartToCalculate,
           "\nTime To Calculate End Dt: ",timeEndToCalculate,
           "\nTime To Calculate Filter: ",isTimeToCalculate,
           "\nTime To Sleep Filter: ",isTimeToSleep);
  }
  
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined...
 
Not sure why you need an expert advisor for what iHighest function does.

So my advice for searching the lowest price within a given time range would be using the iLowest function.

p.s. Also I checked your code, and I find strange usage of ObjectCreate function. Are you sure with arguments senfing to that function?
iLowest - Timeseries and Indicators Access - MQL4 Reference
iLowest - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
iLowest - Timeseries and Indicators Access - MQL4 Reference
 
double lowestClose = -DBL_MAX; // make it a global variable


      if (lowestClose  < closePrice){
       
            lowestClose  = closePrice;
      }

it could work like that, might not be the best approach, but if lowestClose was initialized as zero than this condition wouldn't work if the close price was ever zero.

There's also MathMax and MathMin that you could use.  How come you use iOpen for close prices instead of iClose?