Download MetaTrader 5

Error 131. Cant understand Why and i have read a lot about it. Please help.

To add comments, please log in or register
Marcos Silva
358
Marcos Silva  

Hi,

Im getting the following message, and ill attach the function that closes the order also...

2013.11.28 05:49:52     2012.12.21 12:40  Yoda GRInD v1.2 EURUSD,M1:  OrderTicket() = 11 CloseSellLot = 0.11 UMBRELLA EPTotalBuyProfit = 13.6
2013.11.28 05:49:52     2012.12.21 12:40  Yoda GRInD v1.2 EURUSD,M1: OrderClose error 131
2013.11.28 05:49:52     2012.12.21 12:40  Yoda GRInD v1.2 EURUSD,M1:  OrderTicket() = 13 CloseBuyLot = 0.11
2013.11.28 05:49:49     2012.12.21 12:10  Yoda GRInD v1.2 EURUSD,M1: modify #13 buy 0.14 EURUSD at 1.32171 sl: 1.22171 tp: 1.32281 ok
2013.11.28 05:49:49     2012.12.21 12:10  Yoda GRInD v1.2 EURUSD,M1: open #13 buy 0.14 EURUSD at 1.32171 ok

And the function:

                SmartEPBuy();
                for (int  e = OrdersTotal(); e >= 0 ; e--) {
                                if ( OrderSelect(e,SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic +4) ){  
                                        if((OrderType()==OP_SELL)  && (SmartEPBuy == true) ){   // && (OrderComment() == "EPUSELL" )
                                        
                                                double CloseSellLot = MathAbs( NormalizeDouble( ((EPCloseLotMult*LotsOptimized()) - OrderLots()),LotDigits)); //)+1
                                                if ((OrderLots()  == LotsOptimized()) || (CloseSellLot > OrderLots()) || (CloseSellLot == 0)) return;    
                                                
                                                Print(" OrderTicket() = " , OrderTicket() ," CloseSellLot = " , CloseSellLot );
                                                
                                                SmartEPBuy();
                                                if (SumOfSell > 2) CloseSellLot = OrderLots();
                                                OrderCloseReliable(OrderTicket(),CloseSellLot,Ask,SLIPPAGE(),CLR_NONE); 
                                                for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--){
                                                                if ( OrderSelect(iPos,SELECT_BY_POS, MODE_HISTORY) && OrderSymbol()==Symbol() && (OrderMagicNumber()==Magic +4) && (OrderType()==OP_SELL) ){  //&& (OrderComment() == "EPUSELL" )
                                                                        EPTotalSellProfit = ((OrderProfit() + OrderCommission() + OrderSwap())) + EPTotalSellProfit; // here 1st/2
                                                                        Print(" OrderTicket() = " , OrderTicket() , " CloseSellLot = " , CloseSellLot ," EPTotalSellProfit = " , EPTotalSellProfit );
                                                                        UTicketSell = OrderTicket()+1;
                                                                        break;
                                                                }
                                                }
                                                break;
                                        }
                                }
                                
                                //if (TicketEPCloseSell > 0){
                                //      CloseSellLot = NormalizeDouble(CloseSellLot,LotDigits);
                                //      OrderSendReliable(Symbol(), OP_BUY,  CloseSellLot, Ask, SLIPPAGE(), 0, 0, DoubleToStr(SPREAD(),1), Magic, 0, CLR_NONE);
                                //}
                }
Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
investguy:

Hi,

Im getting the following message, and ill attach the function that closes the order also...

And the function:


You have to be sure that the position size you are trying to close is >= Min Lot and a multiple of Lot Step

What is reporting the error ? OrderCloseReliable ? is it doing it correctly ?

whroeder1
14779
whroeder1  
RaptorUK: You have to be sure that the position size you are trying to close is >= Min Lot and a multiple of Lot Step
  1. The amount trying to close is >= min lot and I think the amount remaining must also be >= min lot.
  2. investguy:
    2013.11.28 05:49:52     2012.12.21 12:40  Yoda GRInD v1.2 EURUSD,M1:  OrderTicket() = 13 CloseBuyLot = 0.11
    2013.11.28 05:49:49     2012.12.21 12:10  Yoda GRInD v1.2 EURUSD,M1: open #13 buy 0.14 EURUSD at 1.32171 ok
    After you close 0.11 out of 0.14 the remaining will be 0.03. What is minlot?
  3. You are using NormalizeDouble(..., LotDigits) This assumes that lotStep is a multiple of 10 (0.1/0.01) and fails for any other. Do it right.
Simon Gniadkowski
Moderator
18003
Simon Gniadkowski  
WHRoeder:
  1. The amount trying to close is >= min lot and I think the amount remaining must also be >= min lot.
Actually . . that would make a huge chunk of sense.
Marcos Silva
358
Marcos Silva  

I have found the problem but still cant understand the reason for the problem.

Here is the problem.. In this part:

SmartEPBuy();
if (SumOfSell > 2) CloseSellLot = OrderLots();

OrderLots() assume a completely different value to the one previously selected by OrderSelect.

Here is SmartEPBuy:

void SmartEPBuy(){      
        double TotalLot = 0;    
        SumOfSell = 0; 
        
                for (int e = OrdersTotal(); e >= 0 ; e--) {
                        if ( OrderSelect(e,SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol() && ((OrderMagicNumber()==Magic ) || (OrderMagicNumber()==Magic +4 )) ){  
                                if(OrderType()==OP_SELL){
                                        TotalLot = OrderLots() + TotalLot;
                                        SumOfSell++;
                                }
                        }
                }
                TotalLotS = TotalLot ;
                if (TotalLot >= EPTotalLotMult*LotsOptimized()) { //(TotalLot >= ((EPTotalLotMult-1)*LotsOptimized()))
                        SmartEPBuy = true;
                        EPUmbrellaBuy = TotalLot ;
                        //return;
                }
                else {
                        SmartEPBuy = false;
                        EPUmbrellaBuy = 0;
                }
        
}

Could it be that the OrderLots() is using the OrderSelect from inside of this function ?

whroeder1
14779
whroeder1  
Ofcouse OrderLots uses the last OrderSelect
Marcos Silva
358
Marcos Silva  

Noted Sir! Thank You for the insights.

Very appreciated.

Marcos Silva
358
Marcos Silva  
Oh, and just for future reference.. The simplest solution i found is to turn OrderLots() and OrderTicket() into a variable before calling my function and using the variables when calling OrderClose(....)
To add comments, please log in or register