Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [grid2] add remove duplicated pins and pull out filter price prec func #1239

Merged
merged 3 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches:
- "main"
- "v*"

jobs:
build:
Expand Down
44 changes: 32 additions & 12 deletions pkg/strategy/grid2/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,48 @@ type Grid struct {

type Pin fixedpoint.Value

// filterPrice filters price with the given precision
func filterPrice(p fixedpoint.Value, prec int) fixedpoint.Value {
var pow10 = math.Pow10(prec)
pp := math.Round(p.Float64()*pow10*10.0) / 10.0
pp = math.Trunc(pp) / pow10

pps := strconv.FormatFloat(pp, 'f', prec, 64)
price := fixedpoint.MustNewFromString(pps)
return price
}

func removeDuplicatedPins(pins []Pin) []Pin {
var buckets = map[string]struct{}{}
var out []Pin

for _, pin := range pins {
p := fixedpoint.Value(pin)

if _, exists := buckets[p.String()]; exists {
continue
Copy link
Collaborator

@gx578007 gx578007 Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth logging errors if any duplicated pin is found.

}

out = append(out, pin)
buckets[p.String()] = struct{}{}
}

return out
}

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.Sub(spread)) <= 0; p = p.Add(spread) {
pp := math.Round(p.Float64()*pow10*10.0) / 10.0
pp = math.Trunc(pp) / pow10

pps := strconv.FormatFloat(pp, 'f', prec, 64)
price := fixedpoint.MustNewFromString(pps)
price := filterPrice(p, prec)
pins = append(pins, Pin(price))
}

// this makes sure there is no error at the upper price
pp := math.Round(upper.Float64()*pow10*10.0) / 10.0
pp = math.Trunc(pp) / pow10

pps := strconv.FormatFloat(pp, 'f', prec, 64)
upperPrice := fixedpoint.MustNewFromString(pps)
upperPrice := filterPrice(upper, prec)
pins = append(pins, Pin(upperPrice))

return pins
Expand Down Expand Up @@ -94,7 +114,7 @@ func (g *Grid) CalculateGeometricPins() {
return nil
}

g.addPins(g.calculator())
g.addPins(removeDuplicatedPins(g.calculator()))
}

func (g *Grid) CalculateArithmeticPins() {
Expand Down
51 changes: 51 additions & 0 deletions pkg/strategy/grid2/grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,54 @@ func Test_calculateArithmeticPins(t *testing.T) {
})
}
}

func Test_filterPrice1(t *testing.T) {
type args struct {
p fixedpoint.Value
prec int
}
tests := []struct {
name string
args args
want string
}{
{
name: "basic",
args: args{p: number("31.2222"), prec: 3},
want: "31.222",
},
{
name: "roundup",
args: args{p: number("31.22295"), prec: 3},
want: "31.223",
},
{
name: "roundup2",
args: args{p: number("31.22290"), prec: 3},
want: "31.222",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rst := filterPrice(tt.args.p, tt.args.prec)
assert.Equalf(t, tt.want, rst.String(), "filterPrice(%v, %v)", tt.args.p, tt.args.prec)
})
}
}

func Test_removeDuplicatedPins(t *testing.T) {
pins := []Pin{
Pin(number("31.222")),
Pin(number("31.222")),
Pin(number("31.223")),
Pin(number("31.224")),
Pin(number("31.224")),
}
out := removeDuplicatedPins(pins)
assert.Equal(t, []Pin{
Pin(number("31.222")),
Pin(number("31.223")),
Pin(number("31.224")),
}, out)

}