grid2: add build tag for backtest_test

This commit is contained in:
c9s 2022-12-06 15:55:52 +08:00
parent 482b6f5e7b
commit 423fe521b6
2 changed files with 29 additions and 4 deletions

View File

@ -1,3 +1,5 @@
//go:build !dnum
package grid2 package grid2
import ( import (

View File

@ -128,6 +128,10 @@ func (s *Strategy) Validate() error {
return fmt.Errorf("upperPrice (%s) should not be less than or equal to lowerPrice (%s)", s.UpperPrice.String(), s.LowerPrice.String()) return fmt.Errorf("upperPrice (%s) should not be less than or equal to lowerPrice (%s)", s.UpperPrice.String(), s.LowerPrice.String())
} }
if s.GridNum == 0 {
return fmt.Errorf("gridNum can not be zero")
}
if s.FeeRate.IsZero() { if s.FeeRate.IsZero() {
s.FeeRate = fixedpoint.NewFromFloat(0.1 * 0.01) // 0.1%, 0.075% with BNB s.FeeRate = fixedpoint.NewFromFloat(0.1 * 0.01) // 0.1%, 0.075% with BNB
} }
@ -145,10 +149,6 @@ func (s *Strategy) Validate() error {
} }
} }
if s.GridNum == 0 {
return fmt.Errorf("gridNum can not be zero")
}
if err := s.QuantityOrAmount.Validate(); err != nil { if err := s.QuantityOrAmount.Validate(); err != nil {
if s.QuoteInvestment.IsZero() && s.BaseInvestment.IsZero() { if s.QuoteInvestment.IsZero() && s.BaseInvestment.IsZero() {
return err return err
@ -936,6 +936,22 @@ func (s *Strategy) getLastTradePrice(ctx context.Context, session *bbgo.Exchange
return fixedpoint.Zero, fmt.Errorf("%s ticker price not found", s.Symbol) return fixedpoint.Zero, fmt.Errorf("%s ticker price not found", s.Symbol)
} }
func (s *Strategy) checkMinimalQuoteInvestment() error {
gridNum := fixedpoint.NewFromInt(s.GridNum)
// gridSpread := s.UpperPrice.Sub(s.LowerPrice).Div(gridNum)
minimalAmountLowerPrice := fixedpoint.Max(s.LowerPrice.Mul(s.Market.MinQuantity), s.Market.MinNotional)
minimalAmountUpperPrice := fixedpoint.Max(s.UpperPrice.Mul(s.Market.MinQuantity), s.Market.MinNotional)
minimalQuoteInvestment := fixedpoint.Max(minimalAmountLowerPrice, minimalAmountUpperPrice).Mul(gridNum)
if s.QuoteInvestment.Compare(minimalQuoteInvestment) <= 0 {
return fmt.Errorf("need at least %f %s for quote investment, %f %s given",
minimalQuoteInvestment.Float64(),
s.Market.QuoteCurrency,
s.QuoteInvestment.Float64(),
s.Market.QuoteCurrency)
}
return nil
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
instanceID := s.InstanceID() instanceID := s.InstanceID()
@ -972,6 +988,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.Position.Reset() s.Position.Reset()
} }
// we need to check the minimal quote investment here, because we need the market info
if s.QuoteInvestment.Sign() > 0 {
if err := s.checkMinimalQuoteInvestment(); err != nil {
return err
}
}
s.historicalTrades = bbgo.NewTradeStore() s.historicalTrades = bbgo.NewTradeStore()
s.historicalTrades.EnablePrune = true s.historicalTrades.EnablePrune = true
s.historicalTrades.BindStream(session.UserDataStream) s.historicalTrades.BindStream(session.UserDataStream)