Deals and orders mixed retrieving history

 

Since I am not satisfied with the format MT5 exports the trade history to excel, I wanted to create my own with my custom format.

Now, while the following function works as expected, getting the trades for the current day, it seems to be retrieving both orders and deals, but I would be interested only in the deal, like the so called "closed position" in MT4.

I think in this case the Metaquotes team has made a horrible work overcomplicating all this stuff positions orders deals....in comparison to MT4....

Anyway, any help pointing out fixes would be appreciated:


void tradesToCsV(){

   MqlDateTime SDateTime;
   TimeToStruct(TimeCurrent(),SDateTime);

   SDateTime.hour=0;
   SDateTime.min=0;
   SDateTime.sec=0;
   datetime  from_date=StructToTime(SDateTime);     

   SDateTime.hour=23;
   SDateTime.min=59;
   SDateTime.sec=59;
   datetime  to_date=StructToTime(SDateTime);     
   to_date+=60*60*24;

   HistorySelect(from_date,to_date);
   uint     total=HistoryDealsTotal();
   ulong    ticket=0;
  
   string csvFile = "myTradesReport.csv";
   int csvHandler = FileOpen(csvFile,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);
   FileWrite(csvHandler,"TICKET", ";", "ENTRY", ";", "STOP LOSS", ";","PROFIT", ";","COMISSION");
  
   FileSeek(csvHandler,0,SEEK_END);
   
   for(uint i=0;i<total;i++){
      ulong ticket = HistoryDealGetTicket(i);
      if(ticket > 0 && HistoryDealGetInteger(ticket, DEAL_TYPE) != DEAL_TYPE_BALANCE){
        
         double entry = HistoryDealGetDouble(ticket,DEAL_PRICE);
         double sl = HistoryDealGetDouble(ticket,DEAL_SL);
         double pro = HistoryDealGetDouble(ticket,DEAL_PROFIT);
         double com = HistoryDealGetDouble(ticket,DEAL_COMMISSION);
         
         FileWrite(csvHandler,ticket, ";", entry, ";", sl, ";",pro, ";",com);
     }
   }
   FileClose(csvHandler);
}
 
Pedro Sanchez:
";"

It looks like you are writing both IN and OUT deals.

If you want closure only, I think you need to only write OUT deals - the code would be something like this:

         string inOrOut = EnumToString( ENUM_DEAL_ENTRY(HistoryDealGetInteger(ticket, DEAL_ENTRY)));
         
         if(StringFind(inOrOut, "OUT") >= 0) 
         FileWrite(csvHandler,ticket, ",", entry, ",", sl, ",",pro, ",",com);


Also I changed the delimiters to commas - easier to load into excel