44 "encoding/hex"
55 "encoding/json"
66 "fmt"
7+ "math"
78 "math/big"
89 "strconv"
910 "strings"
@@ -56,8 +57,8 @@ func OrderRequestToWire(req OrderRequest, meta map[string]AssetInfo, isSpot bool
5657 return OrderWire {
5758 Asset : assetId ,
5859 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 ),
6162 ReduceOnly : req .ReduceOnly ,
6263 OrderType : OrderTypeToWire (req .OrderType ),
6364 Cloid : req .Cloid ,
@@ -80,8 +81,8 @@ func ModifyOrderRequestToWire(req ModifyOrderRequest, meta map[string]AssetInfo,
8081 Order : OrderWire {
8182 Asset : assetId ,
8283 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 ),
8586 ReduceOnly : req .ReduceOnly ,
8687 OrderType : OrderTypeToWire (req .OrderType ),
8788 },
@@ -143,3 +144,38 @@ func StructToMap(strct any) (res map[string]interface{}, err error) {
143144 json .Unmarshal (a , & res )
144145 return res , nil
145146}
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