Rss & SiteMap

金字塔客服中心 - 专业程序化交易软件提供商 http://www.weistock.com/bbs/

专业程序化软件提供商
共2 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:[分享]vba 加权平均数

1楼
lizhi 发表于:2019/4/18 21:48:07
'计算加权平均数
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       '加权平均数
 '调试代码
 Application.ClearMsg   '清除消息窗口中的输出值
 Application.MsgOut(sumLength) '输出sumLength的值 
 Application.MsgOut(WMA)   '输出WMA的值
 '------------------------------------------------------------------------
 Set histroyData = Nothing '从内存中释放对象变量
 '-------------------------------------------------------------------------
 MsgBox "脚本运行的时间=" & Timer - StarTimer & "秒。"
End Sub
2楼
lizhi 发表于:2019/4/18 21:55:06

'==============计算加权平均数=======================
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

共2 条记录, 每页显示 10 条, 页签: [1]


Powered By Dvbbs Version 8.3.0
Processed in 0.03125 s, 2 queries.