No line showing in my indicator

 

I just need a little help to get off the ground. I'm new to MQL5, and I have copied code from here and there. I am missing something in my code.

The code compiles, but the indicator line does not show.

Thanks.

//+------------------------------------------------------------------+
//|                                                MAIndicator01.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
// 2022-07-01 1657   MAIndicator01
//                   Initial version.
//                   Draws a line:
//                   When MAFast is below Midline, -1.
//                   When MAFast is above Midline, +1.
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property description "An indicator to show the Midline, or the midpoint between the Open and Close"
#property description "It draws a line of a specified color at the Midline"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- Line properties are set using the compiler directives
#property indicator_label1  "Line"      // Name of a plot for the Data Window
#property indicator_type1   DRAW_LINE   // Type of plotting is line
#property indicator_color1  clrRed      // Line color
#property indicator_style1  STYLE_SOLID // Line style
#property indicator_width1  1           // Line Width

//--- input parameters
input int    YellowLinePeriod =2;  // Yellow MA trend period

//--- indicator handles
int               m_handle_Midline;              // midline indicator handle
int               m_handle_YellowMA;              // moving average indicator handle

//--- indicator buffers
double            m_buff_YellowMA[];                 // YellowMA indicator buffer
double            m_buff_Midline[];                 // Midline indicator buffer

//--- indicator data for processing
double            m_Midline_1BarAgo;
double            m_YellowMA_1BarAgo;

//--- An indicator buffer for the plot
double         LineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Binding an array and an indicator buffer
   SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);

//--- create Midline indicator (White Line) and add it to collection
   if(m_handle_Midline==INVALID_HANDLE)
      if((m_handle_Midline=(PRICE_OPEN + PRICE_CLOSE) / 2)==INVALID_HANDLE)
        {
         printf("Error creating Midline indicator");
         return(false);
        }

//--- create Exponential Moving Average indicator (Yellow Line) and add it to collection
   if(m_handle_YellowMA==INVALID_HANDLE)
      if((m_handle_YellowMA=iMA(NULL,0,YellowLinePeriod,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)
        {
         printf("Error creating YellowMA indicator");
         return(false);
        }

CopyBuffer(m_handle_Midline,0,0,2,m_buff_Midline);
CopyBuffer(m_handle_YellowMA,0,0,2,m_buff_YellowMA);

ArraySetAsSeries(m_buff_Midline,true);
ArraySetAsSeries(m_buff_YellowMA,true);

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[])
  {
//--- to simplify the coding and speed up access
//--- data are put into internal variables
   m_YellowMA_1BarAgo      =m_buff_YellowMA[1];
   m_Midline_1BarAgo       =m_buff_Midline[1];

//--- Block for calculating indicator values
   for(int i=0;i<rates_total;i++)
     {
      if(m_YellowMA_1BarAgo > m_Midline_1BarAgo)
         LineBuffer[i]=+1;
      else
         if(m_YellowMA_1BarAgo < m_Midline_1BarAgo)
               LineBuffer[i]=-1;
           else LineBuffer[i]=0;
     }
 
//--- Return the prev_calculated value for the next call of the function
   return(rates_total);
  }
 
coderlen:

I just need a little help to get off the ground. I'm new to MQL5, and I have copied code from here and there. I am missing something in my code.

The code compiles, but the indicator line does not show.

Thanks.

What's the default value of "LineBuffer"?
What is this PRICE_OPEN + PRICE_CLOSEsupposed to be?
Remove if(m_handle_Midline==INVALID_HANDLE)  and if(m_handle_YellowMA==INVALID_HANDLE).

Move CopyBuffer(m_handle_Midline,0,0,2,m_buff_Midline); and CopyBuffer(m_handle_YellowMA,0,0,2,m_buff_YellowMA); inside OnCalculate function.

Optimize and debug your code. Don't loop over precalculated bars unless needed. Prevent your your indicator from such errors as if there isn't enough bars for calculation.
 

_MAHA, thanks for your reply.

I didn't understand this:

"Optimize and debug your code. Don't loop over precalculated bars unless needed. Prevent your your indicator from such errors as if there isn't enough bars for calculation."

I'm not sure how to give LineBuffer a default value.

(PRICE_OPEN + PRICE_CLOSE) / 2) is supposed to be the Midpoint between the Open and the Close price. I have developed an indicator which draws a line through the middle of each candlestick. Here is the code for that indicatorL

//+------------------------------------------------------------------+
//|                                                    Midline01.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
// 2022-07-01 1657   Removed random changing of color, width and style
//                   of the line, and allowed for dynamic changes to
//                   these properties.
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property description "An indicator to show the Midline, or the midpoint between the Open and Close"
#property description "It draws a line of a specified color at the Midline"
//#property description "Color, width and style of lines is changed randomly"
//#property description "after every N ticks"
 
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- Line properties are set using the compiler directives
#property indicator_label1  "Line"      // Name of a plot for the Data Window
#property indicator_type1   DRAW_LINE   // Type of plotting is line
#property indicator_color1  clrRed      // Line color
#property indicator_style1  STYLE_SOLID // Line style
#property indicator_width1  1           // Line Width
//--- input parameter
//input int      N=5;         // Number of ticks to change 
input 
//--- An indicator buffer for the plot
double         LineBuffer[];
//--- An array to store colors
//color colors[]={clrRed,clrBlue,clrGreen};
//--- An array to store the line styles
//ENUM_LINE_STYLE styles[]={STYLE_SOLID,STYLE_DASH,STYLE_DOT,STYLE_DASHDOT,STYLE_DASHDOTDOT};
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Binding an array and an indicator buffer
   SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);
//--- Initializing the generator of pseudo-random numbers
//   MathSrand(GetTickCount());
//---
   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[])
  {
//   static int ticks=0;
//--- Calculate ticks to change the style, color and width of the line
//   ticks++;
//--- If a critical number of ticks has been accumulated
//   if(ticks>=N)
//     {
//      //--- Change the line properties
//      ChangeLineAppearance();
//      //--- Reset the counter of ticks to zero
//      ticks=0;
//     }
 
//--- Block for calculating indicator values
   for(int i=0;i<rates_total;i++)
     {
      LineBuffer[i]=(open[i] + close[i]) / 2;      // Midpoint is average of Open and Close
     }
 
//--- Return the prev_calculated value for the next call of the function
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Changes the appearance of the drawn line in the indicator        |
//+------------------------------------------------------------------+
//void ChangeLineAppearance()
//  {
////--- A string for the formation of information about the line properties
//   string comm="";
////--- A block for changing the color of the line
////--- Get a random number
//   int number=MathRand();
////--- The divisor is equal to the size of the colors[] array
//   int size=ArraySize(colors);
////--- Get the index to select a new color as the remainder of integer division
//   int color_index=number%size;
////--- Set the color as the PLOT_LINE_COLOR property
//   PlotIndexSetInteger(0,PLOT_LINE_COLOR,colors[color_index]);
////--- Write the line color
//   comm=comm+(string)colors[color_index];
 
////--- A block for changing the width of the line
//   number=MathRand();
////--- Get the width of the remainder of integer division
//   int width=number%5; // The width is set from 0 to 4
////--- Set the color as the PLOT_LINE_WIDTH property
//   PlotIndexSetInteger(0,PLOT_LINE_WIDTH,width);
////--- Write the line width
//   comm=comm+", Width="+IntegerToString(width);
 
////--- A block for changing the style of the line
//   number=MathRand();
////--- The divisor is equal to the size of the styles array
//   size=ArraySize(styles);
////--- Get the index to select a new style as the remainder of integer division
//   int style_index=number%size;
////--- Set the color as the PLOT_LINE_COLOR property
//   PlotIndexSetInteger(0,PLOT_LINE_STYLE,styles[style_index]);
////--- Write the line style
//   comm=EnumToString(styles[style_index])+", "+comm;
////--- Show the information on the chart using a comment
//   Comment(comm);
//  }


Here is the code for my new iindicator, after applying your suggestions. It still does not draw a line. I commented out the lines you said to remove.

//+------------------------------------------------------------------+
//|                                                MAIndicator01.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
// 2022-07-01 1657   MAIndicator01
//                   Initial version.
//                   Draws a line:
//                   When MAFast is below Midline, -1.
//                   When MAFast is above Midline, +1.
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property description "An indicator to show the Midline, or the midpoint between the Open and Close"
#property description "It draws a line of a specified color at the Midline"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- Line properties are set using the compiler directives
#property indicator_label1  "Line"      // Name of a plot for the Data Window
#property indicator_type1   DRAW_LINE   // Type of plotting is line
#property indicator_color1  clrRed      // Line color
#property indicator_style1  STYLE_SOLID // Line style
#property indicator_width1  1           // Line Width

//--- input parameters
input int    YellowLinePeriod =2;  // Yellow MA trend period

//--- indicator handles
int               m_handle_Midline;              // midline indicator handle
int               m_handle_YellowMA;              // moving average indicator handle

//--- indicator buffers
double            m_buff_YellowMA[];                 // YellowMA indicator buffer
double            m_buff_Midline[];                 // Midline indicator buffer

//--- indicator data for processing
double            m_Midline_1BarAgo;
double            m_YellowMA_1BarAgo;

//--- An indicator buffer for the plot
double         LineBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- Binding an array and an indicator buffer
   SetIndexBuffer(0,LineBuffer,INDICATOR_DATA);

////--- create Midline indicator (White Line) and add it to collection
//   if(m_handle_Midline==INVALID_HANDLE)
//      if((m_handle_Midline=(PRICE_OPEN + PRICE_CLOSE) / 2)==INVALID_HANDLE)
//        {
//         printf("Error creating Midline indicator");
//         return(false);
//        }

////--- create Exponential Moving Average indicator (Yellow Line) and add it to collection
//   if(m_handle_YellowMA==INVALID_HANDLE)
//      if((m_handle_YellowMA=iMA(NULL,0,YellowLinePeriod,0,MODE_EMA,PRICE_CLOSE))==INVALID_HANDLE)
//        {
//         printf("Error creating YellowMA indicator");
//         return(false);
//        }

ArraySetAsSeries(m_buff_Midline,true);
ArraySetAsSeries(m_buff_YellowMA,true);

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[])
  {
//--- to simplify the coding and speed up access
//--- data are put into internal variables

CopyBuffer(m_handle_Midline,0,0,2,m_buff_Midline);
CopyBuffer(m_handle_YellowMA,0,0,2,m_buff_YellowMA);

   m_YellowMA_1BarAgo      =m_buff_YellowMA[1];
   m_Midline_1BarAgo       =m_buff_Midline[1];



//--- Block for calculating indicator values
   for(int i=0;i<rates_total;i++)
     {
      if(m_YellowMA_1BarAgo > m_Midline_1BarAgo)
         LineBuffer[i]=+1;
      else
         if(m_YellowMA_1BarAgo < m_Midline_1BarAgo)
               LineBuffer[i]=-1;
           else LineBuffer[i]=0;
     }
 
//--- Return the prev_calculated value for the next call of the function
   return(rates_total);
  }
 
coderlen #:

_MAHA, thanks for your reply.

I didn't understand this:

"Optimize and debug your code. Don't loop over precalculated bars unless needed. Prevent your your indicator from such errors as if there isn't enough bars for calculation."

I'm not sure how to give LineBuffer a default value.

(PRICE_OPEN + PRICE_CLOSE) / 2) is supposed to be the Midpoint between the Open and the Close price. I have developed an indicator which draws a line through the middle of each candlestick. Here is the code for that indicatorL


Here is the code for my new iindicator, after applying your suggestions. It still does not draw a line. I commented out the lines you said to remove.

I didn't say it is going to draw by removing\commenting those lines. Btw I said to remove only 2 lines, not the entire if blocks.

What do you mean how to give a LineBuffer default value? What is your indicator going to plot\indicate?

For the midline, Use iCustom to access to your previously created custom indicator handle.
But it is recommonded do not use iMA, iCustom, and other indicators function since their calculations are well known. You can code it\them inside yours.

For the loop over precalculated bars part, You are not going to calculate sth which is already calculated. Are you? But your "for loop" does, making it inefficient.

Forget what I said about optimizing... Your desire\goal is just fixing the broken\missed parts of your codes, not makeing it as perfect, effective, or functional as possible, though optimizing is extremely important.

For the debug part, You will get an "array out of range" error if there isn't enough bar for the calculation. Or wrong inputs by the user thus making indicator not to work properly. These are just a few...


Pick up an indicator source code from codebase or from mql5\indicator\examples folders. See how it is handled and organized. Then apply it in yours.

Documentation on MQL5: Technical Indicators / iCustom
Documentation on MQL5: Technical Indicators / iCustom
  • www.mql5.com
iCustom - Technical Indicators - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
coderlen #: (PRICE_OPEN + PRICE_CLOSE) / 2) is supposed to be the Midpoint b

No! They are not. They are the constants zero and one. You can use ENUM_APPLIED_PRICE to get prices in some function calls.

 
I suggest you declare and initialise EVERY buffer you have like in plots and buffers and then in SetIndexBuffer() you can choose between Indicator Data, Color and Calculation. Indicator Calculation buffers won't be plotted.
Is this new to you? 

CopyBuffer works only if you initialise the handle.
But you can use CopyHigh and CopyLow to get these values into buffers.

Nonetheless you could initialise an MA with period "1" and let it display Median. There is your midline... 
 

Many thanks to those of you who have replied. I am working diligently to take in all your suggestions. I am reworking the code completely,

as there are certain things that I did not understand before. My goal is to get the program working, and then post the code back here so

that other newbies can benefit from what I have learned.

Thanks. I'll be posting the results here, hopefully in a few days, when I get it working.

Reason: