bbgo_origin/pkg/strategy/example/livenote/strategy.go

68 lines
2.0 KiB
Go
Raw Normal View History

package livenote
import (
"context"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
// ID is the unique strategy ID, it needs to be in all lower case
// For example, grid strategy uses "grid"
const ID = "livenote"
// log is a logrus.Entry that will be reused.
// This line attaches the strategy field to the logger with our ID, so that the logs from this strategy will be tagged with our ID
var log = logrus.WithField("strategy", ID)
var ten = fixedpoint.NewFromInt(10)
// init is a special function of golang, it will be called when the program is started
// importing this package will trigger the init function call.
func init() {
// Register our struct type to BBGO
// Note that you don't need to field the fields.
// BBGO uses reflect to parse your type information.
bbgo.RegisterStrategy(ID, &Strategy{})
}
// Strategy is a struct that contains the settings of your strategy.
// These settings will be loaded from the BBGO YAML config file "bbgo.yaml" automatically.
type Strategy struct {
Symbol string `json:"symbol"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) InstanceID() string {
return ID + "-" + s.Symbol
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
}
// This strategy simply spent all available quote currency to buy the symbol whenever kline gets closed
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
callback := func(k types.KLine) {
// bbgo.PostLiveNote(k)
}
// register our kline event handler
session.MarketDataStream.OnKLine(callback)
session.MarketDataStream.OnKLineClosed(callback)
// if you need to do something when the user data stream is ready
// note that you only receive order update, trade update, balance update when the user data stream is connect.
session.UserDataStream.OnStart(func() {
log.Infof("connected")
})
return nil
}