What is the most stable way to code a function to retrieve the last trades closed @ tp ONE BY ONE from history pool?

 

Hello dear coders,


This way can work only if there only one trade at a time and the tp can be hit only by one trade at a time:

if(OrdersHistoryTotal() > prevhistorytotal)
   {
      for(int i=OrdersHistoryTotal()-1; i>=0; i--)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if(PipsProfit(OrderTicket()) >= TPLevel_pips)
         ...

But if the EA closes more trades @ the same tp levels and I DON'T WANT TO USE comment "[tp]" strings lookup or other ways broker-sensitive, 
How can i code this function so that my EA will screen all the history pool only on the event of NEW TRADE CLOSED BY TP and check the characteristics of every single trades just closed
in order to take decisions on the open position?

Thanks
 
 
ordo: This way can work only if there only one trade at a time and the tp can be hit only by one trade at a time:
if(OrdersHistoryTotal() > prevhistorytotal)
   {
      for(int i=OrdersHistoryTotal()-1; i>=0; i--)
      {
         OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
         if(PipsProfit(OrderTicket()) >= TPLevel_pips)

You're half way there

  1. Check your return codes (OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. filter out just that EAs orders order accounting - MQL4 forum
  3. I use this:
    double ocp = OrderClosePrice();
    bool isTP = MathAbs(OrderTakeProfit() - ocp) < MathAbs(OrderStopLoss() - ocp);
  4. Find latest.
    datetime lastCloseDT = 0;
    double   lastCloseTP;
    for(int i=OrdersHistoryTotal()-1; i>=0; i--) if(
       OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
    && OrderMagicNumber() == MY_MAGIC
    && OrderSymbol()      == Symbol()
    && PipsProfit(OrderTicket()) >= TPLevel_pips
    && lastCloseDT < OrderCloseTime()
       ){
       lastCloseDT = OrderCloseTime();
       lastCloseTP = OrderTakeProfit();
    }
    if(lastCloseDT == 0) ... // None
  5. && PipsProfit(OrderTicket()) >= TPLevel_pips
    Passing the ticket to that function is suspect. You shouldn't do an OrderSelect in the function when you call the function inside an OrderSelect loop. Also since SL/TP are market orders, that comparison may not be true.
 
ordo:


Hello dear coders,


This way can work only if there only one trade at a time and the tp can be hit only by one trade at a time:


You could try something along the OrderProfit() Comparison

for(int i = OrdersHistoryTotal()-1; i>=0; i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
       {
        Print("Access to history failed with error (",GetLastError(),")")
        break;
       }
             
      if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==true) //MODE_HISTORY selects closed and cancelled 
         { 
         if(OrderMagicNumber() == MagicNumber) //if you don't want these filters and want ALL orders from all symbols then get rid of these 2 lines
            if(OrderSymbol()== Symbol())       /////
               {             
                  if(OrderProfit()<0) //trade was a losing trade meaning negative profit 
                                                        
                     Print("Order LotSize was ",OrderLots()," Increasing to ", mlots," Ticket#", OrderTicket());
                     break; // break stops counting back as soon as something is found
                     }
                  if(OrderProfit() > 0 && OrderProfit() == TPlevels)  //trade was a winner and matches the TP level
                                                                      //TPlevels could be your extern double TP*pips2double or whatever you use for 3/5 Digit brokers
                                                                      //with break finds only 1 and exits, remove the break to keep counting back and further filtering code can be added below
                     {
                                                          
                     Print("Ticket Number ",OrderTicket()," Closed by TPlevel "," TPlevel ",TPlevel, "=" OrderProfit ",OrderProfit());
                     break;
 
                     }
                }  
         }
                 
      }

//I would put this function in my OrderEntry function
//And call it with something like double OrderCheck() or some such function

I only suggest this because I'm noobish coder and this is what I can understand.
These gurus have much better and simpler methods I have no doubt, but this would work for me and I would use it as a function() to keep my head on straight. If code gets too long I get confused too easy.

Hope this gives you some ideas for what you want
 
Agent86: I only suggest this because I'm noobish coder and this is what I can understand.
OrderProfit() == TPlevels
This will fail
  1. Slippage after TP triggers and becomes a market order
  2. The == operand. - MQL4 forum
Reason: