Account information to email Expert Advisor

 

Hi!

I am trying to create an Expert Advisor which will send every day an e-mail at a specified time to my e-mail adress. I writed the code and it works quiet good (It send the mail with the information about the account), the only problem is that sometimes it sends the information about the opened position multiple times. An example is below: As you see the information came multiple time about the same position. The main goal would be that 1 position would be only once in the list. I quess the problem is in the for cycle, but I am not sure. Could somebody help me where is the problem in the code? (Attached below, too)


Thanks in advance,

Roland

"

Account information (71197844):

2017.11.03 20:00

Account Currency: EUR
Equity: 5003.43
Balance: 4985.50
Margin: 10.00
Free Margin: 4993.43
Profit: 17.93

Open Positions:

1: EURUSD Open Time: 2017.10.20 12:00 Order Type: Sell Lots: 0.01 Open Price: 1.1816 Price: 1.1620 Stop Loss: 1.1976 Take Profit: 1.1446 Swap: 0.00
Profit: 16.83

1: EURUSD Open Time: 2017.10.20 12:00 Order Type: Sell Lots: 0.01 Open Price: 1.1816 Price: 1.1642 Stop Loss: 1.1976 Take Profit: 1.1446 Swap: 0.00
Profit: 14.92

1: EURUSD Open Time: 2017.10.20 12:00 Order Type: Sell Lots: 0.01 Open Price: 1.1816 Price: 1.1659 Stop Loss: 1.1976 Take Profit: 1.1446 Swap: 0.00
Profit: 13.45

1: EURUSD Open Time: 2017.10.20 12:00 Order Type: Sell Lots: 0.01 Open Price: 1.1816 Price: 1.1646 Stop Loss: 1.1976 Take Profit: 1.1446 Swap: 0.00
Profit: 14.56

1: EURUSD Open Time: 2017.10.20 12:00 Order Type: Sell Lots: 0.01 Open Price: 1.1816 Price: 1.1607 Stop Loss: 1.1976 Take Profit: 1.1446 Swap: 0.00
Profit: 17.93

Closed Positions:

1: AUDJPY Open Time: 2017.10.26 22:00 Order Type: Sell Lots: 0.01 Open Price: 87.3470 Close Time: 2017.11.01 Close Price: 87.5980 Swap: 0.00
Profit: -1.89

1: Open Time: 2017.11.01 08:41 Order Type: Lots: 0.01 Open Price: 0.0000 Close Time: 2017.11.01 Close Price: 0.0000 Swap: 0.00
Profit: -0.06



Best Regards,
R&R Forex


"



#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int i;
string OrdersInfo;
string Message;
int total=OrdersTotal();;
int timecount=0;
int hstTotal=OrdersHistoryTotal();
string OrdersInfoHistory;
string Message2;
void start()
{  
   
     
    if(timecount==0){  
                     if(TimeHour(TimeLocal())==20 && TimeMinute(TimeLocal())>=0  // Időpont 
                        && TimeDayOfWeek(TimeLocal())>0 && TimeDayOfWeek(TimeLocal())<=5) //Hétköznapok
                           {SendtoMail ();
                            timecount=1 ; 
                           }
                    }
    if(TimeHour(TimeLocal())==21)timecount=0;            
   
}



 void SendtoMail ()
         {
         
          for(i=0;i<=total-1; i++)
               {
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
                string Type;
                if(OrderType()==0)Type="Buy";
                if(OrderType()==1)Type="Sell";
                double Price;
                if(OrderType()==0)Price=MarketInfo(OrderSymbol(),MODE_ASK); 
                if(OrderType()==1)Price=MarketInfo(OrderSymbol(),MODE_BID); 
;
                
                OrdersInfo=   i+1+": "+OrderSymbol()+" Open Time: "+TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES)
                              + " Order Type: "+Type+" Lots: "+OrderLots()+" Open Price: "+DoubleToString(OrderOpenPrice(),4)
                              +" Price: "+DoubleToString(Price,4)+" Stop Loss: "+DoubleToString(OrderStopLoss(),4)+" Take Profit: "
                              +DoubleToString(OrderTakeProfit(),4)
                              +" Swap: "+DoubleToString(OrderSwap(),2)+ "<br>Profit: <b>"+DoubleToString(OrderProfit(),2)+"</b><br>"+"<br>";
                Message=Message + OrdersInfo;
                
                } 
                 
          for(i=0;i<hstTotal;i++) 
                   { 
              
                    OrderSelect(i,SELECT_BY_POS,MODE_HISTORY); 
                     if (TimeDay(OrderCloseTime())==TimeDay(TimeLocal()))
                        {
                               string Type;
                               if(OrderType()==0)Type="Buy";
                               if(OrderType()==1)Type="Sell";
                               double Price;
                               if(OrderType()==0)Price=MarketInfo(OrderSymbol(),MODE_ASK); 
                               if(OrderType()==1)Price=MarketInfo(OrderSymbol(),MODE_BID); 
                        
                               int x=0;
                        
                      OrdersInfoHistory=   x+1+": "+OrderSymbol()+" Open Time: "+TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES)
                                             + " Order Type: "+Type+" Lots: "+OrderLots()+" Open Price: "+DoubleToString(OrderOpenPrice(),4)
                                             + " Close Time: "+TimeToStr(OrderCloseTime(),TIME_DATE)+" Close Price: "+DoubleToString(OrderClosePrice(),4)
                                             +" Swap: "+DoubleToString(OrderSwap(),2)+ "<br>Profit: <b>"+DoubleToString(OrderProfit(),2)+"</b><br>"+"<br>";     
                   
                   
                                    x=x+1;
                     Message2=Message2+OrdersInfoHistory;               
                     
                              }
                  }
         
         SendMail("Daily account report: "+AccountNumber()+", "+TimeToStr(TimeLocal(),TIME_DATE|TIME_MINUTES), //Subject
         
         "<html>Account information (<b>"+AccountNumber()+"</b>):"+"<br>"+"<br>"         //Message
           +"<b>"+TimeToStr(TimeLocal(),TIME_DATE|TIME_MINUTES)+"</b>"+"<br>"+"<br>"+   
         
         "Account Currency:  <b>"+AccountCurrency()+"</b><br>"+
         "Equity:  <b>"+DoubleToString(AccountEquity(),2)+"</b><br>"+
         "Balance:  <b>"+DoubleToString(AccountBalance(),2)+"</b><br>"+
         "Margin:  <b>"+DoubleToString(AccountMargin(),2)+"</b><br>"+
         "Free Margin:  <b>"+DoubleToString(AccountFreeMargin(),2)+"</b><br>"+
         "Profit:  <b>"+DoubleToString(AccountProfit(),2)+"</b><br>"+"<br>"
         
         +"Open Positions:<br><br>"
         +Message
         +"Closed Positions:<br><br>"
         +Message2
         +"<br>"+"<br>"
         "Best Regards,"+"<br>"
         "<b>R&R Forex</b>"    
         
        );
         
 
         }  
 

Anyboy any idea?

 
  1. int total=OrdersTotal();;
    int hstTotal=OrdersHistoryTotal();
    These variables aren't being updated. Assign them in your function.

  2. for(i=0;i<=total-1; i++){
       OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles