Questions from Beginners MQL5 MT5 MetaTrader 5 - page 784

 
Aleksey Vyazmikin:

Please help me with making up a function.

I have a current TF and an estimated TF, the function should return the difference between the two TFs in any combinations, if the measurement were in bars

Calculation TF 15 15 15 60 60 60 240 240 240
Current TF 15 5 1 15 5 1 60 15 5
Difference in Nbar 1 3 15 4 12 60 4 16 48

How to implement this correctly?


I did it this way.

int TFk()
{
   int K=0;
   K=TF_transfer(TF)/TF_transfer(Period());
//   Print(TF_transfer(TF),"   ",TF_transfer(Period()));
//Print (K);
   if   (K<1)return(1);
   else      return(K);
}


int TF_transfer(ENUM_TIMEFRAMES tf)
  {
   if (tf==PERIOD_CURRENT) return(0);
   if (tf==PERIOD_M1) return(1);
   if (tf==PERIOD_M5) return(5);
   if (tf==PERIOD_M15) return(15);
   if (tf==PERIOD_M30) return(30);
   if (tf==PERIOD_H1) return(60);
   if (tf==PERIOD_H4) return(240);
   if (tf==PERIOD_D1) return(1440);
   if (tf==PERIOD_W1) return(10080);
   if (tf==PERIOD_MN1) return(43200);  
   if (tf==PERIOD_M2) return(2);
   if (tf==PERIOD_M3) return(3);
   if (tf==PERIOD_M4) return(4);
   if (tf==PERIOD_M6) return(6);
   if (tf==PERIOD_M10) return(10);
   if (tf==PERIOD_M12) return(12);
   if (tf==PERIOD_H2) return(120);
   if (tf==PERIOD_H3) return(180);  
   if (tf==PERIOD_M6) return(360);
   if (tf==PERIOD_H8) return(480);
   if (tf==PERIOD_H12) return(720);
   return(1);
  }

The problem is, I need this happiness for the indicator, but it does not want to initialize itself correctly and to this code

int OnInit()
  {
   Print ("K=",TFk()," Период=",Period());

return(INIT_SUCCEEDED); }

It reacts like this:

2017.09.22 14:23:27.742 donchian_channel_prognoz_MTF (Eu Splice,M5) K=3 Period=5

2017.09.22 14:23:27.776 donchian_channel_prognoz_MTF (Eu Splice,M5) test

2017.09.22 14:23:27.790 donchian_channel_prognoz_MTF (Eu Splice,M15) K=1 Period=15

The most confusing thing is that it prints a test that is already in the body of OnCalculate!

if(BarsCalculated(handle)<0) {Print ("test");return(0);}

The first time the timeframe was defined correctly, but the second time it wasn't...

I don't know what's so strange...

Although I know, it's the indicator call from the indicator... running in circles...
 
Aleksey Vyazmikin:

I did it like this

The problem is, I need this happiness for the indicator, but it is strange and does not want to initialize correctly, in response to this code

It reacts like this:

2017.09.22 14:23:27.742 donchian_channel_prognoz_MTF (Eu Splice,M5) K=3 Period=5

2017.09.22 14:23:27.776 donchian_channel_prognoz_MTF (Eu Splice,M5) test

2017.09.22 14:23:27.790 donchian_channel_prognoz_MTF (Eu Splice,M15) K=1 Period=15

The most confusing thing is that it prints a test that is already in the body of OnCalculate!

The first time the timeframe was defined correctly, but the second time it wasn't...

I don't know what these wonders are.

Although I know, it's calling the indicator from the indicator... running in circles...

Why do I have to go through all that trouble?

Isn't that easier?

K = PeriodSeconds(TF)/PeriodSeconds(Period());
 
Alexey Viktorov:

What's with all the twists and turns?

Wouldn't that make it easier?


So the thread is for beginners, that's why I didn't know about this possibility, thanks for pointing it out.

 

Can you please tell me what is the most reliableiBarShift function with two TFs right now ?

I have a task to find, for example, on a minute bar the indicator value from the upper TF, for example, M15. The timeframes can be any, which complicates the solution.

For an hour TF, I did through the structure, but if it is M15, then here I am confused...

 
Aleksey Vyazmikin:

Can you please tell me what is the most reliable iBarShift function with two TFs right now ?

I have a task to find, for example, on a minute bar the indicator value from the upper TF, for example, M15. The timeframes can be any, which complicates the solution.

For an hour TF, I did through the structure, but if it is M15, here I am confused...

Spin this script:Searching Nearest Bar

 
Vladimir Karputov:

Spin this script:Searching Nearest Bar


Thank you.

 
Aleksey Vyazmikin:

So the thread is for beginners, that's why I didn't know about this feature, thanks for the tip.

Aleksey, mql4 has it a long time ago.


Aleksey Vyazmikin:

Can you please tell me what is the most reliable iBarShift function with two TFs right now ?

I have a task to find, for example, on a minute bar the value of the indicator from the upper TF, for example M15. The timeframes can be any, which complicates the solution.

For an hour TF, I did through the structure, but if it's M15, I'm confused here...

In this case you should try to forget about numbering of bars as it was in mql4. In mql5 everything is almost solved by bar time. In a pinch, you can use the Bars() function.

int  Bars(
   string           symbol_name,     // имя символа
   ENUM_TIMEFRAMES  timeframe,       // период
   datetime         start_time,      // с какой даты
   datetime         stop_time        // по какую дату
   );
Документация по MQL5: Доступ к таймсериям и индикаторам / Bars
Документация по MQL5: Доступ к таймсериям и индикаторам / Bars
  • www.mql5.com
Доступ к таймсериям и индикаторам / Bars - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Alexey Viktorov:
Alexei, this has been available in mql4 for a long time.

Apparently this shows that I don't know mql4 well either, which I never claimed...

Alexey Viktorov:

And in this case try to forget about bar numbering as it was in mql4. In mql5 everything is almost solved by bar time. In a pinch, try to use the Bars() function.

Are you sure about rounding?

Suppose the time on m1 is 14:44, but I need 14:30 (first bar of M15).

 
Aleksey Vyazmikin:

Apparently this shows that I don't know mql4 very well either, which I never claimed...

Are you sure about the rounding?

Let's say the time on m1 is 14:44 and I need to get a time of 14:30 (first bar of m15).

This is something out of a different series. What do you need to get at 14:44 from the M15 bar period of 14:30? Take the function Copy**** in it put PERIOD_M15 and time 14:44 see what you get. And when the result is not what you expect, then the questions will be different.

 
Alexey Viktorov:

It's something from another series. What do you need to get at 14:44 from the M15 bar period of 14:30? Take the function Copy**** in it put PERIOD_M15 and time 14:44 see what you get. And when the result is not what you expect, then the questions will be different.


Yes, that was the direction I was working in. Anyway, I got some help. Thank you, too.