package sig import ( "sig-pub/pkg/indicator" "sig-pub/pkg/types" "sig-pub/pkg/types/series" "sig-pub/pkg/zlog" ) type IOffsetIndicatorContext interface { indicator.IIndicatorContext SetOffset(offset int16) AddOffset(offset int16) GetOffset() (offset int16) } // IndicatorContext 指标上下文, 提供k线序列给指标计算使用 type IndicatorContext struct { IOffsetIndicatorContext input types.Input kSeries *KlineSeries offset int16 } func NewIndicatorContext(input types.Input, kSeries *KlineSeries) *IndicatorContext { return &IndicatorContext{ input: input, kSeries: kSeries, } } func (c *IndicatorContext) SetOffset(offset int16) { c.offset = offset } func (c *IndicatorContext) AddOffset(offset int16) { c.offset += offset } func (c *IndicatorContext) GetOffset() (offset int16) { return c.offset } func (c *IndicatorContext) Input() (in types.Input) { return c.input } func (c *IndicatorContext) Get(offset int16) (kline types.Kline) { offset += c.offset k, err := c.kSeries.Get(offset) if err != nil { lastTs := c.kSeries.LastTs() zlog.Warningf("get kline series offset out of range: offset=%d, length=%d, lastTs=%d", offset, c.kSeries.Length(), lastTs) panic(err) } return k } func (c *IndicatorContext) Series(offset, count int16) (klines series.Klines) { offset += c.offset ks, err := c.kSeries.Series(offset, count) if err != nil { zlog.Warningf("get kline series offset out of range: offset=%d, count=%d, length=%d, lastTs=%d", offset, count, c.kSeries.Length(), c.kSeries.LastTs()) panic(err) } return ks }