Help: Why The EA does not close a trade - page 4

 

You gonna have problem that no one replies you if you're not using SRC button to post your code :D

 
crossy:

Hello freinds,


OrderT is the order Ticket number,




My PROBLEM is: The EA sometimes closes the trade very well, but sometimes the EA closes the tarde BUT it is continuing to look for the ticket to close it, although it was closed already.

(I get the error: "Unknown ticket number.." .

I think taht I have a logic mistake.

Thank you for you kind help.

Y

It's very, very simple . . . .

Just answer this simple question and you will have your answer . . .

You pass the TICKET number to the Function so the ticket can be closed . . . . why do you then loop through ALL the other orders ? you already KNOW the Ticket number you need to close . . . why the loop ?

 
...
   for(int cnt = OrdersTotal()-1; cnt >= 0 ; cnt--) 
   {
      CodeY = false;
      if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) )
      {
         CodeY    = true;
         OrderS   = OrderSymbol();
         OrderT   = OrderTicket();
         OrderM   = OrderMagicNumber();
         OrderTyp = OrderType();
         OrderL   = OrderLots();
      }   
      if ( CodeY && OrderS == Pair1 && OrderTyp == OP_BUY && OrderM == MAG &&.... )
    
         CLOSE_Single_P(Pair1,"LONG",OrderT,OrderL);

//-----------------------------------------------+
void CLOSE_Single_P(string SYMB, string TREN, int TICKET, double LOTT)
{
   double PRC;
   bool Check = false;

   for ( int cntt=0; cntt<20000; cntt++ )
   {
      if ( Check )
      {
         Check_Ini();
         return;
      }
      for ( int vvv = OrdersTotal()-1; vvv >= 0; vvv-- )
      {  
         if ( OrderSelect(vvv, SELECT_BY_POS, MODE_TRADES) )
         {   
            Check = false;
            if ( TREN == "LONG" )
              PRC = MarketInfo(SYMB,MODE_BID);
            else
            if ( TREN == "SHORT" )
               PRC = MarketInfo(SYMB,MODE_ASK);
         
            Check = OrderClose(TICKET,LOTT,PRC,Slippage,Yellow);
            if ( Check )
               break;
         }      
      } 
   }
   return;
}
//-----------------------------------------------+

I hope that it is more clear, now.

Thanks

 
RaptorUK:

It's very, very simple . . . .

Just answer this simple question and you will have your answer . . .

You pass the TICKET number to the Function so the ticket can be closed . . . . why do you then loop through ALL the other orders ? you already KNOW the Ticket number you need to close . . . why the loop ?


Thanks Raptor, I did it once without the ORDERSELECT, but it had the same mistake.

Y.

 
I already gave a clue on very first page. https://www.mql5.com/en/forum/139543
 

Thanks onewithzachy, and I did as you suggested and I got the error

massage that it did not found the TICKET..

 
crossy:

Hello freinds,


OrderT is the order Ticket number,




My PROBLEM is: The EA sometimes closes the trade very well, but sometimes the EA closes the tarde BUT it is continuing to look for the ticket to close it, although it was closed already.

(I get the error: "Unknown ticket number.." .

I think taht I have a logic mistake.

Thank you for you kind help.

Y

It's very, very simple . . . .

Just answer this simple question and you will have your answer . . .

You pass the TICKET number to the Function so the ticket can be closed . . . . why do you then loop through ALL the other orders ? you already KNOW the Ticket number you need to close . . . why the loop ?

 
onewithzachy:
I already gave a clue on very first page. https://www.mql5.com/en/forum/139543
And I gave the answer too . . . https://www.mql5.com/en/forum/139543
 
crossy:


Thanks Raptor, I did it once without the ORDERSELECT, but it had the same mistake.

You aren't reading what I have written and asked . . . . why the loop ? what is the function of the loop in this situation ?
 
...
   for(int cnt = OrdersTotal()-1; cnt >= 0 ; cnt--) 
   {
      CodeY = false;
      if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) )
      {
         CodeY    = true;
         OrderS   = OrderSymbol();
         OrderT   = OrderTicket();
         OrderM   = OrderMagicNumber();
         OrderTyp = OrderType();
         OrderL   = OrderLots();
      }   
      if ( CodeY && OrderS == Pair1 && OrderTyp == OP_BUY && OrderM == MAG &&.... )
    
         CLOSE_Single_P(Pair1,"LONG",OrderT,OrderL);

//-----------------------------------------------+
void CLOSE_Single_P(string SYMB, string TREN, int TICKET, double LOTT)
{
   double PRC;
   bool Check = false;

   for ( int cntt=0; cntt<20000; cntt++ )
   {
      if ( Check )
      {
         Check_Ini();
         return;
      }
      Check = false;
      if ( TREN == "LONG" )
         PRC = MarketInfo(SYMB,MODE_BID);
      else
      if ( TREN == "SHORT" )
         PRC = MarketInfo(SYMB,MODE_ASK);
         
      Check = OrderClose(TICKET,LOTT,PRC,Slippage,Yellow);
   }
   return;
}
//-----------------------------------------------+

O.K. Here is the modified version, which I got the same mistake.

Y.