Colored Moving Average - page 2

 
Arthur Singer:

Hello, I have developed a Moving Average, which consists of two lines with different colors. However, I have the problem that when the color changes, there is a gap. Is there a way to fill this gap? Many thanks in advance.

The way you write this code and especially the for loop is wrong. see example codes in meta-trader to learn more.  

 
Mladen Rakic:

Don't. That will cause a classical repaint

If you want colors in separate buffers, then use the mode applied in mt4 nrp indicators - and then you shall need at least 3 buffers to make it non-repainting - but if you plan to use multiple buffers to check trend, then you are on a wrong path (it is much easier to only read a color buffer then to use multiple buffers to try to find out a "trend"/"slope")

I tried, and it's almost working. But I have the problem that the change is not shown at the beginning.

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                        Copyright 2019, Arthur S. |
//|                    https://www.mql5.com/en/users/michael12345678 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Arthur S."
#property link      "https://www.mql5.com/en/users/michael12345678"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 3

#property indicator_label1 "MovingAverage"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrBlue,clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2

#property indicator_label2 "Up"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrAqua

#property indicator_label3 "Down"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrHotPink

int MovingAverage;

double MovingAverageBuffer[];
double MovingAverageColor[];

double UpBuffer[];
double DownBuffer[];

input int Periode=14;
input ENUM_MA_METHOD Methode=MODE_EMA;
input ENUM_APPLIED_PRICE Preis=PRICE_CLOSE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,MovingAverageBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,MovingAverageColor,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,UpBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,DownBuffer,INDICATOR_DATA);
   
   MovingAverage=iMA(_Symbol,_Period,Periode,0,Methode,Preis);
   if(MovingAverage==INVALID_HANDLE)
   {
      Print("The iMA object was not created: Error ",GetLastError());
      return INIT_FAILED;
   }
   
   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[])
{
   ArraySetAsSeries(MovingAverageBuffer,true);
   ArraySetAsSeries(MovingAverageColor,true);
   ArraySetAsSeries(UpBuffer,true);
   ArraySetAsSeries(DownBuffer,true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(close,true);
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   
   int copied=CopyBuffer(MovingAverage,0,0,rates_total,MovingAverageBuffer);
   
   int limit=!prev_calculated?rates_total-Periode-1:rates_total!=prev_calculated?rates_total-prev_calculated-1:0;
   
   for(int i=limit;i>=0 && !_StopFlag;i--)
   {
      UpBuffer[i]=EMPTY_VALUE;
      DownBuffer[i]=EMPTY_VALUE;
      
      MovingAverageColor[i]=MovingAverageBuffer[i]>MovingAverageBuffer[i+1]?0:1;
      
      if(MovingAverageColor[i]==0 && MovingAverageColor[i]!=1)
      {
         UpBuffer[i]=low[i]-0.00030;
      }
      else if(MovingAverageColor[i]==1 && MovingAverageColor[i]!=0)
      {
         DownBuffer[i]=high[i]+0.00030;
      }
   }
   
   return(rates_total);
}
//+------------------------------------------------------------------+
 
   int limit=!prev_calculated?rates_total-Periode-1:rates_total!=prev_calculated?rates_total-prev_calculated-1:0;
   for(int i=limit;i>=0 && !_StopFlag;i--)
     {
      MovingAverageColor[i]=MovingAverageBuffer[i]>MovingAverageBuffer[i+1]?0:1;
      UpBuffer[i]=MovingAverageColor[i]==0 && MovingAverageColor[i+1]==1?low[i]-300*_Point:EMPTY_VALUE;
      DownBuffer[i]=MovingAverageColor[i]==1 && MovingAverageColor[i+1]==0?high[i]+300*_Point:EMPTY_VALUE;
     }
 
Ernst Van Der Merwe:

Many Thanks!!!

Reason: