bbgo_origin/pkg/store/marketdata.go

43 lines
988 B
Go
Raw Normal View History

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
)
//go:generate callbackgen -type MarketDataStore
type MarketDataStore struct {
2020-10-17 16:06:08 +00:00
Symbol string
// KLineWindows stores all loaded klines per interval
2020-10-18 04:23:00 +00:00
KLineWindows map[types.Interval]types.KLineWindow `json:"-"`
2020-10-18 04:23:00 +00:00
updateCallbacks []func(kline types.KLine)
2020-09-05 08:22:46 +00:00
}
2020-10-17 16:06:08 +00:00
func NewMarketDataStore(symbol string) *MarketDataStore {
return &MarketDataStore{
2020-10-17 16:06:08 +00:00
Symbol: symbol,
// KLineWindows stores all loaded klines per interval
2020-10-18 04:23:00 +00:00
KLineWindows: make(map[types.Interval]types.KLineWindow),
2020-09-05 08:22:46 +00:00
}
}
func (store *MarketDataStore) BindStream(stream types.Stream) {
2020-09-05 08:22:46 +00:00
stream.OnKLineClosed(store.handleKLineClosed)
}
func (store *MarketDataStore) handleKLineClosed(kline types.KLine) {
2020-10-17 16:06:08 +00:00
if kline.Symbol == store.Symbol {
store.AddKLine(kline)
}
2020-09-05 08:22:46 +00:00
}
func (store *MarketDataStore) AddKLine(kline types.KLine) {
2020-10-18 04:23:00 +00:00
var interval = types.Interval(kline.Interval)
var window = store.KLineWindows[interval]
2020-09-05 08:22:46 +00:00
window.Add(kline)
store.EmitUpdate(kline)
2020-09-05 08:22:46 +00:00
}