2020-09-05 08:22:46 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
2020-10-05 06:25:58 +00:00
|
|
|
"github.com/c9s/bbgo/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-03 12:09:22 +00:00
|
|
|
func (store *MarketDataStore) BindPrivateStream(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
|
|
|
}
|