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.
81 lines
2.3 KiB
81 lines
2.3 KiB
package sig |
|
|
|
import ( |
|
"fmt" |
|
"sig-pub/pkg/indicator" |
|
"sig-pub/pkg/types/series" |
|
"sig-pub/pkg/utils/collect" |
|
) |
|
|
|
// WindowIndicatorSeries 封装 |
|
type WindowIndicatorSeries struct { |
|
indicator.IIndicatorSeries |
|
indicator indicator.IIndicator |
|
indicatorContext IOffsetIndicatorContext |
|
} |
|
|
|
func NewWindowIndicatorSeries(indicator indicator.IIndicator, indicatorContext IOffsetIndicatorContext) *WindowIndicatorSeries { |
|
return &WindowIndicatorSeries{ |
|
indicator: indicator, |
|
indicatorContext: indicatorContext, |
|
} |
|
} |
|
|
|
func (s *WindowIndicatorSeries) CandlePeriods() int16 { |
|
return s.indicator.CandlePeriods(s.indicatorContext) |
|
} |
|
|
|
func (s *WindowIndicatorSeries) Get(offset int16) (vector float64) { |
|
// 根据当前相对offset |
|
s.indicatorContext.AddOffset(offset) |
|
vector = s.indicator.Calculate(s.indicatorContext) |
|
// 计算结束后还原 |
|
s.indicatorContext.AddOffset(-offset) |
|
return |
|
} |
|
|
|
// Series 返回指标值序列[时间降序] |
|
func (s *WindowIndicatorSeries) Series(offset, count int16) (matrix series.Floats) { |
|
// 设置当前相对offset |
|
s.indicatorContext.AddOffset(offset) |
|
for range count { |
|
vector := s.indicator.Calculate(s.indicatorContext) |
|
matrix.Push(vector) |
|
|
|
offset++ |
|
s.indicatorContext.AddOffset(1) |
|
} |
|
// 计算结束后还原 |
|
s.indicatorContext.AddOffset(-offset) |
|
return |
|
} |
|
|
|
// Summary指标 计算完毕获取计算结果 |
|
func (s *WindowIndicatorSeries) Summary(offset, count int16) (summary any, ok bool) { |
|
// summary, ok = s.indicator.Summary(s.indicatorContext) |
|
return |
|
} |
|
|
|
func (s *WindowIndicatorSeries) State(k string, offset int16) (state float64) { |
|
if collect.NotIn(k, s.indicator.Meta().State...) { |
|
panic(fmt.Errorf("indicator %s not export state %s", s.indicator.Meta().Name, k)) |
|
} |
|
_ = s.Get(offset) // 计算指标 |
|
state, ok := s.indicatorContext.State().Get(k, offset) |
|
if !ok { |
|
panic(fmt.Errorf("state %s offset %d not exists", k, offset)) |
|
} |
|
return |
|
} |
|
|
|
func (s *WindowIndicatorSeries) StateSeries(k string, offset, count int16) (matrix series.Floats) { |
|
if collect.NotIn(k, s.indicator.Meta().State...) { |
|
panic(fmt.Errorf("indicator %s not export state %s", s.indicator.Meta().Name, k)) |
|
} |
|
_ = s.Series(offset, count) // 计算指标 |
|
matrix, ok := s.indicatorContext.State().Series(k, offset, count) |
|
if !ok { |
|
panic(fmt.Errorf("state series %s error, offset %d, count %d", k, offset, count)) |
|
} |
|
return |
|
}
|
|
|