44 lines
970 B
Go
44 lines
970 B
Go
package kline
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"git.qtrade.icu/lychiyu/qbtrade/pkg/qbtrade"
|
|
"git.qtrade.icu/lychiyu/qbtrade/pkg/types"
|
|
)
|
|
|
|
const ID = "kline"
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
|
|
|
func init() {
|
|
qbtrade.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 *qbtrade.ExchangeSession) {
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.MovingAverage.Interval})
|
|
}
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, session *qbtrade.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
|
|
}
|