Day rectangle Indent 2

Author: Copyright © 2020-2021, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Day rectangle Indent 2
ÿþ//+------------------------------------------------------------------+

//|                                       Day rectangle Indent 2.mq5 |

//|                         Copyright © 2020-2021, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020-2021, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "2.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

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

//| Enum Pips Or Points                                              |

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

enum ENUM_PIPS_OR_POINTS

  {

   pips=0,     // Pips (1.00045-1.00055=1 pips)

   points=1,   // Points (1.00045-1.00055=10 points)

  };

//--- input parameters

input ENUM_PIPS_OR_POINTS  InpPipsOrPoints   = pips;           // Pips Or Points:

input uint                 InpIndent         = 5;              // Indent, in pips (1.00045-1.00055=1 pips)

input group             "Sunday"

input bool                 InpSundayUse      = true;           // Use Sunday

input color                InpSundayColor    = clrCyan;        // Sunday color

input group             "Monday"

input bool                  InpMondayUse     = true;           // Use Monday

input color                InpMondayColor    = clrRed;         // Monday color

input group             "Tuesday"

input bool                 InpTuesdayUse     = true;           // Use Tuesday

input color                InpTuesdayColor   = clrYellowGreen; // Tuesday color

input group             "Wednesday"

input bool                 InpWednesdayUse   = true;           // Use Wednesday

input color                InpWednesdayColor = clrBlue;        // Wednesday color

input group             "Thursday"

input bool                 InpThursdayUse    = true;           // Use Thursday

input color                InpThursdayColor  = clrYellow;      // Thursday color

input group             "Friday"

input bool                 InpFridayUse      = true;           // Use Friday

input color                InpFridayColor    = clrNavy;        // Friday color

input group             "Saturday"

input bool                 InpSaturdayUse    = true;           // Use Saturday

input color                InpSaturdayColor  = clrGold;        // Saturday color

//---

double   m_indent             = 0.0;      // Indent         -> double

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

string   m_prefix             ="DrI_";

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- tuning for 3 or 5 digits

   int digits_adjust=1;

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

      digits_adjust=10;

   m_adjusted_point=Point()*digits_adjust;

   if(InpPipsOrPoints==pips) // Pips (1.00045-1.00055=1 pips)

     {

      m_indent                   = InpIndent                   * m_adjusted_point;

     }

   else // Points (1.00045-1.00055=10 points)

     {

      m_indent                   = InpIndent                   * Point();

     }

//---

   return(INIT_SUCCEEDED);

  }

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

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

  {

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      int bar_shift_d1=iBarShift(Symbol(),PERIOD_D1,time[i],false);

      if(bar_shift_d1<0)

         return(0);

      MqlRates rates_d1[];

      ArraySetAsSeries(rates_d1,true);

      if(CopyRates(Symbol(),PERIOD_D1,bar_shift_d1,1,rates_d1)<1)

         return(0);

      MqlDateTime STime;

      TimeToStruct(rates_d1[0].time,STime);

      string rectangle_name=m_prefix+TimeToString(rates_d1[0].time,TIME_DATE|TIME_MINUTES);

      if(ObjectFind(0,rectangle_name)<0)

        {

         switch(STime.day_of_week)

           {

            case  0: // Sunday

               if(InpSundayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpSundayColor,"Sunday");

               break;

            case  1: // Monday

               if(InpMondayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpMondayColor,"Monday");

               break;

            case  2: // Tuesday

               if(InpTuesdayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpTuesdayColor,"Tuesday");

               break;

            case  3: // Wednesday

               if(InpWednesdayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpWednesdayColor,"Wednesday");

               break;

            case  4: // Thursday

               if(InpThursdayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpThursdayColor,"Thursday");

               break;

            case  5: // Friday

               if(InpFridayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpFridayColor,"Friday");

               break;

            default: // Saturday

               if(InpSaturdayUse)

                  RectangleCreate(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent,time[i],rates_d1[0].high+m_indent,InpSaturdayColor,"Saturday");

               break;

           }

        }

      else

        {

         RectanglePointChange(0,rectangle_name,0,rates_d1[0].time,rates_d1[0].low-m_indent);

         RectanglePointChange(0,rectangle_name,1,time[i],rates_d1[0].high+m_indent);

        }

     }

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

   return(rates_total);

  }

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

//| Create rectangle by the given coordinates                        |

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

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

                     const string          name="Rectangle",  // rectangle name

                     const int             sub_window=0,      // subwindow index

                     datetime              time1=0,           // first point time

                     double                price1=0,          // first point price

                     datetime              time2=0,           // second point time

                     double                price2=0,          // second point price

                     const color           clr=clrRed,        // rectangle color

                     const string          text="Rectangle",  // rectangle text

                     const ENUM_LINE_STYLE style=STYLE_DASH,  // style of rectangle lines

                     const int             width=2,           // width of rectangle lines

                     const bool            fill=false,        // filling rectangle with color

                     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 rectangle by the given coordinates

   if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2))

     {

      Print(__FUNCTION__,

            ": failed to create a rectangle! Error code = ",GetLastError());

      return(false);

     }

//--- set rectangle color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//---

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set the style of rectangle lines

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set width of the rectangle lines

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- enable (true) or disable (false) the mode of filling the rectangle

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of highlighting the rectangle for moving

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   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 rectangle anchor point                                  |

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

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

                          const string name="Rectangle", // rectangle name

                          const int    point_index=0,    // anchor point index

                          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,point_index,time,price))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,m_prefix,0,OBJ_RECTANGLE);

  }

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

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