2020-09-05 08:22:46 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-09-05 08:22:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Interval string
|
|
|
|
|
|
|
|
var Interval1m = Interval("1m")
|
|
|
|
var Interval5m = Interval("5m")
|
|
|
|
var Interval1h = Interval("1h")
|
|
|
|
var Interval1d = Interval("1d")
|
|
|
|
|
2020-09-16 04:28:15 +00:00
|
|
|
type KLineCallback func(kline types.KLine)
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2020-09-16 04:28:15 +00:00
|
|
|
//go:generate callbackgen -type MarketDataStore
|
|
|
|
type MarketDataStore struct {
|
2020-09-08 06:56:08 +00:00
|
|
|
// KLineWindows stores all loaded klines per interval
|
|
|
|
KLineWindows map[Interval]types.KLineWindow `json:"-"`
|
2020-09-16 04:28:15 +00:00
|
|
|
|
|
|
|
updateCallbacks []KLineCallback
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 06:56:08 +00:00
|
|
|
func NewMarketDataStore() *MarketDataStore {
|
|
|
|
return &MarketDataStore{
|
|
|
|
// KLineWindows stores all loaded klines per interval
|
|
|
|
KLineWindows: make(map[Interval]types.KLineWindow),
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
func (store *MarketDataStore) BindStream(stream types.Stream) {
|
2020-09-05 08:22:46 +00:00
|
|
|
stream.OnKLineClosed(store.handleKLineClosed)
|
|
|
|
}
|
|
|
|
|
2020-09-16 04:28:15 +00:00
|
|
|
func (store *MarketDataStore) handleKLineClosed(kline types.KLine) {
|
|
|
|
store.AddKLine(kline)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 06:56:08 +00:00
|
|
|
func (store *MarketDataStore) AddKLine(kline types.KLine) {
|
2020-09-05 08:22:46 +00:00
|
|
|
var interval = 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-09-16 04:28:15 +00:00
|
|
|
store.EmitUpdate(kline)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|