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

//|                                                      Day Box.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property description ""

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "Common parameters"

//input datetime             InpTimeLeft       = D'2021.05.31 00:00';  // Time Left

input double               InpPriceUp              = 110.19;               // Trend Line Up: Price Up

input double               InpRectanglePriceUp     = 110.10;               // Rectangle: Price Up

input double               InpRectanglePriceDown   = 109.66;               // Rectangle:Price Down

//input datetime             InpTimeRight      = D'2021.07.08 00:00';  // Time Right

input double               InpPriceDown            = 109.53;               // Trend Line Down:Price Down

input group             "Rectangle"

input string               InpRectangleName        = "Day Box Rectangle";  // Rectangle name

input color                InpRectangleColor       = C'81,211,255';        // Rectangle color

input ENUM_LINE_STYLE      InpRectangleStyle       = STYLE_SOLID;          // Style of rectangle lines

input bool                 InpRectangleFill        = true;                 // Filling rectangle with color

input group             "Trend Line's"

input string               InpTrendUpName    = "Day Box Trend Up";   // Trend Line Up name

input color                InpTrendUpColor   = C'188,222,118';       // Trend Line Up color

input ENUM_LINE_STYLE      InpTrendUpStyle   = STYLE_DOT;           // Style of Trend Line Up

input string               InpTrendDownName  = "Day Box Trend Down"; // Trend Line Down name

input color                InpTrendDownColor = C'188,222,118';       // Trend Line Down color

input ENUM_LINE_STYLE      InpTrendDownStyle = STYLE_DOT;           // Style of Trend Line Down

//---

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---

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

      long chart_id=ChartID();

      //--- Rectangle

      if(ObjectFind(chart_id,InpRectangleName)<0)

        {

         RectangleCreate(chart_id,InpRectangleName,0,rates_d1[0].time,InpRectanglePriceUp,time[i],InpRectanglePriceDown,

                         InpRectangleColor,InpRectangleName,InpRectangleStyle,1,InpRectangleFill);

        }

      else

        {

         RectanglePointChange(chart_id,InpRectangleName,0,rates_d1[0].time,InpRectanglePriceUp);

         RectanglePointChange(chart_id,InpRectangleName,1,time[i],InpRectanglePriceDown);

        }

      //--- Trend Line Up

      if(ObjectFind(chart_id,InpTrendUpName)<0)

        {

         TrendCreate(chart_id,InpTrendUpName,0,rates_d1[0].time,InpPriceUp,time[i],InpPriceUp,InpTrendUpColor,InpTrendUpName,InpTrendUpStyle);

        }

      else

        {

         TrendPointChange(chart_id,InpTrendUpName,0,rates_d1[0].time,InpPriceUp);

         TrendPointChange(chart_id,InpTrendUpName,1,time[i],InpPriceUp);

        }

      //--- Trend Line Down

      if(ObjectFind(chart_id,InpTrendDownName)<0)

        {

         TrendCreate(chart_id,InpTrendDownName,0,rates_d1[0].time,InpPriceDown,time[i],InpPriceDown,InpTrendDownColor,InpTrendDownName,InpTrendDownStyle);

        }

      else

        {

         TrendPointChange(chart_id,InpTrendDownName,0,rates_d1[0].time,InpPriceDown);

         TrendPointChange(chart_id,InpTrendDownName,1,time[i],InpPriceDown);

        }

     }

//--- 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=1,           // 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);

  }

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

//| Create a trend line by the given coordinates                     |

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

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

                 const string          name="TrendLine",  // line 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,        // line color

                 const string          text="Trend line", // trend line text

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 const bool            back=false,        // in the background

                 const bool            selection=false,   // highlight to move

                 const bool            ray_left=false,    // line's continuation to the left

                 const bool            ray_right=false,   // line's continuation to the right

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

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

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//---

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set line display style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set line width

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- 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 line by mouse

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

//--- enable (true) or disable (false) the mode of continuation of the line's display to the left

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_LEFT,ray_left);

//--- enable (true) or disable (false) the mode of continuation of the line's display to the right

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

//--- 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 trend line anchor point                                     |

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

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

                      const string name="TrendLine", // line 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 trend line's 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)

  {

   ObjectDelete(ChartID(),InpRectangleName);

   ObjectDelete(ChartID(),InpTrendUpName);

   ObjectDelete(ChartID(),InpTrendDownName);

  }

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

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