Magic Number Statistics

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
Orders Execution
Checks for the total of open orders
0 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