I am trying to write a user defined ema function to use on a worksheet. The code I have so far is:
Function mtgEMA(InCell As Range, period As Integer)
ema = 0
ep = 2 / (period + 1)
For i = 1 To period
ema = ema * (1 - ep) + (InCell.Offset(-i + 1, 0) * ep)
Next i
mtgEMA = ema
End Function
This works well enough with a long list of values, but the initial averages at the beginning of the list are not correct. What I really need the function to do is to calculate a Simple Moving Average for the first "Periods" and then use this calculated value as the starting number for calculating the ema. Any assistance appreciated.
~kabe~
Function mtgEMA(InCell As Range, period As Integer)
ema = 0
ep = 2 / (period + 1)
For i = 1 To period
ema = ema * (1 - ep) + (InCell.Offset(-i + 1, 0) * ep)
Next i
mtgEMA = ema
End Function
This works well enough with a long list of values, but the initial averages at the beginning of the list are not correct. What I really need the function to do is to calculate a Simple Moving Average for the first "Periods" and then use this calculated value as the starting number for calculating the ema. Any assistance appreciated.
~kabe~