From 5755c44845fbaf791c9d41fafe8409bee3bd8038 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 12 Jan 2022 14:33:55 +0800 Subject: [PATCH] move PriceHeartBeat to types --- pkg/strategy/xmaker/strategy.go | 28 ++++------------------------ pkg/types/price_volume_heartbeat.go | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 pkg/types/price_volume_heartbeat.go diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 494df9a60..7b82cde06 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -20,29 +20,9 @@ import ( "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) -const priceNotUpdatingTimeout = 30 * time.Second +const priceUpdateTimeout = 30 * time.Second const ID = "xmaker" @@ -130,7 +110,7 @@ type Strategy struct { lastBidPrice, lastAskPrice fixedpoint.Value lastBidPriceTime, lastAskPriceTime time.Time - askPriceHeartBeat, bidPriceHeartBeat PriceHeartBeat + askPriceHeartBeat, bidPriceHeartBeat types.PriceHeartBeat lastPrice float64 groupID uint32 @@ -212,12 +192,12 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or // use mid-price for the last price 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) 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) return } diff --git a/pkg/types/price_volume_heartbeat.go b/pkg/types/price_volume_heartbeat.go new file mode 100644 index 000000000..6dda71604 --- /dev/null +++ b/pkg/types/price_volume_heartbeat.go @@ -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 +}