Trend Lines Last Two Days

Author: Copyright © 2020, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Trend Lines Last Two Days
ÿþ//+------------------------------------------------------------------+

//|                                    Trend Lines Last Two Days.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.000"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input string   InpPrefix            = "TLLTD_"; // Prefix for all lines

//---

string   m_line_high_name           = "_";

string   m_line_low_name            = "_";

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

bool     m_global_error             = false;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   m_global_error=false;

   if(Period()>=PERIOD_D1)

     {

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

                      ""09<D@59< =5 <>65B 1KBL 1KBL >= 'D1'!":

                      "The timeframe cannot be > = 'D1'!";

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

      m_global_error=true;

      return(INIT_SUCCEEDED);

     }

//--- indicator buffers mapping

   m_line_high_name=InpPrefix+"High";

   m_line_low_name=InpPrefix+"Low";

//---

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

  {

   if(m_global_error)

      return(0);

//--- we work 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];

//--- new bar

   MqlRates rates_d1[];

   ArraySetAsSeries(rates_d1,false);

   int start_pos=0,count=3;

   if(CopyRates(Symbol(),PERIOD_D1,start_pos,count,rates_d1)!=count)

     {

      m_prev_bars=0;

      return(rates_total);

     }

//--- search High and Low on the current timeframe

   datetime    time1_max = 0;             // first point time

   datetime    time2_max = 0;             // second point time

   datetime    time1_min = 0;             // first point time

   datetime    time2_min = 0;             // second point time



   double price1_max=DBL_MIN,price2_max=DBL_MIN;

   double price1_min=DBL_MAX,price2_min=DBL_MAX;



   for(int i=rates_total-1; i>=0; i--)

     {

      if(time[i]>rates_d1[2].time)

         continue;

      if(time[i]>=rates_d1[1].time && time[i]<=rates_d1[2].time)

        {

         if(high[i]>price2_max)

           {

            price2_max=high[i];

            time2_max=time[i];

           }

         if(low[i]<price2_min)

           {

            price2_min=low[i];

            time2_min=time[i];

           }

        }

      else

        {

         if(time[i]>=rates_d1[0].time && time[i]<=rates_d1[1].time)

           {

            if(high[i]>price1_max)

              {

               price1_max=high[i];

               time1_max=time[i];

              }

            if(low[i]<price1_min)

              {

               price1_min=low[i];

               time1_min=time[i];

              }

           }

         else

            break;

        }

     }

//--- check time's

   if(time1_max==D'1970.01.01 00:00' || time2_max==D'1970.01.01 00:00' || time1_min==D'1970.01.01 00:00' || time1_min==D'1970.01.01 00:00')

     {

      m_prev_bars=0;

      return(rates_total);

     }

//--- move or create trend lines

   if(ObjectFind(0,m_line_high_name)<0)

     {

      if(!TrendCreate(0,m_line_high_name,0,time1_max,price1_max,time2_max,price2_max,clrRed))

        {

         m_prev_bars=0;

         return(rates_total);

        }

     }

   else

     {

      TrendPointChange(0,m_line_high_name,0,time1_max,price1_max);

      TrendPointChange(0,m_line_high_name,1,time2_max,price2_max);

     }



   if(ObjectFind(0,m_line_low_name)<0)

     {

      if(!TrendCreate(0,m_line_low_name,0,time1_min,price1_min,time2_min,price2_min,clrBlue))

        {

         m_prev_bars=0;

         return(rates_total);

        }

     }

   else

     {

      TrendPointChange(0,m_line_low_name,0,time1_min,price1_min);

      TrendPointChange(0,m_line_low_name,1,time2_min,price2_min);

     }

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

   return(rates_total);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,InpPrefix,0,OBJ_TREND);

  }

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

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

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

  }

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

//| The function deletes the trend line from the chart.              |

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

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

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

  {

//--- reset the error value

   ResetLastError();

//--- delete a trend line

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete a trend line! 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