Order result help requested

 

I am working on an order result portion of my EA.  The issue I am running into is that this seems to be running nearly every tick when I thought I made it so that the result would only be displayed when one order was closed at a time.

The desired result is to tell me if the order was closed by my parameters or stoploss.

The journal gives me this:

2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:50    2013.03.07 13:47  ADVISOR edit USDJPYpro,M15: This Order closed by parameters.5
2013.03.27 12:36:49    2013.03.04 09:30  ADVISOR edit USDJPYpro,M15: close #1 buy 3.00 USDJPYpro at 93.613 sl: 93.512 at price 93.587

2013.03.27 12:36:49    2013.03.04 09:30  ADVISOR edit USDJPYpro,M15: Time to close order.


Each time I run the tester I will get a random different Ordered ticket, but always a continuous loop such as this. Pointing me in the right direction is greatly appreciated.  Thank You.


   int cnt;
   int cntAgain;
   cnt = OrdersHistoryTotal() - 1;
   if( (OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)) && (cnt != cntAgain) )
   {
    cnt = cntAgain;
    if(StringFind(OrderComment(), "[s/l]", 0) != -1)
    {
     Print("This Order closed by Stoploss: ", OrderTicket() );
    }
    else
    {
     Print("This Order closed by parameters.", OrderTicket() );
    }
 
asham:

I am working on an order result portion of my EA.  The issue I am running into is that this seems to be running nearly every tick when I thought I made it so that the result would only be displayed when one order was closed at a time.

The desired result is to tell me if the order was closed by my parameters or stoploss.

If your cntAgain is not globally declared it will be re-initialised each time you get a new tick,  either make it a static int or declare it globally.

 
if(StringFind(OrderComment(), "[s/l]", 0) != -1)
Not all brokers modify comments with s/l. Some don't modify at all. Some completely replace the comments. https://www.mql5.com/en/forum/134204
 
RaptorUK:

If your cntAgain is not globally declared it will be re-initialised each time you get a new tick,  either make it a static int or declare it globally.

 

Your advice once again saves a troubled programmer.  Thank you for your swift reply RaptorUK.

I had read your reply right after you posted, but took me a little time to make the static int work the way I wanted after I got lost trying to learn global variables.

 
asham:

Your advice once again saves a troubled programmer.  Thank you for your swift reply RaptorUK.

I had read your reply right after you posted, but took me a little time to make the static int work the way I wanted after I got lost trying to learn global variables.

Don't get mixed up between GlobalVariables and globally declared variables.
 
WHRoeder:
Not all brokers modify comments with s/l. Some don't modify at all. Some completely replace the comments. https://www.mql5.com/en/forum/134204

I understand what you mean, based on the other forum explanations. Since the comment search is a bad idea, is there another way that I can determine if the order that just closed was either closed due to my parameters or a stoploss close?  So far I have not seen one, but am still looking.


Thank you for that insight WHRoeder, I would have been mad if this had worked for a while then one day gave me error after error when the broker decided to change their comments.

 
asham:

I understand what you mean, based on the other forum explanations. Since the comment search is a bad idea, is there another way that I can determine if the order that just closed was either closed due to my parameters or a stoploss close?  So far I have not seen one, but am still looking.

You can check the price that the trade closed at and see how close it is to the Stop Loss you had set and make an assumption based on that.  You also can get your EA to remember which trades it closed . . . and which it didn't,  the ones it didn't close must have been closed for SL, TP or margin call.
 
RaptorUK:
You can check the price that the trade closed at and see how close it is to the Stop Loss you had set and make an assumption based on that.  You also can get your EA to remember which trades it closed . . . and which it didn't,  the ones it didn't close must have been closed for SL, TP or margin call.


Once again I give you my thanks RaptorUK.  It is working the exact way I intended now.  You have sped up my learning and programming by hours (if not days or weeks).  It must be rough dealing with newbie questions again and again. Thank you for all your assistance.

I may not be the best yet, but I will eventually get it right after I exhaust all other options... unless I have this forum to help me. :)

 
asham:

Once again I give you my thanks RaptorUK.  It is working the exact way I intended now.  You have sped up my learning and programming by hours (if not days or weeks).  It must be rough dealing with newbie questions again and again. Thank you for all your assistance.

I'm happy to try and help anyone that is willing to try and help themselves first.  :-)
 
asham: is there another way that I can determine if the order that just closed was either closed due to my parameters or a stoploss close? 
bool isClosedBySL = MathAbs( OrderClosePrice() - OrderStopLoss() ) < 2 * pips2dbl;
Reason: