Created
June 1, 2018 18:14
-
-
Save snowkidind/746efe42cedb4019603adb4426bac221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
EMA: function(originalArray, emaLength) { | |
let array = originalArray.slice().reverse(); // easier on my limited brain to think of the array in the "proper" order | |
// trim initial NaN values | |
let iPos = 0; | |
for (iPos = 0; iPos < array.length && isNaN(array[iPos]); iPos++) { | |
} | |
array = array.slice(iPos);// trim initial NaN values from array | |
let ema = []; | |
const k = 2 / (emaLength + 1); | |
for (let i = 0; i < emaLength - 1; i++) { | |
ema[i] = NaN; | |
} | |
// console.log(originalArray[0]); | |
// notice improperly formatted data bugs out here, have to JSON.parse to remove " | |
ema[emaLength - 1] = array.slice(0, emaLength).reduce(function (a, b) { | |
return a + b | |
}) / emaLength; | |
for (let i = emaLength; i < array.length; i++) { | |
ema[i] = array[i] * k + ema[i - 1] * (1 - k); | |
} | |
ema.reverse();// reverse back for main consumption | |
for (let i = 0; i < iPos; i++) { | |
ema.push(NaN); | |
} | |
return ema; | |
}, | |
MACD: function(array, i12, i26, i9, oCandle) { | |
const ema12 = module.exports.EMA(oCandle.c, i12); | |
const ema26 = module.exports.EMA(oCandle.c, i26); | |
const macd = []; | |
for (let i = 0; i < ema12.length; i++) { | |
macd.push(ema12[i] - ema26[i]); | |
} | |
const signal = module.exports.EMA(macd, i9); | |
const histogram = []; | |
for (let i = 0; i < macd.length; i++) { | |
histogram.push(macd[i] - signal[i]); | |
} | |
return {macd: macd, signal: signal, histogram: histogram}; | |
}, | |
SMA: function( array, smaLength ){ | |
array.reverse(); // easier on my limited brain to think of the array in the "proper" order | |
let sma = []; | |
for (let i=0; i<smaLength-1; i++){ | |
sma[i] = NaN; | |
} | |
sma[smaLength-1] = array.slice(0,smaLength).reduce(function(a, b) { return a + b }) / smaLength; | |
for(let i=smaLength; i<array.length; i++){ | |
sma[i] = sma[i-1] + (array[i] - array[i-smaLength]) / smaLength; | |
} | |
sma.reverse();// reverse back for main consumption | |
array.reverse();// reverse back | |
return sma; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment