Help: Why The EA does not close a trade

 

Hello freinds,

I need your help.

I built an EA that part of its process has to close a trade using a fuction. Here are the relevant kod's rows.

Can some help me why the EA doesn't close the trade? Thank You.

(Maybe, since while I use ORDERSELECT at the main part, It can't use ORDERSELECT at the function, too????)

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if ( OrderSymbol() == Pair && OrderType() == OP_BUY )

{

if ( MarketInfo(Pair,MODE_BID) >= Open_P_BUY + MarketInfo(Pair,MODE_POINT)*factor*TP )

{

CLOSE_Single_P(Pair,"LONG", OrderTicket(), OrderLots());

}

}

}

//-----------------------------------------------+

void CLOSE_Single_P(string SYMB, string TREN, int TICKET, double LOTT)

{

int Code, sig;

double PRC;

if ( TREN == "LONG" )

Code = 0;//OP_BUY

else

if ( TREN == "SHORT" )

Code = 1;//OP_SELL

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

{

OrderSelect(k, SELECT_BY_POS, MODE_TRADES);

if( OrderSymbol() == SYMB && OrderType() == Code && OrderLots() == LOTT && OrderTicket() == TICKET )

{

if ( OrderType() == OP_BUY )

PRC = MarketInfo(OrderSymbol(),MODE_BID);

else

if ( OrderType() == OP_SELL )

PRC = MarketInfo(OrderSymbol(),MODE_ASK);

OrderClose(TICKET,LOTT,PRC,Slippage,Yellow);

break;

}

}

return;

}

 

Use SRC button to display your code for better reading, please :)

You OrderSelect() is coded in a wrong way. Compare your code with this. Then write back if you still have the problem or not :)

   for(int cnt=0; cnt<OrdersTotal(); cnt++)
     {
      if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) == true &&
          OrderSymbol()==Pair && 
          OrderType()==OP_BUY)
          {
          // code to close
          }
      }
 
crossy:

Hello freinds,

I need your help.

I built an EA that part of its process has to close a trade using a fuction. Here are the relevant kod's rows.

Can some help me why the EA doesn't close the trade? Thank You.

When you have a loop and you are closing or deleting (Pending) orders you MUST count down and NOT up . . .

Why do you have a loop in the Close_Single_P Function ? isn't it designed to close a single order ? when you call the Function you pass the symbol, ticket number, order type and position size . . . that is all you need to close the order . . . well almost, you need the price to close at and for that you can use OrderClosePrice() and then you need not worry if it is a Buy or Sell . . . in fact, just get rid of the Function and use this line in place of the function call . . .

OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Yellow);

. . . by the way, where are you setting Slippage ? and are you compensating for 4/5 Digit Brokers ?

When you use Order functions they return a value . . . this return value tells you if the function worked or not . . . learn to use this return value and if it tells you there was a problem use a Print statement to report the releant information to the logs . . . . then you will know what went wrong instead of guessing . . . .

Should your Order be closed ? have you verified that this . . .

if ( MarketInfo(Pair,MODE_BID) >= Open_P_BUY + MarketInfo(Pair,MODE_POINT)*factor*TP )

. . . is true ?

Why not add a print statement which is executed if that line is true . . . then you will know if it is or isn't . . .

if ( MarketInfo(Pair,MODE_BID) >= Open_P_BUY + MarketInfo(Pair,MODE_POINT)*factor*TP )
   {
   Print("Trying to close Order: ", OrderTicket());                             // <---- tells you that the   if  returned true
   if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Yellow)
      Print("OrderClose failed, error # ", GetLastError());                          // <------- reports the error if the OrderClose failed . . 
   }
 
onewithzachy:

Use SRC button to display your code for better reading, please :)

What's the return of GetLastError() then ?


The code has about 4,000 lines...

Anyway, I do not get any error massage.

 
crossy:


The code has about 4,000 lines...

Anyway, I do not get any error massage.

You don't have to post ALL your code . . . but when you post ANY code please use the SRC button . . .

You don't get any error messages because you don't report them to the log . . . you must check return values and report errors.

 
RaptorUK:

When you have a loop and you are closing or deleting (Pending) orders you MUST count down and NOT up . . .

Why do you have a loop in the Close_Single_P Function ? isn't it designed to close a single order ? when you call the Function you pass the symbol, ticket number, order type and position size . . . that is all you need to close the order . . . well almost, you need the price to close at and for that you can use OrderClosePrice() and then you need not worry if it is a Buy or Sell . . . in fact, just get rid of the Function and use this line in place of the function call . . .

. . . by the way, where are you setting Slippage ? and are you compensating for 4/5 Digit Brokers ?

When you use Order functions they return a value . . . this return value tells you if the function worked or not . . . learn to use this return value and if it tells you there was a problem use a Print statement to report the releant information to the logs . . . . then you will know what went wrong instead of guessing . . . .

Should your Order be closed ? have you verified that this . . .

. . . is true ?

Why not add a print statement which is executed if that line is true . . . then you will know if it is or isn't . . .


Thank you Raptor for your wize offers. ALWAYS, you help me wizely and clearly.

I will try what you just suggested.

Best wishes.

 

You already have the ticket selected, why go through a second orderSelect loop to find the same ticket?

Why are you not checking the OrderClose return value and printing out the error to find out WHY?

 
WHRoeder:

You already have the ticket selected, why go through a second orderSelect loop to find the same ticket?

Why are you not checking the OrderClose return value and printing out the error to find out WHY?


Well freinds, Thank you for your efforts.

I did some changes and nothing was helpful, till I saw WHRoeder comment.

You are geniose, I think you are rigth.

I will try the modified EA on Monday.

Have a nice weekend.

 
crossy:


Well freinds, Thank you for your efforts.

I did some changes and nothing was helpful, till I saw WHRoeder comment.

You are geniose, I think you are rigth.

I will try the modified EA on Monday.

Have a nice weekend.


Well, My freinds,

I did all your suggestions - and the EA failed.

Any more ideas?

 

Let's do it again

  #include "..\libraries\stdlib.mq4" // drag and drop from libraries to very top of your code
  
  for(int cnt = OrdersTotal(); cnt >= 0; cnt--)  // from RaptorUK, count it down to zero 
     {
     if (OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) == true && OrderSymbol() == Pair) // this is how to write OrderSelect()
        {
        if(OrderType()== OP_BUY) // the code below is to close buy position only
          {
          
          //---
           if(MarketInfo(Pair,MODE_BID) >= Open_P_BUY + MarketInfo(Pair,MODE_POINT) * factor * TP) // from RaptorUK, is this statemement true ?
             {
              //CLOSE_Single_P(Pair,"LONG",OrderTicket(),OrderLots()) // From RaptorUK & WHRoeder, you don't have to do OrderSelect() twice
             Print("Trying to close buy position : ", OrderTicket());                            
             if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Yellow)) // From Raptor UK, are you doing the slippage correctly ?
                Print("failed to close buy position ", ErrorDescription(GetLastError())  ); 
             }
             else
             {
             Print ("Not qualify to close buy position ", OrderTicket()); // from RaptorUK, is this statemement true ?, if not this print will tell you
             }
          //---
             
           }
           /*
           else
           {
           if (OrderType() == OP_SELL)
              {
              
              }
           }
           */
        }
     }
      

While I'm still reading Valentino Rossi's MotoGP Le Mans second podium, I hope I write this correctly :)

 
onewithzachy:

Let's do it again

While I'm still reading Valentino Rossi's MotoGP Le Mans second podium, I hope I write this correctly :)


Thanks onewithzachy,

First I think that you have a small mistake with:

for(int cnt = OrdersTotal() -1 ; cnt >= 0; cnt--)

You have to add the green and bold correction.

But, I do not understand how the following line can help us?

#include "..\libraries\stdlib.mq4"

Thanks, Y.

Reason: