Skip to content

Commit 956645b

Browse files
author
Fernando Fernandes
committed
Create KucoinOrderParameters
- Create an order via API is WORKING! 🎉
1 parent 7fa8e32 commit 956645b

12 files changed

+187
-80
lines changed

Sources/SwiftTrader/Model/Kucoin/KucoinError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public struct KucoinSystemError: Codable {
1414
public let code: String
1515
public let message: String
1616

17-
public enum CodingKeys: String, CodingKey {
17+
enum CodingKeys: String, CodingKey {
1818
case code
1919
case message = "msg"
2020
}

Sources/SwiftTrader/Model/Kucoin/KucoinFuturesPositionList.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,33 @@ public struct KucoinFuturesPosition: Codable {
1919

2020
// MARK: - Properties
2121

22-
let id: String
22+
public let id: String
2323

2424
/// E.g.: XBTUSDTM
25-
let symbol: String
26-
let autoDeposit: Bool
27-
let realLeverage: Double
28-
let crossMode: Bool
29-
let delevPercentage: Double
25+
public let symbol: String
26+
public let autoDeposit: Bool
27+
public let realLeverage: Double
28+
public let crossMode: Bool
29+
public let delevPercentage: Double
3030

3131
/// Opening date in milliseconds.
32-
let openingTimestamp: Int64
32+
public let openingTimestamp: Int64
3333
/// Opening date as string (E.g.: "Saturday, 5. February 2022 at 22:32:16").
34-
let openingTimestampString: String
34+
public let openingTimestampString: String
3535

3636
/// Current date in milliseconds.
37-
let currentTimestamp: Int64
37+
public let currentTimestamp: Int64
3838
/// Current date as string (E.g.: "Saturday, 5. February 2022 at 22:32:16").
39-
let currentTimestampString: String
39+
public let currentTimestampString: String
4040

41-
let posInit: Double
42-
let currentQty: Int
43-
let isOpen: Bool
44-
let realisedPnl: Double
45-
let unrealisedPnl: Double
46-
let avgEntryPrice: Double
47-
let liquidationPrice: Double
41+
public let posInit: Double
42+
public let currentQty: Int
43+
public let isOpen: Bool
44+
public let markPrice: Double
45+
public let realisedPnl: Double
46+
public let unrealisedPnl: Double
47+
public let avgEntryPrice: Double
48+
public let liquidationPrice: Double
4849

4950
enum CodingKeys: String, CodingKey {
5051
case id
@@ -60,6 +61,7 @@ public struct KucoinFuturesPosition: Codable {
6061
case posInit
6162
case currentQty
6263
case isOpen
64+
case markPrice
6365
case realisedPnl
6466
case unrealisedPnl
6567
case avgEntryPrice
@@ -86,6 +88,7 @@ public struct KucoinFuturesPosition: Codable {
8688
self.posInit = try container.decode(Double.self, forKey: .posInit)
8789
self.currentQty = try container.decode(Int.self, forKey: .currentQty)
8890
self.isOpen = try container.decode(Bool.self, forKey: .isOpen)
91+
self.markPrice = try container.decode(Double.self, forKey: .markPrice)
8992
self.realisedPnl = try container.decode(Double.self, forKey: .realisedPnl)
9093
self.unrealisedPnl = try container.decode(Double.self, forKey: .unrealisedPnl)
9194
self.avgEntryPrice = try container.decode(Double.self, forKey: .avgEntryPrice)

Sources/SwiftTrader/Model/Kucoin/Order/KucoinFuturesOrderList.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,65 @@ public struct KucoinFuturesOrderList: Codable {
1717

1818
/// Encapsulates an array of `KucoinFuturesOrder` plus pagination information.
1919
public struct KucoinFuturesOrderData: Codable {
20-
let currentPage: Int
21-
let pageSize: Int
22-
let totalNum: Int
23-
let totalPage: Int
24-
let items: [KucoinFuturesOrder]
20+
public let currentPage: Int
21+
public let pageSize: Int
22+
public let totalNum: Int
23+
public let totalPage: Int
24+
public let items: [KucoinFuturesOrder]
2525
}
2626

2727
public struct KucoinFuturesOrder: Codable {
2828

2929
// MARK: - Properties
3030

31-
let id: String
31+
public let id: String
3232

3333
/// E.g.: BTCUSDT
34-
let symbol: String
34+
public let symbol: String
3535

36-
let type: KucoinOrderType
37-
let side: KucoinOrderSide
36+
public let type: KucoinOrderType
37+
public let side: KucoinOrderSide
3838

3939
/// The price of one asset's unit.
40-
let price: String
40+
public let price: String
4141

4242
/// How much assets were bought.
43-
let size: Int
43+
public let size: Int
4444

4545
/// The total value of the order.
46-
let value: String
47-
let filledValue: String
48-
let filledSize: Int
49-
let stp: KucoinOrderSTP?
46+
public let value: String
47+
public let filledValue: String
48+
public let filledSize: Int
49+
public let stp: KucoinOrderSTP?
5050

5151
/// Stop order type (limit or market).
52-
let stop: KucoinOrderType?
52+
public let stop: KucoinOrderType?
5353

5454
/// Whether the stop order is triggered.
55-
let stopTriggered: Bool
55+
public let stopTriggered: Bool
5656

5757
/// Whether the stop order is triggered.
58-
let stopPrice: String?
59-
let leverage: String
60-
let reduceOnly: Bool
58+
public let stopPrice: String?
59+
public let leverage: String
60+
public let reduceOnly: Bool
6161

6262
/// Unique order id created by users to identify their orders.
63-
let clientOid: String?
64-
let isActive: Bool
63+
public let clientOid: String?
64+
public let isActive: Bool
6565

6666
/// Mark of the canceled orders.
67-
let cancelExist: Bool
68-
let status: KucoinOrderStatus
67+
public let cancelExist: Bool
68+
public let status: KucoinOrderStatus
6969

7070
/// Creation date in milliseconds.
71-
let createdAt: Int64
71+
public let createdAt: Int64
7272
/// Creation date as string (E.g.: "Saturday, 5. February 2022 at 22:32:16").
73-
let createdAtString: String
73+
public let createdAtString: String
7474

7575
/// Last update time in milliseconds.
76-
let updatedAt: Int64
76+
public let updatedAt: Int64
7777
/// Last update time as string (E.g.: "Saturday, 5. February 2022 at 22:32:16")
78-
let updatedAtString: String
78+
public let updatedAtString: String
7979

8080
enum CodingKeys: String, CodingKey {
8181
case id

Sources/SwiftTrader/Model/Kucoin/Order/KucoinFuturesPlaceOrder.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,13 @@ import Foundation
1313
public struct KucoinFuturesPlaceOrder: Codable {
1414
public let code: String
1515
public let msg: String?
16-
public let orderId: String?
16+
public let data: OrderPlaced?
17+
}
18+
19+
public struct OrderPlaced: Codable {
20+
public let orderID: String
21+
22+
enum CodingKeys: String, CodingKey {
23+
case orderID = "orderId"
24+
}
1725
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// KucoinOrderParameters.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 10.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Encapsulates parameters do place an order, for example via `KucoinFuturesPlaceOrdersRequest`.
11+
///
12+
/// https://docs.kucoin.com/futures/#place-an-order
13+
public struct KucoinOrderParameters {
14+
15+
// MARK: - Properties
16+
17+
public let clientOid = UUID().uuidString
18+
public let side: KucoinOrderSide
19+
public let symbol: String
20+
public let type: KucoinOrderType
21+
public let stop: KucoinOrderStop
22+
public let stopPriceType: KucoinOrderStopPriceType
23+
public let stopPrice: String
24+
public let reduceOnly: Bool
25+
public let closeOrder: Bool
26+
public let price: String
27+
28+
// MARK: - Lifecycle
29+
30+
public init(side: KucoinOrderSide,
31+
symbol: String,
32+
type: KucoinOrderType,
33+
stop: KucoinOrderStop,
34+
stopPriceType: KucoinOrderStopPriceType,
35+
stopPrice: String,
36+
reduceOnly: Bool,
37+
closeOrder: Bool,
38+
price: String) {
39+
self.side = side
40+
self.symbol = symbol
41+
self.type = type
42+
self.stop = stop
43+
self.stopPriceType = stopPriceType
44+
self.stopPrice = stopPrice
45+
self.reduceOnly = reduceOnly
46+
self.closeOrder = closeOrder
47+
self.price = price
48+
}
49+
}
50+
51+
/// Holds the keys of the parameters for placing a Kucoin order.
52+
public enum KucoinOrderParameterKey: String {
53+
case clientOid
54+
case side
55+
case symbol
56+
case type
57+
case stop
58+
case stopPriceType
59+
case stopPrice
60+
case reduceOnly
61+
case closeOrder
62+
case price
63+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// KucoinOrderStop.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 10.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Either down or up.
11+
public enum KucoinOrderStop: String {
12+
13+
/// Triggers when the price reaches or goes below a stop price.
14+
case down
15+
16+
/// Triggers when the price reaches or goes above the a stop price.
17+
case up
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// KucoinOrderStopPriceType.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 10.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Either `TP`, `IP` or `MP`.
11+
///
12+
/// Need to be defined if `KucoinOrderStop` is specified.
13+
public enum KucoinOrderStopPriceType: String {
14+
15+
/// Trade price.
16+
case TP
17+
18+
/// Index price
19+
case IP
20+
21+
/// Mark price.
22+
case MP
23+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ public struct KucoinFuturesAccountOverviewRequest: NetworkRequest {
4949
/// Creates a new `KucoinFuturesAccountOverviewRequest` instance.
5050
///
5151
/// - Parameters:
52-
/// - session: `URLSession`, default is `.shared`.
5352
/// - currencySymbol: `CurrencySymbol`, default is `.USDT`.
5453
/// - kucoinAuth: Kucoin authentication data.
54+
/// - session: `URLSession`, default is `.shared`.
5555
/// - settings: `NetworkRequestSettings`.
56-
public init(session: URLSession = .shared,
57-
currencySymbol: CurrencySymbol = .USDT,
56+
public init(currencySymbol: CurrencySymbol = .USDT,
5857
kucoinAuth: KucoinAuth,
58+
session: URLSession = .shared,
5959
settings: NetworkRequestSettings) {
60-
self.session = session
6160
self.currencySymbol = currencySymbol
6261
self.kucoinAuth = kucoinAuth
62+
self.session = session
6363
self.settings = settings
6464
}
6565
}

Sources/SwiftTrader/Network/Kucoin/Orders/KucoinFuturesOrdersListRequest.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ public struct KucoinFuturesOrdersListRequest: NetworkRequest {
4949
/// Creates a new `KucoinFuturesOrdersListRequest` instance.
5050
///
5151
/// - Parameters:
52-
/// - session: `URLSession`, default is `.shared`.
5352
/// - orderStatus: `KucoinFuturesOrderStatus`, default is `.active`.
5453
/// - kucoinAuth: Kucoin authentication data.
54+
/// - session: `URLSession`, default is `.shared`.
5555
/// - settings: `NetworkRequestSettings`.
56-
public init(session: URLSession = .shared,
57-
orderStatus: KucoinOrderStatus = .active,
56+
public init(orderStatus: KucoinOrderStatus = .active,
5857
kucoinAuth: KucoinAuth,
58+
session: URLSession = .shared,
5959
settings: NetworkRequestSettings) {
60-
self.session = session
6160
self.orderStatus = orderStatus
6261
self.kucoinAuth = kucoinAuth
62+
self.session = session
6363
self.settings = settings
6464
}
6565
}

Sources/SwiftTrader/Network/Kucoin/Orders/KucoinFuturesPlaceOrderRequest.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,22 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
2828

2929
public var request: URLRequest {
3030
get throws {
31-
let futuresPlaceOrderResource = KucoinFuturesPlaceOrderResource(orderStatus: .active)
31+
let futuresPlaceOrderResource = KucoinFuturesPlaceOrderResource()
3232
var urlRequest = URLRequest(url: try futuresPlaceOrderResource.url)
3333
urlRequest.httpMethod = HTTPMethod.POST.rawValue
3434

3535
#warning("TODO: This has to be parameterized")
3636
var json = [String:Any]()
3737
json["clientOid"] = UUID().uuidString
38+
json["side"] = "sell"
3839
json["symbol"] = "XBTUSDTM"
39-
json["price"] = "42000"
40+
json["type"] = "limit"
41+
json["stop"] = "down"
42+
json["stopPriceType"] = "TP"
43+
json["stopPrice"] = "44970"
44+
json["reduceOnly"] = true
4045
json["closeOrder"] = true
46+
json["price"] = "44970"
4147

4248
do {
4349
let data = try JSONSerialization.data(withJSONObject: json, options: [])
@@ -57,8 +63,7 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
5763

5864
// MARK: Private
5965

60-
#warning("TODO: change from orderStatus to whatever needs sending")
61-
private let orderStatus: KucoinOrderStatus
66+
private let parameters: KucoinOrderParameters
6267

6368
private let kucoinAuth: KucoinAuth
6469

@@ -67,17 +72,17 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
6772
/// Creates a new `KucoinFuturesPlaceOrdersRequest` instance.
6873
///
6974
/// - Parameters:
70-
/// - session: `URLSession`, default is `.shared`.
71-
/// - orderStatus: `KucoinFuturesOrderStatus`, default is `.active`.
75+
/// - parameters: `KucoinOrderParameters` that defines an order.
7276
/// - kucoinAuth: Kucoin authentication data.
77+
/// - session: `URLSession`, default is `.shared`.
7378
/// - settings: `NetworkRequestSettings`.
74-
public init(session: URLSession = .shared,
75-
orderStatus: KucoinOrderStatus = .active,
79+
public init(parameters: KucoinOrderParameters,
7680
kucoinAuth: KucoinAuth,
81+
session: URLSession = .shared,
7782
settings: NetworkRequestSettings) {
78-
self.session = session
79-
self.orderStatus = orderStatus
83+
self.parameters = parameters
8084
self.kucoinAuth = kucoinAuth
85+
self.session = session
8186
self.settings = settings
8287
}
8388
}

0 commit comments

Comments
 (0)