Take profit event handling function?

 

Hey Guys

 

Is there a function for an EA that gets triggered when a profit target is hit for a certain position? I can't find one :(

 

Thanks for  helping me out :)

 
No there is not. You need to code it by checking the orders/deals history.
 

This is one of my functions I use ...

void LastClosedTrade(){
   int cnt, total;
   total = OrdersHistoryTotal();
   for(cnt=0;cnt<total; cnt++){
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)==true)
  
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNo){
         LastOrderType = OrderType();
         LastOrderLots = OrderLots();
         LastOrderProfit = OrderProfit();
      }
   }
}

 If you tweak the last line of code slightly, you could easily get what you want.

 

Thanks a lot!

 

One question remains though: When I select an order via OrderSelect, can i be sure that the position with the highest index is always the last closed order (in case of MODE_HISTORY)? Or do i have to dig through all indices and sort out the latest with OrderCloseTime()?

 
RichPiano:

Thanks a lot!

 

One question remains though: When I select an order via OrderSelect, can i be sure that the position with the highest index is always the last closed order (in case of MODE_HISTORY)? Or do i have to dig through all indices and sort out the latest with OrderCloseTime()?

It is probably best to sort them yourself. There is some anecdotal evidence suggesting the orders are not necessarily always sorted chronologically.

 
honest_knave:

It is probably best to sort them yourself. There is some anecdotal evidence suggesting the orders are not necessarily always sorted chronologically.

I think it depends on how your broker sorts orders once they are closed, I believe it is chronological

 
agu2a:

I think it depends on how your broker sorts orders once they are closed, I believe it is chronological

Not always, it would seem: 

https://www.mql5.com/en/forum/139121

https://www.mql5.com/en/forum/138127

https://www.mql5.com/en/forum/137936 

 
honest_knave:

It is probably best to sort them yourself. There is some anecdotal evidence suggesting the orders are not necessarily always sorted chronologically.

Anecdotal evidence ? It's documented.

Consecutive selection of orders using the SELECT_BY_POS parameter returns information in the sequence in which it was received from the trading server. Sorting of the resulting list of orders cannot be guaranteed.

OrderSelect - Trade Functions - MQL4 Reference
OrderSelect - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderSelect - Trade Functions - MQL4 Reference
 
Osama Shaban:

This is one of my functions I use ...

void LastClosedTrade(){
   int cnt, total;
   total = OrdersHistoryTotal();
   for(cnt=0;cnt<total; cnt++){
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)==true)
  
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNo){
         LastOrderType = OrderType();
         LastOrderLots = OrderLots();
         LastOrderProfit = OrderProfit();
      }
   }
}

 If you tweak the last line of code slightly, you could easily get what you want.

Thanks! This was very helpful.