Translate to MT5

 

Hi is there someone who can translate this MQL4 code into MQL5 ?


//+------------------------------------------------------------------+

//| Calculate open positions                                         |

//+------------------------------------------------------------------+

int CalculateCurrentOrders(string symbol)

  {

  //Comment("Situazione: " + "Cazzo!");

   int buys=0,sells=0;

//---

   for(int i=0;i<OrdersTotal();i++)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) 

        {

        break;

        }

      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)

        {

         if(OrderType()==OP_BUY)  buys++;

         if(OrderType()==OP_SELL) sells++;

        }

     }

//--- return orders volume

   if(buys>0) return(buys);

   else       return(-sells);

  }

//+------------------------------------------------------------------+

Thank you
 
Alessandro Furlani: Hi is there someone who can translate this MQL4 code into MQL5 ?
  1. Yes.
              Only ask questions with yes/no answers if you want “yes” or “no” as the answer.

  2. MT4: orders are everything, MT5: orders are pending only. Opened orders are deals.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

    Show us your attempt (using the CODE button) and state the nature of your difficulty.
              No free help (2017)

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2018)

    We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
              No free help (2017)

 
Alessandro Furlani:

Hi is there someone who can translate this MQL4 code into MQL5 ?


Thank you

Why have a parametrized function (symbol as arguments) if it would not be used.

//+------------------------------------------------------------------+
//|    Calculate open positions.                                     |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,
       sells = 0;
   ulong ticket = 0;
   for(int i=0; i<PositionsTotal(); i++)
     {
      ticket = PositionGetTicket(i);
      if(ticket>0)
        {
         if(PositionSelectByTicket(ticket))
           {
            string sym = PositionGetSymbol(POSITION_SYMBOL);
            int mag_no = PositionGetInteger(POSITION_MAGIC);
            ENUM_POSITION_TYPE type = PositionGetInteger(POSITION_TYPE);
            if(sym==Symbol()&& mag_no==MAGICMA)
              {
               if(type == POSITION_TYPE_BUY)
                  buys++;
               else
                  sells++;
              }
           }
        }
     }
   return buys;
  }

then I returned only the buys,  you can copy your existing logic and input it there, but the logic is not too clear, why count sells and buys only to return one.

Look up CPosition class it can be used to reduce the above, when you understand it.

Reason: