2023-03-22 13:17:33 +00:00
|
|
|
package xfunding
|
2022-01-13 11:35:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-03-22 13:36:42 +00:00
|
|
|
"fmt"
|
2022-04-23 07:43:11 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2023-03-22 13:17:33 +00:00
|
|
|
const ID = "xfunding"
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2023-03-22 13:42:06 +00:00
|
|
|
type PositionAction int
|
|
|
|
|
2023-03-22 13:38:56 +00:00
|
|
|
const (
|
2023-03-22 13:42:06 +00:00
|
|
|
PositionNoOp PositionAction = iota
|
2023-03-22 13:38:56 +00:00
|
|
|
PositionOpening
|
|
|
|
PositionClosing
|
|
|
|
)
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
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 {
|
2023-03-22 13:36:42 +00:00
|
|
|
Environment *bbgo.Environment
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
// These fields will be filled from the config file (it translates YAML to JSON)
|
2022-01-14 08:12:35 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Market types.Market `json:"-"`
|
|
|
|
Quantity fixedpoint.Value `json:"quantity,omitempty"`
|
|
|
|
MaxExposurePosition fixedpoint.Value `json:"maxExposurePosition"`
|
2022-07-26 10:35:50 +00:00
|
|
|
// Interval types.Interval `json:"interval"`
|
2022-01-13 11:35:05 +00:00
|
|
|
|
|
|
|
FundingRate *struct {
|
2023-03-22 13:36:42 +00:00
|
|
|
High fixedpoint.Value `json:"high"`
|
|
|
|
Neutral fixedpoint.Value `json:"neutral"`
|
2022-01-13 11:35:05 +00:00
|
|
|
} `json:"fundingRate"`
|
|
|
|
|
|
|
|
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
|
2022-07-26 10:35:50 +00:00
|
|
|
// MovingAverageInterval types.Interval `json:"movingAverageInterval"`
|
2022-01-14 08:12:35 +00:00
|
|
|
//
|
2022-07-26 10:35:50 +00:00
|
|
|
// // 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"`
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2022-01-14 08:12:35 +00:00
|
|
|
MovingAverageIntervalWindow types.IntervalWindow `json:"movingAverageIntervalWindow"`
|
2022-01-13 11:35:05 +00:00
|
|
|
|
|
|
|
MinVolume fixedpoint.Value `json:"minVolume"`
|
|
|
|
|
|
|
|
MinQuoteVolume fixedpoint.Value `json:"minQuoteVolume"`
|
|
|
|
} `json:"supportDetection"`
|
2023-03-22 13:36:42 +00:00
|
|
|
|
|
|
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
|
|
|
|
SpotPosition *types.Position `persistence:"spot_position"`
|
|
|
|
FuturesPosition *types.Position `persistence:"futures_position"`
|
|
|
|
|
|
|
|
spotSession, futuresSession *bbgo.ExchangeSession
|
|
|
|
|
|
|
|
spotOrderExecutor, futuresOrderExecutor bbgo.OrderExecutor
|
|
|
|
spotMarket, futuresMarket types.Market
|
|
|
|
|
|
|
|
SpotSession string `json:"spotSession"`
|
|
|
|
FuturesSession string `json:"futuresSession"`
|
2023-03-22 13:42:06 +00:00
|
|
|
|
|
|
|
// positionAction is default to NoOp
|
|
|
|
positionAction PositionAction
|
2022-01-13 11:35:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) ID() string {
|
|
|
|
return ID
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:17:33 +00:00
|
|
|
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {
|
2023-03-22 13:42:06 +00:00
|
|
|
// TODO: add safety check
|
|
|
|
spotSession := sessions[s.SpotSession]
|
|
|
|
futuresSession := sessions[s.FuturesSession]
|
|
|
|
|
|
|
|
spotSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
|
|
|
Interval: types.Interval1m,
|
|
|
|
})
|
|
|
|
|
|
|
|
futuresSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
|
|
|
Interval: types.Interval1m,
|
|
|
|
})
|
2023-03-22 13:17:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
|
|
|
// session.Subscribe(types.BookChannel, s.Symbol, types.SubscribeOptions{})
|
2022-01-14 08:12:35 +00:00
|
|
|
|
2022-07-26 10:35:50 +00:00
|
|
|
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
2022-01-14 08:12:35 +00:00
|
|
|
// Interval: string(s.Interval),
|
2022-07-26 10:35:50 +00:00
|
|
|
// })
|
2022-01-14 08:12:35 +00:00
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
for _, detection := range s.SupportDetection {
|
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
2022-05-19 01:48:36 +00:00
|
|
|
Interval: detection.Interval,
|
2022-01-13 11:35:05 +00:00
|
|
|
})
|
|
|
|
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
|
2022-05-19 01:48:36 +00:00
|
|
|
Interval: detection.MovingAverageIntervalWindow.Interval,
|
2022-01-13 11:35:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) Validate() error {
|
|
|
|
if len(s.Symbol) == 0 {
|
|
|
|
return errors.New("symbol is required")
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:42:44 +00:00
|
|
|
if len(s.SpotSession) == 0 {
|
|
|
|
return errors.New("spotSession name is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.FuturesSession) == 0 {
|
|
|
|
return errors.New("futuresSession name is required")
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
func (s *Strategy) InstanceID() string {
|
|
|
|
return fmt.Sprintf("%s-%s", ID, s.Symbol)
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
2022-07-26 10:35:50 +00:00
|
|
|
standardIndicatorSet := session.StandardIndicatorSet(s.Symbol)
|
2022-01-14 08:12:35 +00:00
|
|
|
|
|
|
|
if !session.Futures {
|
|
|
|
log.Error("futures not enabled in config for this strategy")
|
|
|
|
return nil
|
2022-01-13 11:35:05 +00:00
|
|
|
}
|
2022-01-14 08:12:35 +00:00
|
|
|
|
|
|
|
var ma types.Float64Indicator
|
|
|
|
for _, detection := range s.SupportDetection {
|
|
|
|
|
|
|
|
switch strings.ToLower(detection.MovingAverageType) {
|
|
|
|
case "sma":
|
|
|
|
ma = standardIndicatorSet.SMA(types.IntervalWindow{
|
|
|
|
Interval: detection.MovingAverageIntervalWindow.Interval,
|
|
|
|
Window: detection.MovingAverageIntervalWindow.Window,
|
|
|
|
})
|
|
|
|
case "ema", "ewma":
|
|
|
|
ma = standardIndicatorSet.EWMA(types.IntervalWindow{
|
|
|
|
Interval: detection.MovingAverageIntervalWindow.Interval,
|
|
|
|
Window: detection.MovingAverageIntervalWindow.Window,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
ma = standardIndicatorSet.EWMA(types.IntervalWindow{
|
|
|
|
Interval: detection.MovingAverageIntervalWindow.Interval,
|
|
|
|
Window: detection.MovingAverageIntervalWindow.Window,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2023-03-22 13:11:58 +00:00
|
|
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) {
|
2023-03-22 13:17:33 +00:00
|
|
|
premiumIndex, err := session.Exchange.(*binance.Exchange).QueryPremiumIndex(ctx, s.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("exchange does not support funding rate api")
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
// skip k-lines from other symbols
|
|
|
|
for _, detection := range s.SupportDetection {
|
2022-01-14 08:12:35 +00:00
|
|
|
var lastMA = ma.Last()
|
2022-01-13 11:35:05 +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
|
|
|
closePrice := kline.GetClose()
|
|
|
|
closePriceF := closePrice.Float64()
|
2022-01-13 11:35:05 +00:00
|
|
|
// skip if the closed price is under the moving average
|
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 closePriceF < lastMA {
|
|
|
|
log.Infof("skip %s closed price %v < last ma %f", s.Symbol, closePrice, lastMA)
|
2022-01-13 11:35:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fundingRate := premiumIndex.LastFundingRate
|
|
|
|
|
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 fundingRate.Compare(s.FundingRate.High) >= 0 {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("%s funding rate %s is too high! threshold %s",
|
2022-01-13 11:35:05 +00:00
|
|
|
s.Symbol,
|
|
|
|
fundingRate.Percentage(),
|
|
|
|
s.FundingRate.High.Percentage(),
|
|
|
|
)
|
|
|
|
} else {
|
2022-01-14 08:12:35 +00:00
|
|
|
log.Infof("skip funding rate is too low")
|
2022-01-13 11:35:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyBaseVolume := s.Market.BaseCurrencyFormatter()
|
|
|
|
prettyQuoteVolume := s.Market.QuoteCurrencyFormatter()
|
|
|
|
|
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 detection.MinVolume.Sign() > 0 && kline.Volume.Compare(detection.MinVolume) > 0 {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("Detected %s %s resistance base volume %s > min base volume %s, quote volume %s",
|
2022-01-13 11:35:05 +00:00
|
|
|
s.Symbol, detection.Interval.String(),
|
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
|
|
|
prettyBaseVolume.FormatMoney(kline.Volume.Trunc()),
|
|
|
|
prettyBaseVolume.FormatMoney(detection.MinVolume.Trunc()),
|
|
|
|
prettyQuoteVolume.FormatMoney(kline.QuoteVolume.Trunc()),
|
2022-01-13 11:35:05 +00:00
|
|
|
)
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify(kline)
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2022-04-23 07:43:11 +00:00
|
|
|
baseBalance, ok := session.GetAccount().Balance(s.Market.BaseCurrency)
|
2022-01-13 11:35:05 +00:00
|
|
|
if !ok {
|
|
|
|
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
|
|
|
if baseBalance.Available.Sign() > 0 && baseBalance.Total().Compare(s.MaxExposurePosition) < 0 {
|
2022-01-14 08:12:35 +00:00
|
|
|
log.Infof("opening a short position")
|
2022-01-13 11:35:05 +00:00
|
|
|
_, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: kline.Symbol,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Type: types.OrderTypeMarket,
|
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.Quantity,
|
2022-01-13 11:35:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("submit order error")
|
|
|
|
}
|
|
|
|
}
|
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 detection.MinQuoteVolume.Sign() > 0 && kline.QuoteVolume.Compare(detection.MinQuoteVolume) > 0 {
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify("Detected %s %s resistance quote volume %s > min quote volume %s, base volume %s",
|
2022-01-13 11:35:05 +00:00
|
|
|
s.Symbol, detection.Interval.String(),
|
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
|
|
|
prettyQuoteVolume.FormatMoney(kline.QuoteVolume.Trunc()),
|
|
|
|
prettyQuoteVolume.FormatMoney(detection.MinQuoteVolume.Trunc()),
|
|
|
|
prettyBaseVolume.FormatMoney(kline.Volume.Trunc()),
|
2022-01-13 11:35:05 +00:00
|
|
|
)
|
2022-06-19 04:29:36 +00:00
|
|
|
bbgo.Notify(kline)
|
2022-01-13 11:35:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-22 13:11:58 +00:00
|
|
|
}))
|
2022-01-13 11:35:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-22 13:17:33 +00:00
|
|
|
|
|
|
|
func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error {
|
2023-03-22 13:36:42 +00:00
|
|
|
instanceID := s.InstanceID()
|
|
|
|
|
|
|
|
// TODO: add safety check
|
|
|
|
s.spotSession = sessions[s.SpotSession]
|
|
|
|
s.futuresSession = sessions[s.FuturesSession]
|
|
|
|
|
|
|
|
s.spotMarket, _ = s.spotSession.Market(s.Symbol)
|
|
|
|
s.futuresMarket, _ = s.futuresSession.Market(s.Symbol)
|
|
|
|
|
|
|
|
if s.ProfitStats == nil {
|
|
|
|
s.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.FuturesPosition == nil {
|
|
|
|
s.FuturesPosition = types.NewPositionFromMarket(s.futuresMarket)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.SpotPosition == nil {
|
|
|
|
s.SpotPosition = types.NewPositionFromMarket(s.spotMarket)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.spotOrderExecutor = s.allocateOrderExecutor(ctx, s.spotSession, instanceID, s.SpotPosition)
|
|
|
|
s.futuresOrderExecutor = s.allocateOrderExecutor(ctx, s.futuresSession, instanceID, s.FuturesPosition)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) allocateOrderExecutor(ctx context.Context, session *bbgo.ExchangeSession, instanceID string, position *types.Position) *bbgo.GeneralOrderExecutor {
|
|
|
|
orderExecutor := bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, position)
|
|
|
|
orderExecutor.BindEnvironment(s.Environment)
|
|
|
|
orderExecutor.Bind()
|
|
|
|
orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, _, _ fixedpoint.Value) {
|
|
|
|
s.ProfitStats.AddTrade(trade)
|
|
|
|
})
|
|
|
|
orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
|
|
|
bbgo.Sync(ctx, s)
|
|
|
|
})
|
|
|
|
return orderExecutor
|
2023-03-22 13:17:33 +00:00
|
|
|
}
|