Var input, macd;
Var calcEMA calcDIF, calcDEA calcMACD;
/*
* K line data
* opening price, closing price, the lowest price, highest
*/
Input=[
{low open: 3.89, close: 3.89:3.86, high: 3.93},
{low open: 3.88, close: 3.85:3.81, high: 3.89},
{low open: 3.85, close: 3.91:3.82, high: 3.95},
{low open: 3.89, close: 4.02:3.89, high: 4.07},
{low open: 4.04, close: 4.05:4.00, high: 4.08},
{low open: 4.05, close: 4.00:3.98, high: 4.08},
{low open: 4.00, close: 4.00:3.97, high: 4.04},
{low open: 3.99, close: 3.90:3.88, high: 4.00},
{low open: 3.89, close: 3.90:3.88, high: 3.92},
{low open: 3.89, close: 3.98:3.88, high: 3.98},
{low open: 3.99, close: 3.98:3.95, high: 4.03},
{low open: 3.98, close: 4.06:3.96, high: 4.08},
{low open: 4.08, close: 4.08:4.02, high: 4.08}
];
/*
* calculation EMA exponential smoothing moving averages for MACD
* @ param {number}
n time window* @ param array data input data attach
* @ param {string} field computing field configuration
*/
CalcEMA=function (n, data, field) {
Var I, l, ema, a;
A=2/(n + 1);
If (field) {
//two dimensional array
Ema=[data [0] [field]].
For (I=1, l=data. Length; i
}
} else {
//ordinary one-dimensional array
Ema=[data [0]];
For (I=1, l=data. Length; i
}
}
Return the ema.
};
/*
* computational DIF express for MACD
* @ param {number}
rapid EMA short time window* @ param {number}
long slow EMA time window* @ param array data input data attach
* @ param {string} field computing field configuration
*/
CalcDIF=function (short, long, data, field) {
Var I, l, dif, emaShort emaLong;
Dif=[];
EmaShort=calcEMA (short, data, field);
EmaLong=calcEMA (long, data, field);
For (I=0, l=data. Length; i
}
Return dif.
};
/*
* DEA slow line for MACD
* @ param {number} mid time window of dif
* @ param array dif attach input data
*/
CalcDEA=function (mids, dif) {
Return calcEMA (mids, dif);
};
/*
* calculate MACD
* @ param {number}
rapid EMA short time window* @ param {number}
long slow EMA time window* @ param {number}
mid dea time window* @ param array data input data attach
* @ param {string} field computing field configuration
*/
CalcMACD=function (short, long, mid, data, field) {
Var I, l, dif, dea, macd, result;
Result={};
Macd=[];
Dif=calcDIF (short, long, data, field);
Dea=calcDEA (mids, dif);
For (I=0, l=data. Length; i
}
Result. The dif=dif;
Result. The dea=dea;
Result. The macd=macd;
return result;
};
Macd=calcMACD (12,26,9, input, "close");
The console. The log (" DIF: ", macd. DIF);
The console. The log (" DEA: ", macd. DEA);
The console. The log (", "MACD, MACD. MACD);