# Getting the High and Low of a Specific time range once

Hey Guys, I'm pretty new to MQL5 and I'm trying to get the high and the low ONLY ONCE from 12:30 AM to 3:30 AM and then take those values after 3:30 AM and find the next new high/low.

Any help would be much appreciated!

```datetime ExtractTime(datetime when, int hours, int minutes)
{
datetime sod = when - when % 86400; // Start of Day
datetime endStart = sod + (hours * 3600) + (minutes * 60);
return endStart;
}

void OnTick()
{
int startHours = 0;
int startMinutes = 30;
int endHours = 3;
int endMinutes = 30;
datetime today = StringToTime(TimeToString(TimeCurrent(), TIME_DATE));
int DaystoDraw = 10;

// Extract the start time 12:30 AM
datetime startTime = ExtractTime(today, startHours, startMinutes);

// Extract the end time 3:30 AM
datetime endTime = ExtractTime(today, endHours, endMinutes);

// Calculate the number of bars between the start and end times
datetime time = TimeCurrent();
string hoursAndMinutes = TimeToString(time, TIME_MINUTES);

double high;
double low;

if (StringSubstr(hoursAndMinutes, 0, 5) >= "00:30" && StringSubstr(hoursAndMinutes, 0, 5) <= "03:30")
{
for(int i = 0; i < DaystoDraw; i++)
{
int startIndex = iBarShift(_Symbol, PERIOD_M5, startTime, true);
int endIndex = iBarShift(_Symbol, PERIOD_M5, endTime, true);
int numBarsInRange = startIndex - endIndex + 1;

int filterHighs = iHighest(_Symbol,PERIOD_M5,MODE_HIGH,endIndex,startIndex);
int filterLows = iLowest(_Symbol,PERIOD_M5,MODE_LOW,endIndex,startIndex);

high = iHigh(_Symbol,PERIOD_M5,filterHighs);
low = iLow(_Symbol,PERIOD_M5,filterLows);

Print("high : ", high," low : ",low);
}
}

double currentHigh = iHigh(_Symbol,PERIOD_M5,0);
double currentLow = iLow(_Symbol,PERIOD_M5,0);
double lastClose = iClose(_Symbol,PERIOD_M5,1);

if(currentHigh > high && lastClose < high)
{
high = currentHigh;
Print("New High : ", high);
}

if(currentLow < low && lastClose > low)
{
low = currentLow;
Print("New Low : ",low);
}
}
```

Please don't create topics randomly in any section. It has been moved to the section: Expert Advisors and Automated Trading

```int filterHighs = iHighest(_Symbol,PERIOD_M5,MODE_HIGH,endIndex,startIndex);
```

The forth field is the number of bars you want to count.

So you should use:

```int filterHighs = iHighest(_Symbol,PERIOD_M5,MODE_HIGH,numBarsInRange,startIndex);
```

another thing is
```for(int i = 0; i < DaystoDraw; i++)
```

The loop is meaningless. You are just doing the same thing over and over. And all the calculations are for today.

Yashar Seyyedin #:
another thing is

The loop is meaningless. You are just doing the same thing over and over. And all the calculations are for today.

Yashar Seyyedin #:

The forth field is the number of bars you want to count.

So you should use:

Thanks for all your help! I got it to work :)

@sarzie can you please share the working code? I'm trying to do something similar.

Sarzie #:
Thanks for all your help! I got it to work :)
can you please share the working code? I'm trying to do something similar.