# How can I get the Highest and Lowest between 2 hours every day

HI,

I really don't know how I can get every day the Highest and Lowest price between for example 04:00 AM and 06:00 AM.

I'm aware how to find the Highest/Lowest of a range of candle with iHigh and iLow but not for a precis time range

Thank you very much.

Jérôme

Some code to play around with

double high = iHigh(_Symbol,PERIOD_H4, iBarShift(_Symbol,PERIOD_H4,StringToTime("4:00")));
double low = iLow(_Symbol,PERIOD_H4, iBarShift(_Symbol,PERIOD_H4,StringToTime("4:00")));
Comment("\n High: ",high,"\n Low: ",low);

maximo #:

Some code to play around with

HI Maximo,

Thank you very much for your help it works great! :-) I searched much more complicated...

Best regards,

Jérôme

Use M15 or M30 bars, it should be iHighest or ILowest for 8 M15 bars or 4 M30 bars, not so difficult.

I highly recommend CDateTime from the Include/Tools folder.
When you feed a datetime to it it is automatically brought into TimeStruct format.

Say

CDateTime my_Time;        //initialisation
my_time.DateTime(TimeCurrent());

if(my_time.hour>=4 &&my_time.hour<=6)
...

have fun.

CopyRates() has a variant that takes the start time and end time explicitly. I prefer CopyRates/CopyHigh/CopyLow as it provides the facility to check that you have all the data before using it further in your logic hence ensuring you have accurate data.
I would also encourage you use the smallest/most atomic timeframe (PERIOD_M1 for mql5) for the interval as this will ensure you are truly flexible in finding the difference between any two times during the day, say if you wanted highest bar between 04:00 and 05:23.

int highest_lowest_in_range(datetime const start_time,datetime const end_time,double &hi_lo_container[])
{
MqlRates rates_arr[];
//guaranteed to go up/down resp.
double highest_rate=INT_MIN;
double lowest_rate=INT_MAX;

int num_rates=   CopyRates(_Symbol,PERIOD_M1,start_time,end_time,rates_arr);
long time_diff_mins=((long)(end_time-start_time))/60;

//You can choose not to proceed if data is not fully fetched as compared to i(Low/High) which will not inform
//in case it cannot get all data
if(num_rates-1!=time_diff_mins)
{
return 0;
}

for(int i=0; i<time_diff_mins; i++)
{
highest_rate=rates_arr[i].high>highest_rate?rates_arr[i].high:highest_rate;
lowest_rate=rates_arr[i].low<lowest_rate?rates_arr[i].low:lowest_rate;
}

hi_lo_container[0]=highest_rate;
hi_lo_container[1]=lowest_rate;

ArrayFree(rates_arr);
return 1;
}

Invoked as:

double hi_lo[2];

if(highest_lowest_in_range(StringToTime("04:00"),StringToTime("10:22"),hi_lo))
{
PrintFormat("Highest price %f, lowest price %f",hi_lo[0],hi_lo[1]);
}
//(ETHUSD,M5)  Highest price 1609.240000, lowest price 1565.730000

salitos #:

CopyRates() has a variant that takes the start time and end time explicitly. I prefer CopyRates/CopyHigh/CopyLow as it provides the facility to check that you have all the data before using it further in your logic hence ensuring you have accurate data.
I would also encourage you use the smallest/most atomic timeframe (PERIOD_M1 for mql5) for the interval as this will ensure you are truly flexible in finding the difference between any two times during the day, say if you wanted highest bar between 04:00 and 05:23.

Invoked as:

HI Salitos,

Thank you Very much I will try your code!I was still struggling to find a solution...

Thanks!!

Jérôme

Comments that do not relate to this topic, have been moved to "Off Topic Posts".
Reason: