mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
grid2: include the order dust for the quote investment calculation
This commit is contained in:
parent
156da92670
commit
55476e4176
|
@ -3,6 +3,7 @@ package grid2
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -558,6 +559,7 @@ func (s *Strategy) calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice f
|
||||||
// q = quoteInvestment / (p1 + p2 + p3)
|
// q = quoteInvestment / (p1 + p2 + p3)
|
||||||
totalQuotePrice := fixedpoint.Zero
|
totalQuotePrice := fixedpoint.Zero
|
||||||
si := len(pins)
|
si := len(pins)
|
||||||
|
cntOrder := 0
|
||||||
for i := len(pins) - 1; i >= 0; i-- {
|
for i := len(pins) - 1; i >= 0; i-- {
|
||||||
pin := pins[i]
|
pin := pins[i]
|
||||||
price := fixedpoint.Value(pin)
|
price := fixedpoint.Value(pin)
|
||||||
|
@ -581,6 +583,8 @@ func (s *Strategy) calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice f
|
||||||
nextLowerPrice := fixedpoint.Value(nextLowerPin)
|
nextLowerPrice := fixedpoint.Value(nextLowerPin)
|
||||||
totalQuotePrice = totalQuotePrice.Add(nextLowerPrice)
|
totalQuotePrice = totalQuotePrice.Add(nextLowerPrice)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cntOrder++
|
||||||
} else {
|
} else {
|
||||||
// for orders that buy
|
// for orders that buy
|
||||||
if s.ProfitSpread.IsZero() && i+1 == si {
|
if s.ProfitSpread.IsZero() && i+1 == si {
|
||||||
|
@ -593,11 +597,14 @@ func (s *Strategy) calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice f
|
||||||
}
|
}
|
||||||
|
|
||||||
totalQuotePrice = totalQuotePrice.Add(price)
|
totalQuotePrice = totalQuotePrice.Add(price)
|
||||||
|
cntOrder++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
q := quoteInvestment.Div(totalQuotePrice)
|
orderDusts := fixedpoint.NewFromFloat(math.Pow10(-s.Market.PricePrecision) * float64(cntOrder))
|
||||||
s.logger.Infof("calculateQuoteInvestmentQuantity: sumOfPrice=%f quantity=%f", totalQuotePrice.Float64(), q.Float64())
|
adjustedQuoteInvestment := quoteInvestment.Sub(orderDusts)
|
||||||
|
q := adjustedQuoteInvestment.Div(totalQuotePrice)
|
||||||
|
s.logger.Infof("calculateQuoteInvestmentQuantity: adjustedQuoteInvestment=%f sumOfPrice=%f quantity=%f", adjustedQuoteInvestment.Float64(), totalQuotePrice.Float64(), q.Float64())
|
||||||
return q, nil
|
return q, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -125,7 +125,7 @@ func TestStrategy_generateGridOrders(t *testing.T) {
|
||||||
|
|
||||||
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, s.grid.Pins)
|
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, s.grid.Pins)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, number(38.75968992).String(), quantity.String())
|
assert.InDelta(t, 38.7364341, quantity.Float64(), 0.00001)
|
||||||
|
|
||||||
s.QuantityOrAmount.Quantity = quantity
|
s.QuantityOrAmount.Quantity = quantity
|
||||||
|
|
||||||
|
@ -284,7 +284,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
|
||||||
Pin(number(15_000.0)),
|
Pin(number(15_000.0)),
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, number(0.2).String(), quantity.String())
|
assert.InDelta(t, 0.199999916, quantity.Float64(), 0.0001)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("quote quantity #2", func(t *testing.T) {
|
t.Run("quote quantity #2", func(t *testing.T) {
|
||||||
|
@ -301,7 +301,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
|
||||||
Pin(number(200.00)),
|
Pin(number(200.00)),
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, number(1.17647058).String(), quantity.String())
|
assert.InDelta(t, 1.1764, quantity.Float64(), 0.00001)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("quote quantity #3", func(t *testing.T) {
|
t.Run("quote quantity #3", func(t *testing.T) {
|
||||||
|
@ -319,7 +319,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
|
||||||
}
|
}
|
||||||
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, pins)
|
quantity, err := s.calculateQuoteInvestmentQuantity(quoteInvestment, lastPrice, pins)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.InDelta(t, 38.75968992, quantity.Float64(), 0.0001)
|
assert.InDelta(t, 38.736434, quantity.Float64(), 0.0001)
|
||||||
|
|
||||||
var totalQuoteUsed = fixedpoint.Zero
|
var totalQuoteUsed = fixedpoint.Zero
|
||||||
for i, pin := range pins {
|
for i, pin := range pins {
|
||||||
|
@ -351,7 +351,7 @@ func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
|
||||||
Pin(number(15_000.0)), // sell order @ 17_000
|
Pin(number(15_000.0)), // sell order @ 17_000
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, number(0.1).String(), quantity.String())
|
assert.InDelta(t, 0.099992, quantity.Float64(), 0.0001)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user