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

 
Igor Zakharov:

Just not the calendar ones. 30 day bars backwards (Sundays, Saturdays)

Judging by the function without the shift parameter you are doing in 5 and you asked the question in 4 :)

Yes, 30 bars is 21 working days if you count from yesterday. (24.02.2019 - 25.01.2019)

I got

For the Month average candle size : 598

For a Week average candle size : 519

And iATR() gives out ten digit numbers...

So I don't understand how to use it yet.

 
Alexander Layzerevich:

And iATR() outputs ten-digit numbers...

Usually, decimal fractions (i.e. like the price. To convert to points you need to divide by _Point).

See illustration: euro/dollar as of today - 560 pips on a daily basis for the month.


 
Igor Zakharov:

Usually, decimal fractions (i.e. like the price. To convert to points, divide by _Point).

See the illustration: euro/dollar as of today - 560 pips on daily basis for a month.


I checked it in MQL4:

iATR

 
Igor Zakharov:

Usually, decimal fractions (i.e. like the price. To convert to points, divide by _Point).

See illustration: euro/dollar as of today - 560 pips on daily basis for a month.


I checked it in MQL5. I wrote the following code to check it:

//************************************************************************************************/
double iPointOrderStep()
{
   int Awerage30 = (iATR (Symbol(),PERIOD_D1, 21));
   Print ("Awerage30 = ", Awerage30);
   int Awerage7 = (iATR (Symbol(),PERIOD_D1, 5));
   Print ("Awerage7 = ", Awerage7);
   
   double iPointOrderStep = NormalizeDouble(((Awerage30+Awerage7)/2),0);
   Print ("iPointOrderStep = ", iPointOrderStep);
   return (iPointOrderStep);
}   
//************************************************************************************************/

This is what it shows:

iATR

I.e., it shows one value, but displays a different one...

 
Alexander Layzerevich:

Checked it in MQL5. Wrote this code to check it:

This is what it outputs:

I.e. it shows one value, but shows a different one...

In MQL5 such a construction of working with the indicator is not suitable

   int Awerage30 = (iATR (Symbol(),PERIOD_D1, 21));
   Print ("Awerage30 = ", Awerage30);

First you have to create a handle, and you've done it, and only after that you can get values fromthe CopyBuffer

// этот код в Init()
      int handleATR=iATR(Symbol(),PERIOD_D1,21);
      if(handleATR==INVALID_HANDLE) return;

// это уже в сам советник/индикатор: OnTick()
      double hATR[];
      CopyBuffer(handleATR,0,0,1,hATR);
// double ATR = hATR[0]; // здесь значение ATR
 
Vitaly Muzichenko:

In MQL5 such a construction of working with the indicator is not suitable

First you have to create a handle, and that's what you've done now, and after that you can get values fromthe CopyBuffer

Thanks, I will take it into account and rewrite the code.

I am just writing it in MT4 and using #include <MT4Orders.mqh> to test it in MT5.

Maybe the library doesn't support Indicators.

 
Alexander Layzerevich:

Thanks, I will take it into account and rewrite the code.

I just write it in MT4 and use #include <MT4Orders.mqh> to test it in MT5.

Maybe the library does not support Indicators.

Yes, only trade functions.

 
Alexander Layzerevich:

Thanks, I will take it into account and rewrite the code.

I just write it in MT4 and use #include <MT4Orders.mqh> to test it in MT5.

Perhaps the library does not support Indicators.

It is a very fast way to combine two platforms, it works, but it's better not to do so in mql5

double ATR(string symb,ENUM_TIMEFRAMES tf,int period,int index) {
 #ifdef __MQL4__
  return(iATR(symb,tf,period,index));
 #else
  double buf[1];
  int handle=iATR(symb,tf,period);
   if(handle<0) {
    PrintFormat("Failed to create handle ATR %s/%s, Error: %d",symb,sTF(tf),GetLastError());
    return(WRONG_VALUE);
   } else {
    if(CopyBuffer(handle,0,index,1,buf)<0) {
     PrintFormat("Failed to copy data from the indicator ATR %s/%s, Error: %d",symb,sTF(tf),GetLastError());
     return(WRONG_VALUE);
    }
   }
   return(buf[0]);
 #endif
 }

And apply the indicator in the same way as in mql4

double atr=ATR(Symbol(),PERIOD_D1, 30);
 
Vitaly Muzichenko:

This is a very fast option to combine the two platforms, it works, but it is better not to do so in mql5

And apply the indicator in the same way as in mql4

Thank you very much for the code.

It has worked:

//************************************************************************************************/
double iPointOrderStep()
{
   double Awerage30 = (int)((ATR(Symbol(),PERIOD_D1,21,1))/Point());
//   Print ("Awerage30 = ", Awerage30);
   double Awerage7 = (int)((ATR(Symbol(),PERIOD_D1,5,1))/Point());
//   Print ("Awerage7 = ", Awerage7);
   
   double iPointOrderStep = NormalizeDouble(((Awerage30+Awerage7)/2/6),0);
//   Print ("iPointOrderStep = ", iPointOrderStep);
   return (iPointOrderStep);
}   
//************************************************************************************************/
double ATR(string symb,ENUM_TIMEFRAMES tf,int period,int index) {
 #ifdef __MQL4__
  return(iATR(symb,tf,period,index));
 #else
  double buf[1];
  int handle=iATR(symb,tf,period);
   if(handle<0) {
    PrintFormat("Failed to create handle ATR %s/%s, Error: %d",symb,sTF(tf),GetLastError());
    return(WRONG_VALUE);
   } else {
    if(CopyBuffer(handle,0,index,1,buf)<0) {
     PrintFormat("Failed to copy data from the indicator ATR %s/%s, Error: %d",symb,sTF(tf),GetLastError());
     return(WRONG_VALUE);
    }
   }
   return(buf[0]);
 #endif
 }
//************************************************************************************************/

Now I have averaged data at the beginning of the EA start. And this data is recalculated each time I access the function.

As I understand it, in order to limit calling, I need to make a buffer record with the calculated data.

So, we need to set the condition for recalculation. On "Mondays" and if there is nothing in the buffer.

I.e. declare variable buferStep = -1; and inOnTick() if buferStep <0 or "Monday" then recalculate.

Here again I'm stuck, how can the Robot declare that today is "Monday" ?

 
Alexander Layzerevich:

Thank you very much for the code.

It worked out like this:

I now have the averaged data at the start of the EA. And this data is recalculated each time the function is called.

As I understand it, in order to limit calling, I need to make a buffer record with the calculated data.

So, we need to set the condition for recalculation. On "Mondays" and if there is nothing in the buffer.

I.e. declare variable buferStep = -1; and inOnTick() if buferStep <0 or "Monday" then recalculate.

Here again I have a hitch, how can Robot declare that today is "Monday" ?

I don't want to use Monday, I just want number of days ago. And I would get it on every new bar so as not to load load the Expert Advisor with calculations on every tick.

Reason: