Different results with same script when calculating high and low of previous day

 

Hello,

i want to calculate the high and low of the previous day between 6.00 in the morning and 20.00 in the evening. I wrote a script to do that.

But when i´m running the script for the 1st time, the alert shows the low of preday by 11434. That´s a false result. When i run the same script 2 minutes later, i get the correct result of low preday 11517. I don´t understand this. Is something wrong with my code?

screenshot 

 Here is the code:

//+------------------------------------------------------------------+
//|                                                highlowpreday.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

extern int      market_open_hour  =6;    //Starttime H für High-Low-Berechnung
extern int      market_open_min   =00;   //Starttime M für High-Low-Berechnung
extern int      market_close_hour =20;   //Endtime H für High-Low-Berechnung
extern int      market_close_min  =00;   //Endtime M für High-Low-Berechnung

void OnStart()
  {
//---
   int             
   startbar1,
   endbar1;
   double open_day = 0;                // Open of today
   double high_pre_day = 0;            // High of previous day
   double low_pre_day = 0;             // Low of previous day
//-------------------------------------------------------------------- 9 --
   // Parameters of technical indicators:
   //-- Calculate high and low from "PREVIOUS DAY" market open to market close interval.
   if(DayOfWeek() >= 1) 
     {                                 
      startbar1 = iBarShift(NULL, 0,(iTime(Symbol(),PERIOD_D1,1)+market_open_hour*60*60+market_open_min*60)); 
      endbar1 = iBarShift(NULL, 0,(iTime(Symbol(),PERIOD_D1,1)+(market_close_hour*60*60+market_close_min*60)));  
      high_pre_day = High[iHighest(NULL, 0, MODE_HIGH, startbar1 - endbar1 + 1, endbar1)];
      low_pre_day = Low[iLowest(NULL, 0, MODE_LOW, startbar1 - endbar1 + 1, endbar1)];
     }
     
   Alert("high preday =", high_pre_day);
   Alert("low preday =", low_pre_day);
   Alert("hour =", Hour());
   Alert("minute =", Minute());
  }
//+------------------------------------------------------------------+

 

Thanks a lot

Michael 

 

Possibly market_close_hour is wrong: your DAX contract runs from 6 UTC until 20, meaning the last bar of the day on (e.g.) hourly chart is the 19h bar.  Returned value of iBarShift (quoting documentation): If there is no bar for the specified time (history "gap"), the function will return -1 or the nearest bar index (depending on exact parameter).


However, I have no clue why you get wrong values once and then correct values when you run the same script again. Did you run it on different timeframes?

 

Not compiled or tested

      datetime previous_midnight;
      datetime today_midnight=TimeCurrent()-(TimeCurrent()%(PERIOD_D1*60));
      int day_number=TimeDayOfWeek(today_midnight);
      if(day_number>1)
         previous_midnight=today_midnight-(PERIOD_D1*60);
      else
         previous_midnight=today_midnight-(PERIOD_D1*60*(day_number+2));
                                          
      startbar1 = iBarShift(NULL, 0,(previous_midnight+market_open_hour*60*60+market_open_min*60)); 
      endbar1 = iBarShift(NULL, 0,(previous_midnight+(market_close_hour*60*60+market_close_min*60)));  
      high_pre_day = High[iHighest(NULL, 0, MODE_HIGH, startbar1 - endbar1 + 1, endbar1)];
      low_pre_day = Low[iLowest(NULL, 0, MODE_LOW, startbar1 - endbar1 + 1, endbar1)];

 

 It is most likely that you are getting the incorrect result because D1 data is updating the first time that you run the script. The 2nd time, D1 has updated.

The above suggestion uses the data from the current chart, so avoids this issue.

I have written it so that Sunday candles will be ignored. If you want to include Sunday, then it will need to be modified slightly. 

Reason: