Get Time of High and Low of the day

 

Hello,

Im trying to get the time of high and low points in my day to determined which one happend first.

How can I do it?

I know I can get the high and low rates of let's say yesterday by this code:

 MqlRates rates[2]; 
 CopyRates(_Symbol,PERIOD_D1,0,2,rates); 
 Print(rates[0].high,rates[0].low);

But how do I know which one happend first?

thank you! 

 
yuv98:

Hello,

Im trying to get the time of high and low points in my day to determined which one happend first.

How can I do it?

I know I can get the high and low rates of let's say yesterday by this code:

But how do I know which one happend first?

thank you! 

Keep track of high and low files in the OnTick() event. That gives the finest granularity.

Reset the high/low values each period, which you say is each day.

 
yuv98:

Hello,

Im trying to get the time of high and low points in my day to determined which one happend first.

How can I do it?

I know I can get the high and low rates of let's say yesterday by this code:

But how do I know which one happend first?

thank you! 


void OnStart()
  {
//---' '
   datetime high = HighOfDayTime(), low = LowOfDayTime();
   Print("High of day = ",high," - Low of day = ",low);
   Print(high>low?"Low happened first":"High happened first");
  }
//+------------------------------------------------------------------+


datetime HighOfDayTime()
{
   MqlRates r[];
   //ArraySetAsSeries(r,true);
   MqlDateTime time_struct;
   TimeCurrent(time_struct);
   time_struct.hour=0;
   time_struct.min=0;
   time_struct.sec=0;
   int total = CopyRates(_Symbol,PERIOD_M1,TimeCurrent(),StructToTime(time_struct),r);
   datetime time = 0;
   double highest = DBL_MIN;
   for(int i=0;i<total;i++)
   {
      if(r[i].high > highest)
      {
         highest = r[i].high;
         time = r[i].time;
      }
   }
   return time;
}

datetime LowOfDayTime()
{
   MqlRates r[];
   //ArraySetAsSeries(r,true);
   MqlDateTime time_struct;
   TimeCurrent(time_struct);
   time_struct.hour=0;
   time_struct.min=0;
   time_struct.sec=0;
   int total = CopyRates(_Symbol,PERIOD_M1,StructToTime(time_struct),TimeCurrent(),r);
   datetime time = 0;
   double lowest = DBL_MAX;
   for(int i=0;i<total;i++)
   {
      if(r[i].low < lowest)
      {
         lowest = r[i].low;
         time = r[i].time;
      }
   }
   return time;
}
 
nicholishen:

Thanks that's awesome! But I still need to ask.
What if the high and low are in the same 1min candle?
How do I know which one happened first, the high point or low point?

And to Anthony Garot,
Do you mean to check every tick?
But what if I need to use it on history data, let's say 2 weeks ago? 
 

yuv98:

Do you mean to check every tick?
If you need it to be that granular.
// Pseudo code in OnTick()
        static double highest = 0.0;
        static double lowest = 9999999999.99;           // ridiculously large #
        static datetime highestTime;
        static datetime lowestTime;

        double curHigh = . . . 
        double curLow = . . . 
        
        if ( curHigh > highest )
        {
                highest = curHigh;
                highestTime = TimeCurrent();
        }
        if ( curLow < lowest )
        {
                lowest = curLow;
                lowestTime = TimeCurrent();
        }
        if ( TimeCurrent() % 86400 == 0 )               // 86400 = # seconds in a day
        {
                highest = 0.0;
                lowest = 9999999999.99;
        }

yuv98:

But what if I need to use it on history data, let's say 2 weeks ago? 

My method would give you a way to store highs / lows over time. You could store these in an array; store them in a file; however you want to use it.

If you need to pull from historical data that you didn't create yourself, you probably want to use something like @nicholishen suggested, but your granularity is limited by the data you pull.

 
Thank you!
Reason: