error in ma code

 

can someone see my error in the code below.... it's unstable and will only show 4 lines instead of 8.... i've seen other indicators that have more than 4 components , so the number of moving averages should not be the issue.... i've replaced parts of the code here and there with ideas from some of the best coders here on the forum but still the last 4 will not plot..... if you plot it and only 2 lines appear, refresh the chart ..... here is a poor reprentation of final product, i had to use 2 indicators in that image instead of 1, to show the idea...... any thoughts......thanks .....h

//+------------------------------------------------------------------+
//| moving average bars.mq4 |
//| .....h |
//| |
//+------------------------------------------------------------------+
#property copyright ".....h"
#property link ""
#property indicator_separate_window
#property indicator_minimum 1
#property indicator_maximum 10
#property indicator_buffers 16


//---- input parameters
extern int MA_Method=0; //0 Simple moving average,
//1 Exponential moving average,
//2 Smoothed moving average,
//3 Linear weighted moving average.

extern int applied_price=PRICE_CLOSE; //0 Close
//1 Open
//2 High
//3 Low
//4 Median
//5 Typical
//6 Weighted


extern int EMA1Period = 10;
extern int EMA2Period = 20;
extern int EMA3Period = 33;
extern int EMA4Period = 50;
extern int EMA5Period=89;
extern int EMA6Period=100;
extern int EMA7Period=150;
extern int EMA8Period=200;

//---- buffers
double aboveEMA1[];
double belowEMA1[];
double aboveEMA2[];
double belowEMA2[];
double aboveEMA3[];
double belowEMA3[];
double aboveEMA4[];
double belowEMA4[];
double aboveEMA5[];
double belowEMA5[];
double aboveEMA6[];
double belowEMA6[];
double aboveEMA7[];
double belowEMA7[];
double aboveEMA8[];
double belowEMA8[];
int i;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorDigits(2);
SetIndexBuffer(0, aboveEMA1);SetIndexDrawBegin(0,i-1);
SetIndexLabel (0, "EMA1");
SetIndexStyle (0, DRAW_LINE, STYLE_SOLID, 4, Red);

SetIndexBuffer(1, belowEMA1);SetIndexDrawBegin(1,i-1);
SetIndexLabel (1, "EMA1");
SetIndexStyle (1, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(2, aboveEMA2);SetIndexDrawBegin(2,i-1);
SetIndexLabel (2, "EMA2");
SetIndexStyle (2, DRAW_LINE, STYLE_SOLID, 4, Red);

SetIndexBuffer(3, belowEMA2);SetIndexDrawBegin(3,i-1);
SetIndexLabel (3, "EMA2");
SetIndexStyle (3, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(4, aboveEMA3);SetIndexDrawBegin(4,i-1);
SetIndexLabel (4, "EMA3");
SetIndexStyle (4, DRAW_LINE, STYLE_SOLID, 4, Red);

SetIndexBuffer(5, belowEMA3);SetIndexDrawBegin(5,i-1);
SetIndexLabel (5, "EMA3");
SetIndexStyle (5, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(6, aboveEMA4);SetIndexDrawBegin(6,i-1);
SetIndexLabel (6, "EMA4");
SetIndexStyle (6, DRAW_LINE, STYLE_SOLID, 4, Red);


SetIndexBuffer(7, belowEMA4);SetIndexDrawBegin(7,i-1);
SetIndexLabel (7, "EMA4");
SetIndexStyle (7, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(8, aboveEMA5);SetIndexDrawBegin(8,i-1);
SetIndexLabel (8, "EMA5");
SetIndexStyle (8, DRAW_LINE, STYLE_SOLID, 4, Red);

SetIndexBuffer(9, belowEMA5);SetIndexDrawBegin(9,i-1);
SetIndexLabel (9, "EMA5");
SetIndexStyle (9, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(10, aboveEMA6);SetIndexDrawBegin(10,i-1);
SetIndexLabel (10, "EMA6");
SetIndexStyle (10, DRAW_LINE, STYLE_SOLID, 4, Red);

SetIndexBuffer(11, belowEMA6);SetIndexDrawBegin(11,i-1);
SetIndexLabel (11, "EMA6");
SetIndexStyle (11, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(12, aboveEMA7);SetIndexDrawBegin(12,i-1);
SetIndexLabel (12, "EMA7");
SetIndexStyle (12, DRAW_LINE, STYLE_SOLID, 4, Red);

SetIndexBuffer(13, belowEMA7);SetIndexDrawBegin(14,i-1);
SetIndexLabel (13, "EMA7");
SetIndexStyle (13, DRAW_LINE, STYLE_SOLID, 4, Lime);

SetIndexBuffer(14, aboveEMA8);SetIndexDrawBegin(14,i-1);
SetIndexLabel (14, "EMA8");
SetIndexStyle (14, DRAW_LINE, STYLE_SOLID, 4, Red);


SetIndexBuffer(15, belowEMA8);SetIndexDrawBegin(15,i-1);
SetIndexLabel (15, "EMA8");
SetIndexStyle (15, DRAW_LINE, STYLE_SOLID, 4, Lime);




return(0);
}


//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{


int i, shift, limit, counted_bars = IndicatorCounted();

//i=Bars
//while(i>=0)


if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)


{
belowEMA1 [shift]= 9;
aboveEMA1 [shift]= 9;
double EmaValue = iMA(NULL, 0, EMA1Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue) aboveEMA1 [shift]= EMPTY_VALUE;
if (Close[shift]<EmaValue) belowEMA1[shift]= EMPTY_VALUE;
}

if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)


{
belowEMA2[shift] = 8;
aboveEMA2[shift] = 8;
double EmaValue2 = iMA(NULL, 0, EMA2Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue2) aboveEMA2[shift] = EMPTY_VALUE;
if (Close[shift]<EmaValue2) belowEMA2[shift] = EMPTY_VALUE;
}


if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)

{
belowEMA3[shift] = 7;
aboveEMA3[shift] = 7;
double EmaValue3 = iMA(NULL, 0, EMA3Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue3) aboveEMA3[shift] = EMPTY_VALUE;
if (Close[shift]<EmaValue3) belowEMA3[shift] = EMPTY_VALUE;
}

if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)


{
belowEMA4[shift] = 6;
aboveEMA4[shift] = 6;
double EmaValue4 = iMA(NULL, 0, EMA4Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue4) aboveEMA4[shift] = EMPTY_VALUE;
if (Close[shift]<EmaValue4) belowEMA4[shift] = EMPTY_VALUE;
}

if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)

{
belowEMA5 [shift]= 5;
aboveEMA5 [shift]= 5;
double EmaValue5 = iMA(NULL, 0, EMA5Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue5) aboveEMA5 [shift]= EMPTY_VALUE;
if (Close[shift]<EmaValue5) belowEMA5[shift]= EMPTY_VALUE;
}


if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)

{
belowEMA6[shift] = 4;
aboveEMA6[shift] = 4;
double EmaValue6 = iMA(NULL, 0, EMA6Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue6) aboveEMA6[shift] = EMPTY_VALUE;
if (Close[shift]<EmaValue6) belowEMA6[shift] = EMPTY_VALUE;
}

if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)


{
belowEMA7[shift] = 3;
aboveEMA7[shift] = 3;
double EmaValue7 = iMA(NULL, 0, EMA7Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue7) aboveEMA7[shift] = EMPTY_VALUE;
if (Close[shift]<EmaValue7) belowEMA7[shift] = EMPTY_VALUE;
}

if (counted_bars<0) return;
if (counted_bars>0) counted_bars--;
counted_bars = Bars - counted_bars;
for (shift=0; shift<counted_bars; shift++)


{
belowEMA8[shift] = 2;
aboveEMA8[shift] = 2;
double EmaValue8 = iMA(NULL, 0, EMA8Period, 0, MA_Method, applied_price, shift);
if (Close[shift]>EmaValue8) aboveEMA8[shift] = EMPTY_VALUE;
if (Close[shift]<EmaValue8) belowEMA8[shift] = EMPTY_VALUE;
}



return(0);
}


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