Gaps in Moving Average Line

 

I'm trying to show the moving average in 2 colors.

Blue if the MA is trending up and red if trend is down.

The problem occurs when the trend is changing from up to down or vice versa.

When the trend changes, there is a gap in the MA line.

In other words the MA line is not continuous.

I need some help solving this problem.  Thanks.

#property strict
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_color1 clrDodgerBlue
#property indicator_color2 clrCrimson
#property indicator_width1 1
#property indicator_width2 1

extern int    maPeriod            = 50;          // Set MA period   
extern ENUM_MA_METHOD maMethod    = MODE_LWMA;   // Set MA method
extern ENUM_APPLIED_PRICE maPrice = PRICE_CLOSE; // Set MA price


double maUp[];       
double maDn[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {  
    SetIndexBuffer(0,maUp);  SetIndexStyle(0,DRAW_LINE);
    SetIndexBuffer(1,maDn);  SetIndexStyle(1,DRAW_LINE);    
    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 limit = MathMax(rates_total - prev_calculated-1, 1);    
  
    for(int i=1; i<=limit; i++)  // process candles right to left
    {
      double currentMA  =iMA(NULL,0,maPeriod,0,maMethod,maPrice,i);
      double previousMA =iMA(NULL,0,maPeriod,0,maMethod,maPrice,i+1);
      
      if(currentMA > previousMA) maUp[i] = currentMA;
      else                       maDn[i] = currentMA;        
    }
    return(rates_total);
  }
 
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Same as any colored line indicator. Look in the CodeBase. If you assign to one color buffer, make the other color buffer EMPTY_VALUE.
              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].
Reason: