Need Help on OrderModify().

 
if(OrdersTotal()>0)
{
handle=FileOpen(FCLOSE,FILE_CSV|FILE_READ,';');
if(handle>0)
{
FileClose(handle);
FileDelete(FCLOSE);
if(OrderSelect(0,SELECT_BY_POS)==true)
ticket0=OrderTicket();
OrderModify(ticket1,0,bidm,TP1,0,Green);
RefreshRates();
if(OrderSelect(1,SELECT_BY_POS)==true)
ticket1=OrderTicket();
OrderModify(ticket1,0,bidm,TP1,0,Green);
RefreshRates();
}
Hi all. MY EA is triggered to modify the stop loss of the 2 open orders after it receives and reads a File name called FCLOSE. The program works fine without any problem so far, however, to avoid any possible problem or error I need a set of commands to make sure if the OrderModify does not work it goes back again and does not stop until the modification happens. I have included part of my program which shows the OrderModify Part. I know I could use GetLastError(); function to detect any possible error, but, just do not know how I could tell the EA to do the OrderModify for the same ticket# again if an error occurs. This is like you are forcing your EA not to stop until the job is done. Thank you in advance for your help.
 

You need to handle errors in the correct manner, in some circumstances you cannot simply keep trying . . . you should read the Documentation regarding the various errors and take action accordingly.

Trading Errors: https://docs.mql4.com/trading/errors

 

Dear RaptorUK:

Thank you for your prompt respond. It seems that my question is not clear. I need to know a way that if an error occurs, ANY ERROR, the EA tries to modify the open trade. That is if modification is not successful for any reason or error, the EA must go back and do the modification again. Please understand I do not care what type error I get, all I need is to trigger the EA to do the process of modification again.


Thank you in advance.

 
zaffrono:

Dear RaptorUK:

Thank you for your prompt respond. It seems that my question is not clear. I need to know a way that if an error occurs, ANY ERROR, the EA tries to modify the open trade. That is if modification is not successful for any reason or error, the EA must go back and do the modification again. Please understand I do not care what type error I get, all I need is to trigger the EA to do the process of modification again.

I understood your question . . . but the point is that you MUST care what the error is . . . for example, error 132, the market is closed . . . you cannot simply retry the OrderModify and hope it will work, or error 4, server is busy . . . you need to wait several minutes before retrying, on the other hand error 135 price changed, you can do a refresh rates and retry immediately.
 
RaptorUK:
I understood your question . . . but the point is that you MUST care what the error is . . . for example, error 132, the market is closed . . . you cannot simply retry the OrderModify and hope it will work, or error 4, server is busy . . . you need to wait several minutes before retrying, on the other hand error 135 price changed, you can do a refresh rates and retry immediately.

Dear Rapto:

This is exactly my point. As you said, if for example, I get error 135, I must do a refresh and retry again. Here comes my question: How can I ask EA to retry again if, for example, error 135 occurs?

 
zaffrono:

Dear Rapto:

This is exactly my point. As you said, if for example, I get error 135, I must do a refresh and retry again. Here comes my question: How can I ask EA to retry again if, for example, error 135 occurs?

Just repeat the OrderModify.
 
RaptorUK:
Just repeat the OrderModify.

Dear Rapto:


Thank you for your quick respond. Unfortunately, either my question is not clear or you have difficulty understanding me. You just said "Just repeat the OrderModify.", this is fine, but, how can I repeat the OrderModify. What code or conditions should I use to repeat the OrderModify?

 
zaffrono:

Thank you for your quick respond. Unfortunately, either my question is not clear or you have difficulty understanding me. You just said "Just repeat the OrderModify.", this is fine, but, how can I repeat the OrderModify. What code or conditions should I use to repeat the OrderModify?

I understand, but I'm not going to write your code for you . . .

You need to check the return value from OrderModify to see if it has failed, if it has failed you need to read the last error, if the error is such that you can repeat the OrderModify immediately then you can do so, you use the same OrderModify as you did before . . . you are simply repeating the same OrderModify. If you need to wait then you will need to wait . . . then you can repeat the OrderModify.

 
zaffrono:

Dear Rapto:


Thank you for your quick respond. Unfortunately, either my question is not clear or you have difficulty understanding me. You just said "Just repeat the OrderModify.", this is fine, but, how can I repeat the OrderModify. What code or conditions should I use to repeat the OrderModify?

use

while(/*some error is returned*/)
{
  //modify the order
}
 
if(OrdersTotal()>0)
This makes the EA incompatible with every other including itself on other charts and manual trading.
int count = 0;
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == Magic.Number                 // my magic number
    &&  OrderSymbol()       == chart.symbol                 // and my pair.
    ){ count++; }
if (count > 0) ..
 

Dear BHC and WHRoeder:


Thank you very much for your help. Finally, you guys understood what I was asking.