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

88 lines
2.5 KiB
Go
Raw Normal View History

package livenote
import (
"context"
2024-11-11 08:52:09 +00:00
"time"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/livenote"
"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"`
Interval types.Interval `json:"interval"`
UserID string `json:"userID"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) InstanceID() string {
return ID + "-" + s.Symbol
}
func (s *Strategy) Defaults() error {
if s.Interval == "" {
s.Interval = types.Interval1m
}
return nil
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}
// 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) {
2024-11-05 09:34:26 +00:00
log.Info(k)
bbgo.PostLiveNote(&k,
livenote.OneTimeMention(s.UserID),
livenote.Comment("please check the deposit"),
2024-11-11 08:52:09 +00:00
livenote.CompareObject(true),
livenote.TimeToLive(time.Minute),
// livenote.Pin(true),
)
}
// 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
}