Magic Number Statistics

Download script
Author: Copyright © 2019, Vladimir Karputov

This script is designed as a monitoring and reporting tool for traders who use automated systems on the MetaTrader 5 platform. Its primary purpose is to provide a real-time summary of active trading activities by categorizing and totaling "Magic Numbers"—which are unique identifiers used to distinguish between different automated trading strategies or groups of trades.

Core Functionality

The logic follows three main steps to organize and display your trading data:

1. Data Identification and Gathering The script scans your live trading account to identify every open position (active trades) and every pending order (orders waiting to be triggered). It looks specifically for the "Magic Number" assigned to these items so it can group them correctly.

2. Categorization of Trade Types Instead of just showing a total number of trades, the script breaks down the data into specific sub-categories based on the type of order. It calculates the count and total volume for:

  • Standard Buys and Sells: Currently active market positions.
  • Buy Limits & Sell Limits: Pending orders set at specific prices to be activated if the market moves to those levels.
  • Buy Stops & Sell Stops: Pending orders set to trigger once the market breaks through a certain price level.

3. Dynamic Reporting The script compiles all this gathered information into a clean, readable text summary. This summary is then displayed directly on your trading chart.

Key Features for the User

  • Real-Time Updates: The script does not refresh constantly (which can slow down your platform). It uses a "Pause Info" setting that allows you to define how often the information should be updated (in seconds).
  • Volume Tracking: Beyond just counting how many orders are open, the script sums up the total volume for each category. This helps traders understand their total exposure at a glance.
  • Magic Number Isolation: By grouping data by Magic Numbers, the tool ensures that if you are running multiple different bots or strategies simultaneously, the statistics for one will not get mixed up with the others.

In summary, this is a transparency tool. It takes complex back-end trading data and turns it into an organized on-screen dashboard, allowing you to see exactly how many "Buy," "Sell," "Limit," and "Stop" orders are active for every specific strategy you have running.

Price Data Components
Series array that contains tick volumes of each bar
Orders Execution
Checks for the total of open orders
9 Views
0 Downloads
0 Favorites
Magic Number Statistics
ÿþ//+------------------------------------------------------------------+

//|                                      Magic Number Statistics.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.000"

/*

   barabashkakvn Trading engine 3.035

*/

#include <Trade\PositionInfo.mqh>

#include <Trade\OrderInfo.mqh>

CPositionInfo  m_position;                   // trade position object

COrderInfo     m_order;                      // pending orders object

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

//| Structure orders                                                 |

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

struct STRUCT_COUNT_ORDERS

  {

   ulong             magic;            // Magic



   int               buy_count;        // Buy count

   double            buy_volume;       // Buy volume



   int               sell_count;       // Sell count

   double            sell_volume;      // Sell volume



   int               buy_limit_count;  // Buy Limit count

   double            buy_limit_volume; // Buy Limit volume



   int               sell_limit_count; // Sell Limit count

   double            sell_limit_volume;// Sell Limit volume



   int               buy_stop_count;   // Buy Stop count

   double            buy_stop_volume;  // Buy Stop volume



   int               sell_stop_count;  // Sell Stop count

   double            sell_stop_volume; // Sell Stop volume

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

   //| Constructor                                                      |

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

                     STRUCT_COUNT_ORDERS()

     {

      magic             = 0;

      buy_count         = 0;

      buy_volume        = 0.0;

      sell_count        = 0;

      sell_volume       = 0.0;

      buy_limit_count   = 0;

      buy_limit_volume  = 0.0;

      sell_limit_count  = 0;

      sell_limit_volume = 0.0;

      buy_stop_count    = 0;

      buy_stop_volume   = 0.0;

      sell_stop_count   = 0;

      sell_stop_volume  = 0.0;

     }

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

   //| Destructor                                                       |

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

                    ~STRUCT_COUNT_ORDERS()

     {

     }

  };

//--- input parameters

input ushort   InpPauseInfo   = 10; // Pause Info, in seconds

//---

datetime ExtLastPauseInfo     = 0;  // "0" -> D'1970.01.01 00:00';

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---



//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   datetime time_current=TimeCurrent();

   if(time_current-ExtLastPauseInfo>=InpPauseInfo)

     {

      STRUCT_COUNT_ORDERS orders[];

      GetInformation(orders);

      int size=ArraySize(orders);

      string text="  Magic "+"   Type  "+"   Count "+"   Volume   "+"\n";

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

        {

         text=text+

              "   "+IntegerToString(orders[i].magic)+"\n";

         text=text+"       "+

              "         Buy      "+IntegerToString(orders[i].buy_count)+

              "               "+DoubleToString(orders[i].buy_volume,2)+"\n";

         text=text+"       "+

              "         Sell        "+IntegerToString(orders[i].sell_count)+

              "               "+DoubleToString(orders[i].sell_volume,2)+"\n";



         text=text+"       "+

              "      Buy Limit   "+IntegerToString(orders[i].buy_limit_count)+

              "               "+DoubleToString(orders[i].buy_limit_volume,2)+"\n";

         text=text+"       "+

              "      Sell Limit  "+IntegerToString(orders[i].sell_limit_count)+

              "               "+DoubleToString(orders[i].sell_limit_volume,2)+"\n";



         text=text+"       "+

              "      Buy Stop    "+IntegerToString(orders[i].buy_stop_count)+

              "               "+DoubleToString(orders[i].buy_stop_volume,2)+"\n";

         text=text+"       "+

              "      Sell Stop   "+IntegerToString(orders[i].sell_stop_count)+

              "               "+DoubleToString(orders[i].sell_stop_volume,2)+"\n";

        }

      Comment(text);



      ExtLastPauseInfo=time_current;

     }

  }

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

//| Get Information                                                  |

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

void GetInformation(STRUCT_COUNT_ORDERS &orders[])

  {

   ArrayFree(orders);

//---

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

        {

         bool found=false;

         int size=ArraySize(orders);

         for(int j=0; j<size; j++)

           {

            if(orders[j].magic==m_position.Magic())

              {

               if(m_position.PositionType()==POSITION_TYPE_BUY)

                 {

                  orders[j].buy_count=orders[j].buy_count+1;

                  orders[j].buy_volume=orders[j].buy_volume+m_position.Volume();

                 }

               if(m_position.PositionType()==POSITION_TYPE_SELL)

                 {

                  orders[j].sell_count=orders[j].sell_count+1;

                  orders[j].sell_volume=orders[j].sell_volume+m_position.Volume();

                 }

               found=true;

               break;

              }

           }

         if(!found)

           {

            ArrayResize(orders,size+1,10);

            orders[size].magic=m_position.Magic();

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               orders[size].buy_count=orders[size].buy_count+1;

               orders[size].buy_volume=orders[size].buy_volume+m_position.Volume();

              }

            else

               if(m_position.PositionType()==POSITION_TYPE_SELL)

                 {

                  orders[size].sell_count=orders[size].sell_count+1;

                  orders[size].sell_volume=orders[size].sell_volume+m_position.Volume();

                 }

           }

        }

//---

   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders

      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties

        {

         bool found=false;

         int size=ArraySize(orders);

         for(int j=0; j<size; j++)

           {

            if(orders[j].magic==m_order.Magic())

              {

               if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT)

                 {

                  orders[j].buy_limit_count=orders[j].buy_limit_count+1;

                  orders[j].buy_limit_volume=orders[j].buy_limit_volume+m_order.VolumeCurrent();

                 }

               else

                  if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)

                    {

                     orders[j].sell_limit_count=orders[j].sell_limit_count+1;

                     orders[j].sell_limit_volume=orders[j].sell_limit_volume+m_order.VolumeCurrent();

                    }

                  else

                     if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)

                       {

                        orders[j].buy_stop_count=orders[j].buy_stop_count+1;

                        orders[j].buy_stop_volume=orders[j].buy_stop_volume+m_order.VolumeCurrent();

                       }

                     else

                        if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)

                          {

                           orders[j].sell_stop_count=orders[j].sell_stop_count+1;

                           orders[j].sell_stop_volume=orders[j].sell_stop_volume+m_order.VolumeCurrent();

                          }

               found=true;

               break;

              }

           }

         if(!found)

           {

            ArrayResize(orders,size+1,10);

            orders[size].magic=m_position.Magic();

            if(m_order.OrderType()==ORDER_TYPE_BUY_LIMIT)

              {

               orders[size].buy_limit_count=orders[size].buy_limit_count+1;

               orders[size].buy_limit_volume=orders[size].buy_limit_volume+m_order.VolumeCurrent();

              }

            else

               if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)

                 {

                  orders[size].sell_limit_count=orders[size].sell_limit_count+1;

                  orders[size].sell_limit_volume=orders[size].sell_limit_volume+m_order.VolumeCurrent();

                 }

               else

                  if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)

                    {

                     orders[size].buy_stop_count=orders[size].buy_stop_count+1;

                     orders[size].buy_stop_volume=orders[size].buy_stop_volume+m_order.VolumeCurrent();

                    }

                  else

                     if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)

                       {

                        orders[size].sell_stop_count=orders[size].sell_stop_count+1;

                        orders[size].sell_stop_volume=orders[size].sell_stop_volume+m_order.VolumeCurrent();

                       }

           }

        }

//---

  }

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

Comments