2020-10-28 08:27:25 +00:00
|
|
|
package bbgo
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2020-10-28 08:27:25 +00:00
|
|
|
import "github.com/c9s/bbgo/pkg/types"
|
2020-09-05 08:22:46 +00:00
|
|
|
|
2021-06-28 06:31:22 +00:00
|
|
|
const MaxNumOfKLines = 5_000
|
2021-11-21 14:18:07 +00:00
|
|
|
const MaxNumOfKLinesTruncate = 100
|
2021-06-28 06:31:22 +00:00
|
|
|
|
2024-09-03 07:34:55 +00:00
|
|
|
const CapacityOfKLineWindowLimit = 5_000
|
|
|
|
|
2022-07-20 17:04:49 +00:00
|
|
|
// MarketDataStore receives and maintain the public market data of a single symbol
|
2020-10-18 12:24:16 +00:00
|
|
|
//go:generate callbackgen -type MarketDataStore
|
|
|
|
type MarketDataStore 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
|
2022-04-08 09:48:33 +00:00
|
|
|
KLineWindows map[types.Interval]*types.KLineWindow `json:"-"`
|
2020-09-16 04:28:15 +00:00
|
|
|
|
2022-04-08 09:48:33 +00:00
|
|
|
kLineWindowUpdateCallbacks []func(interval types.Interval, klines types.KLineWindow)
|
2022-07-20 17:04:49 +00:00
|
|
|
kLineClosedCallbacks []func(k types.KLine)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func NewMarketDataStore(symbol string) *MarketDataStore {
|
|
|
|
return &MarketDataStore{
|
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
|
2022-10-19 04:17:44 +00:00
|
|
|
KLineWindows: make(map[types.Interval]*types.KLineWindow, len(types.SupportedIntervals)), // 13 interval, 1s,1m,5m,15m,30m,1h,2h,4h,6h,12h,1d,3d,1w
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 09:48:33 +00:00
|
|
|
func (store *MarketDataStore) SetKLineWindows(windows map[types.Interval]*types.KLineWindow) {
|
2020-10-19 13:58:50 +00:00
|
|
|
store.KLineWindows = windows
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:43:37 +00:00
|
|
|
// KLinesOfInterval returns the kline window of the given interval
|
2022-04-08 09:48:33 +00:00
|
|
|
func (store *MarketDataStore) KLinesOfInterval(interval types.Interval) (kLines *types.KLineWindow, ok bool) {
|
2020-10-18 12:43:37 +00:00
|
|
|
kLines, ok = store.KLineWindows[interval]
|
|
|
|
return kLines, ok
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func (store *MarketDataStore) BindStream(stream types.Stream) {
|
2020-09-05 08:22:46 +00:00
|
|
|
stream.OnKLineClosed(store.handleKLineClosed)
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:24:16 +00:00
|
|
|
func (store *MarketDataStore) handleKLineClosed(kline types.KLine) {
|
2020-10-18 12:41:17 +00:00
|
|
|
if kline.Symbol != store.Symbol {
|
|
|
|
return
|
2020-10-17 16:06:08 +00:00
|
|
|
}
|
2020-10-18 12:41:17 +00:00
|
|
|
|
|
|
|
store.AddKLine(kline)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 17:04:49 +00:00
|
|
|
func (store *MarketDataStore) AddKLine(k types.KLine) {
|
|
|
|
window, ok := store.KLineWindows[k.Interval]
|
2020-10-28 09:44:37 +00:00
|
|
|
if !ok {
|
2022-04-08 09:48:33 +00:00
|
|
|
var tmp = make(types.KLineWindow, 0, 1000)
|
2022-07-20 17:04:49 +00:00
|
|
|
store.KLineWindows[k.Interval] = &tmp
|
2022-04-08 09:48:33 +00:00
|
|
|
window = &tmp
|
2020-10-28 09:44:37 +00:00
|
|
|
}
|
2022-07-20 17:04:49 +00:00
|
|
|
window.Add(k)
|
2021-06-28 06:31:22 +00:00
|
|
|
|
2024-09-03 07:34:55 +00:00
|
|
|
truncateKLineWindowIfNeeded(window)
|
2021-06-28 06:31:22 +00:00
|
|
|
|
2022-07-20 17:04:49 +00:00
|
|
|
store.EmitKLineClosed(k)
|
|
|
|
store.EmitKLineWindowUpdate(k.Interval, *window)
|
2020-09-05 08:22:46 +00:00
|
|
|
}
|
2024-09-03 07:34:55 +00:00
|
|
|
|
|
|
|
func truncateKLineWindowIfNeeded(window *types.KLineWindow) {
|
|
|
|
lenOfWindow := len(*window)
|
|
|
|
capOfWindow := cap(*window)
|
|
|
|
|
|
|
|
if lenOfWindow == capOfWindow && capOfWindow > CapacityOfKLineWindowLimit {
|
|
|
|
size := CapacityOfKLineWindowLimit / 2
|
|
|
|
start := lenOfWindow - size
|
|
|
|
copy(*window, (*window)[start:])
|
|
|
|
*window = (*window)[:size]
|
|
|
|
}
|
|
|
|
}
|