OrderSelect refuse to select any order. Why? - page 2

 

Oah, that is diabolic!! I have do the changes that you have recomended, and it works, exceptly for 1 detail:

Why do the program fall into BOTH if's? I mean, the if(Cmd==OP_BUY) --- AND --- if(Cmd==OP_SELL) ???

The error 4105 dissappears, and now I have cach why finally!!

The unike problem is that the code falls into 2 if's that ask any different...

Thank you all very much, that loop problem was the problem. Any idea why that concern to the if's?

 

Did you read this thread: Loops and Closing or Deleting Orders ?

Especially the bit where it says . . . "The key difference is that the loop decrements from ( TotalNumberOfOrders - 1 ) to 0"

 

Yes, I read that thread, and do what it says.

Same case. The last version of the code is here:

void CloseMagic() {
   int Cmd,Ticket1,Lots,OpenPrice,ClosePrice,Cmd1;
   RefreshRates();
   for(int i=OrdersTotal()-1;i>=0;i--) {
      if(OrderSelect(i,SELECT_BY_POS)) {
         Ticket1=OrderTicket();
         Lots=OrderLots();
         OpenPrice=OrderOpenPrice();
         ClosePrice=OrderClosePrice();
         Cmd=OrderType();
      
         if(Cmd==OP_BUY) {
            IDPrice=OpenPrice+Paso-Spread*Point; IDCmd=OP_SELL; IDCmd2=6; Razon="Buscar grupo desde BUY."; TicketID=IDTicket();
            if(TicketID!=0) {
               if(OrderSelect(TicketID,SELECT_BY_TICKET)) {
                  OrderClose(OrderTicket(),Lots,OrderClosePrice(),3,Blue);
                  OrderClose(Ticket1,Lots,ClosePrice,3,Blue);
                  i=0;
               }
            }
         }
         if(Cmd==OP_SELL) {
            IDPrice=OpenPrice-Paso+Spread*Point; IDCmd=OP_BUY; IDCmd2=6; Razon="Buscar grupo desde SELL."; TicketID=IDTicket();
            if(TicketID!=0) {
               if(OrderSelect(TicketID,SELECT_BY_TICKET)) {
                  OrderClose(OrderTicket(),Lots,OrderClosePrice(),3,Blue);
                  OrderClose(Ticket1,Lots,ClosePrice,3,Blue);
                  i=0;
               }
            }
         }
      }
   }
   Error=GetLastError();
   if(Error!=0) Print("Error de CloseMagic: ",Error);
}
 
eliusm:

Oah, that is diabolic!! I have do the changes that you have recomended, and it works, exceptly for 1 detail:

Why do the program fall into BOTH if's? I mean, the if(Cmd==OP_BUY) --- AND --- if(Cmd==OP_SELL) ???

The error 4105 dissappears, and now I have cach why finally!!

The unike problem is that the code falls into 2 if's that ask any different...

Thank you all very much, that loop problem was the problem. Any idea why that concern to the if's?

My apology I misunderstood you first Q.

The best way is to print the result of orderselect, that way you may find the answer to your question quickly.

:D

 

Ok, no problem Onewithzachy.

Now, it is my turn to solve this problem. Do not spend more time here until I resolve them.

When I get it, I will publicate the function that works. Meanwhile I will fill the function with Prints to check the status of the operation step by step.

Thank you all!!

 
eliusm:

Now, it is my turn to solve this problem.

Well said!

But one last tip to save you some money.

https://docs.mql4.com/trading/ordercloseby

You are apparently closing a BUY and a SELL at the same time. By using an OrderCloseBy instead of just closing both orders you save the spread on the common part of the position. So if you close a 0.1 lot against a 0.15 lot you save the spread on 0.1 lots. It is a more complicated way of doing it but it actually produces an additional profit just by a bit of coding!

 
eliusm:

Yes, I read that thread, and do what it says.

Same case. The last version of the code is here:

Perhaps you should explain what the purpose of this function is . . . I see some strange things in your code that don't make sense to me. For example:

         if(Cmd==OP_BUY) {
            IDPrice=OpenPrice+Paso-Spread*Point; IDCmd=OP_SELL; IDCmd2=6; Razon="Buscar grupo desde BUY."; TicketID=IDTicket();  // what does this function do ?
            if(TicketID!=0) {
               if(OrderSelect(TicketID,SELECT_BY_TICKET)) {                       //  why a new OrderSelect ?
                  OrderClose(OrderTicket(),Lots,OrderClosePrice(),3,Blue);
                  OrderClose(Ticket1,Lots,ClosePrice,3,Blue);                   //  why the 2nd OrderClose ?
                  i=0;                                                        //  now you have just set the loop to it's last value,  why ? did you mean to use  break instead
 

Dabbler, very interesting function. I have only 1 question about it:

If I have big orders (a lot of lots and a small FreeMargin), and have one Sell and one Buy and apply the OrderCloseBy function. Closes it the entire Sell and the entire Buy order? Or step by step, lot by lot to care the FreeMargin?

RaptorUK:

To your first question: my IDTicket function only returns a Ticket of an order that have been founded in the tradingpool that coincides with the given parameters, for example the IDPrice parameter, but the last OrderSelect what have do the IDTicket function is for the last order in the pool, and not necessary the order what I are searching for. If the order with the given parameters are founded, I must select it to work with it.

Second question: because I need to close 2 orders: the first selected with the first OrderSelect, and the seccond founded by the IDTicket function.

For the third question: because of the thread what have you posted to me. I understand that if I have closed any order, I need to search again in the complete tradingpool to not jump over any order.

Thank you very much!

 
eliusm:

Dabbler, very interesting function. I have only 1 question about it:

If I have big orders (a lot of lots and a small FreeMargin), and have one Sell and one Buy and apply the OrderCloseBy function. Closes it the entire Sell and the entire Buy order? Or step by step, lot by lot to care the FreeMargin?

RaptorUK:

1. To your first question: my IDTicket function only returns a Ticket of an order that have been founded in the tradingpool that coincides with the given parameters, for example the IDPrice parameter, but the last OrderSelect what have do the IDTicket function is for the last order in the pool, and not necessary the order what I are searching for. If the order with the given parameters are founded, I must select it to work with it.

2. For the third question: because of the thread what have you posted to me. I understand that if I have closed any order, I need to search again in the complete tradingpool to not jump over any order.

Thank you very much!

1. if IDTicket doesn't find the order what is returned ? and then you go on to perform an OrderSelect based on this return value ?

2. no, if you are closing or deleting orders you MUST count down in the loop . . and you are now doing that.

 
eliusm:

Dabbler, very interesting function. I have only 1 question about it:

If I have big orders (a lot of lots and a small FreeMargin), and have one Sell and one Buy and apply the OrderCloseBy function. Closes it the entire Sell and the entire Buy order? Or step by step, lot by lot to care the FreeMargin?

Well the FreeMargin has nothing to do with it. If you have a BUY and a SELL then which ever one is bigger gets left over at the end of the OrderCloseBy call.

So

0.10 LONG

0.15 SHORT

you will end up with 0.05 SHORT, although it is not clear to me what happens if that is smaller than MODE_MINLOT. Probably the OrderCloseBy will fail since it would create an unacceptable position. I tried closing the big one against the small one, and the other way around, and it seemed to make no difference (from memory). Since it "creates money" (or at least wastes less on the close) it is well worth experimenting with this function if you do have opposite orders in your system.

Reason: