Daily Trading Statistics Indicator 2

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Daily Trading Statistics Indicator 2
ÿþ//+------------------------------------------------------------------+

//|                         Daily Trading Statistics Indicator 2.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "2.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "Label Trades"

input string               InpTradesName        = "Trades";       // Label Trades: Name

input int                  InpTradesYShift      = 30;             // Label Trades: Y Shift

input string               InpTradesFont        = "Tahoma";       // Label Trades: Font

input int                  InpTradesFontSize    = 14;             // Label Trades: Font size

input color                InpTradesColor       = clrPink;        // Label Trades: Color

input double               InpTradesAngle       = 0.0;            // Label Trades: Slope angle in degrees

input ENUM_ANCHOR_POINT    InpTradesAnchor      = ANCHOR_LEFT_UPPER; // Label Trades: Anchor type

input bool                 InpTradesBack        = false;          // Label Trades: Background object

input bool                 InpTradesSelection   = false;          // Label Trades: Highlight to move

input bool                 InpTradesHidden      = true;           // Label Trades: Hidden in the object list

input long                 InpTradesZOrder      = 0;              // Label Trades: Priority for mouse click

input group             "Label Wining"

input string               InpWiningName        = "Wining";       // Label Wining: Name

input int                  InpWiningYShift      = 30;             // Label Wining: Y Shift

input string               InpWiningFont        = "Tahoma";       // Label Wining: Font

input int                  InpWiningFontSize    = 14;             // Label Wining: Font size

input color                InpWiningColor       = clrRed;         // Label Wining: Color

input double               InpWiningAngle       = 0.0;            // Label Wining: Slope angle in degrees

input ENUM_ANCHOR_POINT    InpWiningAnchor      = ANCHOR_LEFT_UPPER; // Label Wining: Anchor type

input bool                 InpWiningBack        = false;          // Label Wining: Background object

input bool                 InpWiningSelection   = false;          // Label Wining: Highlight to move

input bool                 InpWiningHidden      = true;           // Label Wining: Hidden in the object list

input long                 InpWiningZOrder      = 0;              // Label Wining: Priority for mouse click

input group             "Label Losing"

input string               InpLosingName        = "Losing";       // Label Losing: Name

input int                  InpLosingYShift      = 30;             // Label Losing: Y Shift

input string               InpLosingFont        = "Tahoma";       // Label Losing: Font

input int                  InpLosingFontSize    = 14;             // Label Losing: Font size

input color                InpLosingColor       = clrGreen;       // Label Losing: Color

input double               InpLosingAngle       = 0.0;            // Label Losing: Slope angle in degrees

input ENUM_ANCHOR_POINT    InpLosingAnchor      = ANCHOR_LEFT_UPPER; // Label Losing: Anchor type

input bool                 InpLosingBack        = false;          // Label Losing: Background object

input bool                 InpLosingSelection   = false;          // Label Losing: Highlight to move

input bool                 InpLosingHidden      = true;           // Label Losing: Hidden in the object list

input long                 InpLosingZOrder      = 0;              // Label Losing: Priority for mouse click

input group             "Label Total"

input string               InpTotalName         = "Total";        // Label Total: Name

input int                  InpTotalYShift       = 30;             // Label Total: Y Shift

input string               InpTotalFont         = "Tahoma";       // Label Total: Font

input int                  InpTotalFontSize     = 14;             // Label Total: Font size

input color                InpTotalColor        = clrBlue;        // Label Total: Color

input double               InpTotalAngle        = 0.0;            // Label Total: Slope angle in degrees

input ENUM_ANCHOR_POINT    InpTotalAnchor       = ANCHOR_LEFT_UPPER; // Label Total: Anchor type

input bool                 InpTotalBack         = false;          // Label Total: Background object

input bool                 InpTotalSelection    = false;          // Label Total: Highlight to move

input bool                 InpTotalHidden       = true;           // Label Total: Hidden in the object list

input long                 InpTotalZOrder       = 0;              // Label Losing: Priority for mouse click

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   LabelDelete(0,InpTradesName);

   LabelDelete(0,InpWiningName);

   LabelDelete(0,InpLosingName);

   LabelDelete(0,InpTotalName);

  }

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

//| 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[])

  {

//--- refresh statistics

   RefreshStatistics();

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

   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

   if(id==CHARTEVENT_CHART_CHANGE)

     {

      if(ChartGetInteger(ChartID(),CHART_SHOW_ONE_CLICK,0))

        {

         int y=50+InpTradesYShift;

         long chart_id=ChartID();

         LabelMove(chart_id,InpTradesName,10,y/*+50*/);

         y+=InpWiningYShift;

         LabelMove(chart_id,InpWiningName,10,y/*+50*/);

         y+=InpLosingYShift;

         LabelMove(chart_id,InpLosingName,10,y/*+50*/);

         y+=InpTotalYShift;

         LabelMove(chart_id,InpTotalName,10,y/*+50*/);

        }

      else

        {

         int y=0+InpTradesYShift;

         long chart_id=ChartID();

         LabelMove(chart_id,InpTradesName,10,y);

         y+=InpWiningYShift;

         LabelMove(chart_id,InpWiningName,10,y);

         y+=InpLosingYShift;

         LabelMove(chart_id,InpLosingName,10,y);

         y+=InpTotalYShift;

         LabelMove(chart_id,InpTotalName,10,y);

        }

      ChartRedraw(0);

     }

  }

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

//| Refresh Statistics                                               |

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

bool RefreshStatistics()

  {

   /*

   Trades of day: xxx

   Wining trade:  U$ xxx.xx

   Losing trade:  U$ xxx.xx

   Total profit:  U$ xxx.xx

   */

//--- request trade history

   MqlDateTime SDateTime;

   TimeToStruct(TimeCurrent(),SDateTime);

   SDateTime.hour=0;

   SDateTime.min=0;

   SDateTime.sec=0;

   datetime  from_date=StructToTime(SDateTime);     // From date

   SDateTime.hour=23;

   SDateTime.min=59;

   SDateTime.sec=59;

   datetime  to_date=StructToTime(SDateTime);     // To date

   to_date+=60*60*24;

   HistorySelect(from_date,to_date);

   int trades_of_day=0;

   double wining_trade=0.0;

   double losing_trade=0.0;

   double total_profit=0.0;

   uint total=HistoryDealsTotal();

   ulong    ticket=0;

//--- for all deals

   for(uint i=0; i<total; i++)

     {

      //--- try to get deals ticket

      if((ticket=HistoryDealGetTicket(i))>0)

        {

         //--- get deals properties

         trades_of_day++;

         double deal_commission=HistoryDealGetDouble(ticket,DEAL_COMMISSION);

         double deal_swap=HistoryDealGetDouble(ticket,DEAL_SWAP);

         double deal_profit=HistoryDealGetDouble(ticket,DEAL_PROFIT);

         double profit=deal_commission+deal_swap+deal_profit;

         if(profit>0.0)

            wining_trade+=profit;

         if(profit<0.0)

            losing_trade+=profit;

         total_profit+=profit;

        }

     }

//---

   int y=0+InpTradesYShift;

   if(ChartGetInteger(ChartID(),CHART_SHOW_ONE_CLICK,0))

      y+=50;

   string text="Trades of day: "+IntegerToString(trades_of_day);

//--- Trades of day

   if(ObjectFind(0,InpTradesName)<0)

     {

      if(!LabelCreate(0,InpTradesName,0,10,y,CORNER_LEFT_UPPER,text,

                      InpTradesFont,InpTradesFontSize,InpTradesColor,InpTradesAngle,InpTradesAnchor,InpTradesBack,InpTradesSelection,

                      InpTradesHidden,InpTradesZOrder))

        {

         return(false);

        }

     }

   else

     {

      if(!LabelTextChange(0,InpTradesName,text))

         return(false);

     }

//---

   string currency=AccountInfoString(ACCOUNT_CURRENCY);

   y+=InpWiningYShift;

   text="Wining trade: "+currency+" "+DoubleToString(wining_trade,2);

//--- Wining trade

   if(ObjectFind(0,InpWiningName)<0)

     {

      if(!LabelCreate(0,InpWiningName,0,10,y,CORNER_LEFT_UPPER,text,

                      InpWiningFont,InpWiningFontSize,InpWiningColor,InpWiningAngle,InpWiningAnchor,InpWiningBack,InpWiningSelection,

                      InpWiningHidden,InpWiningZOrder))

        {

         return(false);

        }

     }

   else

     {

      if(!LabelTextChange(0,InpWiningName,text))

         return(false);

     }

//---

   y+=InpLosingYShift;

   text="Losing trade: "+currency+" "+DoubleToString(losing_trade,2);

//--- Losing trade

   if(ObjectFind(0,InpLosingName)<0)

     {

      if(!LabelCreate(0,InpLosingName,0,10,y,CORNER_LEFT_UPPER,text,

                      InpLosingFont,InpLosingFontSize,InpLosingColor,InpLosingAngle,InpLosingAnchor,InpLosingBack,InpLosingSelection,

                      InpLosingHidden,InpLosingZOrder))

        {

         return(false);

        }

     }

   else

     {

      if(!LabelTextChange(0,InpLosingName,text))

         return(false);

     }

//---

   y+=InpTotalYShift;

   text="Total profit: "+currency+" "+DoubleToString(total_profit,2);

//--- Total profit

   if(ObjectFind(0,InpTotalName)<0)

     {

      if(!LabelCreate(0,InpTotalName,0,10,y,CORNER_LEFT_UPPER,text,

                      InpTotalFont,InpTotalFontSize,InpTotalColor,InpTotalAngle,InpTotalAnchor,InpTotalBack,InpTotalSelection,

                      InpTotalHidden,InpTotalZOrder))

        {

         return(false);

        }

     }

   else

     {

      if(!LabelTextChange(0,InpTotalName,text))

         return(false);

     }

//---

   return(true);

  }

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

//| Create a text label                                              |

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

bool LabelCreate(const long              chart_ID=0,               // chart's ID

                 const string            name="Label",             // label name

                 const int               sub_window=0,             // subwindow index

                 const int               x=0,                      // X coordinate

                 const int               y=0,                      // Y coordinate

                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring

                 const string            text="Label",             // text

                 const string            font="Tahoma",             // font

                 const int               font_size=10,             // font size

                 const color             clr=clrRed,               // color

                 const double            angle=0.0,                // text slope

                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type

                 const bool              back=false,               // in the background

                 const bool              selection=false,          // highlight to move

                 const bool              hidden=true,              // hidden in the object list

                 const long              z_order=0)                // priority for mouse click

  {

//--- reset the error value

   ResetLastError();

//--- create a text label

   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create text label! Error code = ",GetLastError());

      return(false);

     }

//--- set label coordinates

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set text font

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- set font size

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- set the slope angle of the text

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the label by mouse

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution

   return(true);

  }

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

//| Move the text label                                              |

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

bool LabelMove(const long   chart_ID=0,   // chart's ID

               const string name="Label", // label name

               const int    x=0,          // X coordinate

               const int    y=0)          // Y coordinate

  {

//--- reset the error value

   ResetLastError();

//--- move the text label

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of the label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Change the label text                                            |

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

bool LabelTextChange(const long   chart_ID=0,   // chart's ID

                     const string name="Label", // object name

                     const string text="Text")  // text

  {

//--- reset the error value

   ResetLastError();

//--- change object text

   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))

     {

      Print(__FUNCTION__,

            ": failed to change the text! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Delete a text label                                              |

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

bool LabelDelete(const long   chart_ID=0,   // chart's ID

                 const string name="Label") // label name

  {

//--- reset the error value

   ResetLastError();

//--- delete the label

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a text label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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



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

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---