Download MetaTrader 5

Automatic Validation problem. Order Send error 131

To add comments, please log in or register
Stephen Reynolds
7587
Stephen Reynolds  

Hi, ive tested the my EA on my current broker Vipromarkets and it test okay with no errors. But, when I send it in for automatic validation I get ordersend error 131 and a zero divide error. After looking into the problem this type of error is associated with lot volume.

The part of code from my EA is as follows :

input double LotSize = 0.1;  // Lot Size

// OnTick() event handler                
void OnTick()
{
   double lotSize = LotSize; 
   
   if(UseMoneyManagement)
   {
      double margin = AccountBalance() * (RiskPercent / 100);  // 10,000 x (2 / 100 = 0.02) = 200
      double tickSize = MarketInfo(_Symbol,MODE_TICKVALUE);  // 1.0
      
      lotSize = (margin / StopLoss) / tickSize; // 200 / 50 = 4    4 / 1.0 = 4

      if(lotSize < MarketInfo(_Symbol,MODE_MINLOT)) lotSize = MarketInfo(_Symbol,MODE_MINLOT);  // lotSize must comply with 
      else if(lotSize > MarketInfo(_Symbol,MODE_MAXLOT)) 
         lotSize = MarketInfo(_Symbol,MODE_MAXLOT);        
      else if(MarketInfo(_Symbol,MODE_LOTSTEP) == 0.1) lotSize = NormalizeDouble(lotSize,1);
         else NormalizeDouble(lotSize,2);      
   }

   // Then i have a trade condition met which opens the following pending buystop orders :
   gBuyTicket = OrderSend(_Symbol,OP_BUYSTOP,lotSize,pendingBuyPrice,Slippage,stopLoss,takeProfit,"BuyOrder",MagicNumber,0,clrBlue); 
   gSellTicket = OrderSend(_Symbol,OP_SELLSTOP,lotSize,pendingSellPrice,Slippage,stopLoss,takeProfit,"SellOrder",MagicNumber,0,clrRed);
 
}

I cant see the problem?

Carl Schreiber
7354
Carl Schreiber  
Go through the function with the debugger and track every variable I guess then you'll see...
whroeder1
16058
whroeder1  
  1.       double tickSize = MarketInfo(_Symbol,MODE_TICKVALUE);  // 1.0
          
          lotSize = (margin / StopLoss) / tickSize; // 200 / 50 = 4    4 / 1.0 = 4
    Do not use tick value by itself
  2.      else if(MarketInfo(_Symbol,MODE_LOTSTEP) == 0.1) lotSize = NormalizeDouble(lotSize,1);
             else NormalizeDouble(lotSize,2);     
    Assumes step is a multiple of 10
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out

  3. gBuyTicket = OrderSend(_Symbol,OP_BUYSTOP,lotSize,pendingBuyPrice,Slippage,stopLoss,takeProfit,"BuyOrder",MagicNumber,0,clrBlue); 
    gSellTicket = OrderSend(_Symbol,OP_SELLSTOP,lotSize,pendingSellPrice,Slippage,stopLoss,takeProfit,"SellOrder",MagicNumber,0,clrRed);
    
    Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Stephen Reynolds
7587
Stephen Reynolds  
whroeder1:
  1. Do not use tick value by itself
  2. Assumes step is a multiple of 10
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out

  3. Check your return codes. What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles


Yes, but my problem is in seeing the return values. Because its the automatic validation that returns error. When I test on my broker it works okay.

So is there any way of finding out the return values of whatever broker or software the automatic validation uses?

whroeder1
16058
whroeder1  
Stephen Reynolds: When I test on my broker it works okay.

  1. Only because of the specific pair/account currency combination that you are using.
  2. Fix your broken code. #1 do not use tick value by it self.
honest_knave
Moderator
2324
honest_knave  

<< moved >>


Forum on trading, automated trading systems and testing trading strategies

OrderSend error 131

Stephen Reynolds, 2017.03.29 21:38

Why am I getting an order send 131 error on the following code :

#property strict

// Input variables
input int MagicNumber = 101;
input int Slippage = 10;

input double LotSize = 1.0;
input int StopLoss = 500;
input int TakeProfit = 500;

// Global variables
int gBuyTicket;


// OnTick() event handler                
void OnTick()
{
   // Our bar count for our condition
   int hour = Hour();
   int timehour = 9;
        
   // Our order condition  
   if(hour == timehour && gBuyTicket == 0) 
   {
      // Open buy order                                                                                     
      gBuyTicket = OrderSend(_Symbol,OP_BUY,LotSize,Ask,Slippage,0,0,"Buy order",MagicNumber,0,clrGreen);
      
      // Add stop loss & take profit to Buy order
      if(gBuyTicket > 0 && (StopLoss > 0 || TakeProfit > 0))
      {
         bool select = OrderSelect(gBuyTicket,SELECT_BY_TICKET);
         
         // Calculate stop loss & take profit
         double stopLoss = 0, takeProfit = 0;
         if(StopLoss > 0) stopLoss = OrderOpenPrice() - (StopLoss * _Point);
         if(TakeProfit > 0) takeProfit = OrderOpenPrice() + (TakeProfit * _Point);
         
         // Verify stop loss & take profit
         double stopLevel = MarketInfo(_Symbol,MODE_STOPLEVEL) * _Point;
         
         RefreshRates();
         double upperStopLevel = Ask + stopLevel;
         double lowerStopLevel = Bid - stopLevel;
         
         if(takeProfit <= upperStopLevel && takeProfit != 0) takeProfit = upperStopLevel + _Point;
         if(stopLoss >= lowerStopLevel && stopLoss  != 0) stopLoss = lowerStopLevel - _Point; 
         
         // Modify order
         bool modify = OrderModify(gBuyTicket,0,stopLoss,takeProfit,0);
      }               
   }
   
   // Reset tickets   
   if(OrdersTotal() == 0 && hour == 23)
   {
      gBuyTicket = 0;
   }                
}      

To add comments, please log in or register