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 04:32:43 +00:00
|
|
|
//go:generate callbackgen -type KLineStore
|
|
|
|
type KLineStore 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 04:23:00 +00:00
|
|
|
updateCallbacks []func(kline types.KLine)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 04:32:43 +00:00
|
|
|
func NewMarketDataStore(symbol string) *KLineStore {
|
|
|
|
return &KLineStore{
|
2020-10-17 16:06:08 +00:00
|
|
|
Symbol: symbol,
|
|
|
|
|
2020-09-08 06:56:08 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 04:32:43 +00:00
|
|
|
func (store *KLineStore) BindStream(stream types.Stream) {
|
2020-09-05 08:22:46 +00:00
|
|
|
stream.OnKLineClosed(store.handleKLineClosed)
|
|
|
|
}
|
|
|
|
|
2020-10-18 04:32:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-18 04:32:43 +00:00
|
|
|
func (store *KLineStore) 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-09-16 04:28:15 +00:00
|
|
|
store.EmitUpdate(kline)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|