Error 4051, somewhere in code?

 

Hi,

Im trying to backtest this martingale EA and i get this error 4051 in the journal, and i can see it

effects up to 50% of orders from opening, when i compiled the code  this warning comes up

"return value of 'OrderModify' should be checked Template EA.mq4 265 14" 

 i have pasted the part of the code below where the warning is, unfortunately i dont know how to code just yet, and wouldnt

have a clue how to fix it.

Anybody can help me with it?

Thanks! 

 

 Error opening order

 

return value of 'OrderModify' should be checked Template EA.mq4 265 14

     if (takeprofit!=0)      
             OrderModify(ticket,OrderOpenPrice(),0,takeprofit,0,CLR_NONE); 
      }
   } 
   else {
      Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Error opening order : ",GetLastError()); 
   }
}
 

First, the 4051 error is a runtime error that exists in your OrderSend() function call.  From the info in the log, it appears that the information contained in the lotsize variable you are passing to OrderSend() is invalid.  Check how you are calculating the lot size to find the error.  Also, please use the search function to compare your calculation for lot size to others who have posted/answered questions in this forum about lot size calculation.  

Finally, here is a link to a good discussion regarding checking return values:

What are Function return values ? How do I use them ?

Just a helpful hint, OrderModify() returns a bool: true for success; otherwise false. 
 

Hi Thirteen,

 

Thanks for the reply i have given a good shot at the information you gave me, but i really have no idea how to code yet, im just starting to learn

and i really want to use and backtest this EA, any chance you can have a quick look at the code for me? i think it is a very easy fix just i wouldnt have a clue where to start..

i attached it if you have the time would be super helpful :) 

Files:
templatekea.mq4  11 kb
 

If you really have no idea how to code yet, it really is better if you start with small and simple code and thereafter work into more and more complex coding once you get better and more comfortable with the language.  Just a suggestion.

I'm not going to debug your code...you will learn more about coding and MQL4 if you do it yourself.  This forum has many, many answers--you just have to search for them.  With that said, I do have a suggestion: try not to use NormalizeDouble() in your normalizelot() function; the results may be unreliable in this context.

Rather than:

Your code

double normalizelot(double size) {
//----
   if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) int digit=2;
   else if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) digit=1;
   else digit=0;

   double val=NormalizeDouble(size,digit);
   if (val<MarketInfo(Symbol(),MODE_MINLOT)) val=MarketInfo(Symbol(),MODE_MINLOT);
   else if (val>MarketInfo(Symbol(),MODE_MAXLOT)) val=MarketInfo(Symbol(),MODE_MAXLOT);
//----
   return(val);  
} 

I suggest the following instead:

double normalizelot(double size) {
   double lotstep = MarketInfo(Symbol(), MODE_LOTSTEP), 
          minlot = MarketInfo(Symbol(), MODE_MINLOT), 
          maxlot = MarketInfo(Symbol(), MODE_MAXLOT);

   size = MathFloor(size / lotstep) * lotstep;
   size = MathMax(MathMin(size, maxlot), minlot);
   return(size);
} 

This code forces your lot size (aka, size) to be a multiple of lotstep, rather than rounded to the nearest decimal digit of lotstep.  It also forces "size" to be at or above minlot and at or below maxlot, as you had in your code.

 
 if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) int digit=2;
   else if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) digit=1;
   else digit=0;
This assumes that lotstep is  1, 0.1, or 0.01. Fails for any other value.
Use Thirteen or mine.
Reason: