Questions from Beginners MQL5 MT5 MetaTrader 5 - page 987

 

Hello, huge respect to all those who help me to understand the features of MT5. Without you it is very difficult to do so... stupor, hang-ups, running in circles. So, RESPECT and Kudos to you.


Question. What is the best way to connectrates_total and history bar limits? Did I link it correctly in the code? Thanks for the reply, hint, hint.

//--- Проверка количества доступных баров
   if(rates_total<24) return 0;
//--- Проверка и расчёт количества просчитываемых баров
   int limit=rates_total-prev_calculated;
   if(limit>1)
     
      limit=rates_total-1;

//Показать историю за CountPeriods недель барах по Н1

int bars=PeriodSeconds(PERIOD_W1)/PeriodSeconds(PERIOD_H1)*CountPeriods;  //  CountPeriods=4; В глобальных переменных

//РЕШИЛ ТАК НО ПО-МОЕМУ ЧУШЬ...

int lm=iBarShift(NULL,PERIOD_H1,iTime(NULL,PERIOD_CURRENT,limit));      //rates_total-1 в днях
int start=lm-(lm-bars);

Comment(start,"    bars    ",bars);  //Равенство значений есть

I WAS JUST WATCHING THE NEW HOUR. EVERYTHING SEEMS TO BE WORKING FINE.

Then a question: was I correct in coding the rates_total moment?

 

Reading the help for the Bars function carefully:

"

If the start_time and stop_time parameters are specified, the function returns the number of bars in the date range. If these parameters are not specified, the function returns the total number of bars.

"

The help doesn't say whether the start dates or stop dates should be included or not, as a result you don't know what to expect from the function.

What's surprising is how the function works:

   datetime         StartDt=StringToTime("2018.01.04 10:00");
   datetime         StopDt=StringToTime("2018.01.03 23:49");
//datetime         StopDt=StringToTime("2018.01.04 10:00");
//datetime         StopDt=StringToTime("2018.01.04 10:01");
   int              BarsGo=Bars(Symbol(),PERIOD_CURRENT,StartDt,StopDt);
   Print("BarsGo=",BarsGo);

With any of the options, including the commented ones, StopDt gets a value of 2!

Especially surprising is the option when the start date (2018.01.04 10:00) is later in time than the end date (2018.01.03 23:49) in the second expression - why is no error or at least 1 not produced?

If the start date and end date are the same, then it makes sense to give out a 1 and not a 2 again!

I check Si instrument on FORTS, one-minute chart.

 

Please help, a piece of indicator

//+------------------------------------------------------------------+
//|  OnCalculate function                                            |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   ArraySetAsSeries(time,true);

   datetime Fp=0,Ep=0,pFp=0,pEp=0,Arr[];
   int Count=0,bars=0,dt=0;

   int limit;
   if(prev_calculated==0 || prev_calculated<0 || prev_calculated>rates_total)
      limit=Nbar;
   else
      limit=rates_total-prev_calculated;

   for(int i=limit;i>=0;i--)
     {

      if(CopyTime(NULL,TimeFrame,time[i],1,Arr)>0)Ep=Arr[0]-1*PeriodSeconds(PERIOD_CURRENT);
      else return(0);

Sometimes time[i] array overflows, for example at night when the market is closed.

2019.01.25 00:06:35.191 i-Regr4_05i (Si Splice,H1)      array out of range in 'i-Regr4_05i.mq5' (134,38)

How to solve this problem?

 

A clue after a declaration like this.

double Price[]; 

Isthe size of the array always 0 ?

 
pivomoe:

A clue after a declaration like this.

Is the size of the array always 0 ?

Yes.

 
Artyom Trishkin, as a professional and responsible moderator, I would very much like to hear from you, especially with reference to the manual, justification for the behaviour of the Bars function, rather than guesswork!
 
Aleksey Vyazmikin:

Please help, a piece of indicator

Sometimes time[i] array overflows, for example at night when the market is closed.

How to solve this problem?

For example to correctly calculateNbarparameter:

   if(prev_calculated==0 || prev_calculated<0 || prev_calculated>rates_total)
      limit=Nbar;
 
Aleksey Vyazmikin:

Reading the help for the Bars function carefully:

"

If the start_time and stop_time parameters are specified, the function returns the number of bars in the date range. If these parameters are not specified, the function returns the total number of bars.

"

The help doesn't say whether the start dates or stop dates should be included or not, as a result you don't know what to expect from the function.

What's surprising is how the function works:

With any of the options, including the commented ones, StopDt gets a value of 2!

Especially surprising is the option when the start date (2018.01.04 10:00) is later in time than the end date (2018.01.03 23:49) in the second expression - why is no error or at least 1 not produced?

If the start date and end date are the same, then it makes sense to give out a 1 and not a 2 again!

I check Si instrument on FORTS, it is a minute chart.

Before discussing inconsistencies, we should show that there are more bars on the chart than those returned by the function.

I work with this function quite a lot and do not get any problems. I am very much surprised why iBarShift and other similar functions were included into mql5.

The fact that the function swaps the 'from' and 'to' times if the programmer suddenly gets it wrong is included into the notion of "Foolproof".

And a word of advice: To make the function work faster, put a bar start time in it. A couple of extra lines will ensure speed. This is especially important for a tester.

 
Vladimir Karputov:

For example, calculate theNbarparameter correctly:

I've already done a check for myself, but this check is for bypassing the error of this function, the help doesn't say at all about the need for a check, which means it should be built in.

And then, you are talking about indicator check, while I use Bars to calculate the correct bar start time, as iBarShift has its own mind and is only suitable for forex, where there are no frequent failures with history due to clearing and trading sessions not for the whole day.

 
Alexey Viktorov:

Before you talk about inconsistencies, you should show that there are more bars on the chart than the function returns.

I work with this function quite a lot and I don't get any problems. I was very surprised why they put iBarShift and similar functions in mql5.

The fact that the function changes the times 'from' and 'to' in places if the programmer suddenly gets them mixed up is also a part of the notion of "Foolproof".

I would also like to advise: To make the function work faster, put bar start time in it. A couple of extra lines will provide speed. This is especially important for a tester.

This is not protection but an obstacle to detecting an error in code!

Moreover, it is not at all logical to return number 2 if the dates coincide - what's the rationale here?

The start time of a bar on FORTS may not coincide, which leads to errors in calculations, for example, a bar opens not at 14:00, but at 14:05 - I suffered with it too.

Reason: