Open Buy Order – Error 131: invalid trade volume

 

Hi

I am sometimes getting this error message on my small live account even though i am doing a check for lotstep and minlot. (I haven't been able to replicate the issue in backtesting, so i can't check the values to see if they had extra decimal places)

I have already normalized the calclot value which becomes my lotsize.

Do i need to use normailze double again after the lotstep and minlot checks? as per this thread - https://www.mql5.com/en/forum/139338

// Lot size calculation
      // sets the dynamic lotsize
      if(DynamicLotSize == true)
      {
       RiskAmount = ((AccountFreeMargin() + ManualBalance) * (MaxPctLoss / 100)); //manualbalance v2.1.4
      TickValue = PipValuePerLot(Symbol());
      stoploss = SL_Pips() / pips2dbl;
      if ( stoploss != 0)
      {CalcLots = NormalizeDouble(RiskAmount / stoploss / TickValue,2);}
      else
            { Alert("WARNING: stoploss == 0 !"); } //-  -turned off for optimization
      LotSize = CalcLots;
      }
      else LotSize = FixedLotSize;
      
      
      ///Check Lotsize against the initial margin required. This has to be done as if increasing the percentage used
      // often you will run out of margin. So Lotsize can only be as big as the margin allows.
      // On a broker using 50:1 leverage this can be quite restrictive to the Agressive lotsizing.

      MaxMarginLotSize = (AccountFreeMargin() - RiskAmount) / MarketInfo(Symbol(),MODE_MARGINREQUIRED);

      if (LotSize > MaxMarginLotSize) 
      {LotSize = MaxMarginLotSize;}
      else LotSize = LotSize;


      // Lot size verification to account for different brokers
   double minLot = MarketInfo(Symbol(), MODE_MINLOT);
   double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   LotSize = MathFloor(LotSize/lotStep)*lotStep;
   if (LotSize<minLot)LotSize=minLot;   
   
      
        

      return (LotSize);

Do i need to add the normalize double to

 LotSize = MathFloor(LotSize/lotStep)*lotStep;

so it becomes

LotSize = NormalizeDouble(MathFloor(LotSize/lotStep)*lotStep,Digits);

thanks

Simon

 
simoncs:

Hi

I am sometimes getting this error message on my small live account even though i am doing a check for lotstep and minlot. (I haven't been able to replicate the issue in backtesting, so i can't check the values to see if they had extra decimal places)

I have already normalized the calclot value which becomes my lotsize.

Do i need to use normailze double again after the lotstep and minlot checks? as per this thread - https://www.mql5.com/en/forum/139338

Do i need to add the normalize double to

so it becomes

thanks

Simon

You don't need to use NormalizeDouble() . . . where is your OrderSend() ? we need to see the call to this function and the OrderSend() and all the code in between . . . when you get an error do you print all the relevant variables so you can determine the cause of the error ? what was the position size when you get this error 131 ?
 
RaptorUK:
You don't need to use NormalizeDouble() . . . where is your OrderSend() ? we need to see the call to this function and the OrderSend() and all the code in between . . . when you get an error do you print all the relevant variables so you can determine the cause of the error ? what was the position size when you get this error 131 ?


I do have print statements, though I wasn't converting this variable to a string and adding precision unfortunately. So it just output 0.08, (I have now added doubletostr in the code below).

OpenBuyOrder(LotSize(),SL(),TP());   ///here is the execution of the order


///and here is the function

   //Buy Orders
   int OpenBuyOrder(double LotSize, double SL, double TP,string commt = "Buy Order")

   {
   while(IsTradeContextBusy()) Sleep(10);
   RefreshRates();
   // Place Buy Order
   BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage*pips2points,0,0,commt+MagicNumber,MagicNumber,0,Green);
   OrderOpened = true;



   Print(StringConcatenate("SwingPips = ",MathAbs(SwingValue[0]-SwingValue[1])/pips2dbl,"| Sl_Pips = ", DoubleToStr(SL_Pips()/pips2dbl ,Digits)," |MaxPctLoss=",MaxPctLoss," |CalcLots = ",CalcLots," |Lotsize = ",DoubleToStr(LotSize,Digits)," |MaxMarginLotSize = ",MaxMarginLotSize," |RiskAmount=",RiskAmount,"| EL = ", DoubleToStr(EL(),5),"| Trend = ", Trend ,"| LowestPrice = ", DoubleToStr(LowestPrice(),Digits) ,"| HighestPrice = ", DoubleToStr(HighestPrice(),Digits) ,"| Fib38 = ", DoubleToStr(Fib38Line,5), "| Fib50 = ", 
   DoubleToStr(Fib50Line,5),"|Fib61 = ", DoubleToStr(Fib61Line,5)," |Fib73 = ", DoubleToStr(Fib73Line,5),"| SD0 =",DoubleToStr(SD0,Digits),"| SD1 =",DoubleToStr(SD1,Digits),"| SK0 =",DoubleToStr(SK0,Digits),"| SK1 =",DoubleToStr(SK1,Digits),"| Tickvalue = ",TickValue,"| Stoploss = ",stoploss,
   "| SwingValue[0] = ", DoubleToStr(SwingValue[0],5),"| SwingValue[1] = ", DoubleToStr(SwingValue[1],5)," | No of bars",CountSwingBars,"| SwingDate[0] = ",TimeToStr(SwingDate[0],TIME_DATE|TIME_SECONDS),"| SwingDate[1] = ", TimeToStr(SwingDate[1],TIME_DATE|TIME_SECONDS),
   "| Bid:",DoubleToStr(Bid,5) ,"| Ask:",DoubleToStr(Ask,5),
   "| SL = ", DoubleToStr(SL(),5),"| TP = ", DoubleToStr(TP(),5)," | Min Lot size expected: ",MODE_STOPLEVEL,"| P/L = ", MathAbs(OrderOpenPrice() - OrderClosePrice()) ));

   //// Error Handling -  -turned off for optimization

   if(BuyTicket == -1)
   {
   ErrorCode = GetLastError();
   string ErrDesc = ErrorDescription(ErrorCode);
   string ErrAlert = StringConcatenate("Open Buy Order – Error ",
   ErrorCode,": ",ErrDesc);
   Alert(ErrAlert);
   string ErrLog = StringConcatenate("Bid: ",Bid," Ask: ",Ask," Lots: ",LotSize,"| Fib38 = ", DoubleToStr(Fib38Line,5), "| Fib50 = ", 
   DoubleToStr(Fib50Line,5),"|Fib61 = ", DoubleToStr(Fib61Line,5),"|Fib73 = ", DoubleToStr(Fib73Line,5),
   "| SwingValue[0] = ", DoubleToStr(SwingValue[0],5),"| SwingValue[1] = ", DoubleToStr(SwingValue[1],5),
   "| SL = ", DoubleToStr(SL(),5),"| TP = ", DoubleToStr(TP(),5),"| EL = ", DoubleToStr(EL(),5)," | Current Spread: ",MODE_SPREAD," | Min Lot size expected: ",MODE_STOPLEVEL,"| prevzigzag = ", DoubleToStr(prevZigZag,5) ,"| newzigzag = ", newZigZag ,
   "| Trend = ", Trend ,"| Barcheck = ", BarCheck ,"| P/L = ", MathAbs(OrderOpenPrice() - OrderClosePrice()));
   Print(ErrLog);
   }

   return(BuyTicket);
   }
 
RaptorUK:
we need to see the call to this function and the OrderSend() and all the code in between . . .

To clarify what I meant . . . the function that returns LotSize, we need to see the call to it and the call to OpenBuyOrder() and the code in between . . . if LotSize is correct when returned by the function and incorrect when used in the OrderSend() then something is being done to it in between . . .
 
RaptorUK:
To clarify what I meant . . . the function that returns LotSize, we need to see the call to it and the call to OpenBuyOrder() and the code in between . . . if LotSize is correct when returned by the function and incorrect when used in the OrderSend() then something is being done to it in between . . .

I see your point, but there is nothing else that touches Lotsize. The lotsize function is as above
the call is here...

if ((EL()+pips2dbl) - buyprice() < Point/2)  // enter when price has gone back above the EL by 1 pip
         if (buyprice()< TP())
               OpenBuyOrder(LotSize(),SL(),TP());

the call to lotsize is in the OpenBuyOrder function.

I don't know if it makes a difference but I am only seeing this on my ECN account, not my standard account.

 
simoncs:

I see your point, but there is nothing else that touches Lotsize. The lotsize function is as above
the call is here...

the call to lotsize is in the OpenBuyOrder function.

I don't know if it makes a difference but I am only seeing this on my ECN account, not my standard account.

OK, thanks for the clarification . . . so now you need to capture all the relevant data for an error and use that data to determine what is causing the error. Then you can possibly determine the cause and fix it . . . or failing that add "belt and braces" code to work around the issue.

Make sure you are printing everything you need to the precision, or better, that you need and wait . . .
 

What is this meant to print ?

Current Spread: ",MODE_SPREAD," | Min Lot size expected: ",MODE_STOPLEVEL,

MODE_SPREAD is 13 MODE_STOPLEVEL is 14

 
RaptorUK:

What is this meant to print ?

MODE_SPREAD is 13 MODE_STOPLEVEL is 14


i can't remember :(

i think it was just left there from when i was doing some other debugging.

i will add some more print statements and hope i find the problem

thanks

 

There seems to be a flurry of posts with exceptionally wide code just lately

simoncs, your 2nd post in this thread is difficult to read, please put long lines of code on 2 or 3 lines so that we don't have to keep scrolling from left to right.