Problem making indicator disappear from the chart

 

Hello Folks,


I'm making an indicators that draws Daily Moving average and a horizontal line at the value of the last bar of the moving average

I made a boolean variable (switch) for each line to control when both lines should be drawn (when the value of the switch =True then draw the lines on the chart, when the value is false the indicator should disappear)

The problem is when the switch value is false,I tried to set the indicator value to be EMPTY_VALUE or ZERO but the indicator doesn't disappear from the chart

Any idea how to solve this problem or what I'm doing wrong? 

Help is really appreciated!

#property copyright   "2009-2020, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "Moving Average with horizontal line"
#include <MovingAverages.mqh>
//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE

#property indicator_color1  Silver
#property indicator_color2  Red

#property indicator_width1  1
#property indicator_width2  1
#property indicator_label1  "MA"
#property indicator_label2  "Horizontal line"

//--- input parameters
input int                MAPeriod=12;              
input ENUM_APPLIED_PRICE MAAppliedPrice=PRICE_CLOSE; // Applied price
input bool draw_switch = false;
//--- indicator buffers
double MABuffer[];
double MAHorizontalLine[];


int    MaHandle;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,MAHorizontalLine,INDICATOR_DATA);
   
        

//--- name for indicator subwindow label
   string short_name=StringFormat("MA(%d) With horizontal line",MAPeriod);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- get MA handles
   MaHandle=iMA(NULL,PERIOD_D1,MAPeriod,0,MODE_EMA,MAAppliedPrice);
   
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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(rates_total<MAPeriod)
      return(0);
//--- not all data may be calculated
   int calculated=BarsCalculated(MaHandle);
   if(calculated<rates_total)
     {
      Print("Not all data of MaHandle is calculated (",calculated," bars). Error ",GetLastError());
      return(0);
     }
   

   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0)
      to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0)
         to_copy++;
     }

   if(IsStopped()) // checking for stop flag
      return(0);
   if(CopyBuffer(MaHandle,0,0,to_copy,MABuffer)<=0)
     {
      Print(MaHandle," Getting MA failed! Error ",GetLastError());
      return(0);
     }

   int start;
   if(prev_calculated==0)
      start=0;
   else
      start=prev_calculated-1;

   for(int i=start; i<rates_total && !IsStopped(); i++)
     {
      
      if(draw_switch)
	//setting all the horizontal line values to be same as last value of MA buffer
         MAHorizontalLine[i] = MABuffer[rates_total-1];
      else
         MAHorizontalLine[i] = EMPTY_VALUE;
     }

//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
 
  1.    for(int i=start; i<rates_total && !IsStopped(); i++)

    You are processing indexes as non-series. You never set your buffers to non-series before adding values.


  2. Your code
       for(int i=start; i<rates_total && !IsStopped(); i++)
         {
          if(draw_switch)
            //setting all the horizontal line values to be same as last value of MA buffer
             MAHorizontalLine[i] = MABuffer[rates_total-1];
          else
             MAHorizontalLine[i] = EMPTY_VALUE;
         }
    
    Simplified
          if(draw_switch)   
            for(int i=start; i<rates_total && !IsStopped(); i++)
         {
             MAHorizontalLine[i] = MABuffer[rates_total-1];
         }
    
  3.    if(prev_calculated==0)
          start=0;
       else
          start=prev_calculated-1;

    None of this is necessary if you return the proper value.
              How to do your lookbacks correctly #9 - #14 & #19

 
William Roeder:You are processing indexes as non-series. You never set your buffers to non-series before adding values.

@William Roeder, this is not the MQL4 section. MQL5 by default treats buffers as non-series.

 
William Roeder:
  1. You are processing indexes as non-series. You never set your buffers to non-series before adding values.


  2. Your code
    Simplified
  3. None of this is necessary if you return the proper value.
              How to do your lookbacks correctly #9 - #14 & #19

Thank you so much william for your reply.

I set the arrays as non timeseries and tried the simplified code but no change  still facing the same problem

also I faced another issue I'm trying to draw the horizontal line of the daily moving average as described in the code, I added the monthly line too (same as the daily and added its own switch).

But when I move to the smaller time frames (4hrs or below)the lines get so weird and messed up I tried a lot of different ways of debugging but I don't know what might be wrong.

Your help is truly appreciated.

The first photo shows how the horizontal line of monthly moving average is drawn on the chart when on the monthly timeframe. and the second one shows what happens when I move to weekly or any lower timeframe

Files:
1stphoto.jpg  85 kb
2ndPhoto.jpg  107 kb