types: add lastUpdateTime field

This commit is contained in:
c9s 2022-01-12 22:07:52 +08:00
parent 915f2c7476
commit 2aeb9e870c
3 changed files with 20 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"os" "os"
"strconv" "strconv"
"sync" "sync"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/sigchan" "github.com/c9s/bbgo/pkg/sigchan"
@ -13,6 +14,7 @@ type OrderBook interface {
Spread() (fixedpoint.Value, bool) Spread() (fixedpoint.Value, bool)
BestAsk() (PriceVolume, bool) BestAsk() (PriceVolume, bool)
BestBid() (PriceVolume, bool) BestBid() (PriceVolume, bool)
LastUpdateTime() time.Time
Reset() Reset()
Load(book SliceOrderBook) Load(book SliceOrderBook)
Update(book SliceOrderBook) Update(book SliceOrderBook)

View File

@ -14,7 +14,8 @@ type RBTOrderBook struct {
Symbol string Symbol string
Bids *RBTree Bids *RBTree
Asks *RBTree Asks *RBTree
LastUpdateTime time.Time
lastUpdateTime time.Time
loadCallbacks []func(book *RBTOrderBook) loadCallbacks []func(book *RBTOrderBook)
updateCallbacks []func(book *RBTOrderBook) updateCallbacks []func(book *RBTOrderBook)
@ -28,6 +29,10 @@ func NewRBOrderBook(symbol string) *RBTOrderBook {
} }
} }
func (b *RBTOrderBook) LastUpdateTime() time.Time {
return b.lastUpdateTime
}
func (b *RBTOrderBook) BestBid() (PriceVolume, bool) { func (b *RBTOrderBook) BestBid() (PriceVolume, bool) {
right := b.Bids.Rightmost() right := b.Bids.Rightmost()
if right != nil { if right != nil {
@ -118,14 +123,14 @@ func (b *RBTOrderBook) updateBids(pvs PriceVolumeSlice) {
func (b *RBTOrderBook) update(book SliceOrderBook) { func (b *RBTOrderBook) update(book SliceOrderBook) {
b.updateBids(book.Bids) b.updateBids(book.Bids)
b.updateAsks(book.Asks) b.updateAsks(book.Asks)
b.LastUpdateTime = time.Now() b.lastUpdateTime = time.Now()
} }
func (b *RBTOrderBook) load(book SliceOrderBook) { func (b *RBTOrderBook) load(book SliceOrderBook) {
b.Reset() b.Reset()
b.updateBids(book.Bids) b.updateBids(book.Bids)
b.updateAsks(book.Asks) b.updateAsks(book.Asks)
b.LastUpdateTime = time.Now() b.lastUpdateTime = time.Now()
} }
func (b *RBTOrderBook) Copy() OrderBook { func (b *RBTOrderBook) Copy() OrderBook {

View File

@ -3,9 +3,11 @@ package types
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
) )
// SliceOrderBook is a general order book structure which could be used // SliceOrderBook is a general order book structure which could be used
@ -16,6 +18,8 @@ type SliceOrderBook struct {
Bids PriceVolumeSlice Bids PriceVolumeSlice
Asks PriceVolumeSlice Asks PriceVolumeSlice
lastUpdateTime time.Time
loadCallbacks []func(book *SliceOrderBook) loadCallbacks []func(book *SliceOrderBook)
updateCallbacks []func(book *SliceOrderBook) updateCallbacks []func(book *SliceOrderBook)
} }
@ -26,6 +30,10 @@ func NewSliceOrderBook(symbol string) *SliceOrderBook {
} }
} }
func (b *SliceOrderBook) LastUpdateTime() time.Time {
return b.lastUpdateTime
}
func (b *SliceOrderBook) Spread() (fixedpoint.Value, bool) { func (b *SliceOrderBook) Spread() (fixedpoint.Value, bool) {
bestBid, ok := b.BestBid() bestBid, ok := b.BestBid()
if !ok { if !ok {
@ -125,6 +133,7 @@ func (b *SliceOrderBook) updateBids(pvs PriceVolumeSlice) {
func (b *SliceOrderBook) update(book SliceOrderBook) { func (b *SliceOrderBook) update(book SliceOrderBook) {
b.updateBids(book.Bids) b.updateBids(book.Bids)
b.updateAsks(book.Asks) b.updateAsks(book.Asks)
b.lastUpdateTime = time.Now()
} }
func (b *SliceOrderBook) Reset() { func (b *SliceOrderBook) Reset() {