how to count buy and sell positions at the same time?

 

This code of mine was not counting only buy positions it count also sell position but I want it to count it separately to have a value of how many buy and sell position I have.

Please help me find the problem.

#include <Trade\Trade.mqh>

CTrade trade;
void OnTick()
  {
   Comment("BUY = ", OpenBuyPositions());
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   double profit = AccountInfoDouble(ACCOUNT_PROFIT);
   if(profit == 50)
     {closeallpositions();}
   if((OrdersTotal()==0)&&(PositionsTotal()==0))
     {
      trade.BuyStop(
         0.01,                                   //Lot Size
         Bid+50*_Point,                          //Entry Price
         _Symbol,                               //Currency Pair
         Bid-200*_Point,                         //Stop Loss
         Bid+300*_Point,                         //Take Profit
         ORDER_TIME_GTC,                        //Expiration Date
         0,                                     //Expiration Time
         NULL                                   //Comments
      );
     }
   PositionSelect(_Symbol);
   double BuyPrice=PositionGetDouble(POSITION_PRICE_OPEN);
   double sl=PositionGetDouble(POSITION_SL);
   double tp=PositionGetDouble(POSITION_TP);
   if((OrdersTotal()==0)&&(OpenBuyPositions() == 1))
     {
      trade.SellStop(
         0.01,                                 //Lot Size
         BuyPrice-50*_Point,                  //Entry Price
         _Symbol,                             //Currency Pair
         tp,                                  //Stop Loss
         sl,                                  //Take Profit
         ORDER_TIME_GTC,                      //Expiration Date
         0,                                   //Expiration Time
         NULL);
     }
   if((OrdersTotal()==0)&&(OpenBuyPositions()==2))
     {
      trade.BuyStop(
         0.01,                                   //Lot Size
         BuyPrice+50*_Point,                   //Entry Price
         _Symbol,                               //Currency Pair
         sl,                                   //Stop Loss
         tp,                                   //Take Profit
         ORDER_TIME_GTC,                        //Expiration Date
         0,                                     //Expiration Time
         NULL                                   //Comments
      );
     }
   if((OrdersTotal()==0)&&(OpenBuyPositions()==3))
     {
      trade.SellStop(
         0.01,                                   //Lot Size
         BuyPrice-50*_Point,                   //Entry Price
         _Symbol,                               //Currency Pair
         tp,                                   //Stop Loss
         sl,                                   //Take Profit
         ORDER_TIME_GTC,                        //Expiration Date
         0,                                     //Expiration Time
         NULL                                   //Comments
      );
     }
   if((OrdersTotal()==0)&&(OpenBuyPositions()==4))
     {
      trade.BuyStop(
         0.01,                                   //Lot Size
         BuyPrice+50*_Point,                   //Entry Price
         _Symbol,                               //Currency Pair
         sl,                                   //Stop Loss
         tp,                                   //Take Profit
         ORDER_TIME_GTC,                        //Expiration Date
         0,                                     //Expiration Time
         NULL                                   //Comments
      );
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void closeallpositions()
  {
   for(int i = PositionsTotal() - 1; i >= 0 ; i--)
     {
      int ticket=PositionGetTicket(i);
      trade.PositionClose(ticket);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OpenBuyPositions()
  {
   int BuyPositionNumer = 0;
   for(int a= PositionsTotal() - 1; a >= 0 ; a--)
     {
      int PositionType = PositionGetInteger(POSITION_TYPE);
      if(PositionType == POSITION_TYPE_BUY)
        {
         BuyPositionNumer = BuyPositionNumer+1;
        }
     }
   return BuyPositionNumer;
  }   
//+------------------------------------------------------------------+



                                          
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Position Properties
  • www.mql5.com
Position ticket. Unique number assigned to each newly opened position. It usually matches the ticket of an order used to open the position except when the ticket is changed as a result of service operations on the server, for example, when charging swaps with position re-opening. To find an order used to open a position, apply the...
 
Please use the button  Codeto insert the code (the first time I corrected your message)
 

Function for calculating BUY and SELL positions:

//+------------------------------------------------------------------+
//| Calculate all positions Buy and Sell                             |
//+------------------------------------------------------------------+
void CalculateAllPositions(int &count_buys,int &count_sells)
  {
   count_buys=0;
   count_sells=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.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic)
           {
            if(m_position.PositionType()==POSITION_TYPE_BUY)
               count_buys++;
            if(m_position.PositionType()==POSITION_TYPE_SELL)
               count_sells++;
           }
//---
   return;
  }
 
Vladimir Karputov:

Function for calculating BUY and SELL positions:

thanks i will try it on mql5.