Formula of iStdDev on EMA SMMA LWMA

 
hi.
i want to calculate iStdDev on excel, so i tried to compare iStdDev value and my calculated value.
at first, simply period=2 and increases 3, 4...
SMA is no problem, but LWMA is not equal. (there is a correlation)
Maybe EMA and SMMA are different too.

so I wondered why.

*calculation accuracy is lowered due to calculation speed?
*formula is different? (i don't know what formula iStdDev used.)

if anyone knows the reason or inportant document(about formula) exist, please help.

The formula to calculate a weighted standard deviation is:
 sqrt( [ sigma w(xi-xbar)^2] / [ (M-1)/M) * sigma wi ] )

thanks.
 

This may help, but it is MT4.  The working may provide  a hint. 

int Length=10;

double avg=0, new_val=0, std_dev=0;

// First calculate the average, whatever type of average happens to be. This example SMA  

// Could replace the following SMA calculation with iMA() function to get StDev of EMA, LWMA, SMMA.

for (int k=0; k<=Length-1; k++) { avg += Close[i+k]; } avg=(avg/Length); // simple moving average. 



// Next calculate the Standard Deviation from that average.  

for (k=0; k<=Length-1; k++) 

{

  new_val = Close[i+k] - avg;   

  sum += new_val * new_val;

}

std_dev = MathSqrt(sum/Length);  // The result being 1x Standard Deviation which can be multiplied, as per bollinger bands.

 
genie:
SMA is no problem, but LWMA is not equal. (there is a correlation)
Maybe EMA and SMMA are different too.

if anyone knows the reason or inportant document(about formula) exist, please help.

The formula to calculate a weighted standard deviation is:
 sqrt( [ sigma w(xi-xbar)^2] / [ (M-1)/M) * sigma wi ] )
  1. Standard deviation - Wikipedia

  2. Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).
              The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io (2019)

  3. For the standard deviation from the mean, it can be calculated in one pass.

          double   sumX  = 0.0, sumX2   = 0.0;
          for(INDEX i=iBar + length-1; i >= iBar; --i){
             CHANGE   tr = TRD(i);   sumX += tr; sumX2 += tr * tr;
          }
          double   atrd     = sumX / agkLength,
                   stdd     = MathSqrt(sumX2/length - atrd*atrd),

  4. Do you really expect an answer with your Excell problem? We can't see your spreadsheet.

 
maximo #:

This may help, but it is MT4.  The working may provide  a hint. 

hi.

thanks for reply.

it will be useful :)

 
William Roeder #:
  1. Standard deviation - Wikipedia

  2. Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).
              The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io (2019)

  3. For the standard deviation from the mean, it can be calculated in one pass.


  4. Do you really expect an answer with your Excell problem? We can't see your spreadsheet.

William Roeder:

thanks for reply.
i'm checking up those reference.

> 2. Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).
oh really? i didn't know that. thanks :)

> 3. For the standard deviation from the mean, it can be calculated in one pass.
sorry. i'm not use MQL4. so i can't understood the code fully.
please gime me a time.
 
by the way.
i tried to upload my spreadsheet. but the sheet is hard to understand.
so i coded to mql5 program. (this code is for the test. verifed not enougth )

according to this.
case of SMA: MA and Deviation matches manual calculation.
case of LWMA: MA matches manual calculattion but Deviation( MA+Dev line ) is not match.

probably LWMA's formula is different :(
Files:
chart.png  9 kb
 
#property description    "deviation calc test"

//--- indicator settings
#property indicator_chart_window
#property indicator_plots   6
//--- #1
#property indicator_label1  "iMA"
#property indicator_type1   DRAW_LINE
#property indicator_style1  STYLE_DOT
#property indicator_color1  clrRed
#property indicator_width1  1
//--- #2
#property indicator_label2  "myMA"
#property indicator_type2   DRAW_LINE
#property indicator_style2  STYLE_SOLID
#property indicator_color2  clrRed
#property indicator_width2  2
//--- #3
#property indicator_label3  "iDEV"
#property indicator_type3   DRAW_NONE
#property indicator_style3  STYLE_SOLID
#property indicator_color3  clrWhite
#property indicator_width3  1
//--- #4
#property indicator_label4  "myDEV"
#property indicator_type4   DRAW_NONE
#property indicator_style4  STYLE_SOLID
#property indicator_color4  clrWhite
#property indicator_width4  1
//--- #5
#property indicator_label5  "iMA+iDEV"
#property indicator_type5   DRAW_LINE
#property indicator_style5  STYLE_DOT
#property indicator_color5  clrWhite
#property indicator_width5  1
//--- #6
#property indicator_label6  "myMA+myDEV"
#property indicator_type6   DRAW_LINE
#property indicator_style6  STYLE_SOLID
#property indicator_color6  clrWhite
#property indicator_width6  2

//--- parameter
sinput ENUM_MA_METHOD      Inp_Method = MODE_SMA;        //method
 input int                 Inp_Period = 10;              //period

//--- indicator buffer
#property indicator_buffers 6
double buf_iMA[];
double buf_myMA[];
double buf_iDEV[];
double buf_myDEV[];
double buf_iMADEV[];
double buf_myMADEV[];

//--- indicator handle
int handleMA;
int handleDEV;

//+------------------------------------------------------------------+
int OnInit(){
//--- indicator buffers mapping
   SetIndexBuffer(0,buf_iMA,INDICATOR_DATA);
   SetIndexBuffer(1,buf_myMA,INDICATOR_DATA);
   SetIndexBuffer(2,buf_iDEV,INDICATOR_DATA);
   SetIndexBuffer(3,buf_myDEV,INDICATOR_DATA);
   SetIndexBuffer(4,buf_iMADEV,INDICATOR_DATA);
   SetIndexBuffer(5,buf_myMADEV,INDICATOR_DATA);
//---
   PlotIndexSetInteger(4,PLOT_SHOW_DATA,false);
   PlotIndexSetInteger(5,PLOT_SHOW_DATA,false);
//--- indicator handle
   handleMA  = iMA(    _Symbol,_Period,Inp_Period,0,Inp_Method,PRICE_CLOSE);
   handleDEV = iStdDev(_Symbol,_Period,Inp_Period,0,Inp_Method,PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit( const int reason ){
//--- indicator release
   IndicatorRelease(handleMA);
   IndicatorRelease(handleDEV);
}
//+------------------------------------------------------------------+
int OnCalculate( const int rates_total, const int prev_calculated,
   const datetime &Time[], const double &Open[], const double &High[], const double &Low[], const double &Close[], const long &TickVolume[], const long &Volume[], const int &Spread[] ){

   if( BarsCalculated(handleMA) < 1 ) return(0);

   double buffer[];
   int start = prev_calculated-1;
   if( start < 0 ) start = 0;
   for( int i = start; i <= rates_total-1 && !IsStopped(); i++ ){
      //--- init buffer
      buf_iMA[i]  = EMPTY_VALUE; buf_iDEV[i]  = EMPTY_VALUE; buf_iMADEV[i]  = EMPTY_VALUE;
      buf_myMA[i] = EMPTY_VALUE; buf_myDEV[i] = EMPTY_VALUE; buf_myMADEV[i] = EMPTY_VALUE;
      if( i >= rates_total-1 ) continue;
      if( i < Inp_Period ) continue;

      //--- use indicator: iMA iStdDev
      double ma  = EMPTY_VALUE; if( 1 == CopyBuffer(handleMA, 0,rates_total-i-1,1,buffer) ) ma  = buffer[0];
      double dev = EMPTY_VALUE; if( 1 == CopyBuffer(handleDEV,0,rates_total-i-1,1,buffer) ) dev = buffer[0];
      buf_iMA[i] = ma; buf_iDEV[i] = dev;
      if( ma != EMPTY_VALUE && dev != EMPTY_VALUE ) buf_iMADEV[i] = ma + dev;

      //--- use manual calculate
      double length = 0.0, sumX = 0.0, sumVar2 = 0.0;
      switch(Inp_Method){
         case MODE_SMA:
            for( int j=i; (i-j)+1 <= Inp_Period; j-- ){ length  += 1.0; }
            for( int j=i; (i-j)+1 <= Inp_Period; j-- ){ sumX    += Close[j]; }
            ma = sumX / length;
            for( int j=i; (i-j)+1 <= Inp_Period; j-- ){ sumVar2 += ( Close[j] - ma ) * ( Close[j] - ma ); }
            dev = MathSqrt( sumVar2 / length );
            break;
         case MODE_LWMA:
            for( int j=i; (i-j)+1 <= Inp_Period; j-- ){ double lw = Inp_Period - (i-j); length  += lw; }
            for( int j=i; (i-j)+1 <= Inp_Period; j-- ){ double lw = Inp_Period - (i-j); sumX    += lw * Close[j]; }
            ma = sumX / length;
            for( int j=i; (i-j)+1 <= Inp_Period; j-- ){ double lw = Inp_Period - (i-j); sumVar2 += lw * ( Close[j] - ma ) * ( Close[j] - ma ); }
            dev = MathSqrt( sumVar2 / length );
            break;
         case MODE_EMA:
            //---
            break;
         case MODE_SMMA:
            //---
            break;
         default:
            break;
      }
      buf_myMA[i] = ma; buf_myDEV[i] = dev;
      if( ma != EMPTY_VALUE && dev != EMPTY_VALUE ) buf_myMADEV[i] = ma + dev;
   }
   //---
   return rates_total-1;
}
//+------------------------------------------------------------------+
 
sorry.
i have solved the problem.

iStdDev has the same formula for all modes (SMA, EMA, SMMA and LWMA).
It differs only mean(xBar).

  deviation = sqrt ( (1/n) * sigma (xi - xBar )^2 )

although i don't know why deviation of LWMA not contains weight, i can proceed.

thanks

 

I have a code to calculate smma50 [moving average smoothed 50] but in chart it seems there some some different value between my code and real indicator value

double CalculateSMMA(int index, int period)
{
    double smma = 0;

    // Calculate the sum of prices for the initial period
    for (int i = 0; i < period; i++)
    {
        smma += GetClosePrice(index + i);
    }

    // Initial SMMA is the average of the initial period
    smma /= period;

    // Calculate SMMA for subsequent periods
    for (int i = period; i > 0; i--)
    {
        smma = (smma * (period - 1) + GetClosePrice(index + period - i)) / period;
    }

    return smma; 
    
}

//define double

double smma50 = CalculateSMMA(0, 50);
Reason: