simple question (I think)

 
Can anyone tell me how to program my EA to select the last trade that closed and determine if it closed by hitting its Stop Loss or closed by hitting its Take Profit (that is he only way trades close with my EA). I know the OrderSelect function is useful for this, but I don't know exactly how to program it for selecting the last closed trade and determining if it closed by Stop Loss or Take Profit.

Thanks,

Mike
 
bool IsLastTrade(string closedBy)
{
     int totalOrders = HistoryTotal();
     if(totalOrders>=1)
     {
          if(OrderSelect(totalOrders-1,SELECT_BY_POS,MODE_HISTORY))
          {
               string orderComment = OrderComment()                              
               return (StringFind(orderComment,closedBy, 0)!=-1);
          }
     }     
     return(0);
}

Hi, mike. Try it. PS.: In "closedBy" parameter you put "[tp]" to know if last trade closed by Take Profit or put "[sl]" to know if closed by Stop Loss.

Bye,

Alessandro Paulino
 
Or you can try it

int LastTrade()
{
/*
     Returns:
 
     0 - Take Profit
     1 - Stop Loss
     2 - Other
*/
     int totalOrders = HistoryTotal();
     if(totalOrders>=1)
     {
          if(OrderSelect(totalOrders-1,SELECT_BY_POS,MODE_HISTORY))
          {
               if(OrderClosePrice()==OrderTakeProfit())
               {
                    return (0)
               }
                if(OrderClosePrice()==OrderStopLoss())
               {
                    return (1)
               }              
          }
     }     
     return(2);
}



Bye,

Alessandro Paulino
 

closing price is not nessarily equal to take profit or asoploss.

If you tracking one order just save it and check it such as below:

int ticket;

int start()
{
...

ticket=OrderSend(...);

...

OrderSelect(ticket,SELECT_BY_TICKET);
if(OrderClosePrice()!=0)
{
if(OrderProfit()>0 //take profit
else // stop loss
}

}

 
Thanks so much
Mike
 
ale_paulino:
Hi, mike. Try it. PS.: In "closedBy" parameter you put "[tp]" to know if last trade closed by Take Profit or put "[sl]" to know if closed by Stop Loss.

Bye,

Alessandro Paulino
I found that for backtesting with tester this works but with my real account the comment is "[completed]". Do you get the same results?
 
Luke:
I found that for backtesting with tester this works but with my real account the comment is "[completed]". Do you get the same results?
The comment is actually "[complete]". I think think only applies to trades which hit the stop loss but I'm not sure. Why the inconsistency though?