xmaker: truncate quantity when hedging

This commit is contained in:
c9s 2021-12-26 15:44:41 +08:00
parent 05a0745d08
commit 902e27ede4
2 changed files with 15 additions and 6 deletions

View File

@ -501,6 +501,7 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
} }
quantity := fixedpoint.Abs(pos) quantity := fixedpoint.Abs(pos)
quantity = s.sourceMarket.TruncateQuantity(quantity)
if pos < 0 { if pos < 0 {
side = types.SideTypeSell side = types.SideTypeSell

View File

@ -8,6 +8,8 @@ import (
"time" "time"
"github.com/leekchan/accounting" "github.com/leekchan/accounting"
"github.com/c9s/bbgo/pkg/fixedpoint"
) )
type Duration time.Duration type Duration time.Duration
@ -93,6 +95,12 @@ type Market struct {
TickSize float64 `json:"tickSize,omitempty"` TickSize float64 `json:"tickSize,omitempty"`
} }
// TruncateQuantity uses the step size to truncate floating number, in order to avoid the rounding issue
func (m Market) TruncateQuantity(quantity fixedpoint.Value) fixedpoint.Value {
stepRound := math.Pow10(-int(math.Log10(m.StepSize)))
return fixedpoint.NewFromFloat(math.Trunc(quantity.Float64()*stepRound) / stepRound)
}
func (m Market) BaseCurrencyFormatter() *accounting.Accounting { func (m Market) BaseCurrencyFormatter() *accounting.Accounting {
a := accounting.DefaultAccounting(m.BaseCurrency, m.VolumePrecision) a := accounting.DefaultAccounting(m.BaseCurrency, m.VolumePrecision)
a.Format = "%v %s" a.Format = "%v %s"