4
4
"encoding/hex"
5
5
"encoding/json"
6
6
"fmt"
7
+ "math"
7
8
"math/big"
8
9
"strconv"
9
10
"strings"
@@ -56,8 +57,8 @@ func OrderRequestToWire(req OrderRequest, meta map[string]AssetInfo, isSpot bool
56
57
return OrderWire {
57
58
Asset : assetId ,
58
59
IsBuy : req .IsBuy ,
59
- LimitPx : FloatToWire (req .LimitPx , maxDecimals , info .SzDecimals ),
60
- SizePx : FloatToWire (req .Sz , maxDecimals , info .SzDecimals ),
60
+ LimitPx : RoundOrderPrice (req .LimitPx , info .SzDecimals , maxDecimals ),
61
+ SizePx : RoundOrderSize (req .Sz , info .SzDecimals ),
61
62
ReduceOnly : req .ReduceOnly ,
62
63
OrderType : OrderTypeToWire (req .OrderType ),
63
64
Cloid : req .Cloid ,
@@ -80,8 +81,8 @@ func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo,
80
81
Order : OrderWire {
81
82
Asset : assetId ,
82
83
IsBuy : req .IsBuy ,
83
- LimitPx : FloatToWire (req .LimitPx , maxDecimals , info .SzDecimals ),
84
- SizePx : FloatToWire (req .Sz , maxDecimals , info .SzDecimals ),
84
+ LimitPx : RoundOrderPrice (req .LimitPx , info .SzDecimals , maxDecimals ),
85
+ SizePx : RoundOrderSize (req .Sz , info .SzDecimals ),
85
86
ReduceOnly : req .ReduceOnly ,
86
87
OrderType : OrderTypeToWire (req .OrderType ),
87
88
},
@@ -143,3 +144,38 @@ func StructToMap(strct any) (res map[string]interface{}, err error) {
143
144
json .Unmarshal (a , & res )
144
145
return res , nil
145
146
}
147
+
148
+ // Round the order size to the nearest tick size
149
+ func RoundOrderSize (x float64 , szDecimals int ) string {
150
+ newX := math .Round (x * math .Pow10 (szDecimals )) / math .Pow10 (szDecimals )
151
+ // TODO: add rounding
152
+ return big .NewFloat (newX ).Text ('f' , szDecimals )
153
+ }
154
+
155
+ // Round the order price to the nearest tick size
156
+ func RoundOrderPrice (x float64 , szDecimals int , maxDecimals int ) string {
157
+ maxSignFigures := 5
158
+ allowedDecimals := maxDecimals - szDecimals
159
+ numberOfDigitsInIntegerPart := len (strconv .Itoa (int (x )))
160
+ if numberOfDigitsInIntegerPart >= maxSignFigures {
161
+ return RoundOrderSize (x , 0 )
162
+ }
163
+ allowedSignFigures := maxSignFigures - numberOfDigitsInIntegerPart
164
+ if x < 1 {
165
+ text := RoundOrderSize (x , allowedDecimals )
166
+ startSignFigures := false
167
+ for i := 2 ; i < len (text ); i ++ {
168
+ if text [i ] == '0' && ! startSignFigures {
169
+ continue
170
+ }
171
+ startSignFigures = true
172
+ allowedSignFigures --
173
+ if allowedSignFigures == 0 {
174
+ return text [:i + 1 ]
175
+ }
176
+ }
177
+ return text
178
+ } else {
179
+ return RoundOrderSize (x , min (allowedSignFigures , allowedDecimals ))
180
+ }
181
+ }
0 commit comments