How to Count Number of Open long/short positions

 

Good afternoon forum,

As per the title of this thread, I want to limit my bot from firing in many, many positions into the market. I am happy to compound and build in to positions each time an order is triggered but I would like a maximum limit to be able to manage my maximum exposure.

Is there a nice, common method used to count the number of BuyOrders and SellOrders currently open on my account in order to use these variables as limits? 

CPerry. 

 
  • Create some variables at the global scope.
  • Reset them to 0 on every new tick.
  • Cycle all your orders (MT4) or positions (MT5) and increase buy counter for each buy trade found, and sell counter for each sell trade found.
  • Use your counters to check conditions and act according.
 
Good morning Fabio, sorry I didn't see you had replied.

I can do the first two steps and the last one but how do I 'find' the amount of buy trades and sell trades? What is the line of code I need to achieve this please?

CPerry. 
 
CPerry #:
Good morning Fabio, sorry I didn't see you had replied.

I can do the first two steps and the last one but how do I 'find' the amount of buy trades and sell trades? What is the line of code I need to achieve this please?

CPerry. 

Hi

I'm new here and just learnign myself, but one of the tutorials listed here https://www.mql5.com/en/forum/447439/page5 gave the following code that I found worked for me:

//count open positions, also detects buy and sell orders
bool CountOpenPositions(int &countBuy, int &countSell){
  countBuy  = 0;
  countSell = 0;
  int total =PositionsTotal();
  for(int i=total-1; i>=0; i--){
    ulong positionTicket = PositionGetTicket(i);
    if(positionTicket<=0){Print("Failed to get ticket");return false;}
    if(!PositionSelectByTicket(positionTicket)){Print("Failed to select position"); return false;}
    long magic;
    if(!PositionGetInteger(POSITION_MAGIC,magic)){Print("Failed to get magic");return false;}
    if(magic ==InpMagicNumber){
      long type;
      if(!PositionGetInteger(POSITION_TYPE,type)){Print ("Failed to get type");return false;}
      if(type==POSITION_TYPE_BUY){countBuy++;}
      if(type==POSITION_TYPE_SELL){countSell++;}
      }
    }
    return true;
}
Programming tutorials - How to create a graphical panel in Mql5 | Part 1/2
Programming tutorials - How to create a graphical panel in Mql5 | Part 1/2
  • 2023.05.24
  • www.mql5.com
How to create a graphical panel in mql5 | part 1/2. We'll set the position, text, text color, background color, and font size for the button. Now, let's define the class for our panel. Inside the class, we will define private and public sections
 
pete sw #:

Hi

I'm new here and just learnign myself, but one of the tutorials listed here https://www.mql5.com/en/forum/447439/page5 gave the following code that I found worked for me:

Thank you very much for taking the time to reply Pete! I'll give this a try this week. Best of luck with all your future trading mate.