Download MetaTrader 5

Take profit event handling function?

To add comments, please log in or register
Sign up for MQL5.community to post your Forum topic
RichPiano
135
RichPiano 2016.11.25 08:45 

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 :)

Alain Verleyen
Moderator
30754
Alain Verleyen 2016.11.25 09:20  
No there is not. You need to code it by checking the orders/deals history.
Osama Shaban
1448
Osama Shaban 2016.11.25 09:28  

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.

RichPiano
135
RichPiano 2016.11.25 10:13  

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()?

honest_knave
Moderator
2241
honest_knave 2016.11.25 10:29  
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.

agu2a
68
agu2a 2016.11.26 04:18  
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

honest_knave
Moderator
2241
honest_knave 2016.11.26 09:50  
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 

Alain Verleyen
Moderator
30754
Alain Verleyen 2016.11.26 16:57  
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
To add comments, please log in or register