How to get trades by comment?

But that is what @tbillingso gave you! He gave you OOP code for MQL5!

Yes and it works fine! Thank you @tbillingso
But I have another problem I need to calculate more than one positions’ profit with the same comment, I print the profit and it is printing 0 value repeatedly.

For God’s sake man, take some pride in your work. That indentation is pure cringe. Also, just like stackoverflow and any other site you’d use to talk programming, post code not screenshots of code!

  1. As you see, I even hesitated to exemplify standard libs functions implication, to not to raise more questions/confusion in his head. (So I didn’t use C… stuff)
  2. I think the OP ,more than anything else, needs to learn how to tweak with others codes, change them, and see what each code line does. your suggested fine touches are for later.
  3. I Copy/Pasted that snipped from another EA, so of course he can edit it to match his needs. (example: use the process only if 10 or more positions are open…) so : if(all>10) makes sense there.
  4. I believe every question should be answered from the viewpoint of questioner, with a thinking/skills closer to his mind. of course it’s his/her obligation to improve beyond that raw answers.

ok sorry, here it is.

double totalProfitCal(void){
   
 double totalPrf=0; 
    CPositionInfo pi;
        CTrade trade;

           ulong Magic=PositionGetInteger(POSITION_MAGIC);
           
               if (pi.SelectByMagic(_Symbol,Magic )&&pi.Comment() =="M0"&&pi.Profit()>=totalProfit)
                    totalPrf += pi.Profit();//+PositionGetDouble(POSITION_SWAP); //*********//commision is omited 
                        PrintFormat("PROFIT= %d",totalPrf);
  
      return totalPrf;

                           }

Hey thanks alot. That was very kind of you

solved!

thank you guys.

bool closeAllPositions(void)
{
  int i;
    
    for(i=0;i<CROSS_NUMBER;i++){
       int k =PositionSelectByTicket(baseTickets[i][0])-1;
       
    while(k>=0){
       if(!closeObj.PositionClose(PositionGetSymbol(k))){
          PrintFormat("Position Close Error = %d",GetLastError());
          return(false);   
       }else{
          k-- ;
       } 
          }
             }
                
         return(true);  
}

hey guys

I think I have the same problem with selecting more than one position among all positions with some specific properties like Comments, tickets or buy or sell. I use the code attached but it seems it doesn’t work correctly. for selecting one position its ok but more than one position with the same identifiers it is showing wrong outcomes.

I want to select all buy positions which have “B1” comment with different Symbols (for example, EURUSD, GBPUSD and USDJPY) and calculate their profit.

//+------------------------------------------------------------------+
//|B1 profit calculator Function                                 |
//+------------------------------------------------------------------+
double B1profit(void){

  CPositionInfo pi;
  CTrade trade;
  double B1Prf=0;
  int i,j;
  for(i=0;i<CROSS_NUMBER;i++){
  for(j=0;j<MAX_LEVEL;j++){
  
  if(pi.SelectByTicket(baseTickets[i][j])&&pi.Comment()=="B1"&&pi.PositionType()==POSITION_TYPE_BUY){
     B1Prf += pi.Profit();//+PositionGetDouble(POSITION_SWAP); //*********//commision is omited 
  }
      }
          }
  return B1Prf;

}

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

Do you really seriously code like this? Please tell me you’re trolling… you’re trolling, right?

I am a newbie, that’s it! What’s the problem in this code I really don’t know. :neutral_face::slightly_frowning_face:

Your style is all over the place. There is no consistency to the indentation and brace placement. You really need to read a C++ style guide and pick an indentation style and stick to it!

Ok sure, I will do that, I didn’t know anything about it thank you.
But my problem is not with indentation and place of braces for now

You will find that if you continue to post poorly formatted and difficult to parse code that those in a position to help you will ignore you.

the reason is that I really didn’t know how to do that and how much this is important, just like my poor coding. I try my best to do it and learn it quickly.

//+------------------------------------------------------------------+
//|B1 profit calculator Function                                 |
//+------------------------------------------------------------------+
double B1profit(void){

  CPositionInfo pi;
      CTrade trade;
         double B1Prf=0;
            int i,j;
 
        for(i=0;i<CROSS_NUMBER;i++) {
            for(j=0;j<MAX_LEVEL;j++){
                  if(pi.SelectByTicket(baseTickets[i][j])&&pi.Comment()=="B1"&&pi.PositionType()==POSITION_TYPE_BUY)
                                    {B1Prf += pi.Profit();//+PositionGetDouble(POSITION_SWAP); //*********//commision is omited 
                                    }
                                    }
                                    }
  return B1Prf;

                     }

Just use the styler (press ctrl + ,) and the styling is done automatically

What problem do you have ? What results are you expecting and what results are you getting ?

thanks for your reply, it is solved now because I found the source of problem. There wasn’t any wrong parameters in my code. In the closing part of my code based on my strategy it closes one basket having “B1” comment and the half of the other one with “B2” comment, I found that when the half of a position is closed the comment will be deleted, so in the next processes it can’t be found as a position with “Bla Bla” comment. :)))

//+------------------------------------------------------------------+
//|B1 profit calculator Function                                 |
//+------------------------------------------------------------------+
double B1profit(void)
  {

   CPositionInfo pi;
   CTrade trade;
   double B1Prf=0;
   int i,j;

   for(i=0;i<CROSS_NUMBER;i++)
     {
      for(j=0;j<MAX_LEVEL;j++)
        {
         if(pi.SelectByTicket(baseTickets[i][j]) && pi.Comment()=="B1" && pi.PositionType()==POSITION_TYPE_BUY)
           {
            B1Prf+=pi.Profit();//+PositionGetDouble(POSITION_SWAP); //*********//commision is omited 
           }
        }
     }
   return B1Prf;

  }
//+------------------------------------------------------------------+
//|B1 close Function                                 |
//+------------------------------------------------------------------+
bool B1close(void)
  {

   CPositionInfo pi;
   CTrade trade;
   int i,j;
   for(i=0;i<CROSS_NUMBER;i++)
     {
      for(j=0;j<MAX_LEVEL;j++)
        {

         if(pi.SelectByTicket(baseTickets[i][j]) && pi.Comment()=="B1" && pi.PositionType()==POSITION_TYPE_BUY)
           {
            if(!trade.PositionClose(pi.Ticket()))
              {
               PrintFormat("Position B1Close Error = %d",GetLastError());
               return(false);
              }
           }

         if(pi.SelectByTicket(baseTickets2[i][j]) && pi.Comment()=="B2" && pi.PositionType()==POSITION_TYPE_SELL)
           {
            if(!trade.PositionClosePartial(pi.Ticket(),pi.Volume()/2))
              {
               PrintFormat("Position B1@PClose Error = %d",GetLastError());
               return(false);
              }

           }
        }
     }
    }

I know you stated earlier in this thread that this code is for testing ONLY, but BE SURE that you do NOT use this code in real accounts, it is very unreliable.

:wink:

Yeah sure, it’s just for testing purposes. :slight_smile:

But why did you say unreliable? Is there any wrong parameter in this code?

Some brokers doesn’t enable the Comment field, others can override the comment field for their own use… so you will be in trouble…

:wink: