Psuedo code for complex(?) financial algorithm?
I am really new at this so don’t beat me please. 
In the following image is a fairly complex financial volatility model I am using (well complex to me at least)
For a scalar  time series x0 , x1, · ··, xn ,
Model calculation image is at http://postimage.org/image/9qtb8b38/
where  Greek delta- and  Greek delta+ are calling different up and down volatility bands respectively (similar to Bollinger bands); sigma () is a selector, i.e. sigma (x) =1 if x>0, else sigma (x) = 0 ; and xhat superscript n is a moving average (MA)

I came up with this pseudo Metatrader 4 MQL4 4 code so let me know where I am going wrong. 

	double UpDownVol()
	{ 	
	//---- indicator buffers
		
	  double UpBuffer[];
	  double DownBuffer[];
	  double MiddleBuffer[];
	  double ATR;
      int limit;
      int counter;
      int    counted_bars=IndicatorCounted();
      double mean;
      double meanSum;
      int k=2; //usually ranges between 1 to 3 
      
      int delta;      
      double diffUp, diffLow,volSquaredUp,volSquaredUp; //vol is volatility
      
      if(counted_bars<0) return(-1);

      if(counted_bars>0) counted_bars--;
      limit=Bars-counted_bars;

      //calculate the Middle band 
      for(shift=limit;shift>=1;shift-- )
        {
         mmin=Low[Lowest(NULL,0,MODE_LOW,per,shift)];
         mmax=High[Highest(NULL,0,MODE_HIGH,per,shift)];
         ATR=0;
         for(counter=Period_ATR;counter>=1;counter--)
           {
            ATR=ATR+MathAbs(High[shift+counter]-Low[shift+counter]);
           }
         ATR=ATR/Period_ATR;
         MiddleBuffer[shift-1]=(mmax+mmin)/2;

        }
        
      //calculate average mean   
      for(shift=limit;shift>=1;shift-- )
        {
		   meanSum += MiddleBuffer[shift];
        }
	 
	   mean = meanSum/limit;
	   
	   //calculate the upper and lower volatility bands
	   
      for(shift=limit;shift>=1;shift-- )
        {
         diffUp=MiddleBuffer[Bars]-mean; 
         if(diffUp==1 && MiddleBuffer[Bars]>0) //maybe should be MiddleBuffer[i-1]?
           delta=0;
         else
	        delta=1;
   
         volSquaredUp=1/(diffUp*diffUp*delta); //squared volatility for Up
         
         diffDown=MiddleBuffer[Bars]-mean; 
         if(diffDown==1 && MiddleBuffer[Bars]>0) //maybe should be MiddleBuffer[i-1]?
           delta=0;
         else
	        delta=1;
	     
         volSquaredDown=1/(diffDown*diffDown*delta); //squared volality for Up

         //calculate upper and lower bands
         UpBuffer[shift-1]=MiddleBuffer[shift-1] + mean + (k * MathSqrt(volSquaredUp));
         DownBuffer[shift-1]=MiddleBuffer[shift-1] - mean + (k * MathSqrt(volSquaredDown));
         Print("Corrected mean average=",mean, " up=",UpBuffer[shift-1], " low=",DownBuffer[shift-1], " sr Up=",MathSqrt(volSquaredUp)," sr Down=",MathSqrt(volSquaredDown));
        
        }
	   

	   return(meanSum/limit);
   }

Thanks for all the help. 

Psuedo code for complex(?) financial algorithm?
===

I am really new at this so don’t beat me please. 
In the following image is a fairly complex financial volatility model I am using (well complex to me at least)
For a scalar  time series x0 , x1, · ··, xn ,
Model calculation image is at http://postimage.org/image/9qtb8b38/
where  Greek delta- and  Greek delta+ are calling different up and down volatility bands respectively (similar to Bollinger bands); sigma () is a selector, i.e. sigma (x) =1 if x>0, else sigma (x) = 0 ; and xhat superscript n is a moving average (MA)

I came up with this pseudo Metatrader 4 MQL4 4 code so let me know where I am going wrong. 

	double UpDownVol()
	{ 	
	//---- indicator buffers
		
	  double UpBuffer[];
	  double DownBuffer[];
	  double MiddleBuffer[];
	  double ATR;
      int limit;
      int counter;
      int    counted_bars=IndicatorCounted();
      double mean;
      double meanSum;
      int k=2; //usually ranges between 1 to 3 
      
      int delta;      
      double diffUp, diffLow,volSquaredUp,volSquaredUp; //vol is volatility
      
      if(counted_bars<0) return(-1);

      if(counted_bars>0) counted_bars--;
      limit=Bars-counted_bars;

      //calculate the Middle band 
      for(shift=limit;shift>=1;shift-- )
        {
         mmin=Low[Lowest(NULL,0,MODE_LOW,per,shift)];
         mmax=High[Highest(NULL,0,MODE_HIGH,per,shift)];
         ATR=0;
         for(counter=Period_ATR;counter>=1;counter--)
           {
            ATR=ATR+MathAbs(High[shift+counter]-Low[shift+counter]);
           }
         ATR=ATR/Period_ATR;
         MiddleBuffer[shift-1]=(mmax+mmin)/2;

        }
        
      //calculate average mean   
      for(shift=limit;shift>=1;shift-- )
        {
		   meanSum += MiddleBuffer[shift];
        }
	 
	   mean = meanSum/limit;
	   
	   //calculate the upper and lower volatility bands
	   
      for(shift=limit;shift>=1;shift-- )
        {
         diffUp=MiddleBuffer[Bars]-mean; 
         if(diffUp==1 && MiddleBuffer[Bars]>0) //maybe should be MiddleBuffer[i-1]?
           delta=0;
         else
	        delta=1;
   
         volSquaredUp=1/(diffUp*diffUp*delta); //squared volatility for Up
         
         diffDown=MiddleBuffer[Bars]-mean; 
         if(diffDown==1 && MiddleBuffer[Bars]>0) //maybe should be MiddleBuffer[i-1]?
           delta=0;
         else
	        delta=1;
	     
         volSquaredDown=1/(diffDown*diffDown*delta); //squared volality for Up

         //calculate upper and lower bands
         UpBuffer[shift-1]=MiddleBuffer[shift-1] + mean + (k * MathSqrt(volSquaredUp));
         DownBuffer[shift-1]=MiddleBuffer[shift-1] - mean + (k * MathSqrt(volSquaredDown));
         Print("Corrected mean average=",mean, " up=",UpBuffer[shift-1], " low=",DownBuffer[shift-1], " sr Up=",MathSqrt(volSquaredUp)," sr Down=",MathSqrt(volSquaredDown));
        
        }
	   

	   return(meanSum/limit);
   }

Thanks for all the help. 
