You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

62 lines
1.3 KiB

package trading
import (
"sig-pub/api/pb"
"sig-pub/pkg/types"
"sig-pub/pkg/types/series"
)
type KlineSeries struct {
Exchange pb.ExchangeType
InstId string
Interval types.Interval
Ts int64
klines []*types.Kline
klineStore *KlineStore
}
func NewKlineSeries(ts int64, interval types.Interval, klineStore *KlineStore) *KlineSeries {
return &KlineSeries{
Ts: ts,
Interval: interval,
klineStore: klineStore,
}
}
// Get
// [0]当前k线
func (a KlineSeries) Get(start int16) types.Kline {
index := len(a.klines) - 1 - int(start)
if index >= 0 && index < len(a.klines)-1 {
return *(a.klines[index])
}
// todo query store
ts := a.Interval.MustAddMul(a.Ts, int64(-start))
for _, k := range a.klines {
if k.Ts == ts {
return *k
}
}
panic("kline not exists")
}
// Series [start...end]
func (a KlineSeries) Series(start, end int16) (klines series.Klines) {
endTs := a.Interval.MustAddMul(a.Ts, int64(-start))
startTs := a.Interval.MustAddMul(a.Ts, int64(-end))
_ = endTs
_ = startTs
// return a.klineStore.GetRange(startTs, endTs)
// todo
return
}
// 检查k线序列完整
func (s *KlineSeries) Update(kline *types.Kline) []types.Kline {
s.klines = append(s.klines, kline)
// todo copy(s.klines, s.klines[0:1]) set index=20, ts=kline.ts
s.Ts = kline.Ts
return nil
}