Skip to content

Commit 39c4f77

Browse files
authored
Merge pull request #3 from michaelibrd/feature/candle-support
Adding support for candles (ohlc)
2 parents c46a8fd + 795e221 commit 39c4f77

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Candle.swift
3+
//
4+
//
5+
// Created by stringcode on 05/04/2021.
6+
//
7+
8+
import Foundation
9+
10+
public typealias CandleList = [Candle]
11+
12+
public struct Candle: Codable {
13+
public let date: Date
14+
public let open: Double
15+
public let high: Double
16+
public let low: Double
17+
public let close: Double
18+
19+
public init(arrayData: [Double]) {
20+
date = Date(timeIntervalSince1970: Double(arrayData[0]) / 1000)
21+
open = arrayData[1]
22+
high = arrayData[2]
23+
low = arrayData[3]
24+
close = arrayData[4]
25+
}
26+
27+
public init(date: Date, open: Double, high: Double, low: Double, close: Double) {
28+
self.date = date
29+
self.open = open
30+
self.high = high
31+
self.low = low
32+
self.close = close
33+
}
34+
}

Sources/CoinGecko/Resources.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public enum Endpoint: String {
1515

1616
case coinsList = "/coins/list"
1717
case coinsMarketChart = "/coins/%@/market_chart"
18-
case coin = "/coins/%@"
18+
case coin = "/coins/%@"
19+
case ohlc = "/coins/%@/ohlc"
1920
}
2021

2122
public enum Resources {}
@@ -81,4 +82,23 @@ extension Resources {
8182
URLQueryItem(name: "days", value: "\(days)")]
8283
return Resource(.coinsMarketChart, method: .GET, pathParam: currencyId, params: params, completion: callback)
8384
}
85+
86+
// Coin Gecko API returns candles of diffrent duration based on `days` parameter
87+
// ```
88+
// 1 - 2 days: 30 minutes
89+
// 3 - 30 days: 4 hours
90+
// 31 and before: 4 days
91+
// ```
92+
public static func candles<CandleList>(currencyId: String, vs: String, days: Int, callback: @escaping Callback<CandleList>) -> Resource<CandleList> {
93+
let params = [URLQueryItem(name: "vs_currency", value: vs),
94+
URLQueryItem(name: "days", value: "\(days)")]
95+
96+
let parse: (Data) -> CandleList = { data in
97+
let decoder = JSONDecoder()
98+
let arrayData = try? decoder.decode([[Double]].self, from: data)
99+
return (arrayData ?? []).map { Candle(arrayData: $0) } as! CandleList
100+
}
101+
102+
return Resource(.ohlc, method: .GET, pathParam: currencyId, params: params, parse: parse, completion: callback)
103+
}
84104
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import XCTest
2+
@testable import CoinGecko
3+
4+
final class CandleTests: XCTestCase {
5+
6+
private let client = CoinGeckoClient()
7+
8+
func testCandles() {
9+
let exp = XCTestExpectation()
10+
let id = "bitcoin"
11+
let vs = "usd"
12+
let candles = Resources.candles(currencyId: id, vs: vs, days: 180) { (result: Result<CandleList, CoinGeckoError>) in
13+
switch result {
14+
case let .success(candleList):
15+
XCTAssertTrue(candleList.count > 44 && candleList.count < 49,
16+
"Unexpected candle count")
17+
case let .failure(error):
18+
XCTFail(error.localizedDescription)
19+
}
20+
exp.fulfill()
21+
}
22+
client.load(candles)
23+
wait(for: [exp], timeout: 10.0)
24+
}
25+
}

0 commit comments

Comments
 (0)