@@ -42,16 +42,15 @@ public extension SwiftTrader {
42
42
// E.g.: 42000.69 * 0.0674 = 2830.846506
43
43
let priceIncrement : Double = input. entryPrice * targetPercentage
44
44
logger. log ( " Price increment: \( priceIncrement. toDecimalString ( ) ) " )
45
+ logger. log ( " Ticker size: \( input. tickerSize) " )
46
+ logger. log ( " Entry price string: \( input. entryPrice. toDecimalString ( ) ) " )
45
47
46
48
// "Long" example: 42000.69 + 2830.846506 = 44831.536506
47
49
// "Short" example: 42000.69 - 2830.846506 = 39169.843494
48
50
let limitPrice : Double = input. isLong ?
49
51
input. entryPrice + priceIncrement :
50
52
input. entryPrice - priceIncrement
51
53
52
- logger. log ( " Entry price: \( input. entryPrice. toDecimalString ( ) ) " )
53
- logger. log ( " Limit price: \( limitPrice. toDecimalString ( ) ) " )
54
-
55
54
var limitPriceString = " \( limitPrice. toDecimalString ( ) ) "
56
55
57
56
// Finally, handle the following requirement:
@@ -81,26 +80,25 @@ public extension SwiftTrader {
81
80
}
82
81
83
82
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 {
87
87
limitPriceString = limitPriceString. dropLast ( ) + " \( tickerLastDigit) "
88
88
89
- } else if limitPriceDigits > tickerDigits {
90
- let digitsToRemove = ( limitPriceDigits - tickerDigits) + 1
89
+ } else if limitPriceDecimalDigits > tickerDigits {
90
+ let digitsToRemove = ( limitPriceDecimalDigits - tickerDigits) + 1
91
91
limitPriceString = limitPriceString. dropLast ( digitsToRemove) + " \( tickerLastDigit) "
92
92
93
- } else if limitPriceDigits < tickerDigits {
94
- let digitsToAdd = ( tickerDigits - limitPriceDigits)
93
+ } else if limitPriceDecimalDigits < tickerDigits,
94
+ limitPriceLastDigit != tickerLastDigit {
95
+ let digitsToAdd = ( tickerDigits - limitPriceDecimalDigits)
95
96
let digits : [ String ] = Array ( repeating: " 0 " , count: digitsToAdd)
96
97
limitPriceString = limitPriceString + digits. joined ( separator: " " )
97
98
limitPriceString = limitPriceString. dropLast ( ) + " \( tickerLastDigit) "
98
99
}
99
100
}
100
101
101
- logger. log ( " Ticker size: \( input. tickerSize) " )
102
- logger. log ( " Limit price string: \( limitPriceString) " )
103
-
104
102
let limitPriceDouble = Double ( limitPriceString) ?? 0
105
103
106
104
if input. isLong {
@@ -124,6 +122,9 @@ public extension SwiftTrader {
124
122
}
125
123
let limitPriceTuple = ( limitPriceString, limitPriceDouble)
126
124
125
+ logger. log ( " Limit price string: \( limitPriceString) " )
126
+ logger. log ( " Limit price double: \( limitPriceDouble) " )
127
+
127
128
// Stop price logic. It aims to give the order some room to be filled.
128
129
//
129
130
// For long positions: the stop price has to be greater than the limit price:
@@ -144,6 +145,9 @@ public extension SwiftTrader {
144
145
let stopPriceString = stopPriceDouble. toDecimalString ( )
145
146
let stopPriceTuple = ( stopPriceString, stopPriceDouble)
146
147
148
+ logger. log ( " Stop price string: \( stopPriceString) " )
149
+ logger. log ( " Stop price double: \( stopPriceDouble) " )
150
+
147
151
return ( stopPriceTuple, limitPriceTuple)
148
152
}
149
153
}
0 commit comments