How do I get Open,Low,High,Close parameters in MQL5? - page 4

 
Alexey Viktorov:

In that case, unambiguously CopyRates from the first bar 10s. And not to be called every tick, but enough at appearance of a new bar.

How do I know if a new bar has appeared without CopyRates? I mean, you have to catch the new bar event first. Without CopyRates is it possible? I'm a little confused).

That's what it means, I have never dealt with this topic on MT5. Please understand)).

 
Alexey Viktorov:

Oh, Vladimir. Although I blurted it out, but unfortunately, I didn't make a reservation. If we need only opening price or time, for example, there's no need to use CopyRates(), in this case CopyOpen or iOpen is quite enough... respectively CopyTime or iTime. It is chosen according to your religious preferences.

And I'll add my caveat: In terms of performance costs, there is no appreciable difference. Accordingly, only religious biases remain.

The fxsaber was checking CopyRates() cost - it's more expensive than getting the required property separately. It doesn't apply to the situation when we need to get all prices at once.

 
Реter Konow:
And how do I know that a new bar appeared without CopyRates? I mean, you need to catch the new bar event first. Is it possible to do it without CopyRates? I'm a little confused).

Without paying attention to anything, monitor the appearance of a new bar and call CopyRates

There are a lot of different functions and ways of detecting a new bar on the site.

 
Реter Konow:

How do I know if a new bar has appeared without CopyRates? I mean, you have to catch the new bar event first. Without CopyRates is it possible? I am a little confused).

That's what it means, never dealt with this topic on MT5. Please understand.))

The easiest way is to compare the opening time of the current bar. If it is not equal to the previously memorised one - have a new bar - do what needs to be done on the new bar and memorise this time for the next check.

 
Alexey Viktorov:

Without paying attention to anything, we track the appearance of a new bar and call CopyRates

There are a lot of different functions and ways of defining a new bar on the site.

The appearance of a new bar in MT5 is not linked to the beginning of the minute. It is not timed at all. There is a catch. How accurately can we understand the appearance of a new bar based only on ticks? You still need to refer to the time series. It would hardly be possible otherwise. Although... I'm not 100% sure.
 
Artyom Trishkin:

fxsaber did a CopyRates() cost check - it is more expensive than getting the required property separately. This does not apply to the situation where you need to get all prices at once.

With all due respect fxsaber, I prefer not to pay attention to such difference. That's why I said that" there is nonoticeable difference", and I am not interested in the difference in 1e6 iterations. It's not like we're writing a shooter where you can fire 1e6 bullets at a time.
 
Artyom Trishkin:

The easiest way is to compare the opening time of the current bar. If it is not equal to the previously memorised one - have a new bar - do what needs to be done on the new bar and memorise this time for the next check.

So, it means to call CopyRates on every tick? Otherwise, how do we know that the opening time of the current bar has changed?
 
Реter Konow:
So, you call the CopyRates on every tick? Otherwise, how would you know, that the opening time of current bar has changed?

you can either class CNewbar, or search the forum for NewBar

SZZ: whole article!https://www.mql5.com/ru/articles/159



Alexey Viktorov:

Igor, you need to go to the first grade. They teach the letters there and after you learn it you will understand the difference between CopyRates and CopyBuffer.

Here's a better suggestion to solve this problem WITHOUT CopyRates


OK, the usual...oops, oops...and not a single line of code ))))

I cited my example to show that it's not always convenient (and seldom necessary) to obtain an array of consecutive OHLC. In practical tasks we usually need to obtain a selection of OHLC values from different bars (indicators are not included)

Alright, take care!

Обработчик события "новый бар"
Обработчик события "новый бар"
  • www.mql5.com
Для создателей индикаторов и экспертов всегда был актуален вопрос написания экономичного кода с точки зрения времени выполнения. Можно подойти к решению этой задачи с разных сторон. Из этой обширной темы в данной статье будет затронут, казалось бы уже решенный вопрос: проверка появления нового бара. Это достаточно популярный способ ограничения...
 
Реter Konow:
So, so on each tick call CopyRates? Otherwise, how do we know that the opening time of the current bar has changed?

Yes, on every tick we check the time:

//+------------------------------------------------------------------+
//| Возвращает время указанного бара                                 |
//+------------------------------------------------------------------+
datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int index)
  {
   datetime array[];
   return(CopyTime(symbol_name,timeframe,index,1,array)==1 ? array[0] : 0);
  }
//+------------------------------------------------------------------+

And in a nutshell, it's like this:

static datetime last_time=0;
datetime time_current=Time(Symbol(),PERIOD_CURRENT,0);
if(time_current==0)
  return;
if(time_current!=last_time)
  {
   // Новый бар или первый запуск (для контроля первого запуска можно сделать флаг - чтобы не обрабатывать его как новый бар)
   // делаем что нужно на новом баре
   last_time=time_current;
  }

I just wrote it on my hand - to show the idea, not the accuracy of writing

 
Реter Konow:
So, on every tick we should call CopyRates? Otherwise, how to know that the opening time of the current bar has changed?

if(prevTime!=iTime(...))

{

// current bar open time changed

}

prevTime=iTime(...);