Author: Copyright © 2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Bar number
ÿþ//+------------------------------------------------------------------+

//|                                                   Bar number.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   "1.001"

#property description "Bar numbering (maximum number is 50)"

#property description "Objects 'OBJ_TEXT' are used for bar numbering"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//--- input parameters

input color    InpColorBullish= clrBlue;     // The color of bullish bars

input color    InpColorBearish= clrRed;      // The color of bearish bars

input bool     InpIndent      = true;        // Indent

//---

string   m_prefix          = "bar_number_";  // prefix for object

string   m_arr_names[];                      // array of names of objects OBJ_TEXT

int      m_number_of_bars  = 50;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---

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

   ResetLastError();

   if(ArrayResize(m_arr_names,m_number_of_bars)==-1)

     {

      Print(__FUNCTION__," ArrayResize error");

      return(INIT_SUCCEEDED);

     }

   ArraySetAsSeries(m_arr_names,true);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

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

  }

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

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

  {

   long chart_id=ChartID();

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   if(prev_calculated==0 || prev_calculated!=rates_total)

     {

      ObjectsDeleteAll(chart_id,m_prefix,0,OBJ_TEXT);

      ResetLastError();

      ArrayFree(m_arr_names);

      if(ArrayResize(m_arr_names,m_number_of_bars)==-1)

        {

         Print(__FUNCTION__,", prev_calculated==0, ArrayResize error");

         return(0);

        }

      //--- create the texts

      int limit=m_number_of_bars-1;

      string name;

      for(int i=limit; i>=0; i--)

        {

         name=m_prefix+TimeToString(time[i]);

         string text=IntegerToString(i);

         if(open[i]>close[i])

           {

            if(InpIndent)

               text=text+" ";

            if(!TextCreate(chart_id,name,0,time[i],low[i],text,90,"Lucida Console",10,InpColorBearish,ANCHOR_RIGHT))

               return(0);

           }

         else

           {

            if(InpIndent)

               text=" "+text;

            if(!TextCreate(chart_id,name,0,time[i],high[i],text,90,"Lucida Console",10,InpColorBullish,ANCHOR_LEFT))

               return(0);

           }

         m_arr_names[i]=name;

        }

      //---

      return(rates_total);

     }

   double   price_   = ObjectGetDouble(0,m_arr_names[0],OBJPROP_PRICE,0);

   long     time_    = ObjectGetInteger(0,m_arr_names[0],OBJPROP_TIME,0);

   string   text     = IntegerToString(0);

   if(open[0]>close[0])

     {

      if(InpIndent)

         text=text+" ";

      TextMove(chart_id,m_arr_names[0],time_,low[0],InpColorBearish,ANCHOR_RIGHT,text);

     }

   else

     {

      if(InpIndent)

         text=" "+text;

      TextMove(chart_id,m_arr_names[0],time_,high[0],InpColorBullish,ANCHOR_LEFT,text);

     }

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

   return(rates_total);

  }

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

//| Creating Text object                                             |

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

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

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

                const int               sub_window=0,             // subwindow index

                datetime                time=0,                   // anchor point time

                double                  price=0,                  // anchor point price

                const string            text="Text",              // the text itself

                const double            angle=90.0,               // text slope

                const string            font="Lucida Console",    // font

                const int               font_size=10,             // font size

                const color             clr=clrBlue,              // color

                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT,       // 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 Text object

   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))

     {

      Print(__FUNCTION__,

            ": failed to create \"Text\" object! Error code = ",GetLastError());

      return(false);

     }

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

//ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,10);

//--- successful execution

   return(true);

  }

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

//| Move the anchor point                                            |

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

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

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

              datetime                 time=0,              // anchor point time coordinate

              double                   price=0,             // anchor point price coordinate

              const color              clr=clrBlue,         // color

              const ENUM_ANCHOR_POINT  anchor=ANCHOR_LEFT,  // anchor type

              const string             text="Text")         // text

  {

//--- if point position is not set, move it to the current bar having Bid price

   if(!time)

      time=TimeCurrent();

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move the anchor point

   if(!ObjectMove(chart_ID,name,0,time,price))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

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

  }

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

Comments