Two Highest Rectangle N Bars

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Two Highest Rectangle N Bars
ÿþ//+------------------------------------------------------------------+

//|                                 Two Highest Rectangle N Bars.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers   0

#property indicator_plots     0

//--- input parameters

input group                "Rectangle 1"

input int                  Inp_1_width       = 10;                // Rectangle 1: width, in bars

input int                  Inp_1_right_bar   = 1;                 // Rectangle 1: right bar number

input color                Inp_1_Color       = clrMediumPurple;   // Rectangle 1: Color

input ENUM_LINE_STYLE      Inp_1_Style       = STYLE_DASH;        // Rectangle 1: Style

input int                  Inp_1_Width       = 2;                 // Rectangle 1: Width

input bool                 Inp_1_Fill        = false;             // Rectangle 1: Filling  color

input bool                 Inp_1_Back        = false;             // Rectangle 1: Background

input bool                 Inp_1_Selection   = false;             // Rectangle 1: Highlight to move

input bool                 Inp_1_Hidden      = true;              // Rectangle 1: Hidden in the object list

input long                 Inp_1_ZOrder      = 0;                 // Rectangle 1: Priority for mouse click

input group                "Rectangle 2"

input int                  Inp_2_width       = 6;                 // Rectangle 2: width, in bars

input int                  Inp_2_right_bar   = 3;                 // Rectangle 2: right bar number

input color                Inp_2_Color       = clrLawnGreen;      // Rectangle 2: Color

input ENUM_LINE_STYLE      Inp_2_Style       = STYLE_DASH;        // Rectangle 2: Style

input int                  Inp_2_Width       = 2;                 // Rectangle 2: Width

input bool                 Inp_2_Fill        = false;             // Rectangle 2: Filling  color

input bool                 Inp_2_Back        = false;             // Rectangle 2: Background

input bool                 Inp_2_Selection   = false;             // Rectangle 2: Highlight to move

input bool                 Inp_2_Hidden      = true;              // Rectangle 2: Hidden in the object list

input long                 Inp_2_ZOrder      = 0;                 // Rectangle 2: Priority for mouse click

/*

Bars        #12 #11 #10 #09 #08 #07 #06 #05 #04 #03 #02 #01 #00

Rectangle 1          -   -   -   -   -   -   -   -   -   -

Rectangle 2                  -   -   -   -   -   -

*/

//---

string   m_prefix="THRNB_";

int      m_start_pos=0;          // start position

int      m_count=0;              // data count to copy

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---

   m_start_pos=(Inp_1_right_bar<=Inp_2_right_bar)?Inp_1_right_bar:Inp_2_right_bar;

   m_count=(Inp_1_width+Inp_1_right_bar>Inp_2_width+Inp_2_right_bar)?Inp_1_width+Inp_1_right_bar:Inp_2_width+Inp_2_right_bar;

//---

   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      _1_start = rates_total-1-Inp_1_right_bar;

   int      _1_end   = _1_start-Inp_1_width;

   datetime _1_time1 = time[_1_start];                   // Rectangle 1: first point time (right point)

   double   _1_price1= DBL_MIN;                          // Rectangle 1: first point price (price max)

   datetime _1_time2 = time[_1_end];                     // Rectangle 1: second point time (left point)

   double   _1_price2= DBL_MAX;                          // Rectangle 1: second point price (price min)

   for(int i=_1_start; i>_1_end; i--)

     {

      if(high[i]>_1_price1)

         _1_price1=high[i];

      if(low[i]<_1_price2)

         _1_price2=low[i];

     }

   string _1_name=m_prefix+"Rectangle 1";

   if(ObjectFind(0,_1_name)<0)

      RectangleCreate(0,_1_name,0,_1_time1,_1_price1,_1_time2,_1_price2,

                      Inp_1_Color,Inp_1_Style,Inp_1_Width,Inp_1_Fill,Inp_1_Back,Inp_1_Selection,Inp_1_Hidden,Inp_1_ZOrder);

   else

     {

      RectanglePointChange(0,_1_name,0,_1_time1,_1_price1);

      RectanglePointChange(0,_1_name,1,_1_time2,_1_price2);

     }

//---

   int      _2_start = rates_total-1-Inp_2_right_bar;

   int      _2_end   = _2_start-Inp_2_width;

   datetime _2_time1 = time[_2_start];                   // Rectangle 2: first point time (right point)

   double   _2_price1= DBL_MIN;                          // Rectangle 2: first point price (price max)

   datetime _2_time2 = time[_2_end];                     // Rectangle 2: second point time (left point)

   double   _2_price2= DBL_MAX;                          // Rectangle 2: second point price (price min)

   for(int i=_2_start; i>_2_end; i--)

     {

      if(high[i]>_2_price1)

         _2_price1=high[i];

      if(low[i]<_2_price2)

         _2_price2=low[i];

     }

   string _2_name=m_prefix+"Rectangle 2";

   if(ObjectFind(0,_2_name)<0)

      RectangleCreate(0,_2_name,0,_2_time1,_2_price1,_2_time2,_2_price2,

                      Inp_2_Color,Inp_2_Style,Inp_2_Width,Inp_2_Fill,Inp_2_Back,Inp_2_Selection,Inp_2_Hidden,Inp_2_ZOrder);

   else

     {

      RectanglePointChange(0,_2_name,0,_2_time1,_2_price1);

      RectanglePointChange(0,_2_name,1,_2_time2,_2_price2);

     }

//--- 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 ENUM_LINE_STYLE style=STYLE_SOLID, // 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=true,    // highlight to move

                     const bool            hidden=true,       // hidden in the object list

                     const long            z_order=0)         // priority for mouse click

  {

//--- set anchor points' coordinates if they are not set

   ChangeRectangleEmptyPoints(time1,price1,time2,price2);

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

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

  }

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

//| Delete the rectangle                                             |

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

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

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

  {

//--- reset the error value

   ResetLastError();

//--- delete rectangle

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Check the values of rectangle's anchor points and set default    |

//| values for empty ones                                            |

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

void ChangeRectangleEmptyPoints(datetime &time1,double &price1,

                                datetime &time2,double &price2)

  {

//--- if the first point's time is not set, it will be on the current bar

   if(!time1)

      time1=TimeCurrent();

//--- if the first point's price is not set, it will have Bid value

   if(!price1)

      price1=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- if the second point's time is not set, it is located 9 bars left from the second one

   if(!time2)

     {

      //--- array for receiving the open time of the last 10 bars

      datetime temp[10];

      CopyTime(Symbol(),Period(),time1,10,temp);

      //--- set the second point 9 bars left from the first one

      time2=temp[0];

     }

//--- if the second point's price is not set, move it 300 points lower than the first one

   if(!price2)

      price2=price1-300*SymbolInfoDouble(Symbol(),SYMBOL_POINT);

  }

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

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