2020-10-18 04:23:00 +00:00
|
|
|
package store
|
2020-09-05 08:22:46 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-09-05 08:22:46 +00:00
|
|
|
)
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
//go:generate callbackgen -type MarketDataStore
|
|
|
|
type MarketDataStore struct {
|
2020-10-17 16:06:08 +00:00
|
|
|
Symbol string
|
|
|
|
|
2020-09-08 06:56:08 +00:00
|
|
|
// KLineWindows stores all loaded klines per interval
|
2020-10-18 04:23:00 +00:00
|
|
|
KLineWindows map[types.Interval]types.KLineWindow `json:"-"`
|
2020-09-16 04:28:15 +00:00
|
|
|
|
2020-10-18 04:32:43 +00:00
|
|
|
LastKLine types.KLine
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
kLineUpdateCallbacks []func(kline types.KLine)
|
2020-10-18 12:41:17 +00:00
|
|
|
|
|
|
|
orderBook *types.StreamOrderBook
|
|
|
|
|
2020-10-19 03:44:15 +00:00
|
|
|
orderBookUpdateCallbacks []func(orderBook *types.StreamOrderBook)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func NewMarketDataStore(symbol string) *MarketDataStore {
|
|
|
|
return &MarketDataStore{
|
2020-10-17 16:06:08 +00:00
|
|
|
Symbol: symbol,
|
|
|
|
|
2020-10-18 12:41:17 +00:00
|
|
|
orderBook: types.NewStreamBook(symbol),
|
|
|
|
|
2020-09-08 06:56:08 +00:00
|
|
|
// KLineWindows stores all loaded klines per interval
|
2020-10-18 12:41:17 +00:00
|
|
|
KLineWindows: make(map[types.Interval]types.KLineWindow, len(types.SupportedIntervals)), // 12 interval, 1m,5m,15m,30m,1h,2h,4h,6h,12h,1d,3d,1w
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 03:44:15 +00:00
|
|
|
func (store *MarketDataStore) OrderBook() types.OrderBook {
|
|
|
|
return store.orderBook.Copy()
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:43:37 +00:00
|
|
|
// KLinesOfInterval returns the kline window of the given interval
|
|
|
|
func (store *MarketDataStore) KLinesOfInterval(interval types.Interval) (kLines types.KLineWindow, ok bool) {
|
|
|
|
kLines, ok = store.KLineWindows[interval]
|
|
|
|
return kLines, ok
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:41:17 +00:00
|
|
|
func (store *MarketDataStore) handleOrderBookUpdate(book types.OrderBook) {
|
|
|
|
if book.Symbol != store.Symbol {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
store.orderBook.Update(book)
|
2020-10-19 03:44:15 +00:00
|
|
|
|
|
|
|
store.EmitOrderBookUpdate(store.orderBook)
|
2020-10-18 12:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MarketDataStore) handleOrderBookSnapshot(book types.OrderBook) {
|
|
|
|
if book.Symbol != store.Symbol {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
store.orderBook.Load(book)
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func (store *MarketDataStore) BindStream(stream types.Stream) {
|
2020-09-05 08:22:46 +00:00
|
|
|
stream.OnKLineClosed(store.handleKLineClosed)
|
2020-10-18 12:41:17 +00:00
|
|
|
stream.OnBookSnapshot(store.handleOrderBookSnapshot)
|
|
|
|
stream.OnBookUpdate(store.handleOrderBookUpdate)
|
|
|
|
|
|
|
|
store.orderBook.BindStream(stream)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func (store *MarketDataStore) handleKLineClosed(kline types.KLine) {
|
2020-10-18 12:41:17 +00:00
|
|
|
if kline.Symbol != store.Symbol {
|
|
|
|
return
|
2020-10-17 16:06:08 +00:00
|
|
|
}
|
2020-10-18 12:41:17 +00:00
|
|
|
|
|
|
|
store.AddKLine(kline)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func (store *MarketDataStore) AddKLine(kline types.KLine) {
|
2020-10-18 04:23:00 +00:00
|
|
|
var interval = types.Interval(kline.Interval)
|
2020-09-08 06:56:08 +00:00
|
|
|
var window = store.KLineWindows[interval]
|
2020-09-05 08:22:46 +00:00
|
|
|
window.Add(kline)
|
|
|
|
|
2020-10-18 04:32:43 +00:00
|
|
|
store.LastKLine = kline
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
store.EmitKLineUpdate(kline)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|