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

View File

@ -14,7 +14,8 @@ type RBTOrderBook struct {
Symbol string
Bids *RBTree
Asks *RBTree
LastUpdateTime time.Time
lastUpdateTime time.Time
loadCallbacks []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) {
right := b.Bids.Rightmost()
if right != nil {
@ -118,14 +123,14 @@ func (b *RBTOrderBook) updateBids(pvs PriceVolumeSlice) {
func (b *RBTOrderBook) update(book SliceOrderBook) {
b.updateBids(book.Bids)
b.updateAsks(book.Asks)
b.LastUpdateTime = time.Now()
b.lastUpdateTime = time.Now()
}
func (b *RBTOrderBook) load(book SliceOrderBook) {
b.Reset()
b.updateBids(book.Bids)
b.updateAsks(book.Asks)
b.LastUpdateTime = time.Now()
b.lastUpdateTime = time.Now()
}
func (b *RBTOrderBook) Copy() OrderBook {

View File

@ -3,9 +3,11 @@ package types
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
)
// SliceOrderBook is a general order book structure which could be used
@ -16,6 +18,8 @@ type SliceOrderBook struct {
Bids PriceVolumeSlice
Asks PriceVolumeSlice
lastUpdateTime time.Time
loadCallbacks []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) {
bestBid, ok := b.BestBid()
if !ok {
@ -125,6 +133,7 @@ func (b *SliceOrderBook) updateBids(pvs PriceVolumeSlice) {
func (b *SliceOrderBook) update(book SliceOrderBook) {
b.updateBids(book.Bids)
b.updateAsks(book.Asks)
b.lastUpdateTime = time.Now()
}
func (b *SliceOrderBook) Reset() {