Need help in avoiding error 131

 
Hi, I need help in avoiding orderSend error 131 and orderClose error 131. Here is the value I assign to lot size:

Double lots=NormalizeDouble(MathAbs((AccountEquity()/10000)),2);

and Here is How I send an order:

OrderSend(Symb,OP_SELL,lots,Bid,2,Ask+600*Point,Ask-2000*Point);

I need to program my EA in such a way that it closes ALL opposite trades Whenever there is an opposite signal.
This here is what I came up with for closing all buy trades( there are no pending orders).
 for(i=1; i<=OrdersTotal(); i++)       
   {
   if(OrderSelect(i-1,SELECT_BY_POS)==true)
   Tip=OrderType();                
   if (Tip==0)
   {
   Ticket_Buy=OrderTicket();
   lot=OrderLots();
   Ans=OrderClose(Ticket_Buy,lot,Bid,2);
   }
  }
I defined the variables before I used them in the code above and I had no problem in compiling. But somehow the value of the variable "lot" is always 0 and I keep getting the 131 error.
I seriously don't know what I'm doing wrong. Please have in mind that I'm new to this so I might need you to spoon feed me. Thanks in advance for your help.
 
farhang:
Hi, I need help in avoiding orderSend error 131 and orderClose error 131. Here is the value I assign to lot size:

Double lots=NormalizeDouble(MathAbs((AccountEquity()/10000)),2);

Error 131 means the volume is invalid. You should verify that your lot size complies with MODE_MINLOT and MODE_LOTSTEP before using it (although I am not sure why u get lots=0, maybe the problem is somewhere else in your code?). Use MarketInfo() to get these values. See here -> https://docs.mql4.com/common/MarketInfo, https://docs.mql4.com/constants/marketinfo.


Order closing loop - you need to decrement the counter and not increment it. See here -> https://www.mql5.com/en/forum/119840. btw - It's recommended to count from 0 instead of using i-1... (but that's up to you). Make sure Tip is not zero by default, otherwise if OrderSelect() fails then you might get an OrderClose() error. You should also use RefreshRates() immediately before attempting the close -> https://docs.mql4.com/windows/RefreshRates.

 
if(OrderSelect(i-1,SELECT_BY_POS)==true)  Tip=OrderType();                
if (Tip==0) { //... order close
Problem one. After the first close, tip is set so you will continue to close all orders even if order select fails.
Problem two. After closing the first order, order position 2 becomes 1, 3 becomes 2, etc. You will close every OTHER order. and then the order selects begin to fail but you don't test for that.
Problem three. Once you do any orderSend/modify/close or delete you must refresh before doing another.
Always count down.
Always test orderSelect.
bool    need2refresh=false;
for(int index = OrdersTotal() - 1; index >= 0; index--) if (
    OrderSelect(index, SELECT_BY_POS)     // Only my orders w/
&&  OrderMagicNumber()  == MagicNumber    // my magic number
&&  OrderSymbol()       == Symbol() ) {   // and period and symbol
    if (need2refresh) { RefreshRates();     need2refresh=false; }
    // ... 
    need2refresh=OrderClose(...
Reason: