Daily Trading Statistics

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

//|                                     Daily Trading Statistics.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.000"

/*

   barabashkakvn Trading engine 3.120

*/

//--- input parameters

input ushort            InpRefreshFrequency  = 10;             // Refresh statistics, in seconds (< "10" -> only on a new bar)

//--- Labels

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 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 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 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

//---

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

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

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---



//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   LabelDelete(0,InpTradesName);

   LabelDelete(0,InpWiningName);

   LabelDelete(0,InpLosingName);

   LabelDelete(0,InpTotalName);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(InpRefreshFrequency>=10) // refresh statistics no more than once every 10 seconds

     {

      datetime time_current=TimeCurrent();

      if(time_current-m_last_refresh>10)

        {

         //--- refresh statistics

         if(!RefreshStatistics())

           {

            m_prev_bars=0;

            return;

           }

         m_last_refresh=time_current;

        }

     }

//--- we work only at the time of the birth of new bar

   datetime time_0=iTime(Symbol(),Period(),0);

   if(time_0==m_prev_bars)

      return;

   m_prev_bars=time_0;

   if(InpRefreshFrequency<10) // refresh statistics only at the time of the birth of new bar

     {

      //--- refresh statistics

      if(!RefreshStatistics())

        {

         m_prev_bars=0;

         return;

        }

     }

  }

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

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

        {

         long entry=HistoryDealGetInteger(ticket,DEAL_ENTRY);

         if(entry==DEAL_ENTRY_IN)

            continue;

         //--- 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;

   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);

  }

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

//| 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 ---