move PriceHeartBeat to types

This commit is contained in:
c9s 2022-01-12 14:33:55 +08:00
parent 8c5096dad6
commit 5755c44845
2 changed files with 30 additions and 24 deletions

View File

@ -20,29 +20,9 @@ import (
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
) )
type PriceHeartBeat struct {
PriceVolume types.PriceVolume
LastTime time.Time
}
func (b *PriceHeartBeat) Update(pv types.PriceVolume) (bool, error) {
if b.PriceVolume.Price == 0 || b.PriceVolume != pv {
b.PriceVolume = pv
b.LastTime = time.Now()
return true, nil // successfully updated
} else if time.Since(b.LastTime) > priceNotUpdatingTimeout {
return false, fmt.Errorf("price %s has not been updating for %s, last update: %s, skip quoting",
b.PriceVolume.String(),
priceNotUpdatingTimeout,
b.LastTime)
}
return false, nil
}
var defaultMargin = fixedpoint.NewFromFloat(0.003) var defaultMargin = fixedpoint.NewFromFloat(0.003)
const priceNotUpdatingTimeout = 30 * time.Second const priceUpdateTimeout = 30 * time.Second
const ID = "xmaker" const ID = "xmaker"
@ -130,7 +110,7 @@ type Strategy struct {
lastBidPrice, lastAskPrice fixedpoint.Value lastBidPrice, lastAskPrice fixedpoint.Value
lastBidPriceTime, lastAskPriceTime time.Time lastBidPriceTime, lastAskPriceTime time.Time
askPriceHeartBeat, bidPriceHeartBeat PriceHeartBeat askPriceHeartBeat, bidPriceHeartBeat types.PriceHeartBeat
lastPrice float64 lastPrice float64
groupID uint32 groupID uint32
@ -212,12 +192,12 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
// use mid-price for the last price // use mid-price for the last price
s.lastPrice = (bestBid.Price + bestAsk.Price).Float64() / 2 s.lastPrice = (bestBid.Price + bestAsk.Price).Float64() / 2
if _, err := s.bidPriceHeartBeat.Update(bestBid) ; err != nil { if _, err := s.bidPriceHeartBeat.Update(bestBid, priceUpdateTimeout) ; err != nil {
log.WithError(err).Errorf("quote update error, %s price not updating", s.Symbol) log.WithError(err).Errorf("quote update error, %s price not updating", s.Symbol)
return return
} }
if _, err := s.askPriceHeartBeat.Update(bestAsk) ; err != nil { if _, err := s.askPriceHeartBeat.Update(bestAsk, priceUpdateTimeout) ; err != nil {
log.WithError(err).Errorf("quote update error, %s price not updating", s.Symbol) log.WithError(err).Errorf("quote update error, %s price not updating", s.Symbol)
return return
} }

View File

@ -0,0 +1,26 @@
package types
import (
"fmt"
"time"
)
type PriceHeartBeat struct {
PriceVolume PriceVolume
LastTime time.Time
}
func (b *PriceHeartBeat) Update(pv PriceVolume, timeout time.Duration) (bool, error) {
if b.PriceVolume.Price == 0 || b.PriceVolume != pv {
b.PriceVolume = pv
b.LastTime = time.Now()
return true, nil // successfully updated
} else if time.Since(b.LastTime) > timeout {
return false, fmt.Errorf("price %s has not been updating for %s, last update: %s, skip quoting",
b.PriceVolume.String(),
time.Since(b.LastTime),
b.LastTime)
}
return false, nil
}