Questions from Beginners MQL4 MT4 MetaTrader 4 - page 196

 
Vitaly Muzichenko:

This is the layout, there is no difference between 4 and 5

The handle should be created once at OnInit(), not at each step. You still don't know that?

 
deleted, need to think about it.
 
Vitaly Muzichenko:

How about this?

And this way you still create a handle on every tick. Use the Styler and you'll see the logic errors immediately:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double MA(string symb,ENUM_TIMEFRAMES tf,int period,int ma_shift,
          ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int index)
  {
#ifdef __MQL4__
   return(iMA(symb,tf,period,ma_shift,method,price,index));
#else
   double buf[1];
   static int hMA=-1;
   int handle=iMA(symb,tf,period,ma_shift,method,price);
   if(handle<hMA)
     {
      PrintFormat("Failed to create handle MA %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 MA %s/%s, Error: %d",symb,sTF(tf),GetLastError());
         return(WRONG_VALUE);
        }
     }
   hMA=handle;
   return(buf[0]);
#endif
  }
 
Vladimir Karputov:

But you are still creating a handle on every tick. Use the Styler and you'll see the logic errors immediately:

Like this

double MA(string symb,ENUM_TIMEFRAMES tf,int period,int ma_shift,
                                      ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int index) {
 #ifdef __MQL4__
  return(iMA(symb,tf,period,ma_shift,method,price,index));
 #else
  double buf[1];
  static int handle=-1;
   if(handle<0) {
    handle=iMA(symb,tf,period,ma_shift,method,price);
   }
   if(handle<0) {
    PrintFormat("Failed to create handle MA %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 MA %s/%s, Error: %d",symb,sTF(tf),GetLastError());
     return(WRONG_VALUE);
    }
   }
   return(buf[0]);
 #endif
 }
Then the rest of the peak search code is cross-platform
 
Vitaly Muzichenko:

I think so.

Then the rest of the peak search code is cross-platform

Yeah, it's...

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double MA(string symb,ENUM_TIMEFRAMES tf,int period,int ma_shift,
          ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int index)
  {
#ifdef __MQL4__
   return(iMA(symb,tf,period,ma_shift,method,price,index));
#else
   double buf[1];
   static int handle=-1;
   if(handle<0)
     {
      handle=iMA(symb,tf,period,ma_shift,method,price);
     }
   if(handle<0)
     {
      PrintFormat("Failed to create handle MA %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 MA %s/%s, Error: %d",symb,sTF(tf),GetLastError());
         return(WRONG_VALUE);
        }
     }
   return(buf[0]);
#endif
  }

clearly better. But this block is out of place - it should be placed immediately after the handle is created.

 
Vladimir Karputov:

Yes, now

clearly better. But this block is in the wrong place - it should be placed immediately after creating the handle.

The best way is probably like this, it will print a creation error once, but then there will be fewer checks for if(...)

double MA(string symb,ENUM_TIMEFRAMES tf,int period,int ma_shift,
                                      ENUM_MA_METHOD method,ENUM_APPLIED_PRICE price,int index) {
 #ifdef __MQL4__
  return(iMA(symb,tf,period,ma_shift,method,price,index));
 #else
  double buf[1];
  static int handle=-1;
   if(handle<0) {
    handle=iMA(symb,tf,period,ma_shift,method,price);
    PrintFormat("Failed to create handle MA %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 MA %s/%s, Error: %d",symb,sTF(tf),GetLastError());
     return(WRONG_VALUE);
    }
   }
   return(buf[0]);
 #endif
 }
 
Vitaly Muzichenko:

The best way is probably like this, it will print a creation error once, but then there will be fewer checks for if(...).

I can't see anything in your mess - it all blurs.

Use the Styler.
 
Vladimir Karputov:

I can't see anything in your mess - it all blends in.

Use the Styler.

If anything, I didn't see my style in the styler, and there are oh so many of them

Files:
cc.PNG  100 kb
 

Unfortunately, my indexer is based on MQL4 and this function is only a small part of it.

That's why it's not so efficient to implement MQL5, and it would be long and problematic to rewrite the whole thing.

 
GlaVredFX:

Unfortunately, my indexer is based on MQL4 and this function is only a small part of it.

That's why it's not so efficient to implement MQL5, and it would be long and problematic to rewrite the whole thing.

An indicator is rewritten in MQL5 on average in half an hour. With a cup of coffee.

Reason: