2020-09-05 08:22:46 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Interval string
|
|
|
|
|
|
|
|
var Interval1m = Interval("1m")
|
|
|
|
var Interval5m = Interval("5m")
|
|
|
|
var Interval1h = Interval("1h")
|
|
|
|
var Interval1d = Interval("1d")
|
|
|
|
|
|
|
|
type KLineStore struct {
|
2020-09-07 06:20:03 +00:00
|
|
|
// MaxChanges stores the max change kline per interval
|
|
|
|
MaxChanges map[Interval]types.KLine `json:"-"`
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
// Windows stores all loaded klines per interval
|
|
|
|
Windows map[Interval]types.KLineWindow `json:"-"`
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewKLineStore() *KLineStore {
|
|
|
|
return &KLineStore{
|
2020-09-07 06:20:03 +00:00
|
|
|
MaxChanges: make(map[Interval]types.KLine),
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
// Windows stores all loaded klines per interval
|
|
|
|
Windows: make(map[Interval]types.KLineWindow),
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *KLineStore) BindPrivateStream(stream *types.StandardPrivateStream) {
|
|
|
|
stream.OnKLineClosed(store.handleKLineClosed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *KLineStore) handleKLineClosed(kline *types.KLine) {
|
|
|
|
store.AddKLine(*kline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *KLineStore) AddKLine(kline types.KLine) {
|
|
|
|
var interval = Interval(kline.Interval)
|
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
var window = store.Windows[interval]
|
2020-09-05 08:22:46 +00:00
|
|
|
window.Add(kline)
|
|
|
|
|
2020-09-07 06:20:03 +00:00
|
|
|
if kline.GetMaxChange() > store.MaxChanges[interval].GetMaxChange() {
|
|
|
|
store.MaxChanges[interval] = kline
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
}
|