2021-10-14 05:10:00 +00:00
|
|
|
package pricedrop
|
2020-10-12 14:46:06 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-13 06:50:59 +00:00
|
|
|
"context"
|
2020-12-14 06:21:02 +00:00
|
|
|
"fmt"
|
2020-10-13 06:50:59 +00:00
|
|
|
|
2020-12-14 06:21:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-10-15 15:38:00 +00:00
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
2020-12-14 06:21:02 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2020-10-12 14:46:06 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2021-10-14 05:10:00 +00:00
|
|
|
const ID = "pricedrop"
|
2021-02-03 01:08:05 +00:00
|
|
|
|
|
|
|
var log = logrus.WithField("strategy", ID)
|
2020-12-14 06:21:02 +00:00
|
|
|
|
2020-10-20 05:52:25 +00:00
|
|
|
func init() {
|
2021-02-03 01:08:05 +00:00
|
|
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
2020-10-20 05:52:25 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:46:06 +00:00
|
|
|
type Strategy struct {
|
2020-12-14 06:21:02 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
|
2020-12-14 06:40:31 +00:00
|
|
|
Interval types.Interval `json:"interval"`
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
BaseQuantity fixedpoint.Value `json:"baseQuantity"`
|
2020-12-14 06:21:02 +00:00
|
|
|
MinDropPercentage fixedpoint.Value `json:"minDropPercentage"`
|
|
|
|
MinDropChange fixedpoint.Value `json:"minDropChange"`
|
2020-12-14 06:40:31 +00:00
|
|
|
|
|
|
|
MovingAverageWindow int `json:"movingAverageWindow"`
|
2020-10-12 14:46:06 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-05-19 01:48:36 +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-12-14 06:40:31 +00:00
|
|
|
if s.Interval == "" {
|
|
|
|
s.Interval = types.Interval5m
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.MovingAverageWindow == 0 {
|
|
|
|
s.MovingAverageWindow = 99
|
|
|
|
}
|
|
|
|
|
2020-12-14 06:21:02 +00:00
|
|
|
// buy when price drops -8%
|
|
|
|
market, ok := session.Market(s.Symbol)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("market %s is not defined", s.Symbol)
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:35:50 +00:00
|
|
|
standardIndicatorSet := session.StandardIndicatorSet(s.Symbol)
|
2020-12-14 06:40:31 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("standardIndicatorSet is nil, symbol %s", s.Symbol)
|
|
|
|
}
|
|
|
|
|
|
|
|
var iw = types.IntervalWindow{Interval: s.Interval, Window: s.MovingAverageWindow}
|
|
|
|
var ema = standardIndicatorSet.EWMA(iw)
|
|
|
|
|
2021-05-27 19:13:50 +00:00
|
|
|
session.MarketDataStream.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-12-14 06:21:02 +00:00
|
|
|
change := kline.GetChange()
|
2020-10-13 06:50:59 +00:00
|
|
|
|
2020-12-14 06:21:02 +00:00
|
|
|
// skip positive change
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
if change.Sign() > 0 {
|
2020-10-26 14:03:42 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-13 08:17:07 +00:00
|
|
|
|
2023-05-31 11:35:44 +00:00
|
|
|
if kline.Close.Float64() > ema.Last(0) {
|
|
|
|
log.Warnf("kline close price %v is above EMA %s %f", kline.Close, ema.IntervalWindow, ema.Last(0))
|
2020-12-14 06:40:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
changeP := change.Div(kline.Open).Abs()
|
2020-12-14 06:21:02 +00:00
|
|
|
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
if !s.MinDropPercentage.IsZero() {
|
|
|
|
if changeP.Compare(s.MinDropPercentage.Abs()) < 0 {
|
2020-12-14 06:21:02 +00:00
|
|
|
return
|
|
|
|
}
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
} else if !s.MinDropChange.IsZero() {
|
|
|
|
if change.Abs().Compare(s.MinDropChange.Abs()) < 0 {
|
2020-12-14 06:21:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// not configured, we shall skip
|
|
|
|
log.Warnf("parameters are not configured, skipping action...")
|
2020-10-26 14:03:42 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-13 08:17:07 +00:00
|
|
|
|
fix bollgrid, emstop, flashcrash, funding, grid, pricealert, pricedrop, rebalance, schedule, swing, xbalance, xgap, xmaker and speedup fixedpoint
2022-02-04 11:39:23 +00:00
|
|
|
quantity := s.BaseQuantity.Mul(fixedpoint.One.Add(changeP))
|
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
|
|
|
|
}
|