|
|
|
@ -11,16 +11,18 @@ import ( |
|
|
|
type StrategyContext struct { |
|
|
|
type StrategyContext struct { |
|
|
|
strategy.ISingleSigStrategyContext |
|
|
|
strategy.ISingleSigStrategyContext |
|
|
|
|
|
|
|
|
|
|
|
input types.Input |
|
|
|
input types.Input |
|
|
|
kSeries *KlineSeries |
|
|
|
kSeries *KlineSeries |
|
|
|
indicatorsReg *indicator.IndicatorRegistry |
|
|
|
indicatorsReg *indicator.IndicatorRegistry |
|
|
|
|
|
|
|
indicatorContextStates IndicatorStates |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewStrategyContext(input types.Input, kSeries *KlineSeries, indicatorsReg *indicator.IndicatorRegistry) *StrategyContext { |
|
|
|
func NewStrategyContext(input types.Input, kSeries *KlineSeries, indicatorsReg *indicator.IndicatorRegistry) *StrategyContext { |
|
|
|
return &StrategyContext{ |
|
|
|
return &StrategyContext{ |
|
|
|
input: input, |
|
|
|
input: input, |
|
|
|
kSeries: kSeries, |
|
|
|
kSeries: kSeries, |
|
|
|
indicatorsReg: indicatorsReg, |
|
|
|
indicatorsReg: indicatorsReg, |
|
|
|
|
|
|
|
indicatorContextStates: NewIndicatorStates(), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -38,35 +40,32 @@ func (c *StrategyContext) Series(offset, count int16) (klines series.Klines) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取窗口类型指标
|
|
|
|
// 获取窗口类型指标
|
|
|
|
func (c *StrategyContext) IndicatorW(name string, window int16, args ...any) (s indicator.IIndicatorSeries) { |
|
|
|
func (c *StrategyContext) Indicator(name string, args ...any) (s indicator.IIndicatorSeries) { |
|
|
|
indicator, ok := c.indicatorsReg.IndicatorW(name) |
|
|
|
indicator, ok := c.indicatorsReg.Indicator(name) |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
panic(fmt.Errorf("indicatorW %s not exists", name)) |
|
|
|
panic(fmt.Errorf("indicator %s not exists", name)) |
|
|
|
} |
|
|
|
} |
|
|
|
var input types.Input |
|
|
|
input := matchIndicatorArgs(args...) |
|
|
|
if len(args) > 0 { |
|
|
|
indicatorContext := NewIndicatorContext(indicator, input, c.indicatorContextStates, c.kSeries, c.indicatorsReg) |
|
|
|
if in, ok := args[0].(types.Input); ok { |
|
|
|
return NewWindowIndicatorSeries(indicator, indicatorContext) |
|
|
|
input = in |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
indicatorContext := NewIndicatorContext(input, c.kSeries) |
|
|
|
|
|
|
|
return NewWindowIndicatorSeries(window, indicator, indicatorContext) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// IntervalStrategyContext 周期策略上下文
|
|
|
|
// IntervalStrategyContext 周期策略上下文
|
|
|
|
type IntervalStrategyContext struct { |
|
|
|
type IntervalStrategyContext struct { |
|
|
|
strategy.IIntervalSigStrategyContext |
|
|
|
strategy.IIntervalSigStrategyContext |
|
|
|
|
|
|
|
|
|
|
|
input types.Input |
|
|
|
input types.Input |
|
|
|
intervalKlineSeries *types.IntervalState[*KlineSeries] |
|
|
|
intervalKlineSeries *types.IntervalState[*KlineSeries] |
|
|
|
indicatorsReg *indicator.IndicatorRegistry |
|
|
|
indicatorsReg *indicator.IndicatorRegistry |
|
|
|
|
|
|
|
intervalIndicatorContextStates map[types.Interval]IndicatorStates |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewIntervalStrategyContext(input types.Input, intervalKlineSeries *types.IntervalState[*KlineSeries], indicatorsReg *indicator.IndicatorRegistry) *IntervalStrategyContext { |
|
|
|
func NewIntervalStrategyContext(input types.Input, intervalKlineSeries *types.IntervalState[*KlineSeries], indicatorsReg *indicator.IndicatorRegistry) *IntervalStrategyContext { |
|
|
|
return &IntervalStrategyContext{ |
|
|
|
return &IntervalStrategyContext{ |
|
|
|
input: input, |
|
|
|
input: input, |
|
|
|
intervalKlineSeries: intervalKlineSeries, |
|
|
|
intervalKlineSeries: intervalKlineSeries, |
|
|
|
indicatorsReg: indicatorsReg, |
|
|
|
indicatorsReg: indicatorsReg, |
|
|
|
|
|
|
|
intervalIndicatorContextStates: make(map[types.Interval]IndicatorStates), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -96,20 +95,20 @@ func (c *IntervalStrategyContext) Series(interval types.Interval, offset, count |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取窗口类型指标
|
|
|
|
// 获取窗口类型指标
|
|
|
|
func (c *IntervalStrategyContext) IndicatorW(interval types.Interval, name string, window int16, args ...any) (series indicator.IIndicatorSeries) { |
|
|
|
func (c *IntervalStrategyContext) Indicator(interval types.Interval, name string, args ...any) (series indicator.IIndicatorSeries) { |
|
|
|
indicator, ok := c.indicatorsReg.IndicatorW(name) |
|
|
|
indicator, ok := c.indicatorsReg.Indicator(name) |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
panic(fmt.Errorf("indicatorW %s not exists", name)) |
|
|
|
panic(fmt.Errorf("indicator %s not exists", name)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var input types.Input |
|
|
|
input := matchIndicatorArgs(args...) |
|
|
|
if len(args) > 0 { |
|
|
|
kSeries := c.getCandleSeries(interval) |
|
|
|
if in, ok := args[0].(types.Input); ok { |
|
|
|
// 状态传递
|
|
|
|
input = in |
|
|
|
state, ok := c.intervalIndicatorContextStates[interval] |
|
|
|
} |
|
|
|
if !ok { |
|
|
|
|
|
|
|
state = NewIndicatorStates() |
|
|
|
|
|
|
|
c.intervalIndicatorContextStates[interval] = state |
|
|
|
} |
|
|
|
} |
|
|
|
cs := c.getCandleSeries(interval) |
|
|
|
indicatorContext := NewIndicatorContext(indicator, input, state, kSeries, c.indicatorsReg) |
|
|
|
indicatorContext := NewIndicatorContext(input, cs) |
|
|
|
return NewWindowIndicatorSeries(indicator, indicatorContext) |
|
|
|
|
|
|
|
|
|
|
|
return NewWindowIndicatorSeries(window, indicator, indicatorContext) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|