Skip to content

Instantly share code, notes, and snippets.

@scott-lydon
Created June 20, 2020 01:56
Show Gist options
  • Save scott-lydon/0d89fc57f144a468e4c61bc08358089c to your computer and use it in GitHub Desktop.
Save scott-lydon/0d89fc57f144a468e4c61bc08358089c to your computer and use it in GitHub Desktop.
ema calculation
/// 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