希望这比昨天更容易阅读。试图找到一种方法来改变用于测量“波动性”的周期“ N”的数目,下面是昨天建议的完整函数的代码,并将“ N”固定为10。该函数对于KAMA很好用,默认值为“ N”(在此默认版本的KAMA函数中,不需要N0Addr和N1Addr,但需要获取变量“ N”的步骤)
此公式在Excel中有效:
=SUMPRODUCT((ABS(I26:I36-I25:I35)))
我还可以获得两个范围内的正确差值之和,但不是绝对值。此VBA代码使用命名范围“ N0Addr”和“ N1Addr”来做到这一点:
Rng0 = WorksheetFunction.Sum(Range(N0Addr)) - WorksheetFunction.Sum(Range(N1Addr))
Function nTEST(Price, nPer, mPer, N)
'Variables
Fast = 2 / (nPer + 1)
Slow = 2 / (mPer + 1)
'One(1) Prior Period Calculation
nTEST1 = Application.Caller.Offset(-1)
N0Addr = Application.WorksheetFunction.Concat(Price.Offset(-N, 0).Address & ":" & Price.Address)
N1Addr = Application.WorksheetFunction.Concat(Price.Offset(-(N + 1), 0).Address & ":" & (Price.Offset(-1, 0).Address))
'Change Formula (Y - Yn)
E = Abs(Price - Price.Offset(-N, 0))
'Volatility Formula { =SUM(ABS(Y:Yn)-(Y1:Yn1))) }
'VOLATILITY (N = 10)
'1-10
R = Abs(Price - Price.Offset(-1, 0)) + Abs(Price.Offset(-1, 0) - Price.Offset(-2, 0)) _
+ Abs(Price.Offset(-2, 0) - Price.Offset(-3, 0)) _
+ Abs(Price.Offset(-3, 0) - Price.Offset(-4, 0)) + Abs(Price.Offset(-4, 0) - Price.Offset(-5, 0)) _
+ Abs(Price.Offset(-5, 0) - Price.Offset(-6, 0)) + Abs(Price.Offset(-6, 0) - Price.Offset(-7, 0)) _
+ Abs(Price.Offset(-7, 0) - Price.Offset(-8, 0)) + Abs(Price.Offset(-8, 0) - Price.Offset(-9, 0)) _
+ Abs(Price.Offset(-9, 0) - Price.Offset(-10, 0))
'EFFICIENCY RATIO
ER = E / R
Smooth = (ER * (Fast - Slow) + Slow) ^ 2
'Formula Calculation
nKAMA = Smooth * Price + (1 - Smooth) * nKAMA1
End Function
寻找一个VBA公式或方法来为“波动率”输入一个可以在N个周期内变化的有效公式。我可以得到差异的总和,但不能得到绝对差异的总和。
Rng0 = WorksheetFunction.Sum(Range(N0Addr)) - WorksheetFunction.Sum(Range(N1Addr))
我还可以在Excel中输入一个公式,该公式完成即可提供绝对差之和。
=SUMPRODUCT((ABS(I26:I36-I25:I35)))