Why do I get "no lines" in some spots?

 

Take a look at the picture.

Broken Lines

Here's the code:


//+------------------------------------------------------------------+
//|                                                HL Volitility.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot HLV
#property indicator_label1  "HLV"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      MAPeriod=4;
//--- indicator buffers
double         HLVBuffer[];
double         HL[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
   SetIndexBuffer(0,HLVBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,HL,INDICATOR_CALCULATIONS);
   SetIndexLabel(1,NULL);
   IndicatorDigits(_Digits);
   
//---
   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[])
  {
//---
   int    i,limit;
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
     {
      limit++;
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HL[i]=high[i]-low[i];
        }
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HLVBuffer[i]=iMAOnArray(HL,0,MAPeriod,0,MODE_SMMA,i);
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Not quite sure what I'm doing wrong here.

 
Michael:

Take a look at the picture.

Here's the code:


Not quite sure what I'm doing wrong here.

This is because the black line (HL) is displayed on the screen. You need to rewrite the program so that this is not displayed.

 
Michael: Take a look at the picture. Here's the code: Not quite sure what I'm doing wrong here.

Remember that you are using MQL4 and not MQL5:

#property indicator_separate_window

// Define Number of Buffers and Plots
#define _PLOTS      1
#define _BUFFERS    2

#ifdef __MQL4__
   #property indicator_buffers   _PLOTS
#else
   #property indicator_buffers   _BUFFERS
   #property indicator_plots     _PLOTS
#endif

// ...

int OnInit()
{
   #ifdef __MQL4__   // Set Total Number of Buffers (MQL4 Only)
      IndicatorBuffers( _BUFFERS );
   #endif
   
   SetIndexBuffer(0,HLVBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,HL,INDICATOR_CALCULATIONS); // INDICATOR_CALCULATIONS is accepted, but has no effect in MQL4
   SetIndexLabel(1,NULL);
   IndicatorDigits(_Digits);
   return(INIT_SUCCEEDED);
}
 

If you are only targeting MQL4, then you can simplify to this (use code in my previous post for targeting both platforms):

#property indicator_separate_window

// Define Number of Buffers and Plots
#define _PLOTS      1
#define _BUFFERS    2

#property indicator_buffers _PLOTS

// ...

int OnInit()
{
   IndicatorBuffers( _BUFFERS );
   SetIndexBuffer(0,HLVBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,HL,INDICATOR_CALCULATIONS); // INDICATOR_CALCULATIONS is accepted, but has no effect in MQL4
   SetIndexLabel(1,NULL);
   IndicatorDigits(_Digits);
   return(INIT_SUCCEEDED);
}
 
Fernando Carreiro:

If you are only targeting MQL4, then you can simplify to this (use code in my previous post for targeting both platforms):

Thanks.  I see my mistake now.

So, let's expand on it.  Say I wanted to do an "up" and "down" kind of thing.  See this pic:

updown

.. And this is the code:

//+------------------------------------------------------------------+
//|                                                HL Volitility.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
// Define Number of Buffers and Plots
#define _PLOTS      2
#define _BUFFERS    4
#property indicator_buffers _PLOTS
#property indicator_plots   2
//--- plot HLV
#property indicator_label1  "HLVup"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_label2  "HLVdown"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input int      MAPeriod=4;
//--- indicator buffers
double         HLVUpBuffer[];
double         HLVDownBuffer[];
double         HLVBuffer[];
double         HL[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(_BUFFERS);
   SetIndexBuffer(0,HLVUpBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,HLVDownBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,HLVBuffer,INDICATOR_CALCULATIONS);
   SetIndexLabel(2,NULL);
   SetIndexBuffer(3,HL,INDICATOR_CALCULATIONS);
   SetIndexLabel(3,NULL);
   IndicatorDigits(_Digits);
   
//---
   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[])
  {
//---
   int    i,limit;
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
     {
      limit++;
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HL[i]=high[i]-low[i];
        }
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HLVBuffer[i]=iMAOnArray(HL,0,MAPeriod,0,MODE_SMMA,i);
        }
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         if(HLVBuffer[i]>HLVBuffer[i+1])
           {
            HLVUpBuffer[i]=HLVBuffer[i];
           }
         if(HLVBuffer[i]<HLVBuffer[i+1])
           {
            HLVDownBuffer[i]=HLVBuffer[i];
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

So, I've got the buffers and plots figured out, but I'm getting hiccups in doing it this way.

PS.  I'm targeting only MQL4.

 
Michael: Thanks.  I see my mistake now.

You are welcome, but it was @Nagisa Unada that first noticed the problem. I just provided the code sample after he identified it.

 
Fernando Carreiro:

You are welcome, but it was @Nagisa Unada that first noticed the problem. I just provided the code sample after he identified it.

Oops.  Thanks @Nagisa Unada

 
Michael: So, let's expand on it.  Say I wanted to do an "up" and "down" kind of thing.  See this pic: .. And this is the code:

So, I've got the buffers and plots figured out, but I'm getting hiccups in doing it this way. PS.  I'm targeting only MQL4.

When drawing the line, you have to include the previous data point or else nothing is drawn (a line has a starting point and and end point).

if(HLVBuffer[i]>HLVBuffer[i+1])
  {
   HLVUpBuffer[i  ]=HLVBuffer[i  ];
   HLVUpBuffer[i+1]=HLVBuffer[i+1];     // Requires previous data point to draw line
  }
if(HLVBuffer[i]<HLVBuffer[i+1])
  {
   HLVDownBuffer[i  ]=HLVBuffer[i  ];
   HLVDownBuffer[i+1]=HLVBuffer[i+1];   // Requires previous data point to draw line
  }

I did not have time to fully analyse all of your code, but you seem to have other logic problems as well, and I think some of the segments will not draw properly (because of plots overlapping each other).

You are also mixing old style MQL4 logic with new style MQL4+. Try to do everything in the new style which is more compatible to the MQL5 way of doing things.
 

If you assign to one color buffer, make the other color buffer(s) EMPTY_VALUE. Then connect to the previous bar.
          HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum 2016.07.09

For MT4 I use:

  1. One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)
  2. Two buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.
  3. Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].
 

Thanks Fernando and William!  With your guys expertise, I got it.

Here's the finished code:

//+------------------------------------------------------------------+
//|                                                HL Volitility.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
// Define Number of Buffers and Plots
#define _PLOTS      3
#define _BUFFERS    4
#property indicator_buffers _PLOTS
#property indicator_plots   3
//--- plot HLV
#property indicator_label2  "HLVup"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrLime
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
#property indicator_label3  "HLVdown"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
#property indicator_label1  "HLV"
#property indicator_type1   DRAW_NONE
#property indicator_color1  clrNONE
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//--- input parameters
input int      MAPeriod=4;
//--- indicator buffers
double         HLVUpBuffer[];
double         HLVDownBuffer[];
double         HLVBuffer[];
double         HL[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(_BUFFERS);
   SetIndexBuffer(1,HLVUpBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,HLVDownBuffer,INDICATOR_DATA);
   SetIndexBuffer(0,HLVBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,HL,INDICATOR_CALCULATIONS);
   IndicatorDigits(_Digits);
   
//---
   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[])
  {
//---
   int    i,limit;
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
     {
      limit++;
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HL[i]=high[i]-low[i];
        }
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         HLVBuffer[i]=iMAOnArray(HL,0,MAPeriod,0,MODE_SMMA,i);
        }
     }
   for(i=limit; i>=0; i--)
     {
      if(i>Bars(Symbol(),0)-MAPeriod*2)
        {
         continue;
        }
      else
        {
         if(HLVBuffer[i]>HLVBuffer[i+1])
           {
            HLVUpBuffer[i]=HLVBuffer[i];
            HLVUpBuffer[i+1]=HLVBuffer[i+1];
           }
         if(HLVBuffer[i]<HLVBuffer[i+1])
           {
            HLVDownBuffer[i]=HLVBuffer[i];
            HLVDownBuffer[i+1]=HLVBuffer[i+1];
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Michael: Thanks Fernando and William!  With your guys expertise, I got it. Here's the finished code:
Please note that your code still has problems. There will be situations when there is an up-down-up-down which will cause lines segments to all draw and one of the buffer plots will overlap the other making all the segments "red".