Opened Positions

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
Series array that contains tick volumes of each bar
0 Views
0 Downloads
0 Favorites
Opened Positions
ÿþ//+------------------------------------------------------------------+

//|                                             Opened Positions.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.001"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//---

#include <Trade\PositionInfo.mqh>

#include <Canvas\Canvas.mqh>

//---

CPositionInfo  m_position;          // CPositionInfo object

CCanvas        m_canvas;            // CCanvas object

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

//| Structure Positions                                              |

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

struct STRUCT_POSITIONS

  {

   ulong             ticket;        // position ticket

   double            volume;        // position volume

   //--- Constructor

                     STRUCT_POSITIONS()

     {

      ticket                     = 0;

      volume                     = 0.0;

     }

  };

//---

int m_width=300,m_height=240;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create canvas

   if(!m_canvas.CreateBitmapLabel("Opened Positions",0,50,m_width,m_height,COLOR_FORMAT_ARGB_NORMALIZE))

     {

      Print("Error creating canvas: ",GetLastError());

      return(-1);

     }

   m_canvas.FontSet("Courier",16);

   string name=m_canvas.ChartObjectName();

   ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);

   ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);

//--- deawing

   m_canvas.Erase(ColorToARGB(clrBlue,125));

   m_canvas.Update();

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   m_canvas.Destroy();

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//---

   int    count_buys    = 0;

   double volume_buys   = 0.0;

   string buys[5];

   int    count_sells   = 0;

   double volume_sells  = 0.0;

   string sells[5];

   STRUCT_POSITIONS Sbuys[5];

   STRUCT_POSITIONS Ssells[5];

   CalculateAllPositions(count_buys,volume_buys,buys,Sbuys,count_sells,volume_sells,sells,Ssells);

   m_canvas.Erase(ColorToARGB(clrBlue,125));

   int x=10,y=16;

   string text=StringFormat("% 12s","  | ");

//--- BUY's

   m_canvas.TextOut(x,y,StringFormat("%-12s","BUY's")+" | "+StringFormat("%-3s",IntegerToString(count_buys))+" | "+DoubleToString(volume_buys,2),ColorToARGB(clrBlue,255));

   y+=14;

   m_canvas.TextOut(x,y,"------------------------------",ColorToARGB(clrBlue,255));

   y+=14;

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

     {

      text=StringFormat("% 12s","  | ");

      m_canvas.TextOut(x,y,StringFormat("%-12s"," #"+IntegerToString(Sbuys[i].ticket))+" |     | "+DoubleToString(Sbuys[i].volume,2),ColorToARGB(clrBlue,255));

      y+=14;

     }

   y+=14;

//--- SELL's

   m_canvas.TextOut(x,y,StringFormat("%-12s","SELL's")+" | "+StringFormat("%-3s",IntegerToString(count_sells))+" | "+DoubleToString(volume_sells,2),ColorToARGB(clrRed,255));

   y+=14;

   m_canvas.TextOut(x,y,"------------------------------",ColorToARGB(clrRed,255));

   y+=14;

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

     {

      text=StringFormat("% 12s","  | ");

      m_canvas.TextOut(x,y,StringFormat("%-12s"," #"+IntegerToString(Ssells[i].ticket))+" |     | "+DoubleToString(Ssells[i].volume,2),ColorToARGB(clrRed,255));

      y+=14;

     }

   m_canvas.Update();

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| Calculate all positions Buy and Sell                             |

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

void CalculateAllPositions(int &count_buys,double &volume_buys,string &buys[],STRUCT_POSITIONS &Sbuys[],

                           int &count_sells,double &volume_sells,string &sells[],STRUCT_POSITIONS &Ssells[])

  {

   count_buys        = 0;

   volume_buys       = 0.0;

   count_sells       = 0;

   volume_sells      = 0.0;

   int counter_buys  = 0;

   int counter_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()==Symbol())

           {

            if(m_position.PositionType()==POSITION_TYPE_BUY)

              {

               count_buys++;

               volume_buys+=m_position.Volume();

               if(counter_buys<5)

                 {

                  Sbuys[counter_buys].ticket=m_position.Ticket();

                  Sbuys[counter_buys].volume=m_position.Volume();

                 }

               counter_buys++;

              }

            if(m_position.PositionType()==POSITION_TYPE_SELL)

              {

               count_sells++;

               volume_sells+=m_position.Volume();

               if(counter_sells<5)

                 {

                  Ssells[counter_sells].ticket=m_position.Ticket();

                  Ssells[counter_sells].volume=m_position.Volume();

                 }

               counter_sells++;

              }

           }

//---

   return;

  }

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

Comments