Lines of specific candles

Author: Copyright © 2020, Vladimir Karputov
Price Data Components
0 Views
0 Downloads
0 Favorites
Lines of specific candles
ÿþ//+------------------------------------------------------------------+

//|                                    Lines of specific candles.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020, Vladimir Karputov"

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

#property version   "1.001"

#property indicator_chart_window

#property indicator_buffers      0

#property indicator_plots        0

//--- input parameters

input uchar    InpLeftCandle     = 9;  // Left Candle

input uchar    InpRightCandle    = 0;  // Right Candle

//---

string      m_prefix                      = "LSC ";                           // prefix for three lines

string      m_left_vertical_line_name     = m_prefix+"Left Vertical Line";    // name for the left vertical line

string      m_right_vertical_line_name    = m_prefix+"Right Vertical Line";   // name for the right vertical line

string      m_trend_line_name             = m_prefix+"Trend Vertical Line";   // name for trend line

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

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

   EventSetTimer(3);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

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

   EventKillTimer();

//--- removes all objects of the specified type using prefix in object names.

   ObjectsDeleteAll(0,m_prefix,0,-1);

  }

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

//| 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(rates_total<InpLeftCandle+1 || rates_total<InpRightCandle+1)

      return(0);

//---

   if(prev_calculated==0)

     {

      OnTimer();

      TrendPointChange(0,m_trend_line_name,0,time[rates_total-1-InpLeftCandle],close[rates_total-1-InpLeftCandle]);

      TrendPointChange(0,m_trend_line_name,1,time[rates_total-1-InpRightCandle],close[rates_total-1-InpRightCandle]);

      VLineMove(0,m_left_vertical_line_name,time[rates_total-1-InpLeftCandle]);

      VLineMove(0,m_right_vertical_line_name,time[rates_total-1-InpRightCandle]);

      return(rates_total);

     }

//---

   TrendPointChange(0,m_trend_line_name,1,time[rates_total-1-InpRightCandle],close[rates_total-1-InpRightCandle]);

//--- set the text

   ObjectSetString(0,m_trend_line_name,OBJPROP_TEXT,m_trend_line_name+" "+

                   DoubleToString((close[rates_total-1-InpRightCandle]-close[rates_total-1-InpLeftCandle])/Point(),0));

//--- we 'VLineMove' and 'TrendPointChange' only at the time of the birth of new bar

   if(time[rates_total-1]==m_prev_bars)

      return(rates_total);

   m_prev_bars=time[rates_total-1];

   TrendPointChange(0,m_trend_line_name,0,time[rates_total-1-InpLeftCandle],close[rates_total-1-InpLeftCandle]);

   VLineMove(0,m_left_vertical_line_name,time[rates_total-1-InpLeftCandle]);

   VLineMove(0,m_right_vertical_line_name,time[rates_total-1-InpRightCandle]);

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

   return(rates_total);

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   if(ObjectFind(0,m_left_vertical_line_name)<0)

      VLineCreate(0,m_left_vertical_line_name,clrDeepSkyBlue,STYLE_DASH,2);

   if(ObjectFind(0,m_right_vertical_line_name)<0)

      VLineCreate(0,m_right_vertical_line_name,clrDeepSkyBlue,STYLE_DASH,2);

   if(ObjectFind(0,m_trend_line_name)<0)

      TrendCreate(0,m_trend_line_name,clrDeepSkyBlue,STYLE_DASH,2);

  }

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

//| Create the vertical line                                         |

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

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

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

                 const color           clr=clrRed,        // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

                 const int             sub_window=0,      // subwindow index

                 datetime              time=0,            // line time

                 const bool            back=false,        // in the background

                 const bool            selection=false,   // highlight to move

                 const bool            ray=true,          // line's continuation down

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

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

  {

//--- if the line time is not set, draw it via the last bar

   if(!time)

      time=TimeCurrent();

//--- reset the error value

   ResetLastError();

//--- create a vertical line

   if(!ObjectCreate(chart_ID,name,OBJ_VLINE,sub_window,time,0))

     {

      Print(__FUNCTION__,

            ": failed to create a vertical 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);

//--- enable (true) or disable (false) the mode of displaying the line in the chart subwindows

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY,ray);

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

//--- successful execution

   return(true);

  }

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

//| Move the vertical line                                           |

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

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

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

               datetime     time=0)       // line time

  {

//--- if line time is not set, move the line to the last bar

   if(!time)

      time=TimeCurrent();

//--- reset the error value

   ResetLastError();

//--- move the vertical line

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

     {

      Print(__FUNCTION__,

            ": failed to move the vertical 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 color           clr=clrRed,        // line color

                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style

                 const int             width=1,           // line width

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

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,name);

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