bbgo_origin/pkg/store/kline_store.go

47 lines
1003 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 KLineStore
type KLineStore 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:"-"`
LastKLine types.KLine
2020-10-18 04:23:00 +00:00
updateCallbacks []func(kline types.KLine)
2020-09-05 08:22:46 +00:00
}
func NewMarketDataStore(symbol string) *KLineStore {
return &KLineStore{
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 *KLineStore) BindStream(stream types.Stream) {
2020-09-05 08:22:46 +00:00
stream.OnKLineClosed(store.handleKLineClosed)
}
func (store *KLineStore) 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 *KLineStore) 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.LastKLine = kline
store.EmitUpdate(kline)
2020-09-05 08:22:46 +00:00
}