How to get profit by EA?

 

If I remove conditionals at line 1 and 2, it works fine getting the profit about time that I want, the problem is that i want to get only a specific  EA profit. Doing this bellow i receive 4755 error(trade deal not found). Why it does not work? 

double getResultadoDiarioEA(){
   double retorno = 0;
   datetime dataInicial = getDataInicioPregaoAtual();
   datetime dataFinal=TimeCurrent();
   
   HistorySelect(dataInicial, dataFinal);
   uint total=HistoryDealsTotal();
   ulong ticket=0;
   for(uint i=0; i < total; i++){
      ticket = HistoryDealGetTicket(i);
      if (HistoryDealSelect(ticket)){      //1
         if (HistoryDealGetInteger(ticket, DEAL_MAGIC) == meuMagic){              //2
            retorno = retorno + HistoryDealGetDouble(ticket, DEAL_PROFIT);
         }
      } else {
          Print ("Erro encontrado ==> " + GetLastError());
      }
   }
   return retorno;
}

 
double getResultadoDiarioEA(){
   double retorno = 0;
   datetime dataInicial = getDataInicioPregaoAtual();
   datetime dataFinal=TimeCurrent();
   
   HistorySelect(dataInicial, dataFinal);
   uint total=HistoryDealsTotal();
   ulong ticket=0;
   for(uint i=0; i < total; i++){
      ticket = HistoryDealGetTicket(i);
         if (HistoryDealGetInteger(ticket, DEAL_MAGIC) == meuMagic){              //2
            retorno = retorno + HistoryDealGetDouble(ticket, DEAL_PROFIT);
         }
   }
   return retorno;
}
 
Trinh Dat:
i tried it but didnt work as well. there is no error but always return zero.
 
Never EA get profit so long.
 
RodrigoMT5:

If I remove conditionals at line 1 and 2, it works fine getting the profit about time that I want, the problem is that i want to get only a specific  EA profit. Doing this bellow i receive 4755 error(trade deal not found). Why it does not work? 

double getResultadoDiarioEA(){
   double retorno = 0;
   datetime dataInicial = getDataInicioPregaoAtual();
   datetime dataFinal=TimeCurrent();
   
   HistorySelect(dataInicial, dataFinal);
   uint total=HistoryDealsTotal();
   ulong ticket=0;
   for(uint i=0; i < total; i++){
      ticket = HistoryDealGetTicket(i);
      if (HistoryDealSelect(ticket)){      //1
         if (HistoryDealGetInteger(ticket, DEAL_MAGIC) == meuMagic){              //2
            retorno = retorno + HistoryDealGetDouble(ticket, DEAL_PROFIT);
         }
      } else {
          Print ("Erro encontrado ==> " + GetLastError());
      }
   }
   return retorno;
}

//probably should have

return(retorno);
 
3rjfx:
3rjfx, unfortunately there is no syntax problem because if i remove conditionals at line 1 and 2, it works fine returning all my profit for the this period. My idea is to get a profit only for a specific EA and magic number was supposed for solve this, I don´t understand why its so hard.  
 
RodrigoMT5:
3rjfx, unfortunately there is no syntax problem because if i remove conditionals at line 1 and 2, it works fine returning all my profit for the this period. My idea is to get a profit only for a specific EA and magic number was supposed for solve this, I don´t understand why its so hard.  
You have to debug your code. What are the dates ? How many deals are selected ?
 
Alain Verleyen:
You have to debug your code. What are the dates ? How many deals are selected ?

Alain,

dates are fine because without conditionals I get the right value of all my profit for this period. The problem is about getting some information about the deal. I debugged and could check that my deal has no magic number, but I use magic in every order that I send. What I want to know is why mt5 does not save my magic.  

 
RodrigoMT5:

Alain,

dates are fine because without conditionals I get the right value of all my profit for this period. The problem is about getting some information about the deal. I debugged and could check that my deal has no magic number, but I use magic in every order that I send. What I want to know is why mt5 does not save my magic.  

How are closed your positions, by SL/TP ?
 
Alain Verleyen:
How are closed your positions, by SL/TP ?
yes, sl or tp.
 
RodrigoMT5:
yes, sl or tp.
This is done by the server, not your EA, then these deals don't have a magic number.
Reason: