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

 
Hi everybody
I am trying to find one or more positions  among other positions by their comment or comments  assigned to them before, and close them if they are in certain amount of profit. I know relying on comment is not a safe way but I really need it for backtesting purposes not trading real money. 
I tried several ways  and searched a lot through net but couldn't find something. 
Forgot to mention, I need this to be done in mql5 coding. 
Any advice would be appreciated.
Thanks in advance 


 
king64:
Hi everybody
I am trying to find one or more positions  among other positions by their comment or comments  assigned to them before, and close them if they are in certain amount of profit. I know relying on comment is not a safe way but I really need it for backtesting purposes not trading real money. 
I tried several ways  and searched a lot through net but couldn't find something. 
Forgot to mention, I need this to be done in mql5 coding. 
Any advice would be appreciated.
Thanks in advance 


I think it's not a unsafe procedure to group/categorize your positions based on comments, since you're dealing with open positions, and comments of open positions are SUPPOSED to be the same strings you set when sending orders. the unsafe part is of course looking into comments of closed positions.

on your problem :
loop through open positions using a for loop ( loop limit set by PositionsTotal() ) , and using PositionInfoString(), read their comment, and if the comment is the one you're looking for, (or more safely, if position comment string contains the string you're looking for ) then check its profit using PositionInfoDouble() function. and close it if conditions are met
 
Code2219 or probably 2319:
I think it's not a unsafe procedure to group/categorize your positions based on comments, since you're dealing with open positions, and comments of open positions are SUPPOSED to be the same strings you set when sending orders. the unsafe part is of course looking into comments of closed positions.

on your problem :
loop through open positions using a for loop ( loop limit set by PositionsTotal() ) , and using PositionInfoString(), read their comment, and if the comment is the one you're looking for, (or more safely, if position comment string contains the string you're looking for ) then check its profit using PositionInfoDouble() function. and close it if conditions are met
Thank you for your replying, I tried to do what you said but again it doesn't work. For sure I did something wrong. Guess some examples make it clear if you mind to give some. 
I am really appreciated 🙏
Files:
 
king64:

what kind of code is that ?!!!
you definitely need to read the mql5 docs, and a few scripts/experts codes, which hundreds of them can be found easily.

int all = PositionsTotal();
if(all>0)
{
        for(int i=0; i<all; i++)
        {
                if(PositionGetSymbol(i)!=_Symbol) continue;                     // optional filter
                if(PositionGetInteger(POSITION_MAGIC) != MyMagic) continue;     // optional
                if(PositionGetString(POSITION_COMMENT) != "BLA BLA") continue;
                if(PositionGetDouble(POSITION_PROFIT)>1000000) // don't settle for less that $ 1 Million :)
                {
                        // close position here :
                        ulong TickeT = (ulong)PositionGetInteger(POSITION_TICKET);
                        // use standard libraries , or whatever,  and close position with the TickeT
                }
        }
}
 
Code2219 or probably 2319:

what kind of code is that ?!!!
you definitely need to read the mql5 docs, and a few scripts/experts codes, which hundreds of them can be found easily.

The first check is redundant since the for loop will never enter if all is <= 0. Also, selecting a position by symbol and magic number is already built into CPositionInfo.  Finally, it is not necessary to use continue statements combined with not conditions. Since MT4 build 600 expressions stop being evaluated after it is evident that the if control statement will resolve to false, so you can just use one singe if statement. 

Example: 

   CPositionInfo pi;
   CTrade trade;
   if(pi.SelectByMagic(_Symbol, MAGIC)
      && pi.Comment() == "BLA BLA"
      && pi.Profit()  >  max_profit
   ){
      if(!trade.PositionClose(pi.Ticket()))
         Print(_LastError);
   }
 
Code2219 or probably 2319:

what kind of code is that ?!!!
you definitely need to read the mql5 docs, and a few scripts/experts codes, which hundreds of them can be found easily.

Great! Thank you so much
Yeah you are right I need to study, train and work! 
Another question: I use baskets in my strategy, I need to calculate the profit of let say 3 symbols together which have the same comment. Hiw can I do that? 
And at last I don't forget your command, for sure never settle the profit to less than one million dollars ;) 
 
nicholi shen:

The first check is redundant since the for loop will never enter if all is <= 0. Also, selecting a position by symbol and magic number is already built into CPositionInfo.  Finally, it is not necessary to use continue statements combined with not conditions. Since MT4 build 600 expressions stop being evaluated after it is evident that the if control statement will resolve to false, so you can just use one singe if statement. 

Example: 


Thanks for your comment nicholi, but this code is for MT5

 
king64: Thanks for your comment nicholi, but this code is for MT5

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

 
Fernando Carreiro:

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

Yes and it works fine! Thank you  @nicholi shen
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. 
Files:
 

Hello family, I am new and would like to have more details on how it works. Thanks in advance

<Edited to translate to English>
 
Ulrich MESSONGONE:

Bnjour la famille,je suis nouveau et aimerai avoir plus de details sur le mode de fonctionnement.Merci d'avance


Write in English please, I don't understand what you are saying