Skip to content

Commit b02cc44

Browse files
committed
Distribute the size evenly between short orders
- Update unit tests.
1 parent 4658dff commit b02cc44

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

Sources/SwiftTrader/BusinessLogic/MultiOrders/SwiftTrader+MultipleShortLimitOrders.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public extension SwiftTrader {
4545
formattedPrices.insert(formattedPrice)
4646
previousPrice = price
4747

48-
let formattedSize = formatSize(orderInput.totalSize, decimalPlaces: decimalPlaces)
48+
let sizePerOrder = orderInput.totalSize / numberOfOrders
49+
let formattedSize = formatSize(sizePerOrder, decimalPlaces: decimalPlaces)
4950

5051
let order = KucoinSpotHFOrderParameters(
5152
symbol: orderInput.symbol,

Sources/SwiftTrader/Model/Kucoin/Order/HF/KucoinSpotHFShortLimitOrdersResult.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public struct KucoinSpotHFShortLimitOrdersResult: Equatable, Codable {
2121
orders.map { $0.price }
2222
}
2323

24+
/// An array of the sizes of the submitted orders.
2425
public var sizes: [String] {
2526
orders.map { $0.size }
2627
}
28+
29+
/// The total size of all the submitted orders.
30+
public var totalSize: Double {
31+
orders.compactMap { Double($0.size) }.reduce(0, +)
32+
}
2733
}

Sources/SwiftTrader/Model/MultiOrders/SwiftTraderMultiShortLimitOrderInput.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public struct SwiftTraderMultiShortLimitOrderInput {
3434
/// - priceDecrement: How much the price of subsequent orders (second, third, fourth, and fifth)
3535
/// should go **down**.
3636
/// The value indicates the percentage. For example: 0,1 = 10%.
37-
/// - totalSize: The total size of the acquired asset. Each created order will use this number as its size.
38-
/// This way, all available assets will be sold once one of the five orders is filled, maximazing the profit.
37+
/// - totalSize: The total size available for the operation. The number will be distributed evenly
38+
/// between all submitted orders.
3939
public init(symbol: String,
4040
initialPrice: String,
4141
targetProfitPercentage: Double,

Tests/SwiftTraderTests/CreateMultiShortLimitOrderTests.swift

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,21 @@ final class CreateMultiShortLimitOrderTests: XCTestCase {
7474
for (index, order) in result.enumerated() {
7575
XCTAssertEqual(
7676
order.size,
77-
testCase.expectedSize,
78-
"Order \(index + 1) size should be \(testCase.expectedSize)"
77+
testCase.expectedSizes[index],
78+
"Order \(index + 1) size should be \(testCase.expectedSizes[index])"
7979
)
8080
}
81+
82+
let totalExpectedSize = testCase.expectedSizes.compactMap { Double($0) }.reduce(0, +)
83+
let totalSize = testCase.input.totalSize
84+
85+
XCTAssertTrue(
86+
totalExpectedSize <= totalSize,
87+
"""
88+
The expected size should be less then/equal to the total size \(totalSize)
89+
-- and not \(totalExpectedSize)
90+
"""
91+
)
8192
}
8293
}
8394
}
@@ -147,7 +158,13 @@ private extension CreateMultiShortLimitOrderTests {
147158
"0.6874",
148159
"0.6806"
149160
],
150-
expectedSize: "92"
161+
expectedSizes: [
162+
"18",
163+
"18",
164+
"18",
165+
"18",
166+
"18"
167+
]
151168
),
152169

153170
TestCase(
@@ -159,7 +176,13 @@ private extension CreateMultiShortLimitOrderTests {
159176
"0.00000178",
160177
"0.00000177"
161178
],
162-
expectedSize: "70485494"
179+
expectedSizes: [
180+
"14097098",
181+
"14097098",
182+
"14097098",
183+
"14097098",
184+
"14097098"
185+
]
163186
),
164187

165188
TestCase(
@@ -171,7 +194,13 @@ private extension CreateMultiShortLimitOrderTests {
171194
"308.85",
172195
"305.76"
173196
],
174-
expectedSize: "0.60"
197+
expectedSizes: [
198+
"0.12",
199+
"0.12",
200+
"0.12",
201+
"0.12",
202+
"0.12"
203+
]
175204
),
176205

177206
TestCase(
@@ -183,7 +212,13 @@ private extension CreateMultiShortLimitOrderTests {
183212
"1.75",
184213
"1.73"
185214
],
186-
expectedSize: "136"
215+
expectedSizes: [
216+
"27",
217+
"27",
218+
"27",
219+
"27",
220+
"27"
221+
]
187222
),
188223

189224
TestCase(
@@ -195,14 +230,20 @@ private extension CreateMultiShortLimitOrderTests {
195230
"0.073",
196231
"0.072"
197232
],
198-
expectedSize: "4333"
233+
expectedSizes: [
234+
"866",
235+
"866",
236+
"866",
237+
"866",
238+
"866"
239+
]
199240
)
200241
]
201242
}
202243

203244
struct TestCase {
204245
let input: SwiftTraderMultiShortLimitOrderInput
205246
let expectedPrices: [String]
206-
let expectedSize: String
247+
let expectedSizes: [String]
207248
}
208249
}

0 commit comments

Comments
 (0)