grid2: fix base + quote order placement and add test case

This commit is contained in:
c9s 2023-05-23 17:34:03 +08:00
parent d5cf53ee94
commit 1cf788c925
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 15 additions and 6 deletions

View File

@ -1351,7 +1351,7 @@ func (s *Strategy) generateGridOrders(totalQuote, totalBase, lastPrice fixedpoin
continue
}
if usedBase.Add(quantity).Compare(totalBase) < 0 {
if usedBase.Add(quantity).Compare(totalBase) <= 0 {
submitOrders = append(submitOrders, types.SubmitOrder{
Symbol: s.Symbol,
Type: types.OrderTypeLimit,

View File

@ -167,14 +167,22 @@ func TestStrategy_generateGridOrders(t *testing.T) {
}, orders)
})
t.Run("base + quote", func(t *testing.T) {
t.Run("base and quote", func(t *testing.T) {
s := newTestStrategy()
s.grid = NewGrid(s.LowerPrice, s.UpperPrice, fixedpoint.NewFromInt(s.GridNum), s.Market.TickSize)
s.grid.CalculateArithmeticPins()
s.QuantityOrAmount.Quantity = number(0.01)
quoteInvestment := number(10_000.0)
baseInvestment := number(0.1)
lastPrice := number(15300)
orders, err := s.generateGridOrders(number(10000.0), number(0.021), lastPrice)
quantity, err := s.calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInvestment, lastPrice, s.grid.Pins)
assert.NoError(t, err)
assert.Equal(t, number(0.025), quantity)
s.QuantityOrAmount.Quantity = quantity
orders, err := s.generateGridOrders(quoteInvestment, baseInvestment, lastPrice)
assert.NoError(t, err)
if !assert.Equal(t, 10, len(orders)) {
for _, o := range orders {
@ -185,8 +193,9 @@ func TestStrategy_generateGridOrders(t *testing.T) {
assertPriceSide(t, []PriceSideAssert{
{number(20000.0), types.SideTypeSell},
{number(19000.0), types.SideTypeSell},
{number(17000.0), types.SideTypeBuy},
{number(16000.0), types.SideTypeBuy},
{number(18000.0), types.SideTypeSell},
{number(17000.0), types.SideTypeSell},
// -- 16_000 should be empty
{number(15000.0), types.SideTypeBuy},
{number(14000.0), types.SideTypeBuy},
{number(13000.0), types.SideTypeBuy},