1

I have created a function to calculate my weekly profit and loss. It worked as per below and I would to break it down to have the only per symbols/pairs. I have been stucked on this aggregation. Any hints are appreciated.

string WeeklyProfit()
{
string msg_WeeklyProfit="";
int i,hstTotal=OrdersHistoryTotal();
double profit;
datetime starttime = iTime(Symbol(), PERIOD_W1, 0);

  for(i=0;i<hstTotal;i++)
    {
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE)
         {
          if(OrderOpenTime() >= starttime)
            {
                  profit += OrderProfit() + OrderSwap() + OrderCommission();
            }
      }
   msg_WeeklyProfit=profit+ " "+ AccountCurrency();
   }

   return(msg_WeeklyProfit);

I tried to create a symbol array but that wasn’t successful. I’m stucked on the total weekly.

mraniki
  • 21
  • 1
  • 6

2 Answers2

0

Here a solution if anyone needs it. But I'm still not able to have the summary per symbol. This display the impact of each trade. Letme know if you have any idea on how instead of having results as

  • EURUSD 1 EUR
  • EURUSD 2 EUR
  • Total: 2 EUR

I 'm missing on how to get to:

  • EURUSD 2 EUR
  • Total: 2 EUR

#include <Arrays\ArrayString.mqh>

string WeeklyProfit()
{

string msg_WeeklyProfit="";
int i,j,OrderCount,hstTotal=OrdersHistoryTotal();
double profit,profitb,profittotal;
string profitmsg="";
CArrayString *result = new CArrayString();
datetime starttime = iTime(Symbol(), PERIOD_W1, 0);
datetime starttime2 = TimeCurrent()-(7*60*60);
Print(starttime);
Print(starttime2);
  for(i=0;i<hstTotal;i++)
    {
     if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE)
         {
          if(OrderOpenTime() >= starttime)
            {
               const string symbol=OrderSymbol();
                if(result.Search(symbol)!=-1)
                  {
                  Print("symbol exist");
                  }
               else
               {
                  result.Add(symbol);
                  result.Sort();
                  Print(symbol);

               }
                  for(j=0;j<result.Total();j++)
                     {
                           if (OrderSymbol()==result.At(j))
                                    {
                                       profit+=OrderProfit()+OrderSwap()+OrderCommission();
                                       profitmsg+=symbol+ " " +  DoubleToStr(profit,2) + " " + AccountCurrency()+"\n";
                                    }
                                    
                        
                     }
                  
                  OrderCount++;
                  profittotal+=OrderProfit()+OrderSwap()+OrderCommission();
                  Print("profit"+ profit);
               }
            }
       }
      

   msg_WeeklyProfit= profitmsg +
                     "\n==============\n" +
                      "Since " + TimeToStr(starttime,TIME_DATE) + " Total: " + DoubleToStr(profittotal,2)+ AccountCurrency()+
                      "\n==============\n" +
                      OrderCount + " Trades" + " (avg: " + DoubleToStr(profittotal/OrderCount,2) + AccountCurrency()+ ") ";
         if (profittotal>=0) 
         {
            msg_WeeklyProfit +=  "\n"+ EMOJI_GREENCIRCLE;
         } 
      else
         {
            msg_WeeklyProfit += "\n"+EMOJI_REDCIRCLE;
         };
   delete result;
   return(msg_WeeklyProfit);
}
mraniki
  • 21
  • 1
  • 6
0

Another iteration and this time i'm able to have the summary but the profit is incremental instead of being per symbol.

  • EURUSD 1 EUR

  • EURGBP 2 EUR

  • total: 2 EUR what am i missing on the profit approach ?

    string WeeklyProfit() {

    string msg_WeeklyProfit=""; int i,j,k,OrderCount,hstTotal=OrdersHistoryTotal(); double profit,profitb,profittotal; string profitmsg=""; CArrayString result = new CArrayString(); datetime starttime2 = TimeCurrent()-(6246060);

    Print(hstTotal); for(i=0;i<hstTotal;i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==TRUE) { if(OrderOpenTime() >= starttime2) { profittotal+=OrderProfit()+OrderSwap()+OrderCommission(); OrderCount++; const string symbol=OrderSymbol(); if(result.Search(symbol)!=-1) { Print("symbol exist"); } else { if (OrderType() == OP_SELL || OrderType() == OP_BUY) { result.Add(symbol); result.Sort(); } } } } }

    for(j=0;j<result.Total();j++)
    {
       for(k=0;k<hstTotal;k++)
          {
             if(OrderSelect(k,SELECT_BY_POS,MODE_HISTORY)==TRUE)
             {
                if(OrderOpenTime() >= starttime2)
                {
                 if (OrderSymbol()==result[j])
                   {
                      if (OrderType() == OP_SELL || OrderType() == OP_BUY) 
                      {
                      profit += OrderProfit() + OrderSwap() + OrderCommission();
                      }
                   }
                 }
              }
           }
    profitmsg+=result[j]+ " " +DoubleToStr(profit,2) +" " + AccountCurrency() + "\n";
    }
    
    profittotal+=OrderProfit()+OrderSwap()+OrderCommission();
    Print("profittotal "+ profittotal);
    
    msg_WeeklyProfit= "Since " +TimeToStr(starttime2,TIME_DATE|TIME_MINUTES)+
                      "\n===================\n" +
                      profitmsg +
                      "===================\n" +
                      "Total: " + DoubleToStr(profittotal,2)+ " "+AccountCurrency()+
                      "\n"+ OrderCount + " Trades" + " (avg: " + DoubleToStr(profittotal/OrderCount,2) + AccountCurrency()+ ") ";
          if (profittotal>=0) 
          {
             msg_WeeklyProfit +=  "\n"+ EMOJI_GREENCIRCLE;
          } 
       else
          {
             msg_WeeklyProfit += "\n"+EMOJI_REDCIRCLE;
          };
    delete result;
    return(msg_WeeklyProfit);
    

    }

mraniki
  • 21
  • 1
  • 6