problem in my function "Oreders Total By MAGIC"

 

Hello,

I have made this function to count the pending orders in the account

it always return 0

int OredersTotalByMAGIC(int The_MAGIC)
{
    int Counter = 0;
    for(int i=OrdersTotal()-1; i>=0; i--)
    {
      ulong ticket=OrderGetTicket(i);
      if(OrderGetInteger(ORDER_MAGIC) == The_MAGIC)
      {
         Counter++;
      }
    }
  return Counter;  
}

i hope someone can help to know why it not work !


thanks,

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

The number of pending orders (only system functions, no OOP).

CalculateAllPendingOrdersSystem function

//+------------------------------------------------------------------+
//| Calculate all pending orders for symbol                          |
//+------------------------------------------------------------------+
int CalculateAllPendingOrdersSystem(const string symbol,const ulong magic)
  {
   int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
     {
      ulong order_ticket=OrderGetTicket(i); // selects the pending order by index for further access to its properties
      if(order_ticket==0)
         continue;
      string order_symbol=OrderGetString(ORDER_SYMBOL); // get symbol
      long order_magic=OrderGetInteger(ORDER_MAGIC); // get magic
      //---
      if(order_symbol==symbol && order_magic==magic)
         total++;
     }
//---
   return(total);
  }

There are two pending orders with Magic '200' and one with Magic '100' in the market - only three pending orders (all three pending orders for the 'EURAUD' symbol).

Run the script:

//+------------------------------------------------------------------+
//|                                                     Script 1.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
#property script_show_inputs
//--- input parameters
input string   InpSymbol   = "EURUSD"; // Symbol
input ulong    InpMagic    = 200;      // Magic number
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int total=CalculateAllPendingOrdersSystem(InpSymbol,InpMagic);
   PrintFormat("Symbol %s, Magic %d, Pending Orders %d:",InpSymbol,InpMagic,total);
  }
//+------------------------------------------------------------------+
//| Calculate all pending orders for symbol                          |
//+------------------------------------------------------------------+
int CalculateAllPendingOrdersSystem(const string symbol,const ulong magic)
  {
   int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
     {
      ulong order_ticket=OrderGetTicket(i); // selects the pending order by index for further access to its properties
      if(order_ticket==0)
         continue;
      string order_symbol=OrderGetString(ORDER_SYMBOL); // get symbol
      long order_magic=OrderGetInteger(ORDER_MAGIC); // get magic
      //---
      if(order_symbol==symbol && order_magic==magic)
         total++;
     }
//---
   return(total);
  }
//+------------------------------------------------------------------+

We get the result:

2021.08.24 08:34:20.871 Script 1 (EURAUD,H1)    Symbol EURUSD, Magic 200, Pending Orders 0:
2021.08.24 08:34:29.510 Script 1 (EURAUD,H1)    Symbol EURAUD, Magic 200, Pending Orders 2:
2021.08.24 08:34:38.224 Script 1 (EURAUD,H1)    Symbol EURAUD, Magic 100, Pending Orders 1:
Files:
Script_1.mq5  4 kb