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

 
crossy:


Here if Orderselect is FALSE how the EA can go back to the code line: for(int cnt=OrdersTotal(); cnt > 0; cnt-- )

???

You can go to the next value in the loop using continue . . . or escape from the loop with break . . . click the links for documentation on each.
 
crossy:

Here if Orderselect is FALSE how the EA can go back to the code line: for(int cnt=OrdersTotal(); cnt > 0; cnt-- )


???

That code is wrong . . . .

cnt needs to start at OrdersTotal() - 1 and decrement down to cnt >= 0 . . . . the first entry is 0 not 1

This is why I asked to see the code . . . and you have shown enough to identify the problem . . . probably. ;-)

 
RaptorUK:

That code is wrong . . . .

cnt needs to start at OrdersTotal() - 1 and decrement down to cnt >= 0 . . . . the first entry is 0 not 1

This is why I asked to see the code . . . and you have shown enough to identify the problem . . . probably. ;-)


Very Correct. Thank you, Wizeman!
 
crossy:

Very Correct. Thank you, Wizeman!


And another idea:

If I have many conditions, you may write:

If( X>b && B>S && NB<F......)

{

BBB= 5;

}f.e.

Here, the EA has to compute every condition within the bracket.

But, if you are looking fo efficiency (4,000 code lines...) I prefer to have:

If ( X>b )

{

while (B>S)

{

While (NB<F..)

{

BBB=5;

}

break;

}

break;

}

Are the 2 variations are the same?

Y.

 

crossy, when you write the code, please use SRC button, it will be much more readable

So it will be shown like this

   //--- first code
   If(X>b && B>S && NB<F......)
     {
      BBB=5;
     }
   
   // --- second code
   If(X>b)
     {
      while(B>S)
        {
         while(NB<F..)
           {
            BBB=5;
           }
         break;
         }
      break;
     }

There explanation about "if, while and for" loop in mql4 https://book.mql4.com/operators/assign

On second code, it's OK since you using break, however, that's a long way just to get BBB = 5, I prefer first code, it use less resources

 
crossy:


And another idea:

If I have many conditions, you may write:

If( X>b && B>S && NB<F......)

{

BBB= 5;

}f.e.

Here, the EA has to compute every condition within the bracket.

But, if you are looking fo efficiency (4,000 code lines...) I prefer to have:

Using while like that you may find yourself in an infinite loop . . .
 
Thank you both!.
 
onewithzachy:

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 :)


Special Thanks to onewithzachy,

I did not know that the ORDERSELECT migth not catch, so you may have unknown errors.

My code that was 4016 code lines became almost 4500 lines - but without errors.

Thank you all, freinds.

Y.

 

Hello freinds,

After a long journy here, I had a good advamtage with my EA, but I have a one more problem, as follow:

I have the following code line:

CLOSE_Single_P(Pair1,"LONG",OrderT,OrderL);

as:

Pair1 is the Symbol,

"LONG" is a sign of a long trade,

OrderT is the order Ticket number,

OrderL is the order lot size.

The function is as follow:

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;
}

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

 

Please . . . . . .


Reason: