Need a little help

 

Hi gurus!


I'd like to write into a file Moving Averages datas. This is my wrong code (it makes an empty file):

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- indicator parameters
extern int MA_Period=13;
//---- indicator buffers
double ExtMapBuffer[];
//----
int ExtCountedBars=0;
extern int    handle;
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
      
   handle=FileOpen("MA.data", FILE_CSV|FILE_READ|FILE_WRITE, ';'); 
//----
   return(0);
  }
  
 
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
   FileClose(handle);
//----
   return(0);
  }
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=MA_Period) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
//----
   
   
   ema();  
   
     
//---- done
   return(0);
  }
 
//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
void ema()
  {
   double pr=2.0/(MA_Period+1);
   int    pos=Bars-2;
   
   if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
//---- main calculation loop
   while(pos>=0)
     {
      if(pos==Bars-2) ExtMapBuffer[pos+1]=Close[pos+1];
      ExtMapBuffer[pos]=Close[pos]*pr+ExtMapBuffer[pos+1]*(1-pr);
      if(handle>0)
        {
          FileSeek(handle, 0, SEEK_END);
          FileWrite(handle, ExtMapBuffer[pos]);
        }
      pos--;
     }
     
  }
  
  
  
 
//+------------------------------------------------------------------+

Can you help me by a working code?

Thank you!

 

I tested your code.

The file created is not empty, it contains line after line of "0".

Check your logic and calculattions.