grid2: add TestStrategy_calculateBaseQuoteInvestmentQuantity test case

This commit is contained in:
c9s 2023-05-19 16:37:44 +08:00
parent 0c4cd7049f
commit 3a2dbc934b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 40 additions and 1 deletions

View File

@ -777,6 +777,9 @@ func (s *Strategy) calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInv
numberOfSellOrders++
}
// avoid placing a sell order above the last price
numberOfSellOrders--
// if the maxBaseQuantity is less than minQuantity, then we need to reduce the number of the sell orders
// so that the quantity can be increased.
baseQuantity := s.Market.TruncateQuantity(
@ -785,7 +788,7 @@ func (s *Strategy) calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInv
int64(numberOfSellOrders))))
minBaseQuantity := fixedpoint.Max(
s.Market.MinNotional.Div(lastPrice),
s.Market.MinNotional.Div(s.UpperPrice),
s.Market.MinQuantity)
if baseQuantity.Compare(minBaseQuantity) <= 0 {

View File

@ -285,6 +285,41 @@ func TestStrategy_checkRequiredInvestmentByAmount(t *testing.T) {
})
}
func TestStrategy_calculateBaseQuoteInvestmentQuantity(t *testing.T) {
t.Run("basic", func(t *testing.T) {
s := newTestStrategy()
s.Market = types.Market{
BaseCurrency: "ETH",
QuoteCurrency: "USDT",
TickSize: number(0.01),
StepSize: number(0.00001),
PricePrecision: 2,
VolumePrecision: 6,
MinNotional: number(8.000),
MinQuantity: number(0.00030),
}
s.UpperPrice = number(200.0)
s.LowerPrice = number(100.0)
s.GridNum = 7
s.Compound = true
lastPrice := number(180.0)
quoteInvestment := number(334.0) // 333.33
baseInvestment := number(0.5)
quantity, err := s.calculateBaseQuoteInvestmentQuantity(quoteInvestment, baseInvestment, lastPrice, []Pin{
Pin(number(100.00)),
Pin(number(116.67)),
Pin(number(133.33)),
Pin(number(150.00)),
Pin(number(166.67)),
Pin(number(183.33)),
Pin(number(200.00)),
})
assert.NoError(t, err)
assert.InDelta(t, 0.5, quantity.Float64(), 0.0001)
})
}
func TestStrategy_calculateQuoteInvestmentQuantity(t *testing.T) {
t.Run("quote quantity", func(t *testing.T) {
@ -380,6 +415,7 @@ func newTestMarket() types.Market {
BaseCurrency: "BTC",
QuoteCurrency: "USDT",
TickSize: number(0.01),
StepSize: number(0.00001),
PricePrecision: 2,
VolumePrecision: 8,
MinNotional: number(10.0),