From a3a262783ff4637848fef16398b4c877081249cb Mon Sep 17 00:00:00 2001 From: zenix Date: Tue, 15 Feb 2022 18:59:10 +0900 Subject: [PATCH] fix: set backtest cancel Delta to be 1e-11 --- pkg/fixedpoint/dec.go | 3 +-- pkg/strategy/grid/strategy.go | 23 ++++++++++++++++++----- pkg/types/account.go | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pkg/fixedpoint/dec.go b/pkg/fixedpoint/dec.go index 1e13e5db5..574f2cf90 100644 --- a/pkg/fixedpoint/dec.go +++ b/pkg/fixedpoint/dec.go @@ -790,8 +790,7 @@ func (dn Value) Int64() int64 { return 0 } if dn.sign != signNegInf && dn.sign != signPosInf { - if 0 < dn.exp && dn.exp < digitsMax && - (dn.coef%pow10[digitsMax-dn.exp]) == 0 { // usual case + if 0 < dn.exp && dn.exp < digitsMax { return int64(dn.sign) * int64(dn.coef/pow10[digitsMax-dn.exp]) } else if dn.exp <= 0 && dn.coef != 0 { result := math.Log10(float64(dn.coef)) - float64(digitsMax) + float64(dn.exp) diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index d76f9a0ec..ffcdd0ffe 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -149,6 +149,12 @@ func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]type numGrids := fixedpoint.NewFromInt(s.GridNum) gridSpread := priceRange.Div(numGrids) + if gridSpread.IsZero() { + return nil, fmt.Errorf( + "either numGrids(%v) is too big or priceRange(%v) is too small, "+ + "the differences of grid prices become zero", numGrids, priceRange) + } + // find the nearest grid price from the current price startPrice := fixedpoint.Max( s.LowerPrice, @@ -156,9 +162,9 @@ func (s *Strategy) generateGridSellOrders(session *bbgo.ExchangeSession) ([]type s.UpperPrice.Sub(currentPrice).Div(gridSpread).Trunc().Mul(gridSpread))) if startPrice.Compare(s.UpperPrice) > 0 { - return nil, fmt.Errorf("current price %s exceeded the upper price boundary %s", - currentPrice.String(), - s.UpperPrice.String()) + return nil, fmt.Errorf("current price %v exceeded the upper price boundary %v", + currentPrice, + s.UpperPrice) } balances := session.Account.Balances() @@ -230,13 +236,20 @@ func (s *Strategy) generateGridBuyOrders(session *bbgo.ExchangeSession) ([]types } if currentPrice.Compare(s.LowerPrice) < 0 { - return nil, fmt.Errorf("current price %s is lower than the lower price %s", currentPrice.String(), s.LowerPrice.String()) + return nil, fmt.Errorf("current price %v is lower than the lower price %v", + currentPrice, s.LowerPrice) } priceRange := s.UpperPrice.Sub(s.LowerPrice) numGrids := fixedpoint.NewFromInt(s.GridNum) gridSpread := priceRange.Div(numGrids) + if gridSpread.IsZero() { + return nil, fmt.Errorf( + "either numGrids(%v) is too big or priceRange(%v) is too small, "+ + "the differences of grid prices become zero", numGrids, priceRange) + } + // Find the nearest grid price for placing buy orders: // buyRange = currentPrice - lowerPrice // numOfBuyGrids = Floor(buyRange / gridSpread) @@ -250,7 +263,7 @@ func (s *Strategy) generateGridBuyOrders(session *bbgo.ExchangeSession) ([]types currentPrice.Sub(s.LowerPrice).Div(gridSpread).Trunc().Mul(gridSpread))) if startPrice.Compare(s.LowerPrice) < 0 { - return nil, fmt.Errorf("current price %s exceeded the lower price boundary %v", + return nil, fmt.Errorf("current price %v exceeded the lower price boundary %v", currentPrice, s.UpperPrice) } diff --git a/pkg/types/account.go b/pkg/types/account.go index a055ae789..e998d2941 100644 --- a/pkg/types/account.go +++ b/pkg/types/account.go @@ -316,7 +316,7 @@ func (a *Account) UseLockedBalance(currency string, fund fixedpoint.Value) error return fmt.Errorf("trying to use more than locked: locked %v < want to use %v", balance.Locked, fund) } -var QuantityDelta = fixedpoint.MustNewFromString("0.0000000000001") +var QuantityDelta = fixedpoint.MustNewFromString("0.00000000001") func (a *Account) UnlockBalance(currency string, unlocked fixedpoint.Value) error { a.Lock()