|
|
|
@ -2,6 +2,7 @@ package trading |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"sig-pub/api/pb" |
|
|
|
"sig-pub/pkg/indicator" |
|
|
|
"sig-pub/pkg/indicator" |
|
|
|
"sig-pub/pkg/strategy" |
|
|
|
"sig-pub/pkg/strategy" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
"sig-pub/pkg/types" |
|
|
|
@ -20,6 +21,9 @@ type StrategyContext struct { |
|
|
|
|
|
|
|
|
|
|
|
indicatorContext *IndicatorContext |
|
|
|
indicatorContext *IndicatorContext |
|
|
|
indicatorsW *collect.SyncMap[string, indicator.IWindowIndicator] |
|
|
|
indicatorsW *collect.SyncMap[string, indicator.IWindowIndicator] |
|
|
|
|
|
|
|
signal []pb.Side // 0.sell,1.buy
|
|
|
|
|
|
|
|
signalTimes []int64 |
|
|
|
|
|
|
|
wins []bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewStrategyContext(klineSeries *KlineSeries, indicatorsW *collect.SyncMap[string, indicator.IWindowIndicator]) *StrategyContext { |
|
|
|
func NewStrategyContext(klineSeries *KlineSeries, indicatorsW *collect.SyncMap[string, indicator.IWindowIndicator]) *StrategyContext { |
|
|
|
@ -44,11 +48,32 @@ func (c *StrategyContext) Series(offset, count int16) (klines series.Klines) { |
|
|
|
// Buy 发出多信号
|
|
|
|
// Buy 发出多信号
|
|
|
|
func (c *StrategyContext) Buy() { |
|
|
|
func (c *StrategyContext) Buy() { |
|
|
|
zlog.Infof("signal buy: %d", c.Get(0).Ts) |
|
|
|
zlog.Infof("signal buy: %d", c.Get(0).Ts) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
c.signal = append(c.signal, pb.Side_BUY) |
|
|
|
|
|
|
|
c.signalTimes = append(c.signalTimes, c.Get(0).Ts) |
|
|
|
|
|
|
|
win := false |
|
|
|
|
|
|
|
signalPrice := c.Get(0).Close |
|
|
|
|
|
|
|
if c.indicatorContext.offset > 0 { |
|
|
|
|
|
|
|
c.indicatorContext.AddOffset(-1) |
|
|
|
|
|
|
|
win = c.Get(0).Close.Cmp(signalPrice) > 0 |
|
|
|
|
|
|
|
c.indicatorContext.AddOffset(1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
c.wins = append(c.wins, win) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Sell 发出空信号
|
|
|
|
// Sell 发出空信号
|
|
|
|
func (c *StrategyContext) Sell() { |
|
|
|
func (c *StrategyContext) Sell() { |
|
|
|
zlog.Infof("signal sell: %d", c.Get(0).Ts) |
|
|
|
zlog.Infof("signal sell: %d", c.Get(0).Ts) |
|
|
|
|
|
|
|
c.signal = append(c.signal, pb.Side_SELL) |
|
|
|
|
|
|
|
c.signalTimes = append(c.signalTimes, c.Get(0).Ts) |
|
|
|
|
|
|
|
win := false |
|
|
|
|
|
|
|
signalPrice := c.Get(0).Close |
|
|
|
|
|
|
|
if c.indicatorContext.offset > 0 { |
|
|
|
|
|
|
|
c.indicatorContext.AddOffset(-1) |
|
|
|
|
|
|
|
win = c.Get(0).Close.Cmp(signalPrice) < 0 |
|
|
|
|
|
|
|
c.indicatorContext.AddOffset(1) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
c.wins = append(c.wins, win) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 获取窗口类型指标
|
|
|
|
// 获取窗口类型指标
|
|
|
|
|