【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实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
1457 1
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
|
2月前
|
数据可视化 数据挖掘 Python
绘制带误差分析的柱状图
【9月更文挑战第1天】在数据分析与科研中,带误差分析的柱状图能直观展示数据分布与不确定性。本文介绍使用Python的Matplotlib库和Excel绘制此类图表的方法,包括安装库、准备数据、绘制图表及添加误差线等步骤,帮助用户根据需求进行调整与定制。
|
6月前
|
数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
绘制GGPLOT2双色XY区间面积图组合交叉折线图数据可视化
|
数据挖掘
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
这图怎么画| 批量小提琴图+箱线图+散点+差异分析
312 0
|
数据挖掘
这图怎么画| 箱线图+散点+中位数连线
这图怎么画| 箱线图+散点+中位数连线
131 0
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
533 0
跟着Nat Commun学作图 | 4.配对箱线图+差异分析
|
数据可视化
R|散点图+边际图(柱形图,小提琴图),颜值区UP
R|散点图+边际图(柱形图,小提琴图),颜值区UP
148 0
|
算法
对分类数据绘制对应的ROC曲线和AUC面积指标
对分类数据绘制对应的ROC曲线和AUC面积指标
278 0
对分类数据绘制对应的ROC曲线和AUC面积指标
|
数据可视化 数据挖掘 Python
跟着Nature学作图:R语言ggplot2柱形图添加误差线和频率分布直方图
跟着Nature学作图:R语言ggplot2柱形图添加误差线和频率分布直方图
|
数据可视化 数据挖掘 Python
跟着Nature学作图:R语言ggplot2频率分布直方图和散点图添加误差线
跟着Nature学作图:R语言ggplot2频率分布直方图和散点图添加误差线