From 854ac4f8eaced83ee36dc5514bb303bad76e17c7 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 1 Feb 2023 18:56:01 +0800 Subject: [PATCH] grid2: fix pin price algorithm --- pkg/strategy/grid2/grid.go | 9 ++++++--- pkg/strategy/grid2/grid_test.go | 18 +++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/pkg/strategy/grid2/grid.go b/pkg/strategy/grid2/grid.go index 792c1a334..d5615626c 100644 --- a/pkg/strategy/grid2/grid.go +++ b/pkg/strategy/grid2/grid.go @@ -35,11 +35,14 @@ type Pin fixedpoint.Value func calculateArithmeticPins(lower, upper, spread, tickSize fixedpoint.Value) []Pin { var pins []Pin + + // tickSize number is like 0.01, 0.1, 0.001 var ts = tickSize.Float64() + var prec = int(math.Round(math.Log10(ts) * -1.0)) + var pow10 = math.Pow10(prec) for p := lower; p.Compare(upper) <= 0; p = p.Add(spread) { - // tickSize here = 0.01 - pp := p.Float64() / ts - pp = math.Round(pp) * ts + pp := math.Round(p.Float64()*pow10*10.0) / 10.0 + pp = math.Trunc(pp) / pow10 pin := Pin(fixedpoint.NewFromFloat(pp)) pins = append(pins, pin) } diff --git a/pkg/strategy/grid2/grid_test.go b/pkg/strategy/grid2/grid_test.go index a5d72281f..b5fdaff98 100644 --- a/pkg/strategy/grid2/grid_test.go +++ b/pkg/strategy/grid2/grid_test.go @@ -172,31 +172,31 @@ func Test_calculateArithmeticPins(t *testing.T) { Pin(number(1000.0)), Pin(number(1066.660)), Pin(number(1133.330)), - Pin(number("1199.99")), + Pin(number("1200.00")), Pin(number(1266.660)), Pin(number(1333.330)), - Pin(number(1399.990)), + Pin(number(1400.000)), Pin(number(1466.660)), Pin(number(1533.330)), - Pin(number(1599.990)), + Pin(number(1600.000)), Pin(number(1666.660)), Pin(number(1733.330)), - Pin(number(1799.990)), + Pin(number(1800.000)), Pin(number(1866.660)), Pin(number(1933.330)), - Pin(number(1999.990)), + Pin(number(2000.000)), Pin(number(2066.660)), Pin(number(2133.330)), - Pin(number("2199.99")), + Pin(number("2200.00")), Pin(number(2266.660)), Pin(number(2333.330)), - Pin(number("2399.99")), + Pin(number("2400.00")), Pin(number(2466.660)), Pin(number(2533.330)), - Pin(number("2599.99")), + Pin(number("2600.00")), Pin(number(2666.660)), Pin(number(2733.330)), - Pin(number(2799.990)), + Pin(number(2800.000)), Pin(number(2866.660)), Pin(number(2933.330)), },