mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
move PriceHeartBeat to types
This commit is contained in:
parent
8c5096dad6
commit
5755c44845
|
@ -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
|
||||
}
|
||||
|
|
26
pkg/types/price_volume_heartbeat.go
Normal file
26
pkg/types/price_volume_heartbeat.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user