High Low N Bar

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

//|                                               High Low N Bar.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.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   2

//--- plot High

#property indicator_label1  "High"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrTomato

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Low

#property indicator_label2  "Low"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrSlateBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- input parameters



input group             "Bar"

input uchar                InpBarN              = 1;           // Bar 'N'

input group             "Arrow"

input uchar                Inp_Arrow_High_Code  = 159;         // High: symbol code from the Wingdings font

input int                  Inp_Arrow_High_Shift = 5;           // High: vertical shift of arrows in pixels

input uchar                Inp_Arrow_Low_Code   = 159;         // Low: symbol code from the Wingdings font

input int                  Inp_Arrow_Low_Shift  = 5;           // Low: vertical shift of arrows in pixels

//--- indicator buffers

double   HighBuffer[];

double   LowBuffer[];

//---

int      day_of_year    = -1;

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

double   n_bar_high     = 0.0;

double   n_bar_low      = 0.0;

int      counter        = 0;        // only if 'Bar 'N'' is greater than zero

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

bool     m_init_error   = false;    // error on InInit

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- forced initialization of variables

   day_of_year    = -1;

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

   n_bar_high     = 0.0;

   n_bar_low      = 0.0;

   counter        = 0;     // only if 'Bar 'N'' is greater than zero

   m_prev_bars    = 0;     // "0" -> D'1970.01.01 00:00', only if 'Bar 'N'' is greater than zero

   m_init_error   = false; // error on InInit

//---

   if(Period()>PERIOD_D1)

     {

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

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

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

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

     }

//--- indicator buffers mapping

   SetIndexBuffer(0,HighBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,LowBuffer,INDICATOR_DATA);

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   PlotIndexSetInteger(0,PLOT_ARROW,Inp_Arrow_High_Code);

   PlotIndexSetInteger(1,PLOT_ARROW,Inp_Arrow_Low_Code);

//--- set the vertical shift of arrows in pixels

   PlotIndexSetInteger(0,PLOT_ARROW_SHIFT,-Inp_Arrow_High_Shift);

   PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,Inp_Arrow_Low_Shift);

//--- set as an empty value 0.0

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

//---

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

      return(0);

//---

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=0;

   for(int i=limit; i<rates_total; i++)

     {

      HighBuffer[i]=0.0;

      LowBuffer[i]=0.0;

      //---

      MqlDateTime STime;

      TimeToStruct(time[i],STime);

      if(STime.day_of_year!=day_of_year) // new day

        {

         day_of_year    = STime.day_of_year;

         if(InpBarN==0)

           {

            time_n_bar  = time[i];

            n_bar_high  = high[i];

            n_bar_low   = low[i];

            counter     = 0;

            m_prev_bars = 0;

           }

         else

           {

            time_n_bar  = 0;

            n_bar_high  = 0.0;

            n_bar_low   = 0.0;

            counter     = 0;

            m_prev_bars = time[i];

           }

        }

      else // inside the day

        {

         if(InpBarN==0)

           {

            if(time_n_bar==time[i])

              {

               n_bar_high=high[i];

               n_bar_low=low[i];

              }

           }

         else

           {

            if(m_prev_bars<time[i]) // new bar

              {

               counter++;

               m_prev_bars=time[i];

              }

            if(counter==InpBarN || time_n_bar==time[i])

              {

               time_n_bar=time[i];

               n_bar_high=high[i];

               n_bar_low=low[i];

              }

           }

        }

      HighBuffer[i]=n_bar_high;

      LowBuffer[i]=n_bar_low;

     }

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