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.
47 lines
1.1 KiB
47 lines
1.1 KiB
package sig |
|
|
|
import ( |
|
"sig-pub/pkg/indicator" |
|
"sig-pub/pkg/types" |
|
"sig-pub/pkg/types/series" |
|
) |
|
|
|
// IndicatorState |
|
// ema, obv 指标递归计算时的状态存储 |
|
type IndicatorState struct { |
|
indicator.IIndicatorState |
|
kSeries *KlineSeries |
|
state map[string]*types.RingSeries[float64] |
|
} |
|
|
|
func NewIndicatorState(kSeries *KlineSeries) *IndicatorState { |
|
return &IndicatorState{ |
|
kSeries: kSeries, |
|
state: make(map[string]*types.RingSeries[float64]), |
|
} |
|
} |
|
|
|
func (s *IndicatorState) ring(k string) *types.RingSeries[float64] { |
|
ring, ok := s.state[k] |
|
if !ok { |
|
ring = types.NewRingSeries[float64](indicator.MaxWindow, 8) |
|
s.state[k] = ring |
|
// 从 kSeries0 开始 calc ind 初始化 |
|
// 递归初始值 |
|
} |
|
return ring |
|
} |
|
|
|
func (s *IndicatorState) Set(k string, v float64) { |
|
s.ring(k).Push(v) |
|
} |
|
|
|
func (s *IndicatorState) Get(k string, offset int16) (v float64, ok bool) { |
|
offset -= 1 |
|
return s.ring(k).Get(int(offset)) |
|
} |
|
|
|
func (s *IndicatorState) Series(k string, offset, count int16) (v series.Floats, ok bool) { |
|
offset -= 1 |
|
return s.ring(k).Series(int(offset), int(count)) |
|
}
|
|
|