【k线】k线图中MA均线计算

简介: 移动平均线,Moving Average,简称MA,MA是用统计分析的方法,将一定时期内的证券价格(指数)加以平均,并把不同时间的平均值连接起来,形成一根MA,用以观察证券价格变动趋势的一种技术指标
移动平均线,Moving Average,简称MA,MA是用统计分析的方法,将一定时期内的证券价格(指数)加以平均,并把不同时间的平均值连接起来,形成一根MA,用以观察证券价格变动趋势的一种技术指标。

封装函数

//数据模型 time0 open1 close2 min3 max4 vol5 tag6 macd7 dif8 dea9

//MA计算公式
function calculateMA(dayCount, data) {

var result = [];
for (var i = 0, len = data.values.length; i < len; i++) {
    if (i < dayCount) {
        result.push('-');
        continue;
    }
    var sum = 0;
    for (var j = 0; j < dayCount; j++) {
        sum += data.values[i - j][1];
    }
    result.push(+(sum / dayCount).toFixed(3));
}
return result;

}

//调用方法
var data={"datas":[[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.1,7.1,7.1,7.1],[7.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1,7.1,7.1]],"times":[["2019-05-18 01:30:00"],["2019-05-18 01:45:00"],["2019-05-18 02:00:00"],["2019-05-18 02:15:00"],["2019-05-18 02:30:00"],["2019-05-18 02:45:00"],["2019-05-18 03:00:00"],["2019-05-18 03:15:00"],["2019-05-18 03:30:00"],["2019-05-18 03:45:00"],["2019-05-18 04:00:00"],["2019-05-18 04:15:00"],["2019-05-18 04:30:00"],["2019-05-18 04:45:00"],["2019-05-18 05:00:00"],["2019-05-18 05:15:00"],["2019-05-18 05:30:00"],["2019-05-18 05:45:00"],["2019-05-18 06:00:00"],["2019-05-18 06:15:00"],["2019-05-18 06:30:00"],["2019-05-18 06:45:00"],["2019-05-18 07:00:00"],["2019-05-18 07:15:00"],["2019-05-18 07:30:00"],["2019-05-18 07:45:00"],["2019-05-18 08:00:00"],["2019-05-18 08:15:00"],["2019-05-18 08:30:00"],["2019-05-18 08:45:00"],["2019-05-18 09:00:00"],["2019-05-18 09:15:00"],["2019-05-18 09:30:00"],["2019-05-18 09:45:00"],["2019-05-18 10:00:00"],["2019-05-18 10:15:00"],["2019-05-18 10:30:00"],["2019-05-18 10:45:00"],["2019-05-18 11:00:00"],["2019-05-18 11:15:00"],["2019-05-18 11:30:00"],["2019-05-18 11:45:00"],["2019-05-18 12:00:00"],["2019-05-18 12:15:00"],["2019-05-18 12:30:00"],["2019-05-18 12:45:00"],["2019-05-18 13:00:00"],["2019-05-18 13:15:00"],["2019-05-18 13:30:00"],["2019-05-18 13:45:00"],["2019-05-18 14:00:00"],["2019-05-18 14:15:00"],["2019-05-18 14:30:00"],["2019-05-18 14:45:00"],["2019-05-18 15:00:00"],["2019-05-18 15:15:00"],["2019-05-18 15:30:00"],["2019-05-18 15:45:00"],["2019-05-18 16:00:00"],["2019-05-18 16:15:00"],["2019-05-18 16:30:00"],["2019-05-18 16:45:00"],["2019-05-18 17:00:00"],["2019-05-18 17:15:00"],["2019-05-18 17:30:00"],["2019-05-18 17:45:00"],["2019-05-18 18:00:00"],["2019-05-18 18:15:00"],["2019-05-18 18:30:00"],["2019-05-18 18:45:00"],["2019-05-18 19:00:00"],["2019-05-18 19:15:00"],["2019-05-18 19:30:00"],["2019-05-18 19:45:00"],["2019-05-18 20:00:00"],["2019-05-18 20:15:00"],["2019-05-18 20:30:00"],["2019-05-18 20:45:00"],["2019-05-18 21:00:00"],["2019-05-18 21:15:00"],["2019-05-18 21:30:00"],["2019-05-18 21:45:00"],["2019-05-18 22:00:00"],["2019-05-18 22:15:00"],["2019-05-18 22:30:00"],["2019-05-18 22:45:00"],["2019-05-18 23:00:00"],["2019-05-18 23:15:00"],["2019-05-18 23:30:00"],["2019-05-18 23:45:00"],["2019-05-19 00:00:00"],["2019-05-19 00:15:00"],["2019-05-19 00:30:00"],["2019-05-19 00:45:00"],["2019-05-19 01:00:00"],["2019-05-19 01:15:00"]],"vols":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"macds":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"difs":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"deas":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}

//MA5
calculateMA(data,5)
//MA10
calculateMA(data,10)
//MA20
calculateMA(data,20)

需要注意数据模型的格式,不同的数据模型的计算大同小异,注意数据对应就好

目录
相关文章
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
1490 1
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
|
7月前
|
数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
跟着 Cell 学作图 | 分组蜂群图+均值线+差异分析(组间+组内)
跟着 Cell 学作图 | 分组蜂群图+均值线+差异分析(组间+组内)
178 0
|
数据挖掘
这图怎么画| 箱线图+散点+中位数连线
这图怎么画| 箱线图+散点+中位数连线
138 0
|
数据挖掘
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
324 0
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
544 0
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
|
数据可视化
R|散点图+边际图(柱形图,小提琴图),颜值区UP
R|散点图+边际图(柱形图,小提琴图),颜值区UP
152 0
|
数据可视化 数据挖掘 Python
跟着Nature学作图:R语言ggplot2柱形图添加误差线和频率分布直方图
跟着Nature学作图:R语言ggplot2柱形图添加误差线和频率分布直方图
|
数据可视化 数据挖掘 Python
跟着Nature学作图:R语言ggplot2频率分布直方图和散点图添加误差线
跟着Nature学作图:R语言ggplot2频率分布直方图和散点图添加误差线
|
Python
使用dash和plotly画出sin函数随振幅、角频率、相位变化的关系
使用dash和plotly画出sin函数随振幅、角频率、相位变化的关系
129 0
使用dash和plotly画出sin函数随振幅、角频率、相位变化的关系