High Low TimeFrame

Author: Copyright © 2021, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
High Low TimeFrame
ÿþ//+------------------------------------------------------------------+

//|                                           High Low TimeFrame.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2021, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.003"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

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

//| Enum Lines                                                       |

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

enum ENUM_LINES

  {

   horizontal=0,  // Horizontal Line

   trend=1,       // Trend Line

  };

//--- input parameters

input group             "Basic settings"

input ENUM_TIMEFRAMES      Inp_period           = PERIOD_D1;   // TimeFrame

input ENUM_LINES           InpLines             = horizontal;  // Line type:

input string               InpHighLineName      = "High";      // High Line Name

input string               InpLowLineName       = "Low";       // Low Line Name

input group             "High Line settings"

input color                InpHighLineColor     = clrRed;      // line color

input ENUM_LINE_STYLE      InpHighLineStyle     = STYLE_DASH;  // line style

input int                  InpHighLineWidth     = 3;           // line width

input bool                 InpHighLineBack      = false;       // in the background

input bool                 InpHighLineSelection = true;        // highlight to move

input bool                 InpHighLineHidden    = true;        // hidden in the object list

input long                 InpHighLineZ_order   = 0;           // priority for mouse click

input group             "Low Line settings"

input color                InpLowLineColor      = clrBlue;     // line color

input ENUM_LINE_STYLE      InpLowLineStyle      = STYLE_DASH;  // line style

input int                  InpLowLineWidth      = 3;           // line width

input bool                 InpLowLineBack       = false;       // in the background

input bool                 InpLowLineSelection  = true;        // highlight to move

input bool                 InpLowLineHidden     = true;        // hidden in the object list

input long                 InpLowLineZ_order    = 0;           // priority for mouse click

//---

datetime m_prev_bars                      = 0;           // "0" -> D'1970.01.01 00:00';

string   m_prefix                         = "HLT_" ;     //

string   m_ending                         = "";          //

bool     m_init_error                     = false;       // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---

   if(Inp_period==PERIOD_CURRENT || (Inp_period!=PERIOD_CURRENT && Inp_period<Inp_period))

     {

      string err_text=(TerminalInfoString(TERMINAL_LANGUAGE)=="Russian")?

                      "'TimeFrame' cannot be <= current timeframe!":

                      "'TimeFrame' =5 <>65B 1KBL <= B5:CI5<C B09<D@59<C!";

      if(MQLInfoInteger(MQL_TESTER)) // when testing, we will only output to the log about incorrect input parameters

         Print(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      else // if the Expert Advisor is run on the chart, tell the user about the error

         Alert(__FILE__," ",__FUNCTION__,", ERROR: ",err_text);

      //---

      m_init_error=true;

      return(INIT_SUCCEEDED);

     }

   m_ending="_"+StringSubstr(EnumToString(Inp_period),7,-1);

   long chart_id=ChartID();

   if(InpLines==horizontal)

     {

      if(!HLineCreate(chart_id,m_prefix+InpHighLineName+m_ending,InpHighLineName+m_ending,0,0.0,InpHighLineColor,

                      InpHighLineStyle,InpHighLineWidth,InpHighLineBack,InpHighLineSelection,InpHighLineHidden,InpHighLineZ_order))

         m_init_error=true;

      if(!HLineCreate(chart_id,m_prefix+InpLowLineName+m_ending,InpLowLineName+m_ending,0,0.0,InpLowLineColor,

                      InpLowLineStyle,InpLowLineWidth,InpLowLineBack,InpLowLineSelection,InpLowLineHidden,InpLowLineZ_order))

         m_init_error=true;

     }

   else

     {

      if(!TrendCreate(chart_id,m_prefix+InpHighLineName+m_ending,InpHighLineName+m_ending,0,0,0.0,0,0.0,InpHighLineColor,

                      InpHighLineStyle,InpHighLineWidth,InpHighLineBack,InpHighLineSelection,false,true,InpHighLineHidden,InpHighLineZ_order))

         m_init_error=true;

      if(!TrendCreate(chart_id,m_prefix+InpLowLineName+m_ending,InpLowLineName+m_ending,0,0,0.0,0,0.0,InpLowLineColor,

                      InpLowLineStyle,InpLowLineWidth,InpLowLineBack,InpLowLineSelection,false,true,InpLowLineHidden,InpLowLineZ_order))

         m_init_error=true;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   ObjectDelete(ChartID(),m_prefix+InpHighLineName+m_ending);

   ObjectDelete(ChartID(),m_prefix+InpLowLineName+m_ending);

  }

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

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

  {

//---

   if(m_init_error)

      return(0);

//---

   datetime time_0=iTime(Symbol(),Period(),0);

   if(time_0==m_prev_bars)

      return(rates_total);

   m_prev_bars=time_0;

//---

   MqlRates rates[];

   ArraySetAsSeries(rates,true);

   int start_pos=0,count=3;

   if(CopyRates(Symbol(),Inp_period,start_pos,count,rates)!=count)

     {

      m_prev_bars=0;

      return(0);

     }

//---

   long chart_id=ChartID();

   if(InpLines==horizontal)

     {

      if(!HLineMove(chart_id,m_prefix+InpHighLineName+m_ending,rates[1].high))

         m_init_error=true;

      if(!HLineMove(chart_id,m_prefix+InpLowLineName+m_ending,rates[1].low))

         m_init_error=true;

     }

   else

     {

      if(!TrendPointChange(chart_id,m_prefix+InpHighLineName+m_ending,0,rates[1].time,rates[1].high))

         m_init_error=true;

      if(!TrendPointChange(chart_id,m_prefix+InpHighLineName+m_ending,1,rates[0].time,rates[1].high))

         m_init_error=true;

      if(!TrendPointChange(chart_id,m_prefix+InpLowLineName+m_ending,0,rates[1].time,rates[1].low))

         m_init_error=true;

      if(!TrendPointChange(chart_id,m_prefix+InpLowLineName+m_ending,1,rates[0].time,rates[1].low))

         m_init_error=true;

     }

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

   return(rates_total);

  }

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

//| Create the horizontal line                                       |

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

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

                 const string          name="HLine",      // line name

                 const string          text="HLine",      // text

                 const int             sub_window=0,      // subwindow index

                 double                price=0,           // line price

                 const color           clr=clrRed,        // line color

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

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

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

  {

//--- if the price is not set, set it at the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- create a horizontal line

   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- set text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

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

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

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

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

               const string name="HLine", // line name

               double       price=0)      // line price

  {

//--- if the line price is not set, move it to the current Bid price level

   if(!price)

      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

//--- reset the error value

   ResetLastError();

//--- move a horizontal line

   if(!ObjectMove(chart_ID,name,0,0,price))

     {

      Print(__FUNCTION__,

            ": failed to move the horizontal line! 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 string          text="TrendLine",  // text

                 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 ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 const bool            back=false,        // in the background

                 const bool            selection=true,    // highlight to move

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

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

                 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

   ChangeTrendEmptyPoints(time1,price1,time2,price2);

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

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set line color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

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

  }

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

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

//| values for empty ones                                            |

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

void ChangeTrendEmptyPoints(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, it is equal to the first point's one

   if(!price2)

      price2=price1;

  }

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

Comments