Time left 2

Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Time left 2
ÿþ//+------------------------------------------------------------------+

//|                                                  Time left 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             "Global"

input bool                 InpTimeTradeServer   = true;              // Show Time TradeServer

input group             "Label"

input string               InpName              = "Time left";       // Label name

input int                  InpX                 = 15;                // X-axis distance

input int                  InpY                 = 15;                // Y-axis distance

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

input string               InpFont              = "Calibri";         // Font

input int                  InpFontSize          = 14;                // Font size

input color                InpColor             = clrDeepSkyBlue;    // Color

input double               InpAngle             = 0.0;               // Slope angle in degrees

input ENUM_ANCHOR_POINT    InpAnchor            = ANCHOR_RIGHT_LOWER;// Anchor type

input bool                 InpBack              = false;             // Background object

input bool                 InpSelection         = false;             // Highlight to move

input bool                 InpHidden            = true;              // Hidden in the object list

input long                 InpZOrder            = 0;                 // Priority for mouse click

//---

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

bool     m_init_error               = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create a Millisecond Timer

   EventSetMillisecondTimer(100);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   ObjectDelete(ChartID(),InpName);

  }

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

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

  {

   if(m_init_error)

      return(0);

   if(m_prev_bars==D'1970.01.01 00:00')

     {

      //--- store the label's coordinates in the local variables

      int x=InpX;

      int y=InpY;

      //--- chart window size

      long x_distance;

      long y_distance;

      //--- set window size

      if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))

        {

         Print("Failed to get the chart width! Error code = ",GetLastError());

         m_prev_bars=0;

         return(0);

        }

      if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))

        {

         Print("Failed to get the chart height! Error code = ",GetLastError());

         m_prev_bars=0;

         return(0);

        }

      //--- check correctness of the input parameters

      if(InpX<0 || InpX>x_distance-1 || InpY<0 || InpY>y_distance-1)

        {

         Print("Error! Incorrect values of input parameters!");

         m_prev_bars=0;

         m_init_error=true;

         return(0);

        }

      //--- create a text label on the chart

      if(!LabelCreate(0,InpName,0,InpX,InpY,InpCorner,"---",InpFont,InpFontSize,

                      InpColor,InpAngle,InpAnchor,InpBack,InpSelection,InpHidden,InpZOrder))

        {

         m_prev_bars=0;

         return(0);

        }

     }

//---

   datetime time_0=time[rates_total-1];

   if(time_0==m_prev_bars)

      return(rates_total);

   m_prev_bars=time_0;

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

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   datetime time_trade_server=TimeTradeServer();

   string time_curr="";

   if(InpTimeTradeServer)

      time_curr=TimeToString(time_trade_server,TIME_DATE|TIME_SECONDS);

   string left=IntegerToString(m_prev_bars+PeriodSeconds(PERIOD_CURRENT)-time_trade_server);

   LabelTextChange(ChartID(),InpName,time_curr+" "+left+" sec");

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//---

  }

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

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

     }

   ChartRedraw(0);

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