grid2: add test case for testing checkMinimalQuoteInvestment

This commit is contained in:
c9s 2022-12-06 16:09:46 +08:00
parent 47759236e0
commit b8e5bf1ddd
2 changed files with 35 additions and 5 deletions

View File

@ -929,12 +929,15 @@ func (s *Strategy) getLastTradePrice(ctx context.Context, session *bbgo.Exchange
return fixedpoint.Zero, fmt.Errorf("%s ticker price not found", s.Symbol)
}
func calculateMinimalQuoteInvestment(market types.Market, lowerPrice, upperPrice fixedpoint.Value, gridNum int64) fixedpoint.Value {
num := fixedpoint.NewFromInt(gridNum)
minimalAmountLowerPrice := fixedpoint.Max(lowerPrice.Mul(market.MinQuantity), market.MinNotional)
minimalAmountUpperPrice := fixedpoint.Max(upperPrice.Mul(market.MinQuantity), market.MinNotional)
return fixedpoint.Max(minimalAmountLowerPrice, minimalAmountUpperPrice).Mul(num)
}
func (s *Strategy) checkMinimalQuoteInvestment() error {
gridNum := fixedpoint.NewFromInt(s.GridNum)
// gridSpread := s.UpperPrice.Sub(s.LowerPrice).Div(gridNum)
minimalAmountLowerPrice := fixedpoint.Max(s.LowerPrice.Mul(s.Market.MinQuantity), s.Market.MinNotional)
minimalAmountUpperPrice := fixedpoint.Max(s.UpperPrice.Mul(s.Market.MinQuantity), s.Market.MinNotional)
minimalQuoteInvestment := fixedpoint.Max(minimalAmountLowerPrice, minimalAmountUpperPrice).Mul(gridNum)
minimalQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, s.LowerPrice, s.UpperPrice, s.GridNum)
if s.QuoteInvestment.Compare(minimalQuoteInvestment) <= 0 {
return fmt.Errorf("need at least %f %s for quote investment, %f %s given",
minimalQuoteInvestment.Float64(),

View File

@ -263,6 +263,8 @@ func newTestStrategy() *Strategy {
TickSize: number(0.01),
PricePrecision: 2,
VolumePrecision: 8,
MinNotional: number(10.0),
MinQuantity: number(0.001),
}
s := &Strategy{
@ -462,6 +464,31 @@ func TestStrategy_aggregateOrderBaseFeeRetry(t *testing.T) {
assert.Equal(t, "0.01", baseFee.String())
}
func TestStrategy_checkMinimalQuoteInvestment(t *testing.T) {
s := newTestStrategy()
t.Run("10 grids", func(t *testing.T) {
s.QuoteInvestment = number(10_000)
s.GridNum = 10
minQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, s.LowerPrice, s.UpperPrice, s.GridNum)
assert.Equal(t, "20", minQuoteInvestment.String())
err := s.checkMinimalQuoteInvestment()
assert.NoError(t, err)
})
t.Run("1000 grids", func(t *testing.T) {
s.QuoteInvestment = number(10_000)
s.GridNum = 1000
minQuoteInvestment := calculateMinimalQuoteInvestment(s.Market, s.LowerPrice, s.UpperPrice, s.GridNum)
assert.Equal(t, "20000", minQuoteInvestment.String())
err := s.checkMinimalQuoteInvestment()
assert.Error(t, err)
assert.EqualError(t, err, "need at least 20000.000000 USDT for quote investment, 10000.000000 USDT given")
})
}
func TestBacktestStrategy(t *testing.T) {
market := types.Market{
BaseCurrency: "BTC",