qbtrade/pkg/strategy/pricealert/strategy.go

49 lines
1.3 KiB
Go
Raw Normal View History

2024-06-27 14:42:38 +00:00
package pricealert
import (
"context"
"git.qtrade.icu/lychiyu/qbtrade/pkg/fixedpoint"
"git.qtrade.icu/lychiyu/qbtrade/pkg/qbtrade"
"git.qtrade.icu/lychiyu/qbtrade/pkg/types"
)
const ID = "pricealert"
func init() {
qbtrade.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
// These fields will be filled from the config file (it translates YAML to JSON)
Symbol string `json:"symbol"`
Interval types.Interval `json:"interval"`
MinChange fixedpoint.Value `json:"minChange"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *qbtrade.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}
func (s *Strategy) Run(ctx context.Context, orderExecutor qbtrade.OrderExecutor, session *qbtrade.ExchangeSession) error {
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 := qbtrade.Notification.RouteSymbol(s.Symbol); ok {
qbtrade.NotifyTo(channel, "%s hit price %s, change %v", s.Symbol, market.FormatPrice(kline.Close), kline.GetChange())
} else {
qbtrade.Notify("%s hit price %s, change %v", s.Symbol, market.FormatPrice(kline.Close), kline.GetChange())
}
}
})
return nil
}