bbgo_origin/pkg/strategy/skeleton/strategy.go

55 lines
1.0 KiB
Go
Raw Normal View History

2020-10-24 09:58:45 +00:00
package skeleton
import (
"context"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
)
2021-02-03 01:08:05 +00:00
const ID = "skeleton"
2020-10-24 09:58:45 +00:00
func init() {
2021-02-03 01:08:05 +00:00
bbgo.RegisterStrategy(ID, &Strategy{})
2020-10-24 09:58:45 +00:00
}
type Strategy struct {
Symbol string `json:"symbol"`
types.Market
2020-10-24 09:58:45 +00:00
}
2021-02-03 01:08:05 +00:00
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
2020-10-24 09:58:45 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"})
}
2020-10-24 09:58:45 +00:00
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
session.Stream.OnKLineClosed(func(kline types.KLine) {
quoteBalance, ok := session.Account.Balance(s.Market.QuoteCurrency)
2020-10-24 09:58:45 +00:00
if !ok {
return
}
_ = quoteBalance
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
2020-10-24 09:58:45 +00:00
Symbol: kline.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeMarket,
Quantity: 0.01,
})
if err != nil {
log.WithError(err).Error("submit order error")
}
})
return nil
}