indicator periods problem

 

I am coding an indicator based on moving averages I have

extern int PERIOD = 5;

if i run my indicator twice on the same chart and change the external input of one of them to 10 everything appears fine at first, it correctly positions 2 separate lines on the chart but as new bars appear the lines converge and run almost on top of each other as if they are both set to 5 periods.

If i remove the 10 periods indicator and reopen it at 10 periods again, the last part of the chart where the lines were almost on top of each other is now drawn correctly but the same thing happens again when new bars appear they converge again at the first new bar and stay almost on top of each other again.

Its like it recognises the first indicator is 5 periods and the second instance is 10 periods when drawing old bars, but when drawing current bars it is doing it wrong, I dont understand this as the indicator doesnt have different code for drawing the current bar it should be drawing the current bar MA by the same code as all the rest

Anyone have any idea what might be causing this to happen ?

Here is the code as you can see it is not finished, it is experimental, the reason for all the multiple instances of each variable/3 is because I was going to experiment by mixing the result from the other lines calculations into them, as the code is right now those parts look suplufluous because they are all still set to the same line.

//+------------------------------------------------------------------+
//|                                                     TESTMA 5.mq4 |
//|                                                              SDC |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "SDC"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  EMPTY
#property indicator_style1  STYLE_SOLID
#property indicator_color2  Red
#property indicator_style2  STYLE_DOT
#property indicator_color3  EMPTY
#property indicator_style3  STYLE_SOLID
#property indicator_color4  EMPTY
#property indicator_style4  STYLE_DOT
#property indicator_color5  Silver
#property indicator_style5  STYLE_DOT
//---- input parameters
extern int PERIOD   = 5;
extern int History  = 500;
//---- indicator buffers
double Buffer_a[];
double Buffer_b[];
double Buffer_c[];
double Buffer_d[];
double Buffer_e[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorShortName("TESTMA4("+PERIOD+")");
   SetIndexBuffer(0,Buffer_a);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(1,Buffer_b);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(2,Buffer_c);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(3,Buffer_d);
   SetIndexBuffer(3,DRAW_LINE);
   SetIndexStyle(4,Buffer_e);
   SetIndexStyle(4,DRAW_LINE);
  }
//+------------------------------------------------------------------+
int start()
  {
   int counted=IndicatorCounted();
   int i=Bars-counted-1;
   
//----

   static double lastMA_a, lastMA_of_MA_a,
                 lastMA_b, lastMA_of_MA_b,
                 lastMA_c, lastMA_of_MA_c,
                 lastMA_d, lastMA_of_MA_d;
   double weight1=2.0/(1+PERIOD);
   double weight2=3.0/((Close[i]-Open[i]*Point)+PERIOD);
   
//----
   if (i>History-1)
      i = History-1;
        
        
   while(i>=0)
     {
      lastMA_a = weight2*(High[i] + Close[i+1])/2 + (1.0-weight2)*(lastMA_a + lastMA_a + lastMA_a + lastMA_a)/4;   //red solid
      lastMA_b = weight2*(High[i] + Open[i])/2 + (1.0-weight2)*(lastMA_b + lastMA_b + lastMA_a + lastMA_a)/4;   //red dot
      lastMA_c = weight2*(Low[i] + Close[i+1])/2 + (1.0-weight2)*(lastMA_c + lastMA_c + lastMA_c + lastMA_c)/4;  //green solid
      lastMA_d = weight2*(Low[i] + Open[i])/2 + (1.0-weight2)*(lastMA_d + lastMA_d + lastMA_d + lastMA_d)/4; //green dot
      
      lastMA_of_MA_a = weight2*(lastMA_a + lastMA_a + lastMA_a)/3 +
      (1.0-weight2)*(lastMA_of_MA_a + lastMA_of_MA_a + lastMA_of_MA_a)/3; //red solid
      
      lastMA_of_MA_b = weight2*(lastMA_b + lastMA_b + lastMA_b)/3 +
      (1.0-weight2)*(lastMA_of_MA_b + lastMA_of_MA_b + lastMA_of_MA_b)/3; //red dot
      
      lastMA_of_MA_c = weight2*(lastMA_c + lastMA_c + lastMA_c)/3 +
      (1.0-weight2)*(lastMA_of_MA_c + lastMA_of_MA_c + lastMA_of_MA_c)/3; //green solid
      
      lastMA_of_MA_d = weight2*(lastMA_d + lastMA_d + lastMA_d)/3 +
      (1.0-weight2)*(lastMA_of_MA_d + lastMA_of_MA_d + lastMA_of_MA_d)/3; //green dot
      
      Buffer_a[i]=2.0*lastMA_a - (lastMA_of_MA_a + lastMA_of_MA_a)/2; //red solid
      Buffer_b[i]=2.0*lastMA_b - (lastMA_of_MA_b + lastMA_of_MA_b)/2; //red dot
      Buffer_c[i]=2.0*lastMA_c - (lastMA_of_MA_c + lastMA_of_MA_c)/2; //green solid
      Buffer_d[i]=2.0*lastMA_d - (lastMA_of_MA_d + lastMA_of_MA_d)/2; //green 
      
      i--;
      
     }

//----

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

//+------------------------------------------------------------------+
Reason: