2020-10-12 14:46:06 +00:00
|
|
|
package buyandhold
|
|
|
|
|
|
|
|
import (
|
2020-10-13 06:50:59 +00:00
|
|
|
"context"
|
2020-10-13 08:17:07 +00:00
|
|
|
"math"
|
2020-10-13 06:50:59 +00:00
|
|
|
|
2020-10-15 15:38:00 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2020-10-20 05:52:25 +00:00
|
|
|
func init() {
|
2020-10-28 23:54:59 +00:00
|
|
|
bbgo.RegisterStrategy("buyandhold", &Strategy{})
|
2020-10-20 05:52:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
type Strategy struct {
|
2020-10-13 08:17:07 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Interval string `json:"interval"`
|
|
|
|
BaseQuantity float64 `json:"baseQuantity"`
|
|
|
|
MinDropPercentage float64 `json:"minDropPercentage"`
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 09:49:49 +00:00
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
2020-10-13 08:17:07 +00:00
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
2020-10-28 09:49:49 +00:00
|
|
|
}
|
2020-10-13 06:50:59 +00:00
|
|
|
|
2020-10-28 09:49:49 +00:00
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
2020-10-26 14:03:42 +00:00
|
|
|
session.Stream.OnKLine(func(kline types.KLine) {
|
2020-10-28 08:27:25 +00:00
|
|
|
// skip k-lines from other symbols
|
|
|
|
if kline.Symbol != s.Symbol {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:03:42 +00:00
|
|
|
changePercentage := kline.GetChange() / kline.Open
|
|
|
|
log.Infof("change %f <=> %f", changePercentage, s.MinDropPercentage)
|
|
|
|
})
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
session.Stream.OnKLineClosed(func(kline types.KLine) {
|
2020-10-28 08:27:25 +00:00
|
|
|
// skip k-lines from other symbols
|
|
|
|
if kline.Symbol != s.Symbol {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-13 06:50:59 +00:00
|
|
|
changePercentage := kline.GetChange() / kline.Open
|
|
|
|
|
2020-10-26 14:03:42 +00:00
|
|
|
if changePercentage > s.MinDropPercentage {
|
|
|
|
return
|
|
|
|
}
|
2020-10-13 08:17:07 +00:00
|
|
|
|
2020-10-26 14:03:42 +00:00
|
|
|
// buy when price drops -8%
|
|
|
|
market, ok := session.Market(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
log.Warnf("market %s is not defined", s.Symbol)
|
|
|
|
return
|
|
|
|
}
|
2020-10-13 08:17:07 +00:00
|
|
|
|
2020-10-28 01:43:19 +00:00
|
|
|
quantity := s.BaseQuantity * (1.0 + math.Abs(changePercentage))
|
2020-10-26 14:03:42 +00:00
|
|
|
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: kline.Symbol,
|
|
|
|
Market: market,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
2020-10-28 01:43:19 +00:00
|
|
|
Quantity: quantity,
|
2020-10-26 14:03:42 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
2020-10-13 06:50:59 +00:00
|
|
|
}
|
2020-10-12 14:46:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|