bbgo_origin/pkg/strategy/techsignal/strategy.go

227 lines
6.8 KiB
Go
Raw Normal View History

2021-10-14 06:24:08 +00:00
package techsignal
import (
"context"
"errors"
"fmt"
2021-10-14 15:01:10 +00:00
"github.com/c9s/bbgo/pkg/exchange/binance"
2021-10-14 06:24:08 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/sirupsen/logrus"
"math"
2021-10-14 06:24:08 +00:00
"strings"
2021-10-14 15:01:10 +00:00
"time"
2021-10-14 06:24:08 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "techsignal"
var log = logrus.WithField("strategy", ID)
func init() {
// Register the pointer of the strategy struct,
// so that bbgo knows what struct to be used to unmarshal the configs (YAML or JSON)
// Note: built-in strategies need to imported manually in the bbgo cmd package.
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
*bbgo.Notifiability
// These fields will be filled from the config file (it translates YAML to JSON)
Symbol string `json:"symbol"`
Market types.Market `json:"-"`
2021-10-14 15:01:10 +00:00
FundingRate *struct {
High fixedpoint.Value `json:"high"`
Neutral fixedpoint.Value `json:"neutral"`
DiffThreshold fixedpoint.Value `json:"diffThreshold"`
} `json:"fundingRate"`
2021-10-14 06:24:08 +00:00
SupportDetection []struct {
Interval types.Interval `json:"interval"`
// MovingAverageType is the moving average indicator type that we want to use,
// it could be SMA or EWMA
MovingAverageType string `json:"movingAverageType"`
// MovingAverageInterval is the interval of k-lines for the moving average indicator to calculate,
// it could be "1m", "5m", "1h" and so on. note that, the moving averages are calculated from
// the k-line data we subscribed
MovingAverageInterval types.Interval `json:"movingAverageInterval"`
// MovingAverageWindow is the number of the window size of the moving average indicator.
// The number of k-lines in the window. generally used window sizes are 7, 25 and 99 in the TradingView.
MovingAverageWindow int `json:"movingAverageWindow"`
MinVolume fixedpoint.Value `json:"minVolume"`
MinQuoteVolume fixedpoint.Value `json:"minQuoteVolume"`
} `json:"supportDetection"`
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
// session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
for _, detection := range s.SupportDetection {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.Interval),
})
2021-10-18 01:00:56 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: string(detection.MovingAverageInterval),
})
2021-10-14 06:24:08 +00:00
}
}
func (s *Strategy) Validate() error {
if len(s.Symbol) == 0 {
return errors.New("symbol is required")
}
return nil
}
2021-10-14 15:01:10 +00:00
func (s *Strategy) listenToFundingRate(ctx context.Context, exchange *binance.Exchange) {
2021-10-20 06:01:19 +00:00
var previousIndex, fundingRate24HoursLowIndex *binance.PremiumIndex
2021-10-14 15:01:10 +00:00
fundingRateTicker := time.NewTicker(1 * time.Hour)
defer fundingRateTicker.Stop()
for {
select {
2021-10-20 06:01:19 +00:00
2021-10-14 15:01:10 +00:00
case <-ctx.Done():
return
2021-10-20 06:01:19 +00:00
2021-10-14 15:01:10 +00:00
case <-fundingRateTicker.C:
2021-10-20 06:01:19 +00:00
index, err := exchange.QueryPremiumIndex(ctx, s.Symbol)
2021-10-14 15:01:10 +00:00
if err != nil {
log.WithError(err).Error("can not query last funding rate")
continue
}
2021-10-20 06:01:19 +00:00
fundingRate := index.LastFundingRate
if fundingRate >= s.FundingRate.High {
s.Notifiability.Notify("%s funding rate is too high! current %s > threshold %s",
2021-10-14 15:01:10 +00:00
s.Symbol,
2021-10-20 06:01:19 +00:00
fundingRate.Percentage(),
2021-10-14 15:01:10 +00:00
s.FundingRate.High.Percentage(),
)
}
2021-10-20 06:01:19 +00:00
if previousIndex != nil {
if s.FundingRate.DiffThreshold == 0 {
s.FundingRate.DiffThreshold = fixedpoint.NewFromFloat(0.005 * 0.01)
}
2021-10-20 06:01:19 +00:00
diff := fundingRate - previousIndex.LastFundingRate
2021-10-17 14:26:04 +00:00
if diff.Abs() > s.FundingRate.DiffThreshold {
s.Notifiability.Notify("%s funding rate changed %s, current funding rate %s",
2021-10-14 15:01:10 +00:00
s.Symbol,
diff.SignedPercentage(),
2021-10-20 06:01:19 +00:00
fundingRate.Percentage(),
2021-10-14 15:01:10 +00:00
)
}
}
2021-10-20 06:01:19 +00:00
previousIndex = index
if fundingRate24HoursLowIndex != nil {
if fundingRate24HoursLowIndex.Time.Before(time.Now().Add(24 * time.Hour)) {
fundingRate24HoursLowIndex = index
2021-10-14 15:01:10 +00:00
}
2021-10-20 06:01:19 +00:00
if fundingRate < fundingRate24HoursLowIndex.LastFundingRate {
fundingRate24HoursLowIndex = index
2021-10-14 15:01:10 +00:00
}
} else {
2021-10-20 06:01:19 +00:00
fundingRate24HoursLowIndex = index
2021-10-14 15:01:10 +00:00
}
}
}
}
2021-10-14 06:24:08 +00:00
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
standardIndicatorSet, ok := session.StandardIndicatorSet(s.Symbol)
if !ok {
return fmt.Errorf("standardIndicatorSet is nil, symbol %s", s.Symbol)
}
2021-10-14 15:01:10 +00:00
if s.FundingRate != nil {
if binanceExchange, ok := session.Exchange.(*binance.Exchange); ok {
go s.listenToFundingRate(ctx, binanceExchange)
} else {
log.Error("exchange does not support funding rate api")
}
}
2021-10-14 06:24:08 +00:00
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
// skip k-lines from other symbols
if kline.Symbol != s.Symbol {
return
}
for _, detection := range s.SupportDetection {
if kline.Interval != detection.Interval {
continue
}
closePriceF := kline.GetClose()
closePrice := fixedpoint.NewFromFloat(closePriceF)
var ma types.Float64Indicator
switch strings.ToLower(detection.MovingAverageType) {
case "sma":
ma = standardIndicatorSet.SMA(types.IntervalWindow{
Interval: detection.MovingAverageInterval,
Window: detection.MovingAverageWindow,
})
case "ema", "ewma":
ma = standardIndicatorSet.EWMA(types.IntervalWindow{
Interval: detection.MovingAverageInterval,
Window: detection.MovingAverageWindow,
})
default:
ma = standardIndicatorSet.EWMA(types.IntervalWindow{
Interval: detection.MovingAverageInterval,
Window: detection.MovingAverageWindow,
})
}
var lastMA = ma.Last()
// skip if the closed price is above the moving average
if closePrice.Float64() > lastMA {
2021-10-18 03:10:54 +00:00
log.Infof("skip %s support closed price %f > last ma %f", s.Symbol, closePrice.Float64(), lastMA)
return
2021-10-14 06:24:08 +00:00
}
prettyBaseVolume := s.Market.BaseCurrencyFormatter()
prettyQuoteVolume := s.Market.QuoteCurrencyFormatter()
2021-10-14 06:24:08 +00:00
if detection.MinVolume > 0 && kline.Volume > detection.MinVolume.Float64() {
s.Notifiability.Notify("Detected %s %s support base volume %s > min base volume %s, quote volume %s",
2021-10-18 11:40:51 +00:00
s.Symbol, detection.Interval.String(),
prettyBaseVolume.FormatMoney(math.Round(kline.Volume)),
prettyBaseVolume.FormatMoney(math.Round(detection.MinVolume.Float64())),
prettyQuoteVolume.FormatMoney(math.Round(kline.QuoteVolume)),
2021-10-14 06:24:08 +00:00
)
2021-10-14 06:32:49 +00:00
s.Notifiability.Notify(kline)
2021-10-14 06:24:08 +00:00
} else if detection.MinQuoteVolume > 0 && kline.QuoteVolume > detection.MinQuoteVolume.Float64() {
s.Notifiability.Notify("Detected %s %s support quote volume %s > min quote volume %s, base volume %s",
2021-10-18 11:40:51 +00:00
s.Symbol, detection.Interval.String(),
prettyQuoteVolume.FormatMoney(math.Round(kline.QuoteVolume)),
prettyQuoteVolume.FormatMoney(math.Round(detection.MinQuoteVolume.Float64())),
prettyBaseVolume.FormatMoney(math.Round(kline.Volume)),
2021-10-14 06:24:08 +00:00
)
2021-10-14 06:32:49 +00:00
s.Notifiability.Notify(kline)
2021-10-14 06:24:08 +00:00
}
}
})
return nil
}