all: add common.NewStrategy helper

This commit is contained in:
c9s 2023-07-12 18:17:03 +08:00
parent 80b740736c
commit 3ed5f00bcc
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 11 additions and 4 deletions

View File

@ -36,6 +36,15 @@ type Strategy struct {
RiskController RiskController
} }
func NewStrategy(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string) *Strategy {
s := &Strategy{
Environ: environ,
Session: session,
}
s.Initialize(ctx, environ, session, market, strategyID, instanceID)
return s
}
func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string) { func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, session *bbgo.ExchangeSession, market types.Market, strategyID, instanceID string) {
s.parent = ctx s.parent = ctx
s.ctx, s.cancel = context.WithCancel(ctx) s.ctx, s.cancel = context.WithCancel(ctx)

View File

@ -49,8 +49,7 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.Strategy = &common.Strategy{} s.Strategy = common.NewStrategy(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
fastRsi := session.Indicators(s.Symbol).RSI(types.IntervalWindow{Interval: s.Interval, Window: s.FastWindow}) fastRsi := session.Indicators(s.Symbol).RSI(types.IntervalWindow{Interval: s.Interval, Window: s.FastWindow})
slowRsi := session.Indicators(s.Symbol).RSI(types.IntervalWindow{Interval: s.Interval, Window: s.SlowWindow}) slowRsi := session.Indicators(s.Symbol).RSI(types.IntervalWindow{Interval: s.Interval, Window: s.SlowWindow})

View File

@ -100,8 +100,7 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.Strategy = &common.Strategy{} s.Strategy = common.NewStrategy(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
s.book = types.NewStreamBook(s.Symbol) s.book = types.NewStreamBook(s.Symbol)
s.book.BindStream(session.UserDataStream) s.book.BindStream(session.UserDataStream)