How to find the total number of open positions by comment and symbol name?

 
Hi,

I want to get the number of positions with different data in the comment column from different symbols.

For example;  How many open positions are there in the EURUSD symbol with "MA BUY" in the comment column?

There is the code I wrote in the attachment.  Where am I making a mistake?


I would appreciate your help.

 
Gökhan Erdoğdu :
Hi,

I want to get the number of positions with different data in the comment column from different symbols.

For example;  How many open positions are there in the EURUSD symbol with "MA BUY" in the comment column?

There is the code I wrote in the attachment.  Where am I making a mistake?


I would appreciate your help.

Paste the code correctly: use the button  Code

 
Vladimir Karputov:

Paste the code correctly: use the button 

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   Comment(ToplamPsarBuy());
  }
//+------------------------------------------------------------------+
//| ToplamPsarBuy                                                    |
//+------------------------------------------------------------------+
int ToplamPsarBuy()
  {
   int i=0;
   int toplam=0;
   for(i; i<PositionsTotal(); i++)
     {
      int ticket=PositionGetTicket(i);
      string symbol=PositionGetSymbol(i);
      PositionSelectByTicket(ticket);
      PositionSelect(symbol);
      if(PositionGetString(POSITION_COMMENT)=="PSAR BUY")
        {toplam=toplam+1;}
     }
   return(toplam);
  }
 
Gökhan Erdoğdu:

Use code:

//+------------------------------------------------------------------+
//|                                                            1.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property script_show_inputs 
//---
#include <Trade\PositionInfo.mqh>
//---
CPositionInfo  m_position;                   // object of CPositionInfo class
//--- input parameters
input string   InpComment  = "PSAR BUY";     // Comment
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int all_positions=CalculateAllPositions(InpComment);
//---
  }
//+------------------------------------------------------------------+
//| Calculate all positions                                          |
//+------------------------------------------------------------------+
int CalculateAllPositions(const string &comment)
  {
   int total=0;
   for(int i=PositionsTotal()-1; i>=0; i--)
      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
         if(m_position.Comment()==comment)
            total++;
//---
   return(total);
  }
//+------------------------------------------------------------------+
Files:
1.mq5  4 kb
 
Vladimir Karputov:

Use code:

Thank you. I will try.
Reason: