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

 
crossy:

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

Y.

You don't need the Function at all . . . . . . get rid of it.

 

Let's do it again part 2 :(

 ...
   for(int cnt = OrdersTotal()-1; cnt >= 0 ; cnt--) 
   {
      CodeY = false;
      if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) )
      {
         // Do the selection and OrderClose() INSIDE HERE ..., please
         
         
         /*
         CodeY    = true;
         OrderS   = OrderSymbol();
         OrderT   = OrderTicket();
         OrderM   = OrderMagicNumber();
         OrderTyp = OrderType();
         OrderL   = OrderLots();
         */
      }   
      //if ( CodeY && OrderS == Pair1 && OrderTyp == OP_BUY && OrderM == MAG &&.... ) // this selection goes up there
    
         //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); // close it up there, and you have to rewrite the parameters
   }
   */
 

Do this . . . ( repeated from here: https://www.mql5.com/en/forum/139543 )

if ( CodeY && OrderS == Pair1 && OrderTyp == OP_BUY && OrderM == MAG &&.... )
   {
   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 . . 
   } 

. . . and you don't need the Function at all.

 
onewithzachy:

Let's do it again part 2 :(


I want your exuss, please, since I have an holiday, So I will be back on sunday nigth.

I will do what you suggested, and I hope to succeed.

Thank you both.

Y.

 
crossy:


I want your exuss, please, since I have an holiday, So I will be back on sunday nigth.


OK, enjoy your Holiday :-)
 
crossy:


I want your exuss, please, since I have an holiday, So I will be back on sunday nigth.

I will do what you suggested, and I hope to succeed.

Thank you both.

Y.

Have a good time :D

Me have F1 Monaco to watch this Sunday :D

 
onewithzachy:

Have a good time :D

Me have F1 Monaco to watch this Sunday :D

And tomorrow, FP3 and Qualifying ;-)
 
crossy:


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.

I find this much simpler, and it works for me:

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

void CheckForClose()
{
int i, res, order_id;

//-------+
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
order_id= OrderTicket();


//---trailing and close
if (OrderType() == OP_BUY)
{
if (Is_Close_Buy==true) //-- the signal to close
{
RefreshRates();// Refresh rates
res= 0;
res= OrderClose(order_id,Lots,Bid,3,Gold);
while(IsTradeAllowed() == false) Sleep(100); //---wait a little to get response from server
if (res>0){
PlaySound("cash_register.wav");} //--happy sound, making money hopefully !!!!
if (res<0)
{
PlaySound("creak.wav");
Print("OrderSend failed with error #",GetLastError());
return(0);
}
return;
}
}//end close Buy Order check
//---
if (OrderType() == OP_SELL)
{
if (Is_Close_Sell==true) //--the signal to close
{
RefreshRates();// Refresh rates
res= 0;
res= OrderClose(order_id,Lots,Ask,3,Gold);
while(IsTradeAllowed() == false) Sleep(100); //---wait a little to get response from server
if (res>0){
PlaySound("cash_register.wav");}
if(res<0)
{
PlaySound("creak.wav");
Print("OrderSend failed with error #",GetLastError());
return(0);
}
return;
}
}//end close Sell Order check
}//--------+
return;
}

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

 
RaptorUK:
And tomorrow, FP3 and Qualifying ;-)

@ RaptorUK, I'm considering wearing crash helmet so at least I can think well ahead than I'm currently now. Isn't that, those drivers do, think well ahead ? :D


@ skaboy, I'm asking politely here, if you don't mind, would you please use SRC button to post your code. We've been asking that too many times in this thread alone.


@ crossy, It's almost a week since your first post. Surely I understand that you may not have a clear head now dealing with 4000 lines, right now I myself have mine here too. If you still don't understand it, - ehm - you can ask us to write that Close code for you. However you still have to write the code even just a little - and show us. :D, RaptorUK's code just above there is more direct than mine, think about it and then you can move on with your EA.

With the way you write code here - too many unnecessary repeated code - you should have a little worry with your 4000 lines of code that it may slow down your EA and makes your EA miss a tick. It's okay to miss a couple point, however what happen if you miss like 10 pips away ? and probably your 4000 lines may still can be reduced to just 400 lines. Who knows ?, your EA, become more lighter and faster. :D

 
skaboy:

I find this much simpler, and it works for me:


It doesn't work if you have multiple orders to be closed . . . read this and learn: Loops and Closing or Deleting Orders
Reason: