Dynamic High and Low

Author: Copyright © 2021, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Dynamic High and Low
ÿþ//+------------------------------------------------------------------+

//|                                         Dynamic High and Low.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.001"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "Main settings"

input uint                 InpShiftUp              = 150;            // Shift Up

input uint                 InpShiftDown            = 150;            // Shift Down

input group             "Line parameters"

input color                InpUpColor              = clrBlue;        // Up: Line color

input ENUM_LINE_STYLE      InpUpStyle              = STYLE_DASH;     // Up: Line style

input int                  InpUpWidth              = 1;              // Up: Line width

input color                InpDownColor            = clrRed;         // Down: Line color

input ENUM_LINE_STYLE      InpDownStyle            = STYLE_DASH;     // Down: Line style

input int                  InpDownWidth            = 1;              // Down: Line width

//---

string   name_high      = "Up Level";

string   name_low       = "Down Level";

//---

double   m_shift_up     = 0.0;         // Shift Up    -> double

double   m_shift_down   = 0.0;         // Shift Down  -> double

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- create a timer with a 1 second period

   EventSetTimer(12);

//---

   m_shift_up     = InpShiftUp   * Point();

   m_shift_down   = InpShiftDown * Point();

//---

   HLineCreate(ChartID(),name_high,0,0.0,InpUpColor,InpUpStyle,InpUpWidth);

   HLineCreate(ChartID(),name_low,0,0.0,InpDownColor,InpDownStyle,InpDownWidth);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//--- destroy the timer after completing the work

   EventKillTimer();

//---

   HLineDelete(ChartID(),name_high);

   HLineDelete(ChartID(),name_low);

  }

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

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

  {

   HLineMove(ChartID(),name_high,close[rates_total-1]+m_shift_up);

   HLineMove(ChartID(),name_low,close[rates_total-1]-m_shift_down);

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

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   if(ObjectFind(ChartID(),name_high)<0)

      HLineCreate(ChartID(),name_high,0,0.0,InpUpColor,InpUpStyle,InpUpWidth);

   if(ObjectFind(ChartID(),name_low)<0)

      HLineCreate(ChartID(),name_low,0,0.0,InpDownColor,InpDownStyle,InpDownWidth);

  }

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

//| Create the horizontal line                                       |

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

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

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

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

  }

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

//| Delete a horizontal line                                         |

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

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

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

  {

//--- reset the error value

   ResetLastError();

//--- delete a horizontal line

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- successful execution

   return(true);

  }

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

Comments