Bars window

Author: Copyright © 2017-2021, Vladimir Karputov
Price Data Components
1 Views
0 Downloads
0 Favorites
Bars window
ÿþ//+------------------------------------------------------------------+

//|                                                  Bars window.mq5 |

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

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

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

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

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

#property version   "1.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots 0

//--- input parameters

input ushort   InputRightBar  = 0;  // The number of the starting bar

input ushort   InputLeftBar   = 50; // The number of the end bar

//---

string   name_rectangle       = "Bars window rectangle";

string   name_right_price_max = "Bars window price Max";

string   name_right_price_min = "Bars window price Min";

bool     m_init_error   = false;                            // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   if(InputRightBar==InputLeftBar)

     {

      Print("The starting and ending bars can not be equal");

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

   if(InputRightBar>InputLeftBar)

     {

      Print("The starting bar can not be more than the final bar");

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   long chart_ID=0;        // chart's ID

   if(!RectangleCreate(chart_ID,name_rectangle))

     {

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

   if(!ArrowRightPriceCreate(chart_ID,name_right_price_max))

     {

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

   if(!ArrowRightPriceCreate(chart_ID,name_right_price_min))

     {

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   RectangleDelete(0,name_rectangle);

   ArrowRightPriceDelete(0,name_right_price_max);

   ArrowRightPriceDelete(0,name_right_price_min);

  }

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

//| Custom indicator iteration function                              |

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

int OnCalculate(const int rates_total,       // size of input time series

                const int prev_calculated,  // bars handled in previous call

                const datetime& time[],     // Time

                const double& open[],       // Open

                const double& high[],       // High

                const double& low[],        // Low

                const double& close[],      // Close

                const long& tick_volume[],  // Tick Volume

                const long& volume[],       // Real Volume

                const int& spread[]         // Spread

               )

  {

//--- the rightmost bar on the chart has an index of "0"

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   int      count                = InputLeftBar-InputRightBar;

   int      index_low_minimum    = ArrayMinimum(low,InputRightBar,count+1);

   if(index_low_minimum==-1)

      return(rates_total);

   double   price_low_minimum    = low[index_low_minimum];

   int      index_high_maximum   = ArrayMaximum(high,InputRightBar,count+1);

   if(index_high_maximum==-1)

      return(rates_total);

      //--- 

   double   price_high_maximum=high[index_high_maximum];

   RectanglePointChange(0,name_rectangle,0,time[InputRightBar],price_low_minimum);

   RectanglePointChange(0,name_rectangle,1,time[InputRightBar+count],price_high_maximum);

//--- set the text

   ObjectSetString(0,name_rectangle,OBJPROP_TEXT,"Height: "+DoubleToString((price_high_maximum-price_low_minimum)/Point(),0)+" points");

   ArrowRightPriceMove(0,name_right_price_max,time[0],price_high_maximum);

   ArrowRightPriceMove(0,name_right_price_min,time[0],price_low_minimum);

//--- 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=clrSeashell,   // rectangle color

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

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

                     const bool            fill=true,         // 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

  {

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

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,"Height: ");

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

  }

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

//| Create the right price label                                     |

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

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

                           const string          name="RightPrice", // price label name

                           const int             sub_window=0,      // subwindow index

                           datetime              time=0,            // anchor point time

                           double                price=0,           // anchor point price

                           const color           clr=clrRed,        // price label color

                           const ENUM_LINE_STYLE style=STYLE_SOLID, // border line style

                           const int             width=1,           // price label size

                           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

   ChangeArrowEmptyPoint(time,price);

//--- reset the error value

   ResetLastError();

//--- create a price label

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

     {

      Print(__FUNCTION__,

            ": failed to create the right price label! Error code = ",GetLastError());

      return(false);

     }

//--- set the label color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set the border line style

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set the label size

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

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

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

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

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

  }

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

//| Delete the right price label from the chart                      |

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

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

                           const string name="RightPrice") // label name

  {

//--- reset the error value

   ResetLastError();

//--- delete the label

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete the right price label! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Check anchor point values and set default values                 |

//| for empty ones                                                   |

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

void ChangeArrowEmptyPoint(datetime &time,double &price)

  {

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

   if(!time)

      time=TimeCurrent();

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

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

  }

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

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