Profit for the last week

Author: Copyright © 2022, Vladimir Karputov
2 Views
0 Downloads
0 Favorites
Profit for the last week
ÿþ//+------------------------------------------------------------------+

//|                                     Profit for the last week.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

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

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

#property copyright "Copyright © 2022, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers   0

#property indicator_plots     0

//--- input parameters

input group             "Common parameters"

input int                  InpX                 = 50;                               // X coordinate

input int                  InpY                 = 50;                               // Y coordinate

input int                  InpYStep             = 12;                               // Y step

input ENUM_BASE_CORNER     InpCorner            = CORNER_LEFT_UPPER;                // Chart corner for anchoring

input string               InpFont              = "Consolas";                       // Font Name

input int                  InpFontSize          = 10;                               // Font size

input ENUM_ANCHOR_POINT    InpAnchor            = ANCHOR_LEFT;                      // Anchor type

//---

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

string   m_prefix    = "Balance for the last week ";  // prefix

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---

   uint  width;   // buffer width in pixels

   uint height;   // buffer height in pixels

   if(!TextGetSize("Common",width,height))

      return(INIT_FAILED);

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

     {

      uint step=(i==0)?0:(height+InpYStep)*i;

      LabelCreate(ChartID(),m_prefix+IntegerToString(i),0,InpX,InpY+step,InpCorner,"- - -",InpFont,InpFontSize,clrGray,0.0,InpAnchor);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//--- reset the error value

   ResetLastError();

//--- delete the labels

   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_LABEL);

  }

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

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

  {

//---

   MqlRates rates_d1[];

   ArraySetAsSeries(rates_d1,true); // rates_d1[count-1].time -> D'2022.03.02 00:00:00', rates_d1[0].time -> D'2022.03.11 00:00:00'

   int start_pos=0,count=5;

   if(CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_d1)!=count)

      return(0);

   struct SYMBOLS_PROFIT

     {

      double         profit;        // profit

      //--- Constructor

                     SYMBOLS_PROFIT()

        {

         profit                     = 0.0;

        }

     };

   SYMBOLS_PROFIT SSymbolsProfit[];

   ArrayResize(SSymbolsProfit,count);

//ArraySetAsSeries(SSymbolsProfit_temp,false);

//ArraySetAsSeries(SSymbolsProfit_main,false);

//--- request trade history

   datetime to_date=rates_d1[0].time+PeriodSeconds(PERIOD_D1); // to date

   HistorySelect(rates_d1[count-1].time,to_date);

   uint total_deals= HistoryDealsTotal();

   ulong ticket_history_deal=0;

//--- for all deals

   for(uint i=0; i<total_deals; i++) // for(uint i=0;i<total_deals;i++) => i #0 - 2016, i #1045 - 2017

     {

      //--- try to get deals ticket_history_deal

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

        {

         long     deal_ticket       = HistoryDealGetInteger(ticket_history_deal,DEAL_TICKET);

         long     deal_time         = HistoryDealGetInteger(ticket_history_deal,DEAL_TIME);

         long     deal_type         = HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);

         long     deal_entry        = HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);

         long     deal_reason       = HistoryDealGetInteger(ticket_history_deal,DEAL_REASON);

         double   deal_commission   = HistoryDealGetDouble(ticket_history_deal,DEAL_COMMISSION);

         double   deal_swap         = HistoryDealGetDouble(ticket_history_deal,DEAL_SWAP);

         double   deal_profit       = HistoryDealGetDouble(ticket_history_deal,DEAL_PROFIT);

         string   deal_symbol       = HistoryDealGetString(ticket_history_deal,DEAL_SYMBOL);

         //---

         if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)

           {

            double profit=deal_commission+deal_swap+deal_profit;

            if(profit!=0.0)

              {

               //--- loop on 'rates_d1' (looking for time)

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

                 {

                  datetime dt_deal_time=(datetime)deal_time;

                  if(deal_time>=rates_d1[j].time)

                    {

                     SSymbolsProfit[j].profit=SSymbolsProfit[j].profit+profit;

                     break;

                    }

                 }

              }

           }

        }

     }

   if(ArraySize(SSymbolsProfit)>0)

     {

      if(TimeCurrent()-m_last_time<3)

         return(rates_total);

      m_last_time=TimeCurrent();

      long chart_id=ChartID();

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

        {

         LabelTextChange(chart_id,m_prefix+IntegerToString(i),TimeToString(rates_d1[i].time,TIME_DATE|TIME_MINUTES),SSymbolsProfit[i].profit);

        }

     }

//---

   return(rates_total);

  }

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

//| 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="Arial",              // 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_CENTER,      // 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

                     const double profit=0.0)      // profit

  {

//--- reset the error value

   ResetLastError();

//--- change object text

   color clr=clrGray;

   if(profit<0.0)

      clr=clrRed;

   if(profit>0.0)

      clr=clrBlue;

//--- change the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text+" "+DoubleToString(profit,2));

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

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