Memory Usage of iStdDevOnArray

 
When i add an indicator using iStdDevOnArray to my chart, MT4 memory jumps by 10megabytes


Open "empty" MT4, 24 megabytes in use.

Add indicator below, un-comment iMAOnArray function, compile.

Memory usage barely changed, still 24 megabytes.

Comment out iMAOnArray and un-comment iStdDevOnArray. Recompile.

MT4 memory usage jumps to 34 megabytes.

Is this necessay? 10 charts with 3 such indicators adds 300meg to MT4 memory usage. I have become fond of standard deviations recently.


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
 
//---- buffers
double lineBuffer[];
 
double testBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,lineBuffer); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  
   ArrayResize(testBuffer, Bars);
   ArraySetAsSeries(testBuffer, true);
  
   for(int i = Bars-1; i >= 0; i--){
       testBuffer[i] = Close[i];
   }
   for(    i = Bars-50; i >= 0; i--){
  
       // TEST -- Pick one or the other below, monitor memory usage in TaskManager
      
      
       // Memory requirement samll for this indicator
       //lineBuffer[i] = iMAOnArray(testBuffer, 0, 30, 0, 0, i);
      
      
       // Memory requirement 10 megabyte per chart for this indicator
       // lineBuffer[i] = iStdDevOnArray(testBuffer, 0, 30, 0, 0, i);
 
   }
   return(0);
}
 
Add into function start()
Comment("Bars=",Bars);
How many bars are there on your charts?
 
The chart had 2000 bars.
 
Thank you, we will check it.
 
Here is updated test indicator...

Change between iStdDeviation line and iMAOnArray line using option.

Memory requirement jumps up and down with change


#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
 
extern bool STD_DEV_LINE = false;
 
//---- buffers
double lineBuffer[];
double testBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,lineBuffer); 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
  
   Comment("\n Bars = ", Bars, "  Time is ", TimeToStr(LocalTime(), TIME_SECONDS));
  
   ArrayResize(testBuffer, Bars);
   ArraySetAsSeries(testBuffer, true);
  
   for(int i = Bars-1; i >= 0; i--){
       testBuffer[i] = Close[i];
   }
   for(    i = Bars-50; i >= 0; i--){
  
       // Memory requirement small for this indicator
        if(STD_DEV_LINE == false) lineBuffer[i] = iMAOnArray(testBuffer, 0, 30, 0, 0, i);
             
       // Memory requirement large for this indicator
        if(STD_DEV_LINE == true)  lineBuffer[i] = iStdDevOnArray(testBuffer, 0, 30, 0, 0, i);
 
   }  
   return(0);
}
 
Bug is fixed, thank you very much! New build will be available soon.
 
BUG! How about that..
 

Yes, bug. iStdevOnArray started again and again and recalculated every time. Thank You. Your code has helped to discover old problem

 

Hi stringo,

Is there any performance difference between the standard iStdevOnArray() and a custom function like MathStdev()? Is iStdevOnArray() in MT4 optimized in some way?

//+------------------------------------------------------------------+
//| Calculates the standard deviation of a set of values in an array.
//| MathStdev() measures how widely values are dispersed from the
//| average value (the mean).
//+------------------------------------------------------------------+
double MathStdev(double &array[])
{
   int size = ArraySize(array);
   double sum,
          stdev,
          mean = MathAvg(array);
   for (int i = 0; i < size; i++)
      sum += MathPow((array[i] - mean), 2);
   stdev = MathSqrt(sum / (size -1));
   return (stdev);
}

//+------------------------------------------------------------------+
//| Calculates the average value (the mean) of a set of values in an
//| array.
//+------------------------------------------------------------------+
double MathAvg(double &array[])
{
   int size = ArraySize(array);
   double sum,
          mean;
   for (int i = 0; i < size; i++)
      sum += array[i];
   mean = sum / size;
   return (mean);
}

Kind regards,


Scott

Reason: