OrderSelect refuse to select any order. Why?

 

Dear friends,

I try to make a code (a function), to close orders following all conditions described in the code below (with all "if's").

BUT it doesn't work. It wouldn't select any order, and returns the Error 4105 (No order selected). In the trading pool are existing many executed and pending orders.

Can anybody help me, and say me why that?

Thank you very much!!

The code:

void CloseMagic() {
   int Cmd,Ticket1,Lots,OpPrice=0,Cmd1;
   RefreshRates();
   for(int i=0;i<=OrdersTotal();i++) {
      if(OrderSelect(i,SELECT_BY_POS)) {			// HERE IS THE PROBLEM!!
         Ticket1=OrderTicket();
         Lots=OrderLots();
         OpPrice=OrderOpenPrice();
         Cmd=OrderType();
      
         if(Lots>AccountFreeMargin()*0.9/MarketInfo(Symbol(),MODE_MARGINREQUIRED)) {       // To not reach the FreeMargin limit
            Lots=AccountFreeMargin()*0.8/MarketInfo(Symbol(),MODE_MARGINREQUIRED);
            i=i+1;
         }
      
         if(Cmd==OP_BUY) {
            IDPrice=OpPrice+Paso-Spread*Point; IDCmd=OP_SELL; IDCmd2=6; Razon="Buscar grupo desde BUY."; TicketID=IDTicket();	// This function searches any
            if(TicketID!=0) {													// order with the given parameters
               if(OrderSelect(TicketID,SELECT_BY_TICKET)) {									// in the trading pool, but the
                  if(OrderOpenPrice()>OpPrice) {										// problem is not here.
                     OrderClose(OrderTicket(),Lots,OrderOpenPrice(),3,Blue);
                     OrderClose(Ticket1,Lots,OpPrice,3,Blue);
                  }
               }
            }
         }
         if(Cmd==OP_SELL) {
            IDPrice=OpPrice-Paso+Spread*Point; IDCmd=OP_BUY; IDCmd2=6; Razon="Buscar grupo desde SELL."; TicketID=IDTicket();
            if(TicketID!=0) {
               if(OrderSelect(TicketID,SELECT_BY_TICKET)) {
                  if(OrderOpenPrice()<OpPrice) {
                     OrderClose(OrderTicket(),Lots,OrderOpenPrice(),3,Blue);
                     OrderClose(Ticket1,Lots,OpPrice,3,Blue);
                  }
               }
            }
         }
      }
   }
   Error=GetLastError();
   if(Error!=0) Print("Error de CloseMagic: ",Error);
}
 

We only partial of the code and partial of logic, and I see plenty error there.

However, assuming other parts of the code is correct, this is what I think for buy and same thing goes for sell (and I highlight them) ...

if(TicketID!=0) 
  {                                                               
  if(OrderSelect(TicketID,SELECT_BY_TICKET)) 
    {                                                            
    if(OrderOpenPrice()>OpPrice) // ==>> this 'if' will never works coz the value of OpPrice is equal with OrderOpenPrice()
       {
       OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), 3, Blue);
       }
    }
 }

 
onewithzachy:

We only partial of the code and partial of logic, and I see plenty error there.

However, assuming other parts of the code is correct, this is what I think for buy and same thing goes for sell (and I highlight them) ...


Dear, I assume that I havn´t explain this correctly.

The Error that appears is the 4105 (ERR_NO_ORDER_SELECTED) and not any other that have a relationship with OrderClose function.

Besides, the IDTicket function searches for any Other order, and not for the same, so that condition is correct (and clearly incorrect if the founded order are the same, but that not happens).

I tried to enclose this part of code (with the IDTicket function (below)) in a script, to make sure that the remanining program is not the problem. The script doesn't work.

I repeat, that the problem is the OrderSelect, that doesn't work, or I havn´t ounderstood what you will say.

(Excuseme for my english grammar, but I speak Spanish and German, but no English).

Thank you a lot!

int IDTicket() {
   int Ticket=0;
   string IDExito="No";
   // Search arguments: IDPrice,IDCmd,IDCmd2,Razon
   for(int k=0;k<=OrdersTotal();k++) {
      if(OrderSelect(k,SELECT_BY_POS)) {
         if((IDCmd==OrderType() || IDCmd2==OrderType()) && IDPrice<OrderOpenPrice()+0.0005 && IDPrice>OrderOpenPrice()-0.0005) {
            Ticket=OrderTicket();
            IDExito="Si";
         }
      }
   }
   Print(IDExito," se encuentra ticket: ",Ticket," al precio buscado de: ",IDPrice," de tipo: ",OrderType(), " || Razón: ",Razon);
   Error=GetLastError();
   if(Error!=0) {
      Print("Error en IDTicket: ",Error," en razón: ",Razon);
      Error=0;
   }
   return(Ticket);
}
 

Although I have no guarantee, try this.

https://www.mql5.com/en/forum/44706

One more thing. Your loop with index k will repeat OrdersTotal() + 1 times.

You should write as below.

for(int k=0;k<OrdersTotal();k++) { // Do not use "<=". Use "<"

Good luck.

 
eliusm:


Dear, I assume that I havn´t explain this correctly.

The Error that appears is the 4105 (ERR_NO_ORDER_SELECTED) and not any other that have a relationship with OrderClose function.

Besides, the IDTicket function searches for any Other order, and not for the same, so that condition is correct (and clearly incorrect if the founded order are the same, but that not happens).

I tried to enclose this part of code (with the IDTicket function (below)) in a script, to make sure that the remanining program is not the problem. The script doesn't work.

I repeat, that the problem is the OrderSelect, that doesn't work, or I havn´t ounderstood what you will say.

(Excuseme for my english grammar, but I speak Spanish and German, but no English).

Thank you a lot!

I see, try this one and see if there's still some error

:D

void CloseMagic()
  {
   int Cmd,Ticket1,Lots,OpPrice=0,Cmd1;
   RefreshRates();
   for(int i=0;i<=OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS))                                    // HERE IS THE PROBLEM!!
        {         
         //Ticket1=OrderTicket();
         //Lots=OrderLots();
         //OpPrice=OrderOpenPrice();
         //Cmd=OrderType();
         
                                                                           // To not reach the FreeMargin limit
         if(Lots>AccountFreeMargin()*0.9/MarketInfo(Symbol(),MODE_MARGINREQUIRED))
           {       
            Lots=AccountFreeMargin()*0.8/MarketInfo(Symbol(),MODE_MARGINREQUIRED);
            i=i+1;
           }

         if(OrderType()==OP_BUY)
           {
            Ticket1=OrderTicket();
            Lots=OrderLots();
            OpPrice=OrderOpenPrice();
           
            IDPrice=OpPrice+Paso-Spread*Point; 
            IDCmd=OP_SELL; 
            IDCmd2=6; 
            Razon="Buscar grupo desde BUY."; 
            TicketID=IDTicket();                                             
            if(TicketID!=0)                                                  // This function searches any
              {                                                              // order with the given parameters
               if(OrderSelect(TicketID,SELECT_BY_TICKET))                    // in the trading pool, but the
                 {                                                           // problem is not here.
                  if(OrderOpenPrice()>OpPrice)// ==>> this 'if' will never works coz the value of OpPrice is equal with OrderOpenPrice()
                    {                                                          
                     //OrderClose(OrderTicket(),Lots,OrderOpenPrice(),3,Blue);
                     //OrderClose(Ticket1,Lots,OpPrice,3,Blue);
                     OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), 3, Blue);
                    }
                 }
              }
           }
         if(OrderType()==OP_SELL)
           {
            Ticket1=OrderTicket();
            Lots=OrderLots();
            OpPrice=OrderOpenPrice();
           
            IDPrice=OpPrice-Paso+Spread*Point; 
            IDCmd=OP_BUY; 
            IDCmd2=6; 
            Razon="Buscar grupo desde SELL."; 
            TicketID=IDTicket();
            if(TicketID!=0)
              {
               if(OrderSelect(TicketID,SELECT_BY_TICKET))
                 {
                  if(OrderOpenPrice()<OpPrice)// ==>> this 'if' will never works coz the value of OpPrice is equal with OrderOpenPrice()
                    {
                     //OrderClose(OrderTicket(),Lots,OrderOpenPrice(),3,Blue);
                     //OrderClose(Ticket1,Lots,OpPrice,3,Blue);
                     OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), 3, Blue);
                    }
                 }
              }
           }
        }
     }
   Error=GetLastError();
   if(Error!=0) Print("Error de CloseMagic: ",Error);
  }
//+------------------------------------------------------------------+
 
eliusm:

Dear friends,

I try to make a code (a function), to close orders following all conditions described in the code below (with all "if's").

BUT it doesn't work. It wouldn't select any order, and returns the Error 4105 (No order selected). In the trading pool are existing many executed and pending orders.

Can anybody help me, and say me why that?

Thank you very much!!

The code:

Read this thread: Loops and Closing or Deleting Orders
 

Dear friends, all of that are not the problem. Thanks for all the other helpful information!

onewithzachy, you are not understanding the code. In the first OrderSelect call, it searches for any order in the trading pool and memorize its parameters, to furhther close it, ONLY if there are ANOTHER order in the pool that were executed (founded with the IDTicket function), have another price (less or over them) and are the contrary Cmd (for example, OP_BUY then OP_SELL).

If the second order are founded, the code must close the first order founded AND the second.

The big both if's are there to searches the second order with an price over the first OrderSelected order, or less of it. When it found the second order, close the first and the second. If no, then not. That is the reason that this if's that you sayd that would'nt work, should be work.

But that are not the problem here. The problem here, is that the first OrderSelect fails and fails and fails. Nothing to do... The Trading pool contains plenty of orders, pendings and executed... but the first OrderSelect no reacts with nothing. That is the reason for Error 4105, and if the OrderClose function were the guilty, the error code were another.

Thanks!!

.

 
eliusm:

But that are not the problem here. The problem here, is that the first OrderSelect fails and fails and fails. Nothing to do... The Trading pool contains plenty of orders, pendings and executed... but the first OrderSelect no reacts with nothing. That is the reason for Error 4105, and if the OrderClose function were the guilty, the error code were another.

.

Well I am confused. OrderSelect cannot throw an error 4105 or indeed any error that GetLastError can retrieve.

https://docs.mql4.com/runtime/errors

However,

for(int i=0;i<=OrdersTotal();i++)

is incorrect since when OrdersTotal is 0 you still allow stuff to happen. Remove the "equal" from the "less than or equal to"

 
dabbler:

Well I am confused. OrderSelect cannot throw an error 4105 or indeed any error that GetLastError can retrieve.

https://docs.mql4.com/runtime/errors

However,

is incorrect since when OrdersTotal is 0 you still allow stuff to happen. Remove the "equal" from the "less than or equal to"

OrderSelect() doesnt but if you try to get any of the order informations such as OrderOpenTime() when an order was not selected it will throw out ERR_NO_ORDER_SELECTED (4105)

I believe you have identified the problem though, the last order selected in the first OrderSelect() loop does not exist.

So attempting to get ...

OrderTicket();
OrderLots();
OrderOpenPrice();
OrderType();

... on an order that does not exist is probably causing the error 4105



 

Aha, that is all right.

But the problem persists on the OrderSelect function, because it aren't selecting any order. Obviously all further functions returns the 4105 error.

Any other idea?

Regards!

 
eliusm:

Aha, that is all right.

But the problem persists on the OrderSelect function, because it aren't selecting any order. Obviously all further functions returns the 4105 error.

Any other idea?

Regards!


did you change the loop like dabbler said you should ?

for(int i = 0; i <= OrdersTotal(); i++) // that is wrong

remember the first order in the pool is index zero so the last order index is not equal to the total orders. If total is 10, first order is index zero, last order is index 9

Reason: