bbgo_origin/pkg/strategy/kline/strategy.go

44 lines
933 B
Go
Raw Permalink Normal View History

2021-11-21 18:14:44 +00:00
package kline
import (
"context"
2022-05-19 01:48:36 +00:00
2021-11-21 18:14:44 +00:00
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "kline"
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
Symbol string `json:"symbol"`
MovingAverage types.IntervalWindow `json:"movingAverage"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
2022-05-19 01:48:36 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverage.Interval})
2021-11-21 18:14:44 +00:00
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
// skip k-lines from other symbols
if kline.Symbol != s.Symbol {
return
}
log.Infof("%s", kline.String())
})
return nil
}