Skip to content

Commit fe17996

Browse files
author
Fernando Fernandes
committed
Parameterize orders placing
1 parent 956645b commit fe17996

12 files changed

+165
-36
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,49 @@ public struct KucoinOrderParameters {
1515
// MARK: - Properties
1616

1717
public let clientOid = UUID().uuidString
18-
public let side: KucoinOrderSide
1918
public let symbol: String
19+
public let side: KucoinOrderSide
2020
public let type: KucoinOrderType
2121
public let stop: KucoinOrderStop
2222
public let stopPriceType: KucoinOrderStopPriceType
2323
public let stopPrice: String
24+
public let price: String
2425
public let reduceOnly: Bool
2526
public let closeOrder: Bool
26-
public let price: String
2727

2828
// MARK: - Lifecycle
2929

30-
public init(side: KucoinOrderSide,
31-
symbol: String,
30+
public init(symbol: String,
31+
side: KucoinOrderSide,
3232
type: KucoinOrderType,
3333
stop: KucoinOrderStop,
3434
stopPriceType: KucoinOrderStopPriceType,
3535
stopPrice: String,
36+
price: String,
3637
reduceOnly: Bool,
37-
closeOrder: Bool,
38-
price: String) {
39-
self.side = side
38+
closeOrder: Bool) {
4039
self.symbol = symbol
40+
self.side = side
4141
self.type = type
4242
self.stop = stop
4343
self.stopPriceType = stopPriceType
4444
self.stopPrice = stopPrice
45+
self.price = price
4546
self.reduceOnly = reduceOnly
4647
self.closeOrder = closeOrder
47-
self.price = price
4848
}
4949
}
5050

5151
/// Holds the keys of the parameters for placing a Kucoin order.
5252
public enum KucoinOrderParameterKey: String {
5353
case clientOid
54-
case side
5554
case symbol
55+
case side
5656
case type
5757
case stop
5858
case stopPriceType
5959
case stopPrice
60+
case price
6061
case reduceOnly
6162
case closeOrder
62-
case price
6363
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// SwiftTraderExchange.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.01.22.
6+
//
7+
8+
import Foundation
9+
10+
/// The list of supported exchanges.
11+
public enum SwiftTraderExchange: String, Codable {
12+
case Binance
13+
case Kucoin
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// SwiftTraderOrderInput.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 11.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Encapsulates all the arguments required for submiting orders to supported exchanges.
11+
public struct SwiftTraderOrderInput {
12+
13+
// MARK: - Properties
14+
15+
public let exchange: SwiftTraderExchange
16+
public let ticker: String
17+
public let entryPrice: Double
18+
public let currentPrice: Double
19+
public let profitPercentage: Double
20+
public let offset: Double
21+
22+
// MARK: - Lifecycle
23+
24+
public init(exchange: SwiftTraderExchange,
25+
ticker: String,
26+
entryPrice: Double,
27+
currentPrice: Double,
28+
profitPercentage: Double,
29+
offset: Double) {
30+
self.exchange = exchange
31+
self.ticker = ticker
32+
self.entryPrice = entryPrice
33+
self.currentPrice = currentPrice
34+
self.profitPercentage = profitPercentage
35+
self.offset = offset
36+
}
37+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// HTTPHeader.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 10.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Holds HTTP header related constants.
11+
public struct HTTPHeader {
12+
13+
public struct Field {
14+
static let contentType = "Content-Type"
15+
static let accept = "Accept"
16+
}
17+
18+
public struct Value {
19+
static let applicationJSON = "application/json"
20+
}
21+
}

Sources/SwiftTrader/HTTPMethod.swift renamed to Sources/SwiftTrader/Network/Constants/HTTPMethod.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Foundation
99

10+
/// Holds HTTP method related constants.
1011
public enum HTTPMethod: String {
1112
case GET
1213
case POST

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,17 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
3232
var urlRequest = URLRequest(url: try futuresPlaceOrderResource.url)
3333
urlRequest.httpMethod = HTTPMethod.POST.rawValue
3434

35-
#warning("TODO: This has to be parameterized")
36-
var json = [String:Any]()
37-
json["clientOid"] = UUID().uuidString
38-
json["side"] = "sell"
39-
json["symbol"] = "XBTUSDTM"
40-
json["type"] = "limit"
41-
json["stop"] = "down"
42-
json["stopPriceType"] = "TP"
43-
json["stopPrice"] = "44970"
44-
json["reduceOnly"] = true
45-
json["closeOrder"] = true
46-
json["price"] = "44970"
47-
35+
// Parameters
36+
let parametersJSON = createJSONParameters(from: orderParameters)
4837
do {
49-
let data = try JSONSerialization.data(withJSONObject: json, options: [])
38+
let data = try JSONSerialization.data(withJSONObject: parametersJSON, options: [])
5039
urlRequest.httpBody = data
51-
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
52-
urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
40+
urlRequest.addValue(HTTPHeader.Value.applicationJSON, forHTTPHeaderField: HTTPHeader.Field.contentType)
41+
urlRequest.addValue(HTTPHeader.Value.applicationJSON, forHTTPHeaderField: HTTPHeader.Field.accept)
5342
} catch {
54-
#warning("TODO: handle error")
55-
print("EEEERRRRRROOORRR")
43+
throw NetworkRequestError.invalidJSONParameters(error: error)
5644
}
45+
5746
try KucoinAPI.setRequestHeaderFields(request: &urlRequest, kucoinAuth: kucoinAuth)
5847
return urlRequest
5948
}
@@ -63,7 +52,7 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
6352

6453
// MARK: Private
6554

66-
private let parameters: KucoinOrderParameters
55+
private let orderParameters: KucoinOrderParameters
6756

6857
private let kucoinAuth: KucoinAuth
6958

@@ -72,15 +61,15 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
7261
/// Creates a new `KucoinFuturesPlaceOrdersRequest` instance.
7362
///
7463
/// - Parameters:
75-
/// - parameters: `KucoinOrderParameters` that defines an order.
64+
/// - orderParameters: `KucoinOrderParameters`, which define an order.
7665
/// - kucoinAuth: Kucoin authentication data.
7766
/// - session: `URLSession`, default is `.shared`.
7867
/// - settings: `NetworkRequestSettings`.
79-
public init(parameters: KucoinOrderParameters,
68+
public init(orderParameters: KucoinOrderParameters,
8069
kucoinAuth: KucoinAuth,
8170
session: URLSession = .shared,
8271
settings: NetworkRequestSettings) {
83-
self.parameters = parameters
72+
self.orderParameters = orderParameters
8473
self.kucoinAuth = kucoinAuth
8574
self.session = session
8675
self.settings = settings
@@ -95,3 +84,23 @@ public extension KucoinFuturesPlaceOrdersRequest {
9584
try JSONDecoder().decode(KucoinFuturesPlaceOrder.self, from: data)
9685
}
9786
}
87+
88+
// MARK: - Private
89+
90+
private extension KucoinFuturesPlaceOrdersRequest {
91+
92+
func createJSONParameters(from orderParameters: KucoinOrderParameters) -> [String: Any] {
93+
[
94+
KucoinOrderParameterKey.clientOid.rawValue: UUID().uuidString,
95+
KucoinOrderParameterKey.symbol.rawValue: orderParameters.symbol,
96+
KucoinOrderParameterKey.side.rawValue: orderParameters.side.rawValue,
97+
KucoinOrderParameterKey.type.rawValue: orderParameters.type.rawValue,
98+
KucoinOrderParameterKey.stop.rawValue: orderParameters.stop.rawValue,
99+
KucoinOrderParameterKey.stopPriceType.rawValue: orderParameters.stopPriceType.rawValue,
100+
KucoinOrderParameterKey.stopPrice.rawValue: orderParameters.stopPrice,
101+
KucoinOrderParameterKey.price.rawValue: orderParameters.price,
102+
KucoinOrderParameterKey.reduceOnly.rawValue: orderParameters.reduceOnly,
103+
KucoinOrderParameterKey.closeOrder.rawValue: orderParameters.closeOrder
104+
]
105+
}
106+
}

Sources/SwiftTrader/Network/NetworkRequest/NetworkRequestError.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public enum NetworkRequestError: Error {
2222
/// Could not instantiate a valid `URLRequest` (e.g.: `NetworkRequest.getter:request`).
2323
case invalidRequest(error: Error)
2424

25+
/// `JSONSerialization` could not produce a valid `JSON` from given parameters.
26+
///
27+
/// E.g.: a `JSON` object could not be created from an instance of `KucoinOrderParameters`.
28+
case invalidJSONParameters(error: Error)
29+
2530
/// Could not instantiate a `URLComponents` struct using the given `String` URL.
2631
case invalidURLString(urlString: String)
2732

0 commit comments

Comments
 (0)