'==============计算加权平均数=======================
Sub myWMA
Dim starTimer
starTimer = Timer '脚本开始运行的时间
'------------------------------------------------------------------------
Dim histroyData,barsCount,i,n,length,sumPrice,sumLength
'声明沪深300指数历史日线数据对象
Set histroyData = marketdata.GetHistoryData("000300","SH",5)
barsCount = histroyData.Count '沪深300指数的数据数量
length = 200 '计算均值的周期数
sumPrice = 0 '给变量赋初始值
n = 0 '给变量赋初始值
For i = barsCount - length To barsCount - 1
n = n + 1 '循环次数
sumPrice = sumPrice + n * histroyData.Close(i) '价格加权求和
Next
sumLength = (Length + 1) * Length / 2 '加权系数求和
WMA = sumPrice / sumLength '加权平均数
'调试代码---------------------------------------------------------------
With Application
.ClearMsg '清除消息窗口中的输出值
.MsgOut(sumLength) '输出sumLength的值
.MsgOut(WMA) '输出WMA的值
End With
'------------------------------------------------------------------------
Set histroyData = Nothing '从内存中释放对象变量
'-------------------------------------------------------------------------
MsgBox "脚本运行的时间=" & Timer - starTimer & "秒。"
End Sub