bbgo_origin/bbgo/store.go

46 lines
1.0 KiB
Go
Raw Normal View History

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")
type KLineCallback func(kline types.KLine)
2020-09-05 08:22:46 +00:00
//go:generate callbackgen -type MarketDataStore
type MarketDataStore struct {
// KLineWindows stores all loaded klines per interval
KLineWindows map[Interval]types.KLineWindow `json:"-"`
updateCallbacks []KLineCallback
2020-09-05 08:22:46 +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)
}
func (store *MarketDataStore) handleKLineClosed(kline types.KLine) {
store.AddKLine(kline)
2020-09-05 08:22:46 +00:00
}
func (store *MarketDataStore) AddKLine(kline types.KLine) {
2020-09-05 08:22:46 +00:00
var interval = 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
}