【k线】k线图中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.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.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]],"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)

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

上一篇:js处理数字科学计数法的显示问题,完美解决数字显示中的'e'和'E'


下一篇:js+jquery实现树状目录