|
|
|
@ -5,6 +5,8 @@ import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"math" |
|
|
|
"math" |
|
|
|
"sig-pub/api/pb" |
|
|
|
"sig-pub/api/pb" |
|
|
|
|
|
|
|
"sig-pub/pkg/data" |
|
|
|
|
|
|
|
"sig-pub/pkg/mq" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
"sig-pub/pkg/utils/collect" |
|
|
|
"sig-pub/pkg/utils/collect" |
|
|
|
"sig-pub/pkg/utils/retry" |
|
|
|
"sig-pub/pkg/utils/retry" |
|
|
|
@ -17,24 +19,96 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type KlineStore struct { |
|
|
|
type KlineStore struct { |
|
|
|
exchangeClient pb.ExchangeServiceClient |
|
|
|
exchangeClient pb.ExchangeServiceClient |
|
|
|
store [3]*collect.ConcurrentMap[string, *KlineStoreInstance] // K线列表: []exchange<instId, interval, klines>
|
|
|
|
subscribeKlineIntervals []string |
|
|
|
|
|
|
|
store [3]*collect.ConcurrentMap[string, *KlineStoreInstance] // K线列表: []exchange<instId, interval, klines>
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewKlineSeriesStore() (kss *KlineStore) { |
|
|
|
func NewKlineSeriesStore(exchangeClient pb.ExchangeServiceClient) (kss *KlineStore) { |
|
|
|
kss = &KlineStore{} |
|
|
|
// 订阅实时k线周期列表
|
|
|
|
|
|
|
|
subscribeKlineIntervals := collect.Map2Slice(types.SupportedIntervals, func(interval types.Interval, _ types.IntervalAdder) string { |
|
|
|
|
|
|
|
return string(interval) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
kss = &KlineStore{ |
|
|
|
|
|
|
|
subscribeKlineIntervals: subscribeKlineIntervals, |
|
|
|
|
|
|
|
exchangeClient: exchangeClient, |
|
|
|
|
|
|
|
} |
|
|
|
kss.store[pb.ExchangeType_OKX] = collect.NewConcurrentMap[string, *KlineStoreInstance](64, func(s string) string { return s }) |
|
|
|
kss.store[pb.ExchangeType_OKX] = collect.NewConcurrentMap[string, *KlineStoreInstance](64, func(s string) string { return s }) |
|
|
|
// kss.klines[pb.ExchangeType_BINANCE] =
|
|
|
|
// kss.klines[pb.ExchangeType_BINANCE] =
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *KlineStore) Init() (err error) { |
|
|
|
|
|
|
|
// 拉取已初始化完成交易产品, 初始化k线, 开始订阅k线
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 订阅交易产品初始化完成事件
|
|
|
|
|
|
|
|
mq.NatsCreateConsumer("trading", mq.StreamExchange, mq.TopicExchangeTradeInstanceInited, func() *mq.PublishExchangeTradeInstanceInited { return new(mq.PublishExchangeTradeInstanceInited) }, |
|
|
|
|
|
|
|
func(msg *mq.PublishExchangeTradeInstanceInited) (err error) { |
|
|
|
|
|
|
|
// 初始化k线, 开始订阅k线
|
|
|
|
|
|
|
|
zlog.Infof("subscribed TopicExchangeTradeInstanceInited: %#v", msg) |
|
|
|
|
|
|
|
go s.subscribeKlines(msg.Exchange, msg.InstId) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *KlineStore) subscribeKlines(exchange pb.ExchangeType, instId string) { |
|
|
|
|
|
|
|
storeInst := s.store[exchange].ComputeIfAbsent(instId, func(k string) *KlineStoreInstance { |
|
|
|
|
|
|
|
return NewKlineStoreInstance(exchange, k) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化最新的 klineSeries
|
|
|
|
|
|
|
|
for _, interval := range s.subscribeKlineIntervals { |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
after, before := int64(0), int64(0) |
|
|
|
|
|
|
|
rsp, err := retry.DoWithFixDelay(math.MaxInt32, 2*time.Second, func(retryTimes uint32) (rsp *pb.RspHistoryKline, err error) { |
|
|
|
|
|
|
|
rsp, err = s.exchangeClient.HistoryKline(context.Background(), &pb.ReqHistoryKline{ |
|
|
|
|
|
|
|
Exchange: exchange, |
|
|
|
|
|
|
|
InstId: instId, |
|
|
|
|
|
|
|
Interval: string(interval), |
|
|
|
|
|
|
|
Count: MaxSeriesKlines, |
|
|
|
|
|
|
|
After: after, |
|
|
|
|
|
|
|
Before: before, |
|
|
|
|
|
|
|
Live: false, |
|
|
|
|
|
|
|
Asc: true, |
|
|
|
|
|
|
|
}, grpc.UseCompressor("snappy")) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
zlog.Errorf("fetch missing klines error: instId=%s(%s) after=%d before=%d, err=%v", instId, exchange, after, before, err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
zlog.Errorf("trade instance initial failed: %s(%s), %v", instId, exchange, err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for _, kline := range rsp.Klines { |
|
|
|
|
|
|
|
k := new(types.Kline) |
|
|
|
|
|
|
|
k.ParsePBKline(exchange, kline) |
|
|
|
|
|
|
|
_, _, err = storeInst.Update(k) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
zlog.Error("update initial kline series error: instId=%s(%s)", instId, exchange, err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.Update(exchange, instId, k) |
|
|
|
|
|
|
|
after = k.Ts |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 开始订阅k线
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Update
|
|
|
|
// Update
|
|
|
|
// kline klineStore -> klineSeries -> strategy -> indicator -> klineSeries.Series
|
|
|
|
// kline klineStore -> klineSeries -> strategy -> indicator -> klineSeries.Series
|
|
|
|
func (s *KlineStore) Update(exchange pb.ExchangeType, instId string, kline *types.Kline) { |
|
|
|
func (s *KlineStore) Update(exchange pb.ExchangeType, instId string, kline *types.Kline) { |
|
|
|
storeInst := s.store[exchange].ComputeIfAbsent(instId, func(k string) *KlineStoreInstance { |
|
|
|
storeInst := s.store[exchange].ComputeIfAbsent(instId, func(k string) *KlineStoreInstance { |
|
|
|
return NewKlineStoreInstance(exchange, k) |
|
|
|
return NewKlineStoreInstance(exchange, k) |
|
|
|
}) |
|
|
|
}) |
|
|
|
if storeInst.padding.Load() { |
|
|
|
|
|
|
|
|
|
|
|
// 只处理已初始化完成的交易产品k线
|
|
|
|
|
|
|
|
if storeInst.status.Load() != int32(data.StatusOk) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
before, serial, err := storeInst.Update(kline) |
|
|
|
before, serial, err := storeInst.Update(kline) |
|
|
|
@ -57,8 +131,8 @@ func (s *KlineStore) paddingMissKlines(storeInst *KlineStoreInstance, exchange p |
|
|
|
Exchange: exchange, |
|
|
|
Exchange: exchange, |
|
|
|
InstId: instId, |
|
|
|
InstId: instId, |
|
|
|
Interval: string(interval), |
|
|
|
Interval: string(interval), |
|
|
|
After: uint64(after), |
|
|
|
After: after, |
|
|
|
Before: uint64(before), |
|
|
|
Before: before, |
|
|
|
Live: false, |
|
|
|
Live: false, |
|
|
|
}, grpc.UseCompressor("snappy")) |
|
|
|
}, grpc.UseCompressor("snappy")) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
@ -88,8 +162,7 @@ type KlineStoreInstance struct { |
|
|
|
Exchange pb.ExchangeType |
|
|
|
Exchange pb.ExchangeType |
|
|
|
InstId string |
|
|
|
InstId string |
|
|
|
intervalKlines *types.IntervalState[*KlineSeries] |
|
|
|
intervalKlines *types.IntervalState[*KlineSeries] |
|
|
|
// tickK *types.Kline // 秒级k线
|
|
|
|
status atomic.Int32 // 交易产品状态
|
|
|
|
padding atomic.Bool // 是否正在拉取历史k线
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewKlineStoreInstance(exchange pb.ExchangeType, instId string) *KlineStoreInstance { |
|
|
|
func NewKlineStoreInstance(exchange pb.ExchangeType, instId string) *KlineStoreInstance { |
|
|
|
@ -98,6 +171,8 @@ func NewKlineStoreInstance(exchange pb.ExchangeType, instId string) *KlineStoreI |
|
|
|
InstId: instId, |
|
|
|
InstId: instId, |
|
|
|
intervalKlines: types.NewIntervalState[*KlineSeries](), |
|
|
|
intervalKlines: types.NewIntervalState[*KlineSeries](), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
si.status.Store(int32(data.StatusProcessing)) |
|
|
|
|
|
|
|
|
|
|
|
for interval := range types.SupportedIntervals { |
|
|
|
for interval := range types.SupportedIntervals { |
|
|
|
si.intervalKlines.Set(interval, NewKlineSeries(exchange, instId, interval)) |
|
|
|
si.intervalKlines.Set(interval, NewKlineSeries(exchange, instId, interval)) |
|
|
|
} |
|
|
|
} |
|
|
|
@ -118,7 +193,7 @@ func (si *KlineStoreInstance) Update(kline *types.Kline) (before int64, serial b |
|
|
|
ks := si.intervalKlines.Get(kline.Interval) |
|
|
|
ks := si.intervalKlines.Get(kline.Interval) |
|
|
|
before, serial = ks.Update(kline) |
|
|
|
before, serial = ks.Update(kline) |
|
|
|
if !serial { |
|
|
|
if !serial { |
|
|
|
si.padding.Store(true) |
|
|
|
// si.status.Store(int32(data.StatusProcessing))
|
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|