Help Detecting Filled Orders

 

I am writing an EA that uses 4 separate Take Profit levels, so I am using the following code to enter the trades.

sv = InitialStopLoss*Point;

for (x=0;x<4;x++)
{
   tv = Ask+targetValue[x]*Point;                     
   longTicket[x] = OrderSend(Symbol(),OP_BUY,tradeSize[x],Ask,slippage,Ask+sv,tv,"",MagicNumber[x],0,Blue);                           
}

Now I need to detect when the Take Profit orders are filled, so I can update the Stop Loss levels on the trades that have not yet exited. I have figured out how to detect when the trade is closed using the following code, but how can I tell if it was exited due to the Take Profit order?

for (int x=0;x<4;x++)
{
   if (OrderSelect(longTicket[x],SELECT_BY_TICKET,MODE_HISTORY))
   {
      if (OrderCloseTime()>0)
      {
         // Order has been closed. How do I know if it was due to Take Profit?
      }
   }       
}
 
  1. MODE_HISTORY is irrelevant when selecting by ticket - you know the order has closed via OrderCloseTime.
  2. is OrderClosePrice close to OrderTakeProfit, then it was likely closed by TP.
  3. Remember, that if the terminal is restarted (OS crash, power glitch, etc) your longTicket[] will be zeros. How does that affect your code?
 

The placing of your trades aren't checked

if you only did the testing with strategy tester it might look good

if your broker is ECN account broker then it surely fails

if tradecontext to busy you might not place all of your trades with this loop

 
My advice - search by the order's comment. If it contains [tp] then it closed on TP. Search the codebade for EventsExpert.mq4, it will help you.
 
pro_:
My advice - search by the order's comment. If it contains [tp] then it closed on TP. Search the codebade for EventsExpert.mq4, it will help you.
Order comments are not always reliable, they can be changed by the Broker
 
RaptorUK:
Order comments are not always reliable, they can be changed by the Broker


after the trade closed it will contain [sl] or [tp] if it closed that way

so yes you can use it for checking how it closed might be brokers change comments for this reason

placing orders without magicnumber and selecting on comments is bad idea...

don't know if this is with all accounts but have seen it happens on several accounts

 
deVries:

The placing of your trades aren't checked

if you only did the testing with strategy tester it might look good

if your broker is ECN account broker then it surely fails

if tradecontext to busy you might not place all of your trades with this loop


How would you recommend I place the trades? Could you provide a sample?


Thank you for everyone's help!!!

 
           How would you recommend I place the trades? Could you provide a sample?

            int error;
            int newticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage.Pips*pips2points,0,0,CommentEA,MagicNumber,0,Green);
            if(newticket>0)
               {
                if(OrderSelect(newticket,SELECT_BY_TICKET,MODE_TRADES))
                   {
                    TotalOpenEA++; 
                    OrderPrint();
                    double SL = OrderStopLoss();
                    double TP = OrderTakeProfit();
                    ordermodify = false;
                    if(StopLoss>0)SL=OrderOpenPrice()-(StopLoss*pips2dbl);
                    if(TakeProfit>0)TP=OrderOpenPrice()+(TakeProfit*pips2dbl);
                         if(SL != OrderStopLoss() || TP != OrderTakeProfit())
                              {ordermodify = OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Green);}
                         if ( ordermodify > 0 )
                            {
                            OrderPrint();
                            Print( "Modify buy ",OrderTicket(), " done!");
                            return(0);
                            }
                          error = GetLastError();
                          Print("Modification buy failed! GetLastError = ", error, ", ErrorDescription =  \"", ErrorDescription( error ), "\"" );       
                   }   
               }
            if(newticket<0)
                 {
                  error=GetLastError();
                  Print("Error open highlow.buy order : ",ErrorDescription(error));
                  return;
                 }   
       

example... a little piece of code

but there are more ways...

Reason: