Skip to content

Commit 57e2784

Browse files
author
Fernando Fernandes
committed
Add stop order mass cancellation
1 parent 181c728 commit 57e2784

11 files changed

+222
-9
lines changed

Sources/SwiftTrader/Model/Error/SwiftTraderError.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ public enum SwiftTraderError: Error {
1818
// MARK: - Kucoin Related
1919

2020
/// And error ocurred while executing the function `SwiftTrader.kucoinFuturesAccountOverview(currencySymbol:)`.
21-
case kucoinFuturesAccountOverviewError(error: Error)
21+
case kucoinFuturesAccountOverview(error: Error)
22+
23+
/// And error ocurred while executing the function `SwiftTrader.kucoinFuturesCancelStopOrders(symbol:)`.
24+
case kucoinFuturesCancelStopOrders(error: Error)
2225

2326
/// Something went wrong while trying to set the target price.
2427
case kucoinCouldNotCalculateTheTargetPrice(input: SwiftTraderOrderInput)
@@ -27,13 +30,13 @@ public enum SwiftTraderError: Error {
2730
case kucoinInvalidTargetPrice(entryPrice: String, targetPrice: String)
2831

2932
/// And error ocurred while executing the function `SwiftTrader.kucoinFuturesPlaceOrder()`.
30-
case kucoinPlaceOrderError(error: Error)
33+
case kucoinPlaceOrder(error: Error)
3134

3235
/// And error ocurred while executing the function `SwiftTrader.kucoinFuturesOrderList(orderStatus:)`.
33-
case kucoinOrderListError(error: Error)
36+
case kucoinOrderList(error: Error)
3437

3538
/// And error ocurred while executing the function `SwiftTrader.kucoinFuturesPositionList()`.
36-
case kucoinPositionListError(error: Error)
39+
case kucoinPositionList(error: Error)
3740

3841
/// The response status code is something other than `200`.
3942
///
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// KucoinCancelledStopOrders.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Holds a list of cancelled orders.
11+
public struct KucoinCancelledStopOrders: Codable {
12+
13+
public let cancelledOrderIDs: [String]
14+
15+
enum CodingKeys: String, CodingKey {
16+
case cancelledOrderIDs = "cancelledOrderIds"
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// KucoinFuturesCancelStopOrders.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Kucoin "Stop Order Mass cancelation" REST API response.
11+
///
12+
/// https://docs.kucoin.com/futures/#stop-order-mass-cancelation
13+
public struct KucoinFuturesCancelStopOrders: Codable {
14+
public let code: String
15+
public let data: KucoinCancelledStopOrders
16+
}

Sources/SwiftTrader/Model/SwiftTraderOperation.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
/// The currently running `SwiftTrader` operation.
1111
public enum SwiftTraderOperation {
1212
case kucoinFuturesAccountOverview
13+
case kucoinFuturesCancelStopOrders
1314
case kucoinFuturesOrderList
1415
case kucoinFuturesPlaceOrder
1516
case kucoinFuturesPositionList

Sources/SwiftTrader/Network/Constants/HTTPMethod.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Foundation
99

1010
/// Holds HTTP method related constants.
1111
public enum HTTPMethod: String {
12+
case DELETE
1213
case GET
1314
case POST
1415
}

Sources/SwiftTrader/Network/Kucoin/KucoinAPI.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public struct KucoinAPI {
1616
static let accountOverview = "/api/v1/account-overview"
1717
static let orders = "/api/v1/orders"
1818
static let positions = "/api/v1/positions"
19+
static let stopOrders = "/api/v1/stopOrders"
1920
}
2021

2122
/// Returns the base `URL` based on an Xcode environment variable.
@@ -48,5 +49,6 @@ public struct KucoinAPI {
4849
public struct QueryParam {
4950
static let currency = "currency"
5051
static let orderStatus = "status"
52+
static let symbol = "symbol"
5153
}
5254
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// KucoinFuturesCancelOrdersRequest.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.02.22.
6+
//
7+
8+
import Foundation
9+
#if canImport(FoundationNetworking)
10+
import FoundationNetworking
11+
#endif
12+
import Logging
13+
14+
/// A **request** for cancelling all untriggered stop orders of a given symbol (contract).
15+
///
16+
/// https://docs.kucoin.com/futures/#stop-order-mass-cancelation
17+
public struct KucoinFuturesCancelOrdersRequest: NetworkRequest {
18+
19+
// MARK: - Properties
20+
21+
public typealias DecodableModel = KucoinFuturesCancelStopOrders
22+
23+
public var logger: Logger {
24+
NetworkRequestLogger().default
25+
}
26+
27+
public var session: URLSession
28+
29+
public var request: URLRequest {
30+
get throws {
31+
let futuresCancelOrdersResource = KucoinFuturesCancelOrdersResource(symbol: symbol)
32+
var urlRequest = URLRequest(url: try futuresCancelOrdersResource.url)
33+
urlRequest.httpMethod = HTTPMethod.DELETE.rawValue
34+
try KucoinAPI.setRequestHeaderFields(request: &urlRequest, kucoinAuth: kucoinAuth)
35+
return urlRequest
36+
}
37+
}
38+
39+
public var settings: NetworkRequestSettings
40+
41+
// MARK: Private
42+
43+
private let symbol: String
44+
45+
private let kucoinAuth: KucoinAuth
46+
47+
// MARK: - Lifecycle
48+
49+
/// Creates a new `KucoinFuturesOrdersListRequest` instance.
50+
///
51+
/// - Parameters:
52+
/// - symbol: `String`, represents the specific contract for which all the untriggered stop orders will be cancelled.
53+
/// E.g.: "XBTUSDM".
54+
/// - kucoinAuth: Kucoin authentication data.
55+
/// - session: `URLSession`, default is `.shared`.
56+
/// - settings: `NetworkRequestSettings`.
57+
public init(symbol: String,
58+
kucoinAuth: KucoinAuth,
59+
session: URLSession = .shared,
60+
settings: NetworkRequestSettings) {
61+
self.symbol = symbol
62+
self.kucoinAuth = kucoinAuth
63+
self.session = session
64+
self.settings = settings
65+
}
66+
}
67+
68+
// MARK: - Network Request Protocol
69+
70+
public extension KucoinFuturesCancelOrdersRequest {
71+
72+
func decode(_ data: Data) throws -> DecodableModel {
73+
try JSONDecoder().decode(KucoinFuturesCancelStopOrders.self, from: data)
74+
}
75+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// KucoinFuturesCancelOrdersResource.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// The **resource** for requesting the cancellation of all untriggered stop orders of a given symbol (contract).
11+
///
12+
/// https://docs.kucoin.com/futures/#stop-order-mass-cancelation
13+
public struct KucoinFuturesCancelOrdersResource: NetworkResource {
14+
15+
// MARK: - Properties
16+
17+
public var url: URL {
18+
get throws {
19+
let baseURLString = KucoinAPI.Futures.baseURL()
20+
guard var urlComponents = URLComponents(string: baseURLString) else {
21+
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
22+
}
23+
urlComponents.path = KucoinAPI.Futures.Path.stopOrders
24+
let queryItems = [
25+
URLQueryItem(name: KucoinAPI.QueryParam.symbol, value: symbol)
26+
]
27+
urlComponents.queryItems = queryItems
28+
guard let url = urlComponents.url else {
29+
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
30+
}
31+
return url
32+
}
33+
}
34+
35+
// MARK: Private
36+
37+
private let symbol: String
38+
39+
// MARK: - Lifecycle
40+
41+
/// Creates a new `KucoinFuturesCancelOrdersResource` instance.
42+
///
43+
/// - Parameter symbol: `String`, represents the specific contract for which all the untriggered stop orders will be cancelled.
44+
/// E.g.: "XBTUSDM".
45+
init(symbol: String) {
46+
self.symbol = symbol
47+
}
48+
}

0 commit comments

Comments
 (0)