grid2: fix error checking and add more tests

This commit is contained in:
c9s 2022-11-16 13:29:55 +08:00
parent dcbce8aa5c
commit f5219ae56b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 42 additions and 3 deletions

View File

@ -225,9 +225,25 @@ func (s *Strategy) checkRequiredInvestmentByQuantity(baseInvestment, quoteInvest
}
if requiredBase.Compare(baseBalance) > 0 && requiredQuote.Compare(quoteBalance) > 0 {
return requiredBase, requiredQuote, fmt.Errorf("both base balance (%f %s) and quote balance (%f %s) are not enough",
return requiredBase, requiredQuote, fmt.Errorf("both base balance (%f %s) or quote balance (%f %s) is not enough, required = base %f + quote %f",
baseBalance.Float64(), s.Market.BaseCurrency,
quoteBalance.Float64(), s.Market.QuoteCurrency)
quoteBalance.Float64(), s.Market.QuoteCurrency,
requiredBase.Float64(),
requiredQuote.Float64())
}
if requiredBase.Compare(baseBalance) > 0 {
return requiredBase, requiredQuote, fmt.Errorf("base balance (%f %s), required = base %f",
baseBalance.Float64(), s.Market.BaseCurrency,
requiredBase.Float64(),
)
}
if requiredQuote.Compare(quoteBalance) > 0 {
return requiredBase, requiredQuote, fmt.Errorf("quote balance (%f %s) is not enough, required = quote %f",
quoteBalance.Float64(), s.Market.QuoteCurrency,
requiredQuote.Float64(),
)
}
return requiredBase, requiredQuote, nil

View File

@ -4,10 +4,17 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/types"
)
func TestStrategy_checkRequiredInvestmentByQuantity(t *testing.T) {
s := &Strategy{}
s := &Strategy{
Market: types.Market{
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
},
}
t.Run("basic base balance check", func(t *testing.T) {
_, _, err := s.checkRequiredInvestmentByQuantity(number(2.0), number(10_000.0),
@ -39,4 +46,20 @@ func TestStrategy_checkRequiredInvestmentByQuantity(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, number(6000.0), requiredQuote)
})
t.Run("quote to base balance conversion not enough", func(t *testing.T) {
_, requiredQuote, err := s.checkRequiredInvestmentByQuantity(number(0.0), number(5_000.0),
number(0.0), number(5_000.0),
number(0.1), number(13_500.0), []Pin{
Pin(number(10_000.0)), // 0.1 * 10_000 = 1000 USD (buy)
Pin(number(11_000.0)), // 0.1 * 11_000 = 1100 USD (buy)
Pin(number(12_000.0)), // 0.1 * 12_000 = 1200 USD (buy)
Pin(number(13_000.0)), // 0.1 * 13_000 = 1300 USD (buy)
Pin(number(14_000.0)), // 0.1 * 14_000 = 1400 USD (buy)
Pin(number(15_000.0)), // 0.1 * 15_000 = 1500 USD
})
assert.EqualError(t, err, "quote balance (5000.000000 USDT) is not enough, required = quote 6000.000000")
assert.Equal(t, number(6000.0), requiredQuote)
})
}