2020-10-28 08:27:25 +00:00
|
|
|
package swing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2020-10-28 23:54:59 +00:00
|
|
|
bbgo.RegisterStrategy("swing", &Strategy{})
|
2020-10-28 08:27:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy struct {
|
|
|
|
// The notification system will be injected into the strategy automatically.
|
|
|
|
*bbgo.Notifiability
|
|
|
|
*bbgo.MarketDataStore
|
2020-10-28 23:44:22 +00:00
|
|
|
types.Market
|
2020-10-28 08:27:25 +00:00
|
|
|
|
2020-10-28 09:44:37 +00:00
|
|
|
// OrderExecutor is an interface for submitting order
|
2020-10-28 08:27:25 +00:00
|
|
|
bbgo.OrderExecutor
|
|
|
|
|
|
|
|
// These fields will be filled from the config file (it translates YAML to JSON)
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Interval string `json:"interval"`
|
|
|
|
MinChange float64 `json:"minChange"`
|
|
|
|
BaseQuantity float64 `json:"baseQuantity"`
|
|
|
|
MovingAverageType string `json:"movingAverageType"`
|
|
|
|
MovingAverageInterval types.Interval `json:"movingAverageInterval"`
|
|
|
|
MovingAverageWindow int `json:"movingAverageWindow"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Float64Indicator interface {
|
|
|
|
Last() float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
|
|
|
indicatorSet, ok := session.StandardIndicatorSet(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return errors.Errorf("indicatorSet of %s is not configured", s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
var inc Float64Indicator
|
2020-10-28 23:44:22 +00:00
|
|
|
var iw = types.IntervalWindow{Interval: s.MovingAverageInterval, Window: s.MovingAverageWindow}
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
switch s.MovingAverageType {
|
|
|
|
case "SMA":
|
2020-10-28 23:44:22 +00:00
|
|
|
inc = indicatorSet.GetSMA(iw)
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
case "EWMA", "EMA":
|
2020-10-28 23:44:22 +00:00
|
|
|
inc = indicatorSet.GetEWMA(iw)
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.Errorf("unsupported moving average type: %s", s.MovingAverageType)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-28 23:49:06 +00:00
|
|
|
// session.Stream.OnKLineClosed
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
movingAveragePrice := inc.Last()
|
|
|
|
|
|
|
|
// skip it if it's near zero
|
|
|
|
if movingAveragePrice < 0.0001 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip if the change is not above the minChange
|
|
|
|
if math.Abs(kline.GetChange()) < s.MinChange {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
closePrice := kline.Close
|
|
|
|
changePercentage := kline.GetChange() / kline.Open
|
|
|
|
quantity := s.BaseQuantity * (1.0 + math.Abs(changePercentage))
|
|
|
|
|
|
|
|
trend := kline.GetTrend()
|
|
|
|
switch trend {
|
|
|
|
case 1:
|
|
|
|
// if it goes up and it's above the moving average price, then we sell
|
|
|
|
if closePrice > movingAveragePrice {
|
2020-10-28 09:44:37 +00:00
|
|
|
s.notify(":chart_with_upwards_trend: closePrice %f is above movingAveragePrice %f, submitting SELL order", closePrice, movingAveragePrice)
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
2020-10-28 23:44:22 +00:00
|
|
|
Market: s.Market,
|
2020-10-28 08:27:25 +00:00
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Quantity: quantity,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case -1:
|
|
|
|
// if it goes down and it's below the moving average price, then we buy
|
|
|
|
if closePrice < movingAveragePrice {
|
2020-10-28 09:44:37 +00:00
|
|
|
s.notify(":chart_with_downwards_trend: closePrice %f is below movingAveragePrice %f, submitting BUY order", closePrice, movingAveragePrice)
|
2020-10-28 08:27:25 +00:00
|
|
|
|
|
|
|
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
2020-10-28 23:44:22 +00:00
|
|
|
Market: s.Market,
|
2020-10-28 08:27:25 +00:00
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeMarket,
|
|
|
|
Quantity: quantity,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) notify(format string, args ...interface{}) {
|
|
|
|
if channel, ok := s.RouteSymbol(s.Symbol); ok {
|
|
|
|
s.NotifyTo(channel, format, args...)
|
|
|
|
} else {
|
|
|
|
s.Notify(format, args...)
|
|
|
|
}
|
|
|
|
}
|