Skip to content

Commit 1233c8c

Browse files
authored
Use string decimal to calculate lot size (#654)
Signed-off-by: adshao <[email protected]>
1 parent 85c3043 commit 1233c8c

File tree

4 files changed

+81
-23
lines changed

4 files changed

+81
-23
lines changed

v2/common/helpers.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ package common
33
import (
44
"bytes"
55
"fmt"
6-
"math"
6+
7+
"github.com/shopspring/decimal"
78
)
89

9-
// AmountToLotSize converts an amount to a lot sized amount
10-
func AmountToLotSize(lot float64, precision int, amount float64) float64 {
11-
return math.Trunc(math.Floor(amount/lot)*lot*math.Pow10(precision)) / math.Pow10(precision)
10+
// AmountToLotSize convert amount to lot size
11+
func AmountToLotSize(amount, minQty, stepSize string, precision int) string {
12+
amountDec := decimal.RequireFromString(amount)
13+
minQtyDec := decimal.RequireFromString(minQty)
14+
baseAmountDec := amountDec.Sub(minQtyDec)
15+
if baseAmountDec.LessThan(decimal.RequireFromString("0")) {
16+
return "0"
17+
}
18+
stepSizeDec := decimal.RequireFromString(stepSize)
19+
baseAmountDec = baseAmountDec.Div(stepSizeDec).Truncate(0).Mul(stepSizeDec)
20+
return baseAmountDec.Add(minQtyDec).Truncate(int32(precision)).String()
1221
}
1322

1423
// ToJSONList convert v to json list if v is a map

v2/common/helpers_test.go

+65-19
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,101 @@ import (
99
func TestAmountToLotSize(t *testing.T) {
1010
assert := assert.New(t)
1111
type args struct {
12-
lot float64
12+
minQty string
13+
stepSize string
14+
amount string
1315
precision int
14-
amount float64
1516
}
1617
tests := []struct {
17-
name string
18-
args args
19-
want float64
18+
name string
19+
args args
20+
expect string
2021
}{
2122
{
2223
name: "test with lot of zero and invalid amount",
2324
args: args{
24-
lot: 0.00100000,
25+
minQty: "0.01",
26+
stepSize: "0.01",
27+
amount: "0.001",
2528
precision: 8,
26-
amount: 0.00010000,
2729
},
28-
want: 0,
30+
expect: "0",
2931
},
3032
{
3133
name: "test with lot",
3234
args: args{
33-
lot: 0.00100000,
34-
precision: 3,
35-
amount: 1.39,
35+
minQty: "0.01",
36+
stepSize: "0.01",
37+
amount: "1.39",
38+
precision: 8,
39+
},
40+
expect: "1.39",
41+
},
42+
{
43+
name: "test with exact precision",
44+
args: args{
45+
minQty: "0.01",
46+
stepSize: "0.01",
47+
amount: "1.39",
48+
precision: 2,
49+
},
50+
expect: "1.39",
51+
},
52+
{
53+
name: "test with small precision",
54+
args: args{
55+
minQty: "0.01",
56+
stepSize: "0.01",
57+
amount: "1.39",
58+
precision: 1,
3659
},
37-
want: 1.389,
60+
expect: "1.3",
61+
},
62+
{
63+
name: "test with zero precision",
64+
args: args{
65+
minQty: "0.01",
66+
stepSize: "0.01",
67+
amount: "1.39",
68+
precision: 0,
69+
},
70+
expect: "1",
3871
},
3972
{
4073
name: "test with big decimal",
4174
args: args{
42-
lot: 0.00100000,
75+
minQty: "0.01",
76+
stepSize: "0.02",
77+
amount: "11.31232419283240912834434",
4378
precision: 8,
44-
amount: 11.31232419283240912834434,
4579
},
46-
want: 11.312,
80+
expect: "11.31",
4781
},
4882
{
4983
name: "test with big number",
5084
args: args{
51-
lot: 0.0010000,
85+
minQty: "0.0001",
86+
stepSize: "0.02",
87+
amount: "11232821093480213.31232419283240912834434",
5288
precision: 8,
53-
amount: 11232821093480213.31232419283240912834434,
5489
},
55-
want: 11232821093480213.3123,
90+
expect: "11232821093480213.3001",
91+
},
92+
{
93+
name: "test with small decimal",
94+
args: args{
95+
minQty: "0.0000010",
96+
stepSize: "0.0000010",
97+
amount: "0.003923153000000002",
98+
precision: 7,
99+
},
100+
expect: "0.003923",
56101
},
57102
}
103+
58104
for _, tt := range tests {
59105
t.Run(tt.name, func(t *testing.T) {
60-
assert.Equal(tt.want, AmountToLotSize(tt.args.lot, tt.args.precision, tt.args.amount))
106+
assert.Equal(tt.expect, AmountToLotSize(tt.args.amount, tt.args.minQty, tt.args.stepSize, tt.args.precision))
61107
})
62108
}
63109
}

v2/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/google/uuid v1.6.0
99
github.com/gorilla/websocket v1.5.0
1010
github.com/jpillora/backoff v1.0.0
11+
github.com/shopspring/decimal v1.4.0
1112
github.com/stretchr/testify v1.8.1
1213
)
1314

v2/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
2020
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
2121
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2222
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
23+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
24+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
2325
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2426
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
2527
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=

0 commit comments

Comments
 (0)