M5 Line on M1

Author: Copyright © 2021, Vladimir Karputov
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
M5 Line on M1
ÿþ//+------------------------------------------------------------------+

//|                                                M5 Line on M1.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.003"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input int      InpBars     = 100;               // Max bars

input color    InpColor    = clrMediumSeaGreen; // Line color

input int      InpWidth    = 2;                 // Line width

//---

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

string   m_prefix       = "M5LOM1_";

bool     m_init_error   = false; // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_VLINE);

//---

   if(Period()!=PERIOD_M1)

     {

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

                      ""09<D@59< 3@0D8:0 4>;65= 1KBL 'M1'!":

                      "Chart timeframe must be 'M1'!";

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

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_VLINE);

  }

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

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

//---

   ArraySetAsSeries(time,true);

//---

   int limit=0;

   if(prev_calculated>0)

      limit=rates_total-prev_calculated+1;

   if(prev_calculated==0)

     {

      if(rates_total<InpBars)

         limit=0;

      else

         limit=InpBars;

     }

   for(int i=limit; i>=0; i--)

     {

      int i_bar_shift_prev=iBarShift(Symbol(),PERIOD_M5,time[i+1],false);

      if(i_bar_shift_prev<0)

         return(0);

      int i_bar_shift=iBarShift(Symbol(),PERIOD_M5,time[i],false);

      if(i_bar_shift<0)

         return(0);

      //---

      if(i_bar_shift_prev>i_bar_shift)

        {

         if(m_last_time<time[i])

           {

            m_last_time=time[i];

            string name=TimeToString(time[i],TIME_DATE|TIME_MINUTES);

            long chart_id=ChartID();

            //--- create a vertical line

            ObjectCreate(chart_id,m_prefix+name,OBJ_VLINE,0,time[i],0);

            //--- set line color

            ObjectSetInteger(chart_id,m_prefix+name,OBJPROP_COLOR,InpColor);

            //--- set line width

            ObjectSetInteger(chart_id,m_prefix+name,OBJPROP_WIDTH,InpWidth);

           }

        }

     }

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

   return(rates_total);

  }

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

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