Size of candles 3 _text_

Author: Copyright © 2019, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Size of candles 3 _text_
ÿþ//+------------------------------------------------------------------+

//|                                     Size of candles 3 _text_.mq5 |

//|                              Copyright © 2019, Vladimir Karputov |

//|                                           http://wmua.ru/slesar/ |

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

#property copyright "Copyright © 2019, Vladimir Karputov"

#property link      "http://wmua.ru/slesar/"

#property version   "1.001"

#property description " "

#property description "The size of the candles in the form of text above each bar"

#property description "---"

#property description "Calculation of candles size: the \"Minuend\" minus the \"Subtrahend\""

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//---

enum OHLC

  {

   Open=0,  // Open 

   High=1,  // High

   Low=2,   // Low 

   Close=3, // Close

  };

//--- input parameters

input bool              InpVisible     = true;              // Visible

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

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

input int               InpIndentY     = 15;                // Indent Y-axis distance 

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

input string            InpFont        = "Lucida Console";  // Font 

input int               InpFontSize    = 12;                // Font size 

input color             InpColor       = clrLawnGreen;      // Color 

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

input ENUM_ANCHOR_POINT InpAnchor      = ANCHOR_RIGHT_LOWER;// Anchor type 

//---

input uchar    InpNumberOfBars= 3;           // Number of bars (do not use "0")

input OHLC     InpMinuend     = High;        // Minuend

input OHLC     InpSubtrahend  = Low;         // Subtrahend

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="size_candles_";     // prefix for object

double         m_adjusted_point;             // point value adjusted for 3 or 5 points

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(InpNumberOfBars==0)

     {

      Print("\"Number of bars\"==0. Do not use \"0\"");

      return(INIT_PARAMETERS_INCORRECT);

     }

//---

   m_prefix+=EnumToString(Period())+"_";

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

   if(Digits()==3 || Digits()==5)

      digits_adjust=10;

   m_adjusted_point=Point()*digits_adjust;

//---

   ObjectsDeleteAll(0,m_prefix,0,OBJ_TEXT);

   ObjectsDeleteAll(0,m_prefix,0,OBJ_LABEL);



   ResetLastError();

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

     {

      Print(__FUNCTION__," ArrayResize error");

      return(INIT_SUCCEEDED);

     }

   ArraySetAsSeries(m_arr_names,true);

//---

   LabelCreate(0,m_prefix+"Bearish",0,InpX,InpY,InpCorner,"Bearish:",InpFont,InpFontSize,InpColorBearish,InpAngle,InpAnchor);

   LabelCreate(0,m_prefix+"Bullish",0,InpX,InpY+InpIndentY,InpCorner,"Bullish:",InpFont,InpFontSize,InpColorBullish,InpAngle,InpAnchor);

   LabelCreate(0,m_prefix+"Number of bars",0,InpX,InpY+InpIndentY*2,InpCorner,"Number of bars: "+IntegerToString(InpNumberOfBars),InpFont,InpFontSize,InpColor,InpAngle,InpAnchor);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,m_prefix,0,OBJ_TEXT);

   ObjectsDeleteAll(0,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[])

  {

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);



   string text="";

   double result=0.0;



   if(InpVisible)

     {

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

        {

         ObjectsDeleteAll(0,m_prefix,0,OBJ_TEXT);



         ResetLastError();

         ArrayFree(m_arr_names);

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

           {

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

            return(0);

           }

         //--- create the texts 

         int limit=InpNumberOfBars-1;

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

           {

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



            Calculate(open[i],high[i],low[i],close[i],text,result);

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

              {

               if(InpIndent)

                  text=text+" ";

               if(!TextCreate(0,name,0,time[i],low[i],text,90,InpFont,InpFontSize,InpColorBearish,ANCHOR_RIGHT))

                  return(0);

              }

            else

              {

               if(InpIndent)

                  text=" "+text;

               if(!TextCreate(0,name,0,time[i],high[i],text,90,InpFont,InpFontSize,InpColorBullish,ANCHOR_LEFT))

                  return(0);

              }

            m_arr_names[i]=name;

           }

         //---

         return(rates_total);

        }

     }

   if(InpVisible)

     {

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

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

      Calculate(open[0],high[0],low[0],close[0],text,result);

      TextChange(0,m_arr_names[0],text);

      if(high[0]>price_)

        {

         TextMove(0,m_arr_names[0],time_,high[0]);

        }

     }

//---

   int limit=InpNumberOfBars-1;

   double bearish=0.0,bullish=0.0;

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

     {

      Calculate(open[i],high[i],low[i],close[i],text,result);

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

        {

         bearish+=result;

        }

      else

        {

         bullish+=result;

        }

     }

//---

   LabelTextChange(0,m_prefix+"Bearish","Bearish: "+DoubleToString(bearish,0));

   LabelTextChange(0,m_prefix+"Bullish","Bullish: "+DoubleToString(bullish,0));

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

  {

//--- set anchor point coordinates if they are not set 

//ChangeTextEmptyPoint(time,price);

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

  {

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

     }

//--- successful execution 

   return(true);

  }

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

//| Change the object text                                           | 

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

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

                const string name="Text", // 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);

  }

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

//| Calculate                                                        |

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

void Calculate(const double open,const double high,const double low,const double close,string &text,double &result)

  {

   double minuend=0.0;

   switch(InpMinuend)

     {

      case  Open:

         minuend=open;

         break;

      case  High:

         minuend=high;

         break;

      case  Low:

         minuend=low;

         break;

      case  Close:

         minuend=close;

         break;

     }

   double subtrahend=0.0;

   switch(InpSubtrahend)

     {

      case  Open:

         subtrahend=open;

         break;

      case  High:

         subtrahend=high;

         break;

      case  Low:

         subtrahend=low;

         break;

      case  Close:

         subtrahend=close;

         break;

     }

//---

   result=MathAbs(minuend-subtrahend)/m_adjusted_point;

   text=DoubleToString(result,0);

  }

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

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

  }

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

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

  }

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

Comments