Name Chart 2

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Name Chart 2
ÿþ//+------------------------------------------------------------------+

//|                                                 Name Chart 2.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   "2.004"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//--- input parameters

input string            InpNameChart      = "Label Chart";     // Label chart name

input string            InpNameTimeframe  = "Label Timeframe"; // Label timeframe name

input string            InpFont           = "Arial";           // Font

input int               InpFontSize       = 20;                // Font size

input color             InpColor          = clrGray;           // Color

input ENUM_ANCHOR_POINT InpAnchor         = ANCHOR_RIGHT_UPPER;// Anchor type

input bool              InpBack           = true;              // 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

//---

double                  m_angle     = 0.0;               // Slope angle in degrees

ENUM_BASE_CORNER        m_corner    = CORNER_LEFT_UPPER; // Chart corner for anchoring

int                     m_correct_x = 0;

int                     m_correct_y = 0;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

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

      return(INIT_SUCCEEDED);

     }

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

     {

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

      return(INIT_SUCCEEDED);

     }

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

   string text=Symbol();

   if(!LabelCreate(0,InpNameChart,0,0,0,m_corner,text,InpFont,InpFontSize,

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

     {

      Print("Failed to create label! Error code = ",GetLastError());

      return(INIT_SUCCEEDED);

     }

   text=Descriptions(Period());

   if(!LabelCreate(0,InpNameTimeframe,0,0,0,m_corner,text,InpFont,InpFontSize,

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

     {

      Print("Failed to create label! Error code = ",GetLastError());

      return(INIT_SUCCEEDED);

     }

   ChartRedraw(0);

//--- calculation of X and Y coordinates

   int ysize_chart=(int)ObjectGetInteger(0,InpNameChart,OBJPROP_YSIZE);

   int ysize_timeframe=(int)ObjectGetInteger(0,InpNameTimeframe,OBJPROP_YSIZE);

   int counter=0;

   while((ysize_chart==0 || ysize_timeframe==0) && !IsStopped() && counter<9)

     {

      ChartRedraw(0);

      ysize_chart=(int)ObjectGetInteger(0,InpNameChart,OBJPROP_YSIZE);

      ysize_timeframe=(int)ObjectGetInteger(0,InpNameTimeframe,OBJPROP_YSIZE);

      counter++;

     }

   if(ysize_chart==0 || ysize_timeframe==0)

      return(INIT_SUCCEEDED);

   int x_chart=0,y_chart=0,x_timeframe=0,y_timeframe=0;

   switch(InpAnchor)

     {

      case  ANCHOR_LEFT_UPPER:

         m_corner=CORNER_LEFT_UPPER;

         x_chart=10;

         y_chart=10;

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      case  ANCHOR_LEFT:

         m_corner=CORNER_LEFT_UPPER;

         x_chart=10;

         y_chart=(int)((y_distance/2)-ysize_chart/2);

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      case  ANCHOR_LEFT_LOWER:

         m_corner=CORNER_LEFT_LOWER;

         x_timeframe=10;

         y_timeframe=10;

         x_chart=x_timeframe;

         y_chart=y_timeframe+ysize_timeframe;

         break;

      case  ANCHOR_LOWER:

         m_corner=CORNER_LEFT_LOWER;

         x_timeframe=(int)(x_distance/2);

         y_timeframe=10;

         x_chart=x_timeframe;

         y_chart=y_timeframe+ysize_timeframe;

         break;

      case  ANCHOR_RIGHT_LOWER:

         m_corner=CORNER_RIGHT_LOWER;

         x_timeframe=10;

         y_timeframe=10;

         x_chart=x_timeframe;

         y_chart=y_timeframe+ysize_timeframe;

         break;

      case  ANCHOR_RIGHT:

         m_corner=CORNER_RIGHT_LOWER;

         x_chart=10;

         y_chart=(int)((y_distance/2)+ysize_chart/2);

         x_timeframe=x_chart;

         y_timeframe=y_chart-ysize_chart;

         break;

      case  ANCHOR_RIGHT_UPPER:

         m_corner=CORNER_RIGHT_UPPER;

         x_chart=10;

         y_chart=10;

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      case  ANCHOR_UPPER:

         m_corner=CORNER_RIGHT_UPPER;

         x_chart=(int)(x_distance/2);

         y_chart=10;

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      default: // ANCHOR_CENTER

         m_corner=CORNER_LEFT_LOWER;

         x_chart=(int)(x_distance/2);

         y_chart=(int)((y_distance/2)+ysize_chart/2);

         x_timeframe=x_chart;

         y_timeframe=y_chart-ysize_chart;

         break;

     }

   LabelMove(0,InpNameChart,x_chart,y_chart,m_corner,InpAnchor);

   LabelMove(0,InpNameTimeframe,x_timeframe,y_timeframe,m_corner,InpAnchor);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   LabelDelete(0,InpNameChart);

   LabelDelete(0,InpNameTimeframe);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

//---



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

      return;

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

      return;

     }

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

     {

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

      return;

     }

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

   string text=Symbol();

   if(ObjectFind(0,InpNameChart)<0)

      if(!LabelCreate(0,InpNameChart,0,0,0,m_corner,text,InpFont,InpFontSize,

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

        {

         Print("Failed to create label! Error code = ",GetLastError());

         return;

        }

   text=Descriptions(Period());

   if(ObjectFind(0,InpNameTimeframe)<0)

      if(!LabelCreate(0,InpNameTimeframe,0,0,0,m_corner,text,InpFont,InpFontSize,

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

        {

         Print("Failed to create label! Error code = ",GetLastError());

         return;

        }

   ChartRedraw(0);

//--- calculation of X and Y coordinates

   int ysize_chart=(int)ObjectGetInteger(0,InpNameChart,OBJPROP_YSIZE);

   int ysize_timeframe=(int)ObjectGetInteger(0,InpNameTimeframe,OBJPROP_YSIZE);

   int counter=0;

   while((ysize_chart==0 || ysize_timeframe==0) && !IsStopped() && counter<9)

     {

      ChartRedraw(0);

      ysize_chart=(int)ObjectGetInteger(0,InpNameChart,OBJPROP_YSIZE);

      ysize_timeframe=(int)ObjectGetInteger(0,InpNameTimeframe,OBJPROP_YSIZE);

      counter++;

     }

   if(ysize_chart==0 || ysize_timeframe==0)

      return;

   int x_chart=0,y_chart=0,x_timeframe=0,y_timeframe=0;

   switch(InpAnchor)

     {

      case  ANCHOR_LEFT_UPPER:

         m_corner=CORNER_LEFT_UPPER;

         x_chart=10;

         y_chart=10;

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      case  ANCHOR_LEFT:

         m_corner=CORNER_LEFT_UPPER;

         x_chart=10;

         y_chart=(int)((y_distance/2)-ysize_chart/2);

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      case  ANCHOR_LEFT_LOWER:

         m_corner=CORNER_LEFT_LOWER;

         x_timeframe=10;

         y_timeframe=10;

         x_chart=x_timeframe;

         y_chart=y_timeframe+ysize_timeframe;

         break;

      case  ANCHOR_LOWER:

         m_corner=CORNER_LEFT_LOWER;

         x_timeframe=(int)(x_distance/2);

         y_timeframe=10;

         x_chart=x_timeframe;

         y_chart=y_timeframe+ysize_timeframe;

         break;

      case  ANCHOR_RIGHT_LOWER:

         m_corner=CORNER_RIGHT_LOWER;

         x_timeframe=10;

         y_timeframe=10;

         x_chart=x_timeframe;

         y_chart=y_timeframe+ysize_timeframe;

         break;

      case  ANCHOR_RIGHT:

         m_corner=CORNER_RIGHT_LOWER;

         x_chart=10;

         y_chart=(int)((y_distance/2)+ysize_chart/2);

         x_timeframe=x_chart;

         y_timeframe=y_chart-ysize_chart;

         break;

      case  ANCHOR_RIGHT_UPPER:

         m_corner=CORNER_RIGHT_UPPER;

         x_chart=10;

         y_chart=10;

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      case  ANCHOR_UPPER:

         m_corner=CORNER_RIGHT_UPPER;

         x_chart=(int)(x_distance/2);

         y_chart=10;

         x_timeframe=x_chart;

         y_timeframe=y_chart+ysize_chart;

         break;

      default: // ANCHOR_CENTER

         m_corner=CORNER_LEFT_LOWER;

         x_chart=(int)(x_distance/2);

         y_chart=(int)((y_distance/2)+ysize_chart/2);

         x_timeframe=x_chart;

         y_timeframe=y_chart-ysize_chart;

         break;

     }

   LabelMove(0,InpNameChart,x_chart,y_chart,m_corner,InpAnchor);

   LabelMove(0,InpNameTimeframe,x_timeframe,y_timeframe,m_corner,InpAnchor);

//---

  }

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

//| 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=clrGray,              // 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

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

               const ENUM_ANCHOR_POINT anchor   = ANCHOR_LEFT_UPPER) // anchor type

  {

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

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

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

  }

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

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

  }

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

//| Descriptions                                                     |

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

string Descriptions(const ENUM_TIMEFRAMES timeframe)

  {

   string result="NONE";

//---

   ENUM_TIMEFRAMES timeframes_array[21]=

     {

      PERIOD_M1,PERIOD_M2,PERIOD_M3,

      PERIOD_M4,PERIOD_M5,PERIOD_M6,

      PERIOD_M10,PERIOD_M12,PERIOD_M15,

      PERIOD_M20,PERIOD_M30,PERIOD_H1,

      PERIOD_H2,PERIOD_H3,PERIOD_H4,

      PERIOD_H6,PERIOD_H8,PERIOD_H12,

      PERIOD_D1,PERIOD_W1,PERIOD_MN1

     };

   string descriptions_array[21]=

     {

      "1 minute","2 minutes","3 minutes",

      "4 minutes","5 minutes","6 minutes",

      "10 minutes","12 minutes","15 minutes",

      "20 minutes","30 minutes","1 hour",

      "2 hours","3 hours","4 hours",

      "6 hours","8 hours","12 hours",

      "1 day","1 week","1 month"

     };



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

      if(timeframe==timeframes_array[i])

         return(descriptions_array[i]);

//---

   return(result);

  }

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

Comments