How to get trades by comment?

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:

Thanks I will remember this ;))