Skip to content

Commit 89635ac

Browse files
author
Fernando Fernandes
committed
Add "list orders" request
1 parent ccf7094 commit 89635ac

16 files changed

+442
-14
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Date+Milliseconds.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 05.02.22.
6+
//
7+
8+
import Foundation
9+
10+
public extension Date {
11+
12+
// MARK: - Properties
13+
14+
var millisecondsSince1970: Int64 {
15+
Int64((timeIntervalSince1970 * 1000).rounded())
16+
}
17+
18+
// MARK: - Lifecycle
19+
20+
init(milliseconds: Int64) {
21+
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 05.02.22.
6+
//
7+
8+
import Foundation
9+
10+
public extension Date {
11+
12+
/// E.g.: *"Saturday, 5. February 2022 at 22:32:16"*.
13+
func toString() -> String {
14+
let formatter = DateFormatter()
15+
formatter.dateStyle = .full
16+
formatter.timeStyle = .medium
17+
return formatter.string(from: self)
18+
}
19+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//
2+
// KucoinFuturesOrderList.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 04.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Kucoin "Get Order List" REST API response.
11+
///
12+
/// https://docs.kucoin.com/futures/#get-order-list
13+
public struct KucoinFuturesOrderList: Codable {
14+
public let code: String
15+
public let data: KucoinFuturesOrderData
16+
}
17+
18+
/// Encapsulates an array of `KucoinFuturesOrder` plus pagination information.
19+
public struct KucoinFuturesOrderData: Codable {
20+
let currentPage: Int
21+
let pageSize: Int
22+
let totalNum: Int
23+
let totalPage: Int
24+
let items: [KucoinFuturesOrder]
25+
}
26+
27+
public struct KucoinFuturesOrder: Codable {
28+
29+
// MARK: - Properties
30+
31+
let id: String
32+
33+
/// E.g.: BTCUSDT
34+
let symbol: String
35+
36+
let type: KucoinOrderType
37+
let side: KucoinOrderSide
38+
39+
/// The price of one asset's unit.
40+
let price: String
41+
42+
/// How much assets were bought.
43+
let size: Int
44+
45+
/// The total value of the order.
46+
let value: String
47+
let filledValue: String
48+
let filledSize: Int
49+
let stp: KucoinOrderSTP?
50+
51+
/// Stop order type (limit or market).
52+
let stop: KucoinOrderType?
53+
54+
/// Whether the stop order is triggered.
55+
let stopTriggered: Bool
56+
57+
/// Whether the stop order is triggered.
58+
let stopPrice: String?
59+
let leverage: String
60+
let reduceOnly: Bool
61+
62+
/// Unique order id created by users to identify their orders.
63+
let clientOid: String?
64+
let isActive: Bool
65+
66+
/// Mark of the canceled orders.
67+
let cancelExist: Bool
68+
let status: KucoinOrderStatus
69+
let createdAt: String
70+
71+
/// Last update time.
72+
let updatedAt: String
73+
74+
enum CodingKeys: String, CodingKey {
75+
case id
76+
case symbol
77+
case type
78+
case side
79+
case price
80+
case size
81+
case value
82+
case filledValue
83+
case filledSize
84+
case stp
85+
case stop
86+
case stopTriggered
87+
case stopPrice
88+
case leverage
89+
case reduceOnly
90+
case clientOid
91+
case isActive
92+
case cancelExist
93+
case createdAt
94+
case status
95+
case updatedAt
96+
}
97+
98+
// MARK: - Lifecycle
99+
100+
public init(from decoder: Decoder) throws {
101+
let container = try decoder.container(keyedBy: CodingKeys.self)
102+
103+
self.id = try container.decode(String.self, forKey: .id)
104+
self.symbol = try container.decode(String.self, forKey: .symbol)
105+
self.type = try container.decode(KucoinOrderType.self, forKey: .type)
106+
self.side = try container.decode(KucoinOrderSide.self, forKey: .side)
107+
self.price = try container.decode(String.self, forKey: .price)
108+
self.size = try container.decode(Int.self, forKey: .size)
109+
self.value = try container.decode(String.self, forKey: .value)
110+
self.filledValue = try container.decode(String.self, forKey: .filledValue)
111+
self.filledSize = try container.decode(Int.self, forKey: .filledSize)
112+
113+
let decodedSTP = try container.decode(String.self, forKey: .stp)
114+
if !decodedSTP.isEmpty,
115+
let orderSTP = KucoinOrderSTP(rawValue: decodedSTP) {
116+
self.stp = orderSTP
117+
} else {
118+
self.stp = nil
119+
}
120+
121+
let decodedStop = try container.decode(String.self, forKey: .stop)
122+
if !decodedStop.isEmpty,
123+
let stopType = KucoinOrderType(rawValue: decodedStop) {
124+
self.stop = stopType
125+
} else {
126+
self.stop = nil
127+
}
128+
129+
self.stopTriggered = try container.decode(Bool.self, forKey: .stopTriggered)
130+
self.stopPrice = try container.decodeIfPresent(String.self, forKey: .stopPrice)
131+
self.leverage = try container.decode(String.self, forKey: .leverage)
132+
self.reduceOnly = try container.decode(Bool.self, forKey: .reduceOnly)
133+
self.clientOid = try container.decodeIfPresent(String.self, forKey: .clientOid)
134+
self.isActive = try container.decode(Bool.self, forKey: .isActive)
135+
self.cancelExist = try container.decode(Bool.self, forKey: .cancelExist)
136+
self.status = try container.decode(KucoinOrderStatus.self, forKey: .status)
137+
138+
let createdAtMilliseconds = try container.decode(Int64.self, forKey: .createdAt)
139+
self.createdAt = Date(milliseconds: createdAtMilliseconds).toString()
140+
141+
let updatedAtMilliseconds = try container.decode(Int64.self, forKey: .updatedAt)
142+
self.updatedAt = Date(milliseconds: updatedAtMilliseconds).toString()
143+
}
144+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// KucoinOrderSTP.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 05.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// STP: Self-Trade Prevention - when specified and when placing orders, the order won't be matched by another one which is also yours.
11+
/// On the contrary, if STP is not specified in advanced, your order can be matched by another one of your own orders.
12+
/// It should be noted that only the taker's protection strategy is effective.
13+
///
14+
/// - CANCEL BOTH(CB)
15+
/// - CANCEL NEWEST(CN)
16+
/// - CANCEL OLDEST(CO)
17+
/// - DECREMENT AND CANCEL(DC)
18+
///
19+
/// https://docs.kucoin.com/#matching-engine
20+
public enum KucoinOrderSTP: String, Codable {
21+
case cancelBoth = "CB"
22+
case cancelNewest = "CN"
23+
case cancelOldest = "CO"
24+
case decrementCancel = "DC"
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// KucoinOrderSide.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 05.02.22.
6+
//
7+
8+
import Foundation
9+
10+
public enum KucoinOrderSide: String, Codable {
11+
case buy
12+
case sell
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// KucoinOrderStatus.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 05.02.22.
6+
//
7+
8+
import Foundation
9+
10+
public enum KucoinOrderStatus: String, Codable {
11+
case active
12+
case done
13+
case open
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// KucoinOrderType.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 05.02.22.
6+
//
7+
8+
import Foundation
9+
10+
public enum KucoinOrderType: String, Codable {
11+
case limit
12+
case market
13+
case limitStop = "limit_stop"
14+
case marketStop = "market_stop"
15+
}

Sources/SwiftTrader/Network/Kucoin/AccountOverview/KucoinFuturesAccountOverviewRequest.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Logging
1515
///
1616
/// https://docs.kucoin.com/futures/#account
1717
public struct KucoinFuturesAccountOverviewRequest: NetworkRequest {
18-
18+
1919
// MARK: - Properties
2020

2121
public typealias DecodableModel = KucoinFuturesAccountOverview
@@ -48,7 +48,11 @@ public struct KucoinFuturesAccountOverviewRequest: NetworkRequest {
4848

4949
/// Creates a new `KucoinFuturesAccountOverviewRequest` instance.
5050
///
51-
/// - Parameter session: `URLSession` instance, the default is `.shared`.
51+
/// - Parameters:
52+
/// - session: `URLSession`, default is `.shared`.
53+
/// - currencySymbol: `CurrencySymbol`, default is `.USDT`.
54+
/// - kucoinAuth: Kucoin authentication data.
55+
/// - settings: `NetworkRequestSettings`.
5256
public init(session: URLSession = .shared,
5357
currencySymbol: CurrencySymbol = .USDT,
5458
kucoinAuth: KucoinAuth,

Sources/SwiftTrader/Network/Kucoin/AccountOverview/KucoinFuturesAccountOverviewResource.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public struct KucoinFuturesAccountOverviewResource: NetworkResource {
4040

4141
/// Creates a new `KucoinFuturesAccountOverviewResource` instance.
4242
///
43-
/// - Parameter currencySymbol: `CurrencySymbol`, default is `USDT`.
44-
init(currencySymbol: CurrencySymbol = .USDT) {
43+
/// - Parameter currencySymbol: `CurrencySymbol`.
44+
init(currencySymbol: CurrencySymbol) {
4545
self.currencySymbol = currencySymbol
4646
}
4747
}

0 commit comments

Comments
 (0)