bbgo_origin/pkg/strategy/pricealert/strategy.go

52 lines
1.4 KiB
Go
Raw Normal View History

package pricealert
import (
"context"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
2021-02-03 01:08:05 +00:00
const ID = "pricealert"
func init() {
2021-02-03 01:08:05 +00:00
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
2020-10-27 07:51:36 +00:00
// The notification system will be injected into the strategy automatically.
2022-03-06 08:09:15 +00:00
*bbgo.Notifiability
2020-10-27 07:51:36 +00:00
// These fields will be filled from the config file (it translates YAML to JSON)
Symbol string `json:"symbol"`
Interval string `json:"interval"`
MinChange fixedpoint.Value `json:"minChange"`
}
2021-02-03 01:08:05 +00:00
func (s *Strategy) ID() string {
return ID
}
2020-10-28 09:49:49 +00:00
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
2020-10-28 09:49:49 +00:00
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
2021-05-27 19:15:29 +00:00
session.MarketDataStream.OnKLine(func(kline types.KLine) {
market, ok := session.Market(kline.Symbol)
if !ok {
return
}
if kline.GetChange().Abs().Compare(s.MinChange) > 0 {
if channel, ok := s.RouteSymbol(s.Symbol); ok {
s.NotifyTo(channel, "%s hit price %s, change %v", s.Symbol, market.FormatPrice(kline.Close), kline.GetChange())
} else {
s.Notify("%s hit price %s, change %v", s.Symbol, market.FormatPrice(kline.Close), kline.GetChange())
}
}
})
return nil
}