Skip to content

Commit

Permalink
fix precision problem
Browse files Browse the repository at this point in the history
  • Loading branch information
kbearXD committed Mar 1, 2024
1 parent b757098 commit 32fc75f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/strategy/dca2/open_position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
23 changes: 20 additions & 3 deletions pkg/strategy/dca2/open_position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
})
}

0 comments on commit 32fc75f

Please sign in to comment.