Created
March 14, 2021 00:25
-
-
Save xeoncross/c7c1eeb4b25964594c4fc7b35efc6f87 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
const LONG = 26; | |
const SHORT = 12; | |
const MID = 9; | |
function sum(inputs) { | |
return inputs.reduce((s, v) => s + v, 0); | |
} | |
function average(N, inputs) { | |
return sum(inputs.slice(0, N)) / N; | |
} | |
function EMA(N, inputs = []) { | |
const avg = average(N, inputs); | |
return inputs.slice(N).reduce( | |
(outs, price, i) => { | |
const prev = outs[outs.length - 1]; | |
outs[i + 1] = (2 * price + (N - 1) * prev) / (N + 1); | |
return outs; | |
}, | |
[avg] | |
); | |
} | |
function doubleEMA(M, N, inputs) { | |
return ((M - 1) * EMA(M, inputs) - (N - 1) * EMA(N, inputs)) / (M - N); | |
} | |
function DIF(inputs = []) { | |
const short = EMA(SHORT, inputs); | |
const long = EMA(LONG, inputs); | |
const diff = short.length - long.length; | |
return short.reduce((outs, s, i) => { | |
outs[i] = i < diff ? 0 : s - long[i - diff]; | |
return outs; | |
}, []); | |
} | |
function DEA(inputs = []) { | |
return EMA(MID, DIF(inputs)); | |
} | |
function MACD(inputs = []) { | |
const dif = DIF(inputs); | |
const dea = DEA(inputs); | |
const diff = dif.length - dea.length; | |
return dif.reduce((outs, m, i) => { | |
outs[i] = i < diff ? 0 : (m - dea[i - diff]) * 2; | |
return outs; | |
}, []) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment