Failed To Calculate All Total Profits in History!

Ifadatty999
33
Ifadatty999  

@William Nascimento @nicholish en @kei yakushiji@David Diez @Ahmed Ahmed @AHMED KHALIFA AHMED ABULFATEH ALI ABULFATEH

Hi Programmers, am new in mql5 language and i was trying to make an EA that would calculate TotalProfits in History either Buy || sell Trades, even both ..... But ended up failing

Here is my code below!

#include <Trade\Trade.mqh>


CTrade trade;

double Ask,Bid;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {


   Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

   if(PositionsTotal()==0)//open position to get closed trades
     {
      trade.Buy(
         0.2,//how much
         NULL,//current symbol
         Ask,//buy prices
         Ask-10,//stop loss
         Ask+10,//take profit
         NULL//comment
      );
     }

   HistoryTotalProfit_Func();
   Comment("    HistoryTotalProfit_Func() : ",HistoryTotalProfit_Func()); // BIG PROBLEM.... It Comments 0.0 while there are closed trades with loss and profit! Why?


  }//ontick


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double HistoryTotalProfit_Func()
  {

   uint  TotalNumberOfDeals=HistoryDealsTotal();
   ulong TicketNumber;
   double DealProfit;
   bool HistoryDeal_Select;
   double TotalProfit=0;

   for(uint i=0; i<TotalNumberOfDeals; i++)

     {

      TicketNumber=HistoryDealGetTicket(i);
      HistoryDeal_Select=HistoryDealSelect(TicketNumber);

      if(TicketNumber>0)
        {
         if(HistoryDeal_Select)
           {


            DealProfit=HistoryDealGetDouble(TicketNumber,DEAL_PROFIT);

            TotalProfit=TotalProfit+DealProfit;


           }
        }
     }

   return(TotalProfit);


  }// HistoryProfit_Func()
David Diez
1922
David Diez  
Ifadatty999:

@William Nascimento @nicholish en @kei yakushiji@David Diez @Ahmed Ahmed @AHMED KHALIFA AHMED ABULFATEH ALI ABULFATEH

Hi Programmers, am new in mql5 language and i was trying to make an EA that would calculate TotalProfits in History either Buy || sell Trades, even both ..... But ended up failing

Here is my code below!

I cant help you on this topic, sorry.
Douglas Prager
115
Douglas Prager  
double HistoryTotalProfit_Func()
  {

   uint  TotalNumberOfDeals=HistoryDealsTotal();

TotalNumberOfDeals will be 0 because you have not first called HistorySelect() or HistorySelectByPosition().

Ifadatty999
33
Ifadatty999  
Douglas Prager:

TotalNumberOfDeals will be 0 because you have not first called HistorySelect() or HistorySelectByPosition().

@Douglas Prager 

Thanks it worked..... But i tried to calculate TotalDealProfitInHistory Based on TradeComments but i failed... gives 0... seems like deals with profit dont have Tradecomments i.e buy,out and sell,out!

whats your sugggestion? here is my code below

#include <Trade\Trade.mqh>


CTrade trade;

input string TradeCommentBuy="v25_BuyStop";
input string TradeCommentSell="v25_SEllStop";



double Ask,Bid;


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {


   Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);


   HistoryTotalProfit_Func();
   Comment("      HistoryTotalProfit : ", HistoryTotalProfit_Func());





  }//ontick



double HistoryTotalProfit_Func()
  {


   uint  TotalNumberOfDeals=HistoryDealsTotal();
   ulong TicketNumber;
   double DealProfit;
   double DealCommission,DealSwap;
   string DealComment;
   double TotalProfit=0;
  


   HistorySelect(0,TimeCurrent());

   for(uint i=0; i<TotalNumberOfDeals; i++)

     {

      TicketNumber=HistoryDealGetTicket(i);
      ENUM_DEAL_TYPE DealType=(ENUM_DEAL_TYPE)HistoryDealGetInteger(TicketNumber,DEAL_TYPE);
       DealComment=HistoryDealGetString(TicketNumber,DEAL_COMMENT);


      if(TicketNumber>0)
        {

         if(DealComment==TradeCommentBuy || DealComment==TradeCommentSell)//when i use DealComment it gives 0 as TotalProfitInHistory! why?
           {


            DealProfit=HistoryDealGetDouble(TicketNumber,DEAL_PROFIT);
            DealCommission=HistoryDealGetDouble(TicketNumber,DEAL_COMMISSION);
            DealSwap=HistoryDealGetDouble(TicketNumber,DEAL_SWAP);
            
            
            TotalProfit=TotalProfit+DealProfit+DealSwap+DealCommission;


           }
        }
     }

   return(TotalProfit);


  }// HistoryProfit_Func()
Douglas Prager
Douglas Prager
  • www.mql5.com
Trader's profile
William Roeder
23727
William Roeder  
  1. Ifadatty999: But ended up failing

    “Doesn't work” is meaningless — just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires — meaningless.
         How To Ask Questions The Smart Way. 2004
              When asking about code

  2. Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?

  3. Don't know about MT5, but on MT4 you can't modify comments.
              Metaquotes language: OrderComment() is not able to be modified - Expert Advisors and Automated Trading - MQL5 programming forum 2013.08.10

    Not a good idea to use comments, brokers can change comments, including complete replacement.


La_patates
105
La_patates  
Ifadatty999:

@Douglas Prager 

Thanks it worked..... But i tried to calculate TotalDealProfitInHistory Based on TradeComments but i failed... gives 0... seems like deals with profit dont have Tradecomments i.e buy,out and sell,out!

whats your sugggestion? here is my code below

Your deal comment condition will target only entry. No profit on entry. You have to look for close.