Can't pull order info into e-mail

 

I'm trying to use this code someone else published to automate e-mails to myself with order data after a SL or TP is hit.

However, when I get the e-mail, it looks like this...

EURUSD OpenTime: 1970.01.01 00:00 Close Time: 1970.01.01 00:00 Order Type BUY Open 0.0000 Close 0.0000 Profit (0.0000)

Can anyone see why this code isn't pulling the order information?

//+------------------------------------------------------------------+
//|                                               MailAlert TEST.mq4 |
//|                                                      Nicholishen |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Nicholishen"
#property link      ""
int k;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
for(int i=0;i<1000;i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         if(OrderSymbol()==Symbol()  ){
            k++;
         }
      }else{
         break;
      }  
   }
   Comment("Init() Trades Count = ",k);
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

int f =0;
   for(int i=0;i<10000;i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         if(OrderSymbol()==Symbol() /*&& OrderMagicNumber()==MAGICMA && OrderComment() ==Cmt(Period())*/ ){
            f++;
          
         }
      }else{
         break;
      }  
   }
   Comment(" Trades in History ",f," Init cnt ",k  );
   if(k < f){
      string ordertyp;
      
      OrderSelect(f,SELECT_BY_POS,MODE_HISTORY);
      if(OrderType()==0)ordertyp="BUY";
      if(OrderType()==1)ordertyp="SELL";
     // SendMail("HI","HI");
      SendMail("CLOSED TRADE","  "+Symbol()+"    OpenTime: "+TimeToStr(OrderOpenTime())+"   Close Time: "+TimeToStr(OrderCloseTime())+"                     "+
      "Order Type "+ordertyp+"   Open "+DoubleToStr(OrderOpenPrice(),4)+"   Close "+DoubleToStr(OrderClosePrice(),4)+"  Profit ("+DoubleToStr(OrderProfit(),4)+")" );
      k++;
   }
 return;
} 


//----
  
//+------------------------------------------------------------------+
 
  1.       OrderSelect(f,SELECT_BY_POS,MODE_HISTORY);
          if(OrderType()==0)ordertyp="BUY";
    At that point, f is the count of all history orders for that pair. If you only have that pair, f-1 is the position last one. If you have other pairs then f or f-1 is meaningless as a position, so the select fails, you don't check for it, and send garbage.
  2. datetime lastClose;
    int init(){
        for(int pos=0; pos < OrdersHistoryTotal(); pos++) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderCloseTime()    > lastClose                 // not yet processed,
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL){// Avoid cr/bal https://www.mql5.com/en/forum/126192
            lastClose = OrderCloseTime();
    }   }
    int start(){
        for(int pos=0; pos < OrdersHistoryTotal(); pos++) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderCloseTime()    > lastClose                 // not yet processed,
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL){// Avoid cr/bal https://www.mql5.com/en/forum/126192
            lastClose = OrderCloseTime();
            send
    }

 
Thank you very much. It works perfectly!
Reason: