Adding comment to a position, then try to call that specific position by its comment to count its profit or loss and close it - page 4

 
king64:

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.


 

What problem do you have ? What results are you expecting and what results are you getting ?
 
Alain Verleyen:
What problem do you have ? What results are you expecting and what results are you getting ?
thanks Alain 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);
              }

           }
        }
     }
    }
 
king64:
thanks Alain 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. :)))

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.

;)

 
Yeah sure, it's just for testing purposes. ☺️
But why did you say unreliable? Is there any wrong parameter in this code? 
 
king64:
Yeah sure, it's just for testing purposes. ☺️
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...

;)

 
Minions Labs:

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

;)

Thanks I will remember this ;))