我正在努力使用由我编写的 UDF 产生的数组作为另一个 UDF 的参数。
该函数返回#value 错误。
我不知道问题出在哪里。
下面是代码。
任何帮助/建议将不胜感激;)
这个有效..
Function fTA_GetSMA(ByRef varData As Variant, ByRef lPeriod As Long) As Variant
' This function computes a simple moving average over a defined period.
Dim l As Long
Dim dSum As Double
Dim var() As Variant
Application.Volatile
varData = varData.Value2
ReDim var(LBound(varData, 1) To UBound(varData, 1), 1)
For l = LBound(varData, 1) To UBound(varData, 1)
If l < lPeriod Then
dSum = dSum + varData(l, 1)
ElseIf l = lPeriod Then
dSum = dSum + varData(l, 1)
var(l, 1) = dSum / lPeriod
ElseIf l > lPeriod Then
dSum = dSum + varData(l, 1) - varData(l - lPeriod, 1)
var(l, 1) = dSum / lPeriod
End If
Next l
fTA_GetSMA = var
结束功能
这一个也有效。
Function fTA_GetTR(ByRef varData As Variant) As Variant ' 该函数计算金融时间序列的真实范围。' 输入数据必须是包含 O、H、L、C 的矩阵。
Dim var() As Variant
Dim l As Long
Dim dMaxTR As Double
Dim dMinTR As Double
Application.Volatile
varData = varData.Value2
ReDim var(LBound(varData, 1) To UBound(varData, 1), 1)
For l = LBound(varData, 1) To UBound(varData, 1)
If l = 1 Then
dMinTR = varData(l, 3)
dMaxTR = varData(l, 2)
ElseIf l > 1 Then
dMaxTR = Application.WorksheetFunction.Max(varData(l, 2), varData(l - 1, 4))
dMinTR = Application.WorksheetFunction.Min(varData(l, 3), varData(l - 1, 4))
End If
var(l, 1) = dMaxTR - dMinTR
Next l
fTA_GetTR = var
结束功能
这是一个不工作的..
Function fTA_GetATR(ByRef varData As Variant, ByRef lPeriod As Long) As Variant ' 该函数计算给定周期数内金融时间序列的平均真实范围。'输入数据必须是一个包含O、H、L、C的矩阵。'平均公式是一个SMA
Dim var() As Variant
Dim varATR() As Variant
ReDim var(LBound(varData, 1) To UBound(varData, 1), 1)
ReDim varATR(LBound(varData, 1) To UBound(varData, 1), 1)
var = fTA_GetTR(varData)
varATR = fTA_GetSMA(var, lPeriod)
Debug.Print varATR
fTA_GetATR = varATR
结束功能