How can we get and collect the profit of the position with ticket numbers in the array ?

 

Example :

open trades ;

 #symbol       #ticket        #profit
USDCAD# 259186565 - 900,57
AUDCAD# 259186566 -1 346,35
EURUSD# 259186568 3 698,94
GOLD# 259186569 -10 538,20
AUDUSD# 259186570 -1 893,29
GER40Cash 259186571 -1 489,92
                                            

the list I want to get the sum of their profits:

int list_of_ticket_numbers[]={ 259186566, 259186571, 259186565};

so I want to get the total cards of the 3 positions I have mentioned above.

double total_profit_of_given_ticket_numbers=;          // -3.736,84 example


sorry for my english :)

 
sanane sanane1:

Example :

open trades ;

 #symbol       #ticket        #profit
USDCAD# 259186565 - 900,57
AUDCAD# 259186566 -1 346,35
EURUSD# 259186568 3 698,94
GOLD# 259186569 -10 538,20
AUDUSD# 259186570 -1 893,29
GER40Cash 259186571 -1 489,92
                                            

the list I want to get the sum of their profits:

int list_of_ticket_numbers[]={ 259186566, 259186571, 259186565};

so I want to get the total cards of the 3 positions I have mentioned above.

double total_profit_of_given_ticket_numbers=;          // -3.736,84 example


sorry for my english :)

When you have non-uniform data but want to store it together like this, a handy way is to embed a structure in an array.

Here is a small (hard coded) example to give you an idea - I used half the data you supplied but I am sure you get the idea.

You could also search on other fields, for example sum all the tickets of the same symbol.


//+------------------------------------------------------------------+
//|                               429420-ArrayOfStruc_SumProfits.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()

//Data Container
  {
   struct ticketRow
     {
      string            symbol;
      ulong             ticketNo;
      double            profit;
     };

   ticketRow ticketTable[] = {};
   ArrayResize(ticketTable, 3);

//Test data setup
   ticketTable[0].symbol   =  "USDCAD";
   ticketTable[0].ticketNo =  259186565;
   ticketTable[0].profit   =  900.57;

   ticketTable[1].symbol   =  "AUDCAD";
   ticketTable[1].ticketNo =  259186566;
   ticketTable[1].profit   =  1346.35;

   ticketTable[2].symbol   =  "EURUSD";
   ticketTable[2].ticketNo =  259186568;
   ticketTable[2].profit   =  -698.94;

//Search Data
   ulong    ticketSearch[]    = {259186565, 259186568};
   double   profitSum         = 0;
   string   report            = "";
   int      matchCount        = 0;

//Search Method
   for(int i = 0; i < ArraySize(ticketSearch); i++)
     {
      for(int j = 0; j < ArraySize(ticketTable); j++)
        {
         report = StringFormat("Checking %d==%d %s $%+8.2f",
                     ticketSearch[i], ticketTable[j].ticketNo, ticketTable[j].symbol, ticketTable[j].profit
                    );

         if(ticketSearch[i] == ticketTable[j].ticketNo)
           {
            report += (" - success");
            profitSum += ticketTable[j].profit;
            matchCount++;
           }
          else
            {
            report += (" - fail");
            } 
         Print(report);   
        }
     }
     
   PrintFormat("Total Profit = %+.2f Tickets Matched = %d/%d\n", profitSum, matchCount, ArraySize(ticketTable));     

  }

//+------------------------------------------------------------------+


The program output is like so:

2022.07.26 17:57:50.842 Checking 259186565==259186565 USDCAD $ +900.57 - success

2022.07.26 17:57:50.842 Checking 259186565==259186566 AUDCAD $+1346.35 - fail

2022.07.26 17:57:50.842 Checking 259186565==259186568 EURUSD $ -698.94 - fail

2022.07.26 17:57:50.842 Checking 259186568==259186565 USDCAD $ +900.57 - fail

2022.07.26 17:57:50.842 Checking 259186568==259186566 AUDCAD $+1346.35 - fail

2022.07.26 17:57:50.842 Checking 259186568==259186568 EURUSD $ -698.94 - success

2022.07.26 17:57:50.843 Total Profit = +201.63 Tickets Matched = 2/3


 
R4tna C #:

When you have non-uniform data but want to store it together like this, a handy way is to embed a structure in an array.

Here is a small (hard coded) example to give you an idea - I used half the data you supplied but I am sure you get the idea.



The program output is like so:

2022.07.26 17:57:50.842 Checking 259186565==259186565 USDCAD $ +900.57 - success

2022.07.26 17:57:50.842 Checking 259186565==259186566 AUDCAD $+1346.35 - fail

2022.07.26 17:57:50.842 Checking 259186565==259186568 EURUSD $ -698.94 - fail

2022.07.26 17:57:50.842 Checking 259186568==259186565 USDCAD $ +900.57 - fail

2022.07.26 17:57:50.842 Checking 259186568==259186566 AUDCAD $+1346.35 - fail

2022.07.26 17:57:50.842 Checking 259186568==259186568 EURUSD $ -698.94 - success

2022.07.26 17:57:50.843 Total Profit = +201.63 Tickets Matched = 2/3



ı dıd this like this and it worked :) Thanks for the help !



void OnTick()
  {
 ulong listofmagic[] = {259368957,259369090};
 double totl=0.00; 
 for (int x= PositionsTotal();x>=0;x--)
 {
 
  ulong ticket =PositionGetTicket(x);
  
     for(int i = 0; i<=ArraySize(listofmagic)-1;i++){
 
          if (ticket== listofmagic[i]){
 
              double profit=PositionGetDouble(POSITION_PROFIT);
              totl+=profit;
                 // printf(profit);
 
 

 }
 
 
 }
 

 
 
 
 }
 
 
 printf(totl);
 

  }


 
R4tna C #: When you have non-uniform data but want to store it together like this, a handy way is to embed a structure in an array.
I agree, and
   struct ticketRow
     {
      string            symbol;
      ulong             ticketNo;
      double            profit;
     };
⋮
   ticketTable[0].symbol   =  "USDCAD";
   ticketTable[0].ticketNo =  259186565;
   ticketTable[0].profit   =  900.57;

   ticketTable[1].symbol   =  "AUDCAD";
   ticketTable[1].ticketNo =  259186566;
   ticketTable[1].profit   =  1346.35;
you can simplify further code:
   struct ticketRow
     {
      string            symbol;
      ulong             ticketNo;
      double            profit;

      void init(string s, ulong t, d p){
        symbol=s; ticketNo=t; profit=p; }
     };
   ⋮
   ticketTable[0].init("USDCAD", 259186565,  900.57);
   ticketTable[1].init("AUDCAD", 259186566, 1346.35);
 
William Roeder #:
I agree, and
you can simplify further code:
Yeah I agree - I started creating a setter method but felt it was overkill for a quick example like this.

Thanks for adding it




 
sanane sanane1 #:


ı dıd this like this and it worked :) Thanks for the help !





Good - I suggest adding a "continue" statement as there is no need to scan further if a match is found, also it helps avoid multiple addition just in case there is a repeat in your search set.

And define the ticket and profit variables before the loop like totl (in the name of efficiency)

 if (ticket== listofmagic[i]){
 
              profit=PositionGetDouble(POSITION_PROFIT);
              totl+=profit;
                 // printf(profit);
continue; //<<
 }
 
 
//like this ? 



void OnTick()
  {
 ulong listofmagic[] = {259368957,259369090};
 double totl=0.00; 
 double profit;
 ulong ticket ;
 for (int x= PositionsTotal();x>=0;x--)
 {
 
  ulong ticket =PositionGetTicket(x);
  
     for(int i = 0; i<=ArraySize(listofmagic)-1;i++){
 
          if (ticket== listofmagic[i]){
 
              double profit=PositionGetDouble(POSITION_PROFIT);
              totl+=profit;
                 // printf(profit);
          continue;
 
 

 }
 
 
 }
 
 
Actually, you want to break out of the inner loop, not continue with it.
 

William Roeder #:
Actually, you want to break out of the inner loop, not continue with it.


ı dıd compile but get warnings 

Files:
warning.PNG  13 kb
 
William Roeder #:
Actually, you want to break out of the inner loop, not continue with it.

Ah yes exactly - was thinking "break", but mistakenly wrote "continue" - thanks

 
sanane sanane1 #:

ı dıd compile but get warnings 

Yes, you have to remove the declarations in the loop - like this:

void OnTick()
  {
   ulong listofmagic[] = {259368957,259369090};
   double totl = 0.00;
   double profit = 0;
   ulong ticket = 0;
   for(int x = PositionsTotal(); x >= 0; x--)
     {

      ticket = PositionGetTicket(x);

      for(int i = 0; i <= ArraySize(listofmagic) - 1; i++)
        {

         if(ticket == listofmagic[i])
           {

            profit = PositionGetDouble(POSITION_PROFIT);
            totl += profit;
            // printf(profit);
            break; //<< match found - exit the inner loop & process next value from PositionsTotal()
           }


        }
     }
  }
Reason: