bbgo_origin/pkg/strategy/grid2/grid.go

256 lines
5.9 KiB
Go
Raw Permalink Normal View History

2022-11-03 05:41:47 +00:00
package grid2
import (
2022-12-03 06:58:53 +00:00
"fmt"
2022-11-03 16:26:28 +00:00
"math"
2022-11-06 02:12:50 +00:00
"sort"
2023-02-15 14:32:55 +00:00
"strconv"
2022-11-03 16:26:28 +00:00
2022-11-03 05:41:47 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2023-02-21 07:50:25 +00:00
"github.com/c9s/bbgo/pkg/types"
2022-11-03 05:41:47 +00:00
)
2022-11-09 11:43:14 +00:00
type PinCalculator func() []Pin
2022-11-03 05:41:47 +00:00
type Grid struct {
UpperPrice fixedpoint.Value `json:"upperPrice"`
LowerPrice fixedpoint.Value `json:"lowerPrice"`
// Size is the number of total grids
Size fixedpoint.Value `json:"size"`
2022-11-07 05:51:44 +00:00
// TickSize is the price tick size, this is used for truncating price
TickSize fixedpoint.Value `json:"tickSize"`
// Spread is a immutable number
Spread fixedpoint.Value `json:"spread"`
2022-11-03 05:41:47 +00:00
// Pins are the pinned grid prices, from low to high
2022-11-03 16:26:28 +00:00
Pins []Pin `json:"pins"`
2022-11-03 05:41:47 +00:00
2022-11-03 16:26:28 +00:00
pinsCache map[Pin]struct{} `json:"-"`
2022-11-09 11:43:14 +00:00
calculator PinCalculator
2022-11-03 05:41:47 +00:00
}
2022-11-03 16:26:28 +00:00
type Pin fixedpoint.Value
// roundAndTruncatePrice rounds the given price at prec-1 and then truncate the price at prec
func roundAndTruncatePrice(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
}
2023-07-22 03:45:30 +00:00
out = append(out, pin)
buckets[p.String()] = struct{}{}
}
return out
}
2022-11-07 05:51:44 +00:00
func calculateArithmeticPins(lower, upper, spread, tickSize fixedpoint.Value) []Pin {
2022-11-03 16:26:28 +00:00
var pins []Pin
2023-02-01 10:56:01 +00:00
// tickSize number is like 0.01, 0.1, 0.001
2022-11-25 10:07:02 +00:00
var ts = tickSize.Float64()
2023-02-01 10:56:01 +00:00
var prec = int(math.Round(math.Log10(ts) * -1.0))
2023-02-10 09:22:19 +00:00
for p := lower; p.Compare(upper.Sub(spread)) <= 0; p = p.Add(spread) {
price := roundAndTruncatePrice(p, prec)
2023-02-15 14:32:55 +00:00
pins = append(pins, Pin(price))
2022-11-03 16:26:28 +00:00
}
2023-02-10 09:22:19 +00:00
// this makes sure there is no error at the upper price
upperPrice := roundAndTruncatePrice(upper, prec)
2023-07-18 07:48:27 +00:00
pins = append(pins, Pin(upperPrice))
2023-02-10 09:22:19 +00:00
2022-11-03 16:26:28 +00:00
return pins
}
2022-11-03 05:41:47 +00:00
2022-11-03 16:26:28 +00:00
func buildPinCache(pins []Pin) map[Pin]struct{} {
cache := make(map[Pin]struct{}, len(pins))
for _, pin := range pins {
cache[pin] = struct{}{}
2022-11-03 05:41:47 +00:00
}
2022-11-03 16:26:28 +00:00
return cache
}
func NewGrid(lower, upper, size, tickSize fixedpoint.Value) *Grid {
height := upper.Sub(lower)
2023-01-16 10:34:08 +00:00
one := fixedpoint.NewFromInt(1)
spread := height.Div(size.Sub(one))
2022-11-03 05:41:47 +00:00
grid := &Grid{
UpperPrice: upper,
LowerPrice: lower,
2022-11-03 16:26:28 +00:00
Size: size,
2022-11-07 05:51:44 +00:00
TickSize: tickSize,
Spread: spread,
2022-11-03 05:41:47 +00:00
}
2022-11-03 16:26:28 +00:00
2022-11-03 05:41:47 +00:00
return grid
}
2022-11-09 11:43:14 +00:00
func (g *Grid) CalculateGeometricPins() {
g.calculator = func() []Pin {
// TODO: implement geometric calculator
2022-11-09 11:43:14 +00:00
// return calculateArithmeticPins(g.LowerPrice, g.UpperPrice, g.Spread, g.TickSize)
return nil
}
g.addPins(removeDuplicatedPins(g.calculator()))
2022-11-09 11:43:14 +00:00
}
func (g *Grid) CalculateArithmeticPins() {
g.calculator = func() []Pin {
2023-01-16 10:34:08 +00:00
one := fixedpoint.NewFromInt(1)
height := g.UpperPrice.Sub(g.LowerPrice)
spread := height.Div(g.Size.Sub(one))
return calculateArithmeticPins(g.LowerPrice, g.UpperPrice, spread, g.TickSize)
2022-11-09 11:43:14 +00:00
}
g.addPins(g.calculator())
2022-11-09 08:26:04 +00:00
}
2022-11-06 02:06:57 +00:00
func (g *Grid) Height() fixedpoint.Value {
return g.UpperPrice.Sub(g.LowerPrice)
}
2022-11-03 05:41:47 +00:00
func (g *Grid) Above(price fixedpoint.Value) bool {
2022-11-03 16:26:28 +00:00
return price.Compare(g.UpperPrice) > 0
2022-11-03 05:41:47 +00:00
}
func (g *Grid) Below(price fixedpoint.Value) bool {
2022-11-03 16:26:28 +00:00
return price.Compare(g.LowerPrice) < 0
2022-11-03 05:41:47 +00:00
}
func (g *Grid) OutOfRange(price fixedpoint.Value) bool {
2022-11-03 16:26:28 +00:00
return price.Compare(g.LowerPrice) < 0 || price.Compare(g.UpperPrice) > 0
2022-11-03 05:41:47 +00:00
}
2022-11-03 16:26:28 +00:00
func (g *Grid) HasPin(pin Pin) (ok bool) {
2022-11-03 05:41:47 +00:00
_, ok = g.pinsCache[pin]
return ok
}
// NextHigherPin finds the next higher pin
func (g *Grid) NextHigherPin(price fixedpoint.Value) (Pin, bool) {
i := g.SearchPin(price)
if i < len(g.Pins) && fixedpoint.Value(g.Pins[i]).Compare(price) == 0 && i+1 < len(g.Pins) {
return g.Pins[i+1], true
}
return Pin(fixedpoint.Zero), false
}
// NextLowerPin finds the next lower pin
func (g *Grid) NextLowerPin(price fixedpoint.Value) (Pin, bool) {
i := g.SearchPin(price)
if i < len(g.Pins) && fixedpoint.Value(g.Pins[i]).Compare(price) == 0 && i-1 >= 0 {
return g.Pins[i-1], true
}
return Pin(fixedpoint.Zero), false
}
2023-02-21 07:50:25 +00:00
func (g *Grid) FilterOrders(orders []types.Order) (ret []types.Order) {
for _, o := range orders {
if !g.HasPrice(o.Price) {
continue
}
ret = append(ret, o)
}
return ret
}
2023-02-08 08:43:25 +00:00
func (g *Grid) HasPrice(price fixedpoint.Value) bool {
2023-02-15 06:57:21 +00:00
if _, exists := g.pinsCache[Pin(price)]; exists {
return exists
}
2023-02-08 08:43:25 +00:00
i := g.SearchPin(price)
2023-02-15 06:57:21 +00:00
if i >= 0 && i < len(g.Pins) {
return fixedpoint.Value(g.Pins[i]).Compare(price) == 0
}
return false
2023-02-08 08:43:25 +00:00
}
func (g *Grid) SearchPin(price fixedpoint.Value) int {
i := sort.Search(len(g.Pins), func(i int) bool {
a := fixedpoint.Value(g.Pins[i])
return a.Compare(price) >= 0
})
return i
}
2022-11-03 16:26:28 +00:00
func (g *Grid) ExtendUpperPrice(upper fixedpoint.Value) (newPins []Pin) {
if upper.Compare(g.UpperPrice) <= 0 {
return nil
}
newPins = calculateArithmeticPins(g.UpperPrice.Add(g.Spread), upper, g.Spread, g.TickSize)
2022-11-06 02:14:07 +00:00
g.UpperPrice = upper
g.addPins(newPins)
2022-11-03 05:41:47 +00:00
return newPins
}
2022-11-03 16:26:28 +00:00
func (g *Grid) ExtendLowerPrice(lower fixedpoint.Value) (newPins []Pin) {
if lower.Compare(g.LowerPrice) >= 0 {
return nil
2022-11-06 02:12:50 +00:00
}
2022-11-06 02:06:57 +00:00
n := g.LowerPrice.Sub(lower).Div(g.Spread).Floor()
lower = g.LowerPrice.Sub(g.Spread.Mul(n))
newPins = calculateArithmeticPins(lower, g.LowerPrice.Sub(g.Spread), g.Spread, g.TickSize)
2022-11-06 02:13:13 +00:00
g.LowerPrice = lower
2022-11-06 02:12:50 +00:00
g.addPins(newPins)
return newPins
}
2022-11-03 05:41:47 +00:00
func (g *Grid) TopPin() Pin {
return g.Pins[len(g.Pins)-1]
}
func (g *Grid) BottomPin() Pin {
return g.Pins[0]
}
2022-11-06 02:12:50 +00:00
func (g *Grid) addPins(pins []Pin) {
g.Pins = append(g.Pins, pins...)
2022-11-03 05:41:47 +00:00
2022-11-06 02:12:50 +00:00
sort.Slice(g.Pins, func(i, j int) bool {
a := fixedpoint.Value(g.Pins[i])
b := fixedpoint.Value(g.Pins[j])
return a.Compare(b) < 0
})
2022-11-03 05:41:47 +00:00
g.updatePinsCache()
}
2022-11-07 05:51:44 +00:00
func (g *Grid) updatePinsCache() {
g.pinsCache = buildPinCache(g.Pins)
}
2022-12-03 06:58:53 +00:00
func (g *Grid) String() string {
2023-02-15 14:32:55 +00:00
return fmt.Sprintf("GRID: priceRange: %f <=> %f size: %f spread: %f tickSize: %f", g.LowerPrice.Float64(), g.UpperPrice.Float64(), g.Size.Float64(), g.Spread.Float64(), g.TickSize.Float64())
2022-12-03 06:58:53 +00:00
}