Skip to content

Instantly share code, notes, and snippets.

@scott-lydon
Last active June 20, 2020 02:27
Show Gist options
  • Save scott-lydon/03ed7036a0de1924e85aec92d4a3fbd8 to your computer and use it in GitHub Desktop.
Save scott-lydon/03ed7036a0de1924e85aec92d4a3fbd8 to your computer and use it in GitHub Desktop.
tests for my ema
fileprivate struct EmaInOuts {
var prices: [Double]
var range: Int
var smoothing: Int
var resultingEMA: [Double?]
}
// Takes an array of tick values (price, close, high, open)
// price: CLOSE
// length: 180
fileprivate var inputOutputs: [EmaInOuts] = [
EmaInOuts(
prices: [1, 1, 1, 1],
range: 4,
smoothing: 2,
resultingEMA: [nil, nil, nil, 1]
),
EmaInOuts(
prices: [1, 2, 5, 3],
range: 4,
smoothing: 2,
resultingEMA: [nil, nil, nil, (1 + 2 + 5 + 3) / 4]
),
EmaInOuts(
prices: [1, 2, 5, 3],
range: 3,
smoothing: 2,
resultingEMA: [
nil,
nil,
(1 + 2 + 5) / 3,
lastItem,
]
),
]
let lastItem: Double = 3 * (Double(2) / (Double(3) + 1)) + (1 + 2 + 5) / 3 * (1 - (Double(2) / (Double(3) + 1)))
///2.833333333333333
extension TimeFountainTests {
func testema() {
inputOutputs.forEach {
let test = $0.prices.emas(for: $0.range)
let correct = $0.resultingEMA
XCTAssert(test == correct, "Failed when test: \(test) was not \(correct)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment