colored MA Indicator- req some correction

 

Hello friends this is checkmail and after searching a lot found this color MA indicator but its having an bug or something is missing in the code.The line is breaking in between on the charts.

Can anyone help in improving the code, here is the code :

 

 

#property copyright ""

#property link ""


#property indicator_chart_window


#property indicator_buffers 3

#property indicator_color1 Lime

#property indicator_color2 Red

#property indicator_color3 Yellow


extern int Period1=12;

extern int Period2=24;

extern int Flat=1;

extern color ColorUp=Lime;

extern color ColorDown=Red;

extern color ColorFlat=Yellow;

extern int Width=2;



double Line1[], Line2[], Line3[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,Width,ColorUp);//DRAW_LINE

SetIndexBuffer(0,Line1);

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,Width,ColorDown);

SetIndexBuffer(1,Line2);

SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,Width,ColorFlat);//DRAW_LINE

SetIndexBuffer(2,Line3);

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

//----

int i, Counted_bars=IndicatorCounted();

i=Bars-Counted_bars-1; //


while(i>=0) //

{

double MA_0=iMA(NULL,0,Period1,0,MODE_EMA,PRICE_CLOSE,i),

MA_2=iMA(NULL,0,Period2,0,MODE_EMA,PRICE_CLOSE,i+1);

if(MA_0>MA_2) Line1[i]=MA_0;

else

if(MA_0<MA_2) Line2[i]=MA_0;

else if(MA_0==MA_2)

Line3[i]=MA_0;

i--;

}

//----

return(0);

}

//+------------------------------------------------------------------+

#property copyright ""

#property link ""



#property indicator_chart_window



#property indicator_buffers 3

#property indicator_color1 Lime

#property indicator_color2 Red

#property indicator_color3 Yellow



extern int Period1=12;

extern int Period2=24;

extern int Flat=1;

extern color ColorUp=Lime;

extern color ColorDown=Red;

extern color ColorFlat=Yellow;

extern int Width=2;





double Line1[], Line2[], Line3[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,Width,ColorUp);//DRAW_LINE

SetIndexBuffer(0,Line1);

SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,Width,ColorDown);

SetIndexBuffer(1,Line2);

SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,Width,ColorFlat);//DRAW_LINE

SetIndexBuffer(2,Line3);

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()

{

//----

int i, Counted_bars=IndicatorCounted();

i=Bars-Counted_bars-1; //



while(i>=0) //

{

double MA_0=iMA(NULL,0,Period1,0,MODE_EMA,PRICE_CLOSE,i),

MA_2=iMA(NULL,0,Period2,0,MODE_EMA,PRICE_CLOSE,i+1);

if(MA_0>MA_2) Line1[i]=MA_0;

else

if(MA_0<MA_2) Line2[i]=MA_0;

else if(MA_0==MA_2)

Line3[i]=MA_0;

i--;

}

//----

return(0);

}

//+------------------------------------------------------------------+
 
The line is breaking in between on the charts.
  1. You must connect the new color line to the previous color line
  2. Don't compare doubles for equality.
Line1[i] = EMPTY_VALUE;
Line2[i] = EMPTY_VALUE;
Line3[i] = EMPTY_VALUE;
     if(MA_0>MA_2+Point){ Line1[i]=MA_0; Line1[i+1] = MA_2; }
else if(MA_0<MA_2-Point){ Line2[i]=MA_0; Line2[i+1] = MA_2; }
else                    { Line3[i]=MA_0; Line3[i+1] = MA_2; }
Reason: