diff --git a/pkg/strategy/dca2/open_position.go b/pkg/strategy/dca2/open_position.go index 312790ac05..e6479c9974 100644 --- a/pkg/strategy/dca2/open_position.go +++ b/pkg/strategy/dca2/open_position.go @@ -89,6 +89,7 @@ func generateOpenPositionOrders(market types.Market, quoteInvestment, profit, pr var quantity fixedpoint.Value // all the profit will use in the first order if i == 0 { + profit = market.TruncatePrice(profit) quantity = market.TruncateQuantity(notional.Add(profit).Div(prices[i])) } else { quantity = market.TruncateQuantity(notional.Div(prices[i])) diff --git a/pkg/strategy/dca2/open_position_test.go b/pkg/strategy/dca2/open_position_test.go index fb8f5705bd..b735b7084f 100644 --- a/pkg/strategy/dca2/open_position_test.go +++ b/pkg/strategy/dca2/open_position_test.go @@ -67,12 +67,29 @@ func TestGenerateOpenPositionOrders(t *testing.T) { assert.Equal(Number("0.102055"), submitOrders[3].Quantity) }) - t.Run("case 2: some orders' price will below 0, so we should not create such order", func(t *testing.T) { + t.Run("case 2: profit need to be truncated to avoid precision problem", func(t *testing.T) { + quoteInvestment := Number("1000") + profit := Number("99.47871711") + askPrice := Number("40409.72") + margin := Number("0.1") + submitOrders, err := generateOpenPositionOrders(strategy.Market, quoteInvestment, profit, askPrice, margin, 2, strategy.OrderGroupID) + if !assert.NoError(err) { + return + } + + assert.Len(submitOrders, 2) + assert.Equal(Number("40409.72"), submitOrders[0].Price) + assert.Equal(Number("0.014834"), submitOrders[0].Quantity) + assert.Equal(Number("36368.74"), submitOrders[1].Price) + assert.Equal(Number("0.013748"), submitOrders[1].Quantity) + }) + + t.Run("case 3: some orders' price will below 0, so we should not create such order", func(t *testing.T) { }) - t.Run("case 3: notional is too small, so we should decrease num of orders", func(t *testing.T) { + t.Run("case 4: notional is too small, so we should decrease num of orders", func(t *testing.T) { }) - t.Run("case 4: quantity is too small, so we should decrease num of orders", func(t *testing.T) { + t.Run("case 5: quantity is too small, so we should decrease num of orders", func(t *testing.T) { }) }