指标
MACD指标:
12日EMA的计算:EMA12 = 前一日EMA12×11/13 + 今日收盘×2/13
26日EMA的计算:EMA26 = 前一日EMA26×25/27 + 今日收盘×2/27
差离值(DIFF)的计算: DIFF = EMA12 - EMA26 。
离差平均值(DEA值): 今日DEA = (前一日DEA×8/10 + 今日DIFF×2/10)
MACD=(DIF-DEA)*2
初始的DIF、DEA、MACD值均为0,首个EMA12和EMA26的值第一个交易日的收盘价。
function GenMADCData(data,startIndex,count) { var result = {}; var diffArr = new Array(); var deaArr = new Array(); var macdArr = new Array(); var lastEma12 = data[0].close; //首个EMA12和EMA26的值第一个交易日的收盘价 var lastEma26 = data[0].close; //首个EMA12和EMA26的值第一个交易日的收盘价 var diff; var dea; for (var i = 0; i < data.length; i++) { if (i === 0) { diffArr.push(0); deaArr.push(0); macdArr.push(0); } else { var ema12 = lastEma12 * 11 / 13 + data[i].close * 2 / 13; //EMA12 = 前一日EMA12×11/13 + 今日收盘×2/13 var ema26 = lastEma26 * 25 / 27 + data[i].close * 2 / 27; //EMA26 = 前一日EMA26×25 / 27 + 今日收盘×2 / 27 diff = ema12 - ema26; //差离值(DIFF)的计算: DIFF = EMA12 - EMA26 //记录上一次ema lastEma12 = ema12; lastEma26 = ema26; //生成dea dea = deaArr[i - 1] * 8 / 10 + diff * 2 / 10; var macd = (diff - dea) * 2; diffArr.push(diff); deaArr.push(dea); macdArr.push(macd); } } for (var i = 0; i < startIndex; i++) { diffArr.shift(); deaArr.shift(); macdArr.shift(); } for (var i = 0; i < data.length - startIndex - count; i++) { diffArr.pop(); deaArr.pop(); macdArr.pop(); } result.DiffArr = diffArr; result.DeaArr = deaArr; result.MacdArr = macdArr; window.Diff = diffArr; window.Dea = deaArr; window.Macd = macdArr; return result; }
RSI相对强弱指标:
RSI6:SMA(MAX(CLOSE-LC,0),N1,1)/SMA(ABS(CLOSE-LC),N1,1)*100;
RSI12:SMA(MAX(CLOSE-LC,0),N2,1)/SMA(ABS(CLOSE-LC),N2,1)*100;
RSI24:SMA(MAX(CLOSE-LC,0),N3,1)/SMA(ABS(CLOSE-LC),N3,1)*100;
CLOSE为收盘价,LC为昨收盘价,SMA(X,N,M) N1,N2,N3分别为6,12,24,M为权重1,Y=(X*M+前一日Y*(N-M))/N
首个交易日Y为0,指标显示为RSI6:- 、RSI12:- 、RSI24:-;
纵坐标位置分别取【0.00;20.00;50.00;80.00;100.00】
function GenRSIData(data, startIndex, count) { var result = {}; var rsi6Arr = new Array(); var rsi12Arr = new Array(); var rsi24Arr = new Array(); var lastY1 = 0; var lastY2 = 0; rsi6Arr.push(NaN); rsi12Arr.push(NaN); rsi24Arr.push(NaN); // getRsi6 for (var i = 1; i < data.length; i++) { var riseRange = data[i].close - data[i - 1].close; var X1; var X2; var Y1; var Y2; var val = 0; if (riseRange >= 0) { X1 = riseRange; X2 = riseRange; } else { X1 = 0; X2 = (-1 * riseRange); } var Y1 = (X1 + lastY1 * 5) / 6; var Y2 = (X2 + lastY2 * 5) / 6; if (Y2 == 0) { val = 100; } else { val = Y1 / Y2 * 100; } rsi6Arr.push(val); lastY1 = Y1; lastY2 = Y2; } // getRsi12 lastY1 = 0; lastY2 = 0; for (var i = 1; i < data.length; i++) { var riseRange = data[i].close - data[i - 1].close; var X1; var X2; var Y1; var Y2; var val = 0; if (riseRange >= 0) { X1 = riseRange; X2 = riseRange; } else { X1 = 0; X2 = (-1 * riseRange); } var Y1 = (X1 + lastY1 * 11) / 12; var Y2 = (X2 + lastY2 * 11) / 12; if (Y2 == 0) { val = 100; } else { val = Y1 / Y2 * 100; } rsi12Arr.push(val); lastY1 = Y1; lastY2 = Y2; } // getRsi24 lastY1 = 0; lastY2 = 0; for (var i = 1; i < data.length; i++) { var riseRange = data[i].close - data[i - 1].close; var X1; var X2; var Y1; var Y2; var val = 0; if (riseRange >= 0) { X1 = riseRange; X2 = riseRange; } else { X1 = 0; X2 = (-1 * riseRange); } var Y1 = (X1 + lastY1 * 23) / 24; var Y2 = (X2 + lastY2 * 23) / 24; if (Y2 == 0) { val = 100; } else { val = Y1 / Y2 * 100; } rsi24Arr.push(val); lastY1 = Y1; lastY2 = Y2; } for (var i = 0; i < startIndex; i++) { rsi6Arr.shift(); rsi12Arr.shift(); rsi24Arr.shift(); } for (var i = 0; i < data.length - startIndex - count; i++) { rsi6Arr.pop(); rsi12Arr.pop(); rsi24Arr.pop(); } result.Rsi6Arr = rsi6Arr; result.Rsi12Arr = rsi12Arr; result.Rsi24Arr = rsi24Arr; window.Rsi6 = rsi6Arr; window.Rsi12 = rsi12Arr; window.Rsi24 = rsi24Arr; return result; }
KDJ随机指标:
n日RSV=(Cn-Ln)÷(Hn-Ln)×100
公式中,Cn为第n日收盘价;Ln为n日内的最低价;Hn为n日内的最高价。
其次,计算K值与D值:
当日K值=2/3×前一日K值+1/3×当日RSV
当日D值=2/3×前一日D值+1/3×当日K值
J值=3*当日K值-2*当日D值
初始的K、D值,取当日RSV的值
纵坐标位置分别取【0.00;20.00;50.00;80.00;100.00】
function GetKDJData(filedata, startIndex, count) { var result = {}; var kl = new Array(); var dl = new Array(); var jl = new Array(); var rsv; var cn; var ln; var hn; for (var i = 0; i < filedata.length; i++) { if (0 === i) { ln = filedata[0].low; hn = filedata[0].high; } else if(i<10) { ln = Math.min(ln, filedata[i].low); hn = Math.max(hn, filedata[i].high); } else { ln = filedata[i].low; hn = filedata[i].high; for (j = i - 8; j < i; j++) { ln = Math.min(ln, filedata[j].low); hn=Math.max(hn,filedata[j].high); } } cn = filedata[i].close; if (hn === ln) rsv = 100; else rsv = (cn - ln) / (hn - ln) * 100; if (0 === i) { //rsv = rsv < 0 ? 0 : (rsv > 100 ? 100 : rsv); kl.push(rsv); dl.push(rsv); jl.push(rsv); } else { //K var itemk = kl[i - 1] * 2 / 3 + rsv / 3; //itemk = itemk < 0 ? 0 : (itemk > 100 ? 100 : itemk); kl.push(itemk); //D var itemd = dl[i - 1] * 2 / 3 + itemk * 1 / 3; //itemd = itemd < 0 ? 0 : (itemd > 100 ? 100 : itemd); dl.push(itemd); //J var itemj = itemk * 3 - itemd * 2; //itemj = itemj < 0 ? 0 : (itemj > 100 ? 100 : itemj); jl.push(itemj); } } for (var i = 0; i < startIndex; i++) { kl.shift(); dl.shift(); jl.shift(); } for (var i = 0; i < filedata.length - startIndex - count; i++) { kl.pop(); dl.pop(); jl.pop(); } result.Kl = kl; result.Dl = dl; result.Jl = jl; window.Kl = kl; window.Dl = dl; window.Jl = jl; return result; }