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

 
The situation is as follows: the X variable stores the index of the bar in the history, we know that when a new bar appears, there is a shift, and therefore X already points to a different bar. How to fix it? The idea: with every new bar we increase the counter (count++) and add it to X. Thus X will always point to the same bar regardless of the offset. But there is a problem - when the EA is initialized the first current bar should not be taken into account. I think it can be easily solved if count is initially just equal to -1. That is, on the current bar after the increment count will be "0", and on the next new bar (when the first shift by 1 bar has happened after initialization) it will be "1". What do you think? Maybe I'm thinking in the wrong place at all?
 
Nerd Trader #:
The situation is as follows: the X variable stores the index of the bar in the history, we know that when a new bar appears, there is a shift, and therefore X already points to a different bar. How to fix it? The idea: with every new bar we increase the counter (count++) and add it to X. Thus X will always point to the same bar regardless of the offset. But there is a problem - when the EA is initialized the first current bar should not be taken into account. I think it can be easily solved if count is initially just equal to -1. That is, on the current bar after the increment count will be "0", and on the next new bar (when the first shift by 1 bar has happened after initialization) it will be "1". What do you think? Maybe I'm thinking in the wrong place at all?

If it is an indicator, it is sufficient

bool  ArraySetAsSeries(
   const void&  array[],    // массив по ссылке
   bool         flag        // true означает обратный порядок индексации
   );

to all arrays and buffers of the indicator used.

Then the zero bar will become rates_total-1

ArraySetAsSeries - Операции с массивами - Справочник MQL4
ArraySetAsSeries - Операции с массивами - Справочник MQL4
  • docs.mql4.com
ArraySetAsSeries - Операции с массивами - Справочник MQL4
 
Nerd Trader #:
The situation is as follows: the X variable stores the index of the bar in the history, we know that when a new bar appears, there is a shift, and therefore X already points to a different bar. How to fix it? The idea: with every new bar we increase the counter (count++) and add it to X. Thus X will always point to the same bar regardless of the offset. But there is a problem - when the EA is initialized the first current bar should not be taken into account. I think it can be easily solved if count is initially just equal to -1. That is, on the current bar after the increment count will be "0", and on the next new bar (when the first shift by 1 bar has happened after initialization) it will be "1". What do you think? Maybe I'm thinking in the wrong place at all?
      datetime x=время нужного бара;
      int index=iBarShift(_Symbol,0,x);
 
MakarFX #:
Why go to all that trouble?
 
Alexey Viktorov #:
Why go to all that trouble?
It's easier for me)
 
MakarFX #:
It's easier for me)

But the processor is heavier...

 
Alexey Viktorov #:

But the processor is heavier...

I don't know which functions carry what load. Is there somewhere to read about this?
 
Nerd Trader #:
The situation is as follows: The X variable stores the index of the bar in the history, and we know that when a new bar appears, it is shifted and therefore X already points to another bar. How to fix it? The idea: with every new bar we increase the counter (count++) and add it to X. Thus X will always point to the same bar regardless of the offset. But there is a problem - when the EA is initialized the first current bar should not be taken into account. I think it can be easily solved if count is initially just equal to -1. That is, on the current bar after the increment count will be "0", and on the next new bar (when the first shift by 1 bar has happened after initialization) it will be "1". What do you think? Maybe I'm thinking in the wrong place at all?

Not there. There is nothing in X when loading EA/indicator if it is inside the terminal, unless it is an intu or extern or global variable outside the terminal, so something has to be written there first. And then we simply read the changes of the total number of bars (account for the appearance of a new bar).

datetime BarTime;

FlagNewBar=false;
if(BarTime!=iTime(NULL,0,0))
{
 BarTime=iTime(NULL,0,0);
 FlagNewBar=true;
}
if(FlagNewBar==true)
{
// И когда тру, делаем что нужно делать когда появился новый бар, 
//на следующем тике флаг нового бара будет ложь, так как БарТайм время все время существования нулевого бара будет равным
}
 

Greetings. Can you give me a hint? How in mt5 at initialization or at tick to define testing?

So far I found only OnTester(), which is called just before OnDeinit().

 
@Alexey Viktorov @MakarFX interesting, quite good solutions. Thank you.

@Valeriy Yastremskiy The index is placed in X still in OnInit(). My code looks like this:
last_time = iTime(NULL, 0, 0);

  if(last_time > bar.time_open){
    bar.time_open = last_time;
    coint++;//стартовое значение -1
  }
The condition with FlagNewBar seems redundant to me.