Created
June 20, 2020 01:56
-
-
Save scott-lydon/0d89fc57f144a468e4c61bc08358089c to your computer and use it in GitHub Desktop.
ema calculation
This file contains hidden or 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
/// Given an array of prices | |
func emas(for range: Int, smoothing: Int = 2) -> [Double?] { | |
var ema: Double? | |
let multiplier: Double = Double(smoothing) / (Double(range) + 1) | |
return (0..<count).map { | |
if let last = ema { | |
//Sean? | |
//ema = last * multiplier + self[$0] * (1 - multiplier) | |
//THEbalance | |
//ema = (self[$0] - last) * multiplier + last | |
// Investopedia | |
ema = self[$0] * multiplier + last * (1 - multiplier) | |
} else { | |
ema = sma(for: range)[$0] | |
} | |
return ema | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment