Skip to content

Commit 0776642

Browse files
committed
Fix a decimal issue calculation
- 1005 was converted to 15 :D
1 parent 1c2c94a commit 0776642

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

Sources/SwiftTrader/Extensions/String+Decimal.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ public extension String {
1717
/// - 0.00003456: returns "8"
1818
/// - 2: returns "0"
1919
func decimalCount() -> Int {
20-
let decimalPartString = self.split(separator: ".").last
21-
return decimalPartString?.count ?? 0
20+
let split = self.split(separator: ".")
21+
guard split.count > 1 else {
22+
return 0
23+
}
24+
guard let decimalPartString = split.last else {
25+
return 0
26+
}
27+
return decimalPartString.count
2228
}
2329
}

Sources/SwiftTrader/SwiftTrader+TrailingStop.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,15 @@ public extension SwiftTrader {
4242
// E.g.: 42000.69 * 0.0674 = 2830.846506
4343
let priceIncrement: Double = input.entryPrice * targetPercentage
4444
logger.log("Price increment: \(priceIncrement.toDecimalString())")
45+
logger.log("Ticker size: \(input.tickerSize)")
46+
logger.log("Entry price string: \(input.entryPrice.toDecimalString())")
4547

4648
// "Long" example: 42000.69 + 2830.846506 = 44831.536506
4749
// "Short" example: 42000.69 - 2830.846506 = 39169.843494
4850
let limitPrice: Double = input.isLong ?
4951
input.entryPrice + priceIncrement :
5052
input.entryPrice - priceIncrement
5153

52-
logger.log("Entry price: \(input.entryPrice.toDecimalString())")
53-
logger.log("Limit price: \(limitPrice.toDecimalString())")
54-
5554
var limitPriceString = "\(limitPrice.toDecimalString())"
5655

5756
// Finally, handle the following requirement:
@@ -81,26 +80,25 @@ public extension SwiftTrader {
8180
}
8281

8382
let tickerDigits = input.tickerSize.decimalCount()
84-
let limitPriceDigits = limitPriceString.decimalCount()
85-
86-
if limitPriceDigits == tickerDigits, limitPriceLastDigit != tickerLastDigit {
83+
let limitPriceDecimalDigits = limitPriceString.decimalCount()
84+
85+
if ((limitPriceDecimalDigits == 0) || (limitPriceDecimalDigits == tickerDigits)),
86+
limitPriceLastDigit != tickerLastDigit {
8787
limitPriceString = limitPriceString.dropLast() + "\(tickerLastDigit)"
8888

89-
} else if limitPriceDigits > tickerDigits {
90-
let digitsToRemove = (limitPriceDigits - tickerDigits) + 1
89+
} else if limitPriceDecimalDigits > tickerDigits {
90+
let digitsToRemove = (limitPriceDecimalDigits - tickerDigits) + 1
9191
limitPriceString = limitPriceString.dropLast(digitsToRemove) + "\(tickerLastDigit)"
9292

93-
} else if limitPriceDigits < tickerDigits {
94-
let digitsToAdd = (tickerDigits - limitPriceDigits)
93+
} else if limitPriceDecimalDigits < tickerDigits,
94+
limitPriceLastDigit != tickerLastDigit {
95+
let digitsToAdd = (tickerDigits - limitPriceDecimalDigits)
9596
let digits: [String] = Array(repeating: "0", count: digitsToAdd)
9697
limitPriceString = limitPriceString + digits.joined(separator: "")
9798
limitPriceString = limitPriceString.dropLast() + "\(tickerLastDigit)"
9899
}
99100
}
100101

101-
logger.log("Ticker size: \(input.tickerSize)")
102-
logger.log("Limit price string: \(limitPriceString)")
103-
104102
let limitPriceDouble = Double(limitPriceString) ?? 0
105103

106104
if input.isLong {
@@ -124,6 +122,9 @@ public extension SwiftTrader {
124122
}
125123
let limitPriceTuple = (limitPriceString, limitPriceDouble)
126124

125+
logger.log("Limit price string: \(limitPriceString)")
126+
logger.log("Limit price double: \(limitPriceDouble)")
127+
127128
// Stop price logic. It aims to give the order some room to be filled.
128129
//
129130
// For long positions: the stop price has to be greater than the limit price:
@@ -144,6 +145,9 @@ public extension SwiftTrader {
144145
let stopPriceString = stopPriceDouble.toDecimalString()
145146
let stopPriceTuple = (stopPriceString, stopPriceDouble)
146147

148+
logger.log("Stop price string: \(stopPriceString)")
149+
logger.log("Stop price double: \(stopPriceDouble)")
150+
147151
return (stopPriceTuple, limitPriceTuple)
148152
}
149153
}

0 commit comments

Comments
 (0)