# Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 255

Alexey Viktorov:
The current time of 17:08 shows 33. So the bar number with a time of 01:00 will be 32.
`datetime t=StringToTime("01:00");`

This is not the correct solution. If you start the test at 00:00, this variant gives 0 bars, and there should be 46 M30 bars before the nearest 01:00 hour.

Nauris Zukas:

Wrong decision. If you start the test at 00.00, this variant produces 0 bars, and there should be 46 M30 bars before the nearest 01.00 hours.

I gave you a way of thinking, and you should know how to make the nearest 01:00 bar at 00:59 on the minutes. There is nothing difficult about it.

Alexey Viktorov:

I gave the direction of thought, and how to make the nearest 01:00 bar at 00:59 on the minutes is up to you. There is nothing difficult about it.

In any case you have to use the loop and look for the nearest time, I do not see any other way.

Alexey Viktorov:
The only problem is that I never fill my memory cell with unnecessary stuff like what the value obtained will equal, given a bar with a specified time or less by 1. I just check every time.

I have no idea how to find the closest specified time for bars without a cycle. If there is nothing complicated about it, can you show me the function?

Dear Experts, could you please advise how to pull test results to a file in mt-4?

for further processing as described here https://www.mql5.com/ru/articles/1467?

Автоматическая оптимизация торгового робота в процессе реальной торговли
• 2007.04.16
• Igor Malcev
• www.mql5.com
В статье описана и представлена библиотека функций, позволяющая проводить оптимизацию входных параметров советника, запуская оптимизацию непосредственно из советника.

Nauris Zukas:

I have no idea how to find the nearest specified time for bars without a cycle. If there is nothing complicated about it, can you show me the function?

..... just no words....

How hard is it to guess that if the time is less than the time you are looking for, then you have to subtract the time you are looking for by 24 hours.

``` string st = "1:00";// можно и так писать "01:00"
datetime t = StringToTime(st);

if(TimeCurrent() < t)
{
MqlDateTime mqlDateTime;
TimeToStruct(t, mqlDateTime);
mqlDateTime.day -= 1;
t = StructToTime(mqlDateTime);
}
int b = Bars(_Symbol, PERIOD_M30, t, TimeCurrent());```

Made without taking into account the outputs and checks for possible execution errors. Other executions are possible, which is why I try not to write example codes. Today I decided to do so, tomorrow I may find another variant more convenient.

Alexey Viktorov:

How hard is it to guess that if the time is less than the time you're looking for, then you have to subtract the time you're looking for by 24 hours.

It is difficult to guess because of this:

Alexey Viktorov:

Made without taking into account weekends and checks for possible execution errors.

How much harder must the code be to get a desirable result without errors at the end! There are weekends and holidays, the code opens at other times. Thanks for tips and examples, I have learned something new, it will come in handy somewhere, but so far I believe my version is the easiest and most reliable.

Nauris Zukas:

It's hard to guess just because of that:

How much more complicated must the code be to get the result you want without errors in the end! There are weekends and holidays when the market opens at a different time. Thanks for tips and examples, I have learned something new, it will definitely be useful, but so far I see that my version is the easiest and most reliable.

Why not? And that option has a right to life. And it even interested me. With some corrections, we got the function that returns the bar number of a specified time and period.

```int findBar(string strTime, ENUM_TIMEFRAMES period = PERIOD_CURRENT)
{
MqlDateTime mqlTime, mqlFindTime;
TimeToStruct(StringToTime(strTime), mqlFindTime);
datetime arrTime[];
int copy = PeriodSeconds(PERIOD_D1)/PeriodSeconds(period);
CopyTime(_Symbol, period, 0, copy, arrTime);
ArraySetAsSeries(arrTime, true);
for(int i = 0; i < copy; i++)
{
TimeToStruct(arrTime[i], mqlTime);
if(mqlTime.hour == mqlFindTime.hour && mqlTime.min <= mqlFindTime.min)
return(i);
}
return(-1);
}/********************************************************************/
```

.

Alexey Viktorov:

Why not? And that option has a right to life. And it even interested me. With some corrections, the function returns the bar number of the specified time and period.

.

Thank you very much!
You mentioned "memory cell", my knowledge on this subject is so far limited. As far as I understand, it affects the speed of tests of the Expert Advisor? Is it somehow measured or is it just determined by the speed of testing?

Nauris Zukas:

Thank you very much!
You mentioned "memory cell", my knowledge is limited at the moment. Does it, as far as I understand, affect the speed of Expert Advisor tests? Is it somehow measured or is it just determined by the speed of testing?

I was talking about my own memory chip. :)))

About not remembering such trifles, which can be easily double-checked, clarified, done as necessary and forgotten again when writing the code.