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"
|
2023-03-23 06:48:24 +00:00
|
|
|
"sync"
|
2023-03-23 04:58:10 +00:00
|
|
|
"time"
|
2022-04-23 07:43:11 +00:00
|
|
|
|
|
|
|
"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"
|
2023-03-23 04:58:10 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util/backoff"
|
2022-01-13 11:35:05 +00:00
|
|
|
|
|
|
|
"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-23 04:58:10 +00:00
|
|
|
//go:generate stringer -type=PositionAction
|
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{})
|
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
// Strategy is the xfunding fee strategy
|
|
|
|
// Right now it only supports short position in the USDT futures account.
|
|
|
|
// When opening the short position, it uses spot account to buy inventory, then transfer the inventory to the futures account as collateral assets.
|
2022-01-13 11:35:05 +00:00
|
|
|
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)
|
2023-03-23 04:58:10 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Market types.Market `json:"-"`
|
|
|
|
|
|
|
|
// Leverage is the leverage of the futures position
|
|
|
|
Leverage fixedpoint.Value `json:"leverage,omitempty"`
|
2023-03-22 16:23:51 +00:00
|
|
|
|
|
|
|
// IncrementalQuoteQuantity is used for opening position incrementally with a small fixed quote quantity
|
|
|
|
// for example, 100usdt per order
|
|
|
|
IncrementalQuoteQuantity fixedpoint.Value `json:"incrementalQuoteQuantity"`
|
|
|
|
|
|
|
|
QuoteInvestment fixedpoint.Value `json:"quoteInvestment"`
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2023-03-22 14:15:01 +00:00
|
|
|
// ShortFundingRate is the funding rate range for short positions
|
|
|
|
// TODO: right now we don't support negative funding rate (long position) since it's rarer
|
|
|
|
ShortFundingRate *struct {
|
|
|
|
High fixedpoint.Value `json:"high"`
|
|
|
|
Low fixedpoint.Value `json:"low"`
|
|
|
|
} `json:"shortFundingRate"`
|
2022-01-13 11:35:05 +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
|
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
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
SpotSession string `json:"spotSession"`
|
|
|
|
FuturesSession string `json:"futuresSession"`
|
2023-03-22 13:36:42 +00:00
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
|
|
|
|
SpotPosition *types.Position `persistence:"spot_position"`
|
|
|
|
FuturesPosition *types.Position `persistence:"futures_position"`
|
2023-03-22 13:36:42 +00:00
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
State *State `persistence:"state"`
|
2023-03-23 09:36:30 +00:00
|
|
|
|
|
|
|
// mu is used for locking state
|
|
|
|
mu sync.Mutex
|
2023-03-22 13:36:42 +00:00
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
spotSession, futuresSession *bbgo.ExchangeSession
|
2023-03-22 16:23:51 +00:00
|
|
|
spotOrderExecutor, futuresOrderExecutor *bbgo.GeneralOrderExecutor
|
2023-03-22 13:36:42 +00:00
|
|
|
spotMarket, futuresMarket types.Market
|
|
|
|
|
2023-03-22 13:42:06 +00:00
|
|
|
// positionAction is default to NoOp
|
|
|
|
positionAction PositionAction
|
2023-03-22 16:23:51 +00:00
|
|
|
|
|
|
|
// positionType is the futures position type
|
|
|
|
// currently we only support short position for the positive funding rate
|
|
|
|
positionType types.PositionType
|
2023-03-23 05:14:59 +00:00
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
type State struct {
|
|
|
|
PendingBaseTransfer fixedpoint.Value `json:"pendingBaseTransfer"`
|
2023-03-23 06:46:02 +00:00
|
|
|
TotalBaseTransfer fixedpoint.Value `json:"totalBaseTransfer"`
|
2023-03-23 05:14:59 +00:00
|
|
|
UsedQuoteInvestment fixedpoint.Value `json:"usedQuoteInvestment"`
|
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) {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
func (s *Strategy) Defaults() error {
|
|
|
|
s.Leverage = fixedpoint.One
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:23:51 +00:00
|
|
|
if s.QuoteInvestment.IsZero() {
|
|
|
|
return errors.New("quoteInvestment can not be zero")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-22 14:15:24 +00:00
|
|
|
_ = ma
|
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()
|
|
|
|
|
|
|
|
s.spotSession = sessions[s.SpotSession]
|
|
|
|
s.futuresSession = sessions[s.FuturesSession]
|
|
|
|
|
|
|
|
s.spotMarket, _ = s.spotSession.Market(s.Symbol)
|
|
|
|
s.futuresMarket, _ = s.futuresSession.Market(s.Symbol)
|
|
|
|
|
2023-03-22 16:40:20 +00:00
|
|
|
// adjust QuoteInvestment
|
|
|
|
if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok {
|
|
|
|
originalQuoteInvestment := s.QuoteInvestment
|
2023-03-22 16:56:28 +00:00
|
|
|
|
|
|
|
// adjust available quote with the fee rate
|
|
|
|
available := b.Available.Mul(fixedpoint.NewFromFloat(1.0 - (0.01 * 0.075)))
|
|
|
|
s.QuoteInvestment = fixedpoint.Min(available, s.QuoteInvestment)
|
2023-03-22 16:40:20 +00:00
|
|
|
|
|
|
|
if originalQuoteInvestment.Compare(s.QuoteInvestment) != 0 {
|
|
|
|
log.Infof("adjusted quoteInvestment from %s to %s according to the balance",
|
|
|
|
originalQuoteInvestment.String(),
|
|
|
|
s.QuoteInvestment.String(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
if s.State == nil {
|
|
|
|
s.State = &State{
|
|
|
|
PendingBaseTransfer: fixedpoint.Zero,
|
2023-03-23 06:46:02 +00:00
|
|
|
TotalBaseTransfer: fixedpoint.Zero,
|
2023-03-23 05:14:59 +00:00
|
|
|
UsedQuoteInvestment: fixedpoint.Zero,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:36:30 +00:00
|
|
|
log.Infof("loaded spot position: %s", s.SpotPosition.String())
|
|
|
|
log.Infof("loaded futures position: %s", s.FuturesPosition.String())
|
|
|
|
|
2023-03-22 16:40:20 +00:00
|
|
|
binanceFutures := s.futuresSession.Exchange.(*binance.Exchange)
|
|
|
|
binanceSpot := s.spotSession.Exchange.(*binance.Exchange)
|
|
|
|
_ = binanceSpot
|
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
s.spotOrderExecutor = s.allocateOrderExecutor(ctx, s.spotSession, instanceID, s.SpotPosition)
|
2023-03-22 16:23:51 +00:00
|
|
|
s.spotOrderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
|
|
|
|
// we act differently on the spot account
|
|
|
|
// when opening a position, we place orders on the spot account first, then the futures account,
|
|
|
|
// and we need to accumulate the used quote amount
|
|
|
|
//
|
|
|
|
// when closing a position, we place orders on the futures account first, then the spot account
|
|
|
|
// we need to close the position according to its base quantity instead of quote quantity
|
|
|
|
if s.positionType == types.PositionShort {
|
|
|
|
switch s.positionAction {
|
|
|
|
case PositionOpening:
|
2023-03-23 04:58:10 +00:00
|
|
|
if trade.Side != types.SideTypeBuy {
|
|
|
|
log.Errorf("unexpected trade side: %+v, expecting BUY trade", trade)
|
2023-03-22 16:23:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
s.State.UsedQuoteInvestment = s.State.UsedQuoteInvestment.Add(trade.QuoteQuantity)
|
|
|
|
if s.State.UsedQuoteInvestment.Compare(s.QuoteInvestment) >= 0 {
|
2023-03-22 16:23:51 +00:00
|
|
|
s.positionAction = PositionNoOp
|
2023-03-22 16:40:20 +00:00
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-22 16:40:20 +00:00
|
|
|
// 1) if we have trade, try to query the balance and transfer the balance to the futures wallet account
|
2023-03-23 04:58:10 +00:00
|
|
|
// TODO: handle missing trades here. If the process crashed during the transfer, how to recover?
|
|
|
|
if err := backoff.RetryGeneric(ctx, func() error {
|
|
|
|
return s.transferIn(ctx, binanceSpot, trade)
|
|
|
|
}); err != nil {
|
2023-03-23 05:14:59 +00:00
|
|
|
log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
|
2023-03-23 04:58:10 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-22 16:40:20 +00:00
|
|
|
// 2) transferred successfully, sync futures position
|
2023-03-23 04:58:10 +00:00
|
|
|
// compare spot position and futures position, increase the position size until they are the same size
|
2023-03-22 16:23:51 +00:00
|
|
|
|
|
|
|
case PositionClosing:
|
2023-03-23 04:58:10 +00:00
|
|
|
if trade.Side != types.SideTypeSell {
|
|
|
|
log.Errorf("unexpected trade side: %+v, expecting SELL trade", trade)
|
2023-03-22 16:23:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
s.futuresOrderExecutor = s.allocateOrderExecutor(ctx, s.futuresSession, instanceID, s.FuturesPosition)
|
2023-03-22 14:15:01 +00:00
|
|
|
|
|
|
|
s.futuresSession.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(kline types.KLine) {
|
2023-03-23 04:58:10 +00:00
|
|
|
// s.queryAndDetectPremiumIndex(ctx, binanceFutures)
|
2023-03-22 14:15:01 +00:00
|
|
|
}))
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
s.queryAndDetectPremiumIndex(ctx, binanceFutures)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TODO: use go routine and time.Ticker to trigger spot sync and futures sync
|
|
|
|
/*
|
|
|
|
s.spotSession.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(k types.KLine) {
|
|
|
|
}))
|
|
|
|
*/
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
func (s *Strategy) queryAndDetectPremiumIndex(ctx context.Context, binanceFutures *binance.Exchange) {
|
|
|
|
premiumIndex, err := binanceFutures.QueryPremiumIndex(ctx, s.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("premium index query error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("premiumIndex: %+v", premiumIndex)
|
|
|
|
|
|
|
|
if changed := s.detectPremiumIndex(premiumIndex); changed {
|
|
|
|
log.Infof("position action: %s %s", s.positionType, s.positionAction.String())
|
|
|
|
s.triggerPositionAction(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:40:20 +00:00
|
|
|
// TODO: replace type binance.Exchange with an interface
|
|
|
|
func (s *Strategy) transferIn(ctx context.Context, ex *binance.Exchange, trade types.Trade) error {
|
2023-03-23 04:58:10 +00:00
|
|
|
currency := s.spotMarket.BaseCurrency
|
|
|
|
|
|
|
|
// base asset needs BUY trades
|
|
|
|
if trade.Side == types.SideTypeSell {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:40:20 +00:00
|
|
|
balances, err := ex.QueryAccountBalances(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
b, ok := balances[currency]
|
2023-03-22 16:40:20 +00:00
|
|
|
if !ok {
|
2023-03-23 04:58:10 +00:00
|
|
|
return fmt.Errorf("%s balance not found", currency)
|
2023-03-22 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
// TODO: according to the fee, we might not be able to get enough balance greater than the trade quantity, we can adjust the quantity here
|
2023-03-23 05:14:59 +00:00
|
|
|
if b.Available.Compare(trade.Quantity) < 0 {
|
|
|
|
log.Infof("adding to pending base transfer: %s %s", trade.Quantity, currency)
|
|
|
|
s.State.PendingBaseTransfer = s.State.PendingBaseTransfer.Add(trade.Quantity)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
amount := s.State.PendingBaseTransfer.Add(trade.Quantity)
|
|
|
|
|
2023-03-23 06:46:02 +00:00
|
|
|
pos := s.SpotPosition.GetBase()
|
|
|
|
rest := pos.Sub(s.State.TotalBaseTransfer)
|
|
|
|
|
|
|
|
if rest.Sign() < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
amount = fixedpoint.Min(rest, amount)
|
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
log.Infof("transfering futures account asset %s %s", amount, currency)
|
|
|
|
if err := ex.TransferFuturesAccountAsset(ctx, currency, amount, types.TransferIn); err != nil {
|
|
|
|
return err
|
2023-03-22 16:40:20 +00:00
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 06:46:02 +00:00
|
|
|
// reset pending transfer
|
2023-03-23 05:14:59 +00:00
|
|
|
s.State.PendingBaseTransfer = fixedpoint.Zero
|
2023-03-23 06:46:02 +00:00
|
|
|
|
|
|
|
// record the transfer in the total base transfer
|
|
|
|
s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Add(amount)
|
2023-03-22 16:40:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
func (s *Strategy) triggerPositionAction(ctx context.Context) {
|
|
|
|
switch s.positionAction {
|
|
|
|
case PositionOpening:
|
2023-03-23 05:02:22 +00:00
|
|
|
s.increaseSpotPosition(ctx)
|
2023-03-23 04:58:10 +00:00
|
|
|
s.syncFuturesPosition(ctx)
|
|
|
|
case PositionClosing:
|
2023-03-23 05:02:22 +00:00
|
|
|
s.reduceFuturesPosition(ctx)
|
2023-03-23 04:58:10 +00:00
|
|
|
s.syncSpotPosition(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
func (s *Strategy) reduceFuturesPosition(ctx context.Context) {
|
|
|
|
switch s.positionAction {
|
|
|
|
case PositionOpening, PositionNoOp:
|
2023-03-23 04:58:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
futuresBase := s.FuturesPosition.GetBase() // should be negative base quantity here
|
|
|
|
|
|
|
|
if futuresBase.Sign() > 0 {
|
|
|
|
// unexpected error
|
|
|
|
log.Errorf("unexpected futures position (got positive, expecting negative)")
|
2023-03-23 05:02:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 05:07:54 +00:00
|
|
|
_ = s.futuresOrderExecutor.GracefulCancel(ctx)
|
|
|
|
|
|
|
|
ticker, err := s.futuresSession.Exchange.QueryTicker(ctx, s.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not query ticker")
|
2023-03-23 05:02:22 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
if futuresBase.Compare(fixedpoint.Zero) < 0 {
|
|
|
|
orderPrice := ticker.Sell
|
|
|
|
|
|
|
|
orderQuantity := futuresBase.Abs()
|
|
|
|
orderQuantity = fixedpoint.Max(orderQuantity, s.futuresMarket.MinQuantity)
|
|
|
|
orderQuantity = s.futuresMarket.AdjustQuantityByMinNotional(orderQuantity, orderPrice)
|
|
|
|
if s.futuresMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
|
|
|
log.Infof("skip futures order with dust quantity %s, market = %+v", orderQuantity.String(), s.futuresMarket)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createdOrders, err := s.futuresOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Quantity: orderQuantity,
|
|
|
|
Price: orderPrice,
|
|
|
|
Market: s.futuresMarket,
|
|
|
|
ReduceOnly: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not submit order")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("created orders: %+v", createdOrders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncFuturesPosition syncs the futures position with the given spot position
|
|
|
|
func (s *Strategy) syncFuturesPosition(ctx context.Context) {
|
|
|
|
if s.positionType != types.PositionShort {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch s.positionAction {
|
|
|
|
case PositionClosing:
|
|
|
|
return
|
|
|
|
case PositionOpening, PositionNoOp:
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
spotBase := s.SpotPosition.GetBase() // should be positive base quantity here
|
|
|
|
futuresBase := s.FuturesPosition.GetBase() // should be negative base quantity here
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 09:36:30 +00:00
|
|
|
if spotBase.IsZero() || spotBase.Sign() < 0 {
|
2023-03-23 05:02:22 +00:00
|
|
|
// skip when spot base is zero
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
log.Infof("position comparision: %s (spot) <=> %s (futures)", spotBase.String(), futuresBase.String())
|
|
|
|
|
|
|
|
if futuresBase.Sign() > 0 {
|
|
|
|
// unexpected error
|
|
|
|
log.Errorf("unexpected futures position (got positive, expecting negative)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
_ = s.futuresOrderExecutor.GracefulCancel(ctx)
|
|
|
|
|
|
|
|
ticker, err := s.futuresSession.Exchange.QueryTicker(ctx, s.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not query ticker")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
// compare with the spot position and increase the position
|
|
|
|
quoteValue, err := bbgo.CalculateQuoteQuantity(ctx, s.futuresSession, s.futuresMarket.QuoteCurrency, s.Leverage)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not calculate futures account quote value")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Infof("calculated futures account quote value = %s", quoteValue.String())
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 09:36:30 +00:00
|
|
|
// max futures base position (without negative sign)
|
|
|
|
maxFuturesBasePosition := fixedpoint.Min(
|
|
|
|
spotBase.Mul(s.Leverage),
|
|
|
|
s.State.TotalBaseTransfer.Mul(s.Leverage))
|
|
|
|
|
|
|
|
// if - futures position < max futures position, increase it
|
|
|
|
if futuresBase.Neg().Compare(maxFuturesBasePosition) < 0 {
|
2023-03-23 05:02:22 +00:00
|
|
|
orderPrice := ticker.Sell
|
2023-03-23 09:36:30 +00:00
|
|
|
diffQuantity := maxFuturesBasePosition.Sub(futuresBase.Neg())
|
|
|
|
|
|
|
|
if diffQuantity.Sign() < 0 {
|
|
|
|
log.Errorf("unexpected negative position diff: %s", diffQuantity.String())
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
log.Infof("position diff quantity: %s", diffQuantity.String())
|
|
|
|
|
|
|
|
orderQuantity := fixedpoint.Max(diffQuantity, s.futuresMarket.MinQuantity)
|
2023-03-23 09:36:30 +00:00
|
|
|
orderQuantity = s.futuresMarket.AdjustQuantityByMinNotional(orderQuantity, orderPrice)
|
2023-03-23 05:02:22 +00:00
|
|
|
if s.futuresMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
|
|
|
log.Infof("skip futures order with dust quantity %s, market = %+v", orderQuantity.String(), s.futuresMarket)
|
2023-03-23 04:58:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
createdOrders, err := s.futuresOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Quantity: orderQuantity,
|
|
|
|
Price: orderPrice,
|
|
|
|
Market: s.futuresMarket,
|
|
|
|
// TimeInForce: types.TimeInForceGTC,
|
|
|
|
})
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
if err != nil {
|
2023-03-23 05:02:22 +00:00
|
|
|
log.WithError(err).Errorf("can not submit order")
|
2023-03-23 04:58:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
log.Infof("created orders: %+v", createdOrders)
|
2023-03-23 04:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:23:51 +00:00
|
|
|
func (s *Strategy) syncSpotPosition(ctx context.Context) {
|
2023-03-23 05:02:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) increaseSpotPosition(ctx context.Context) {
|
2023-03-22 16:23:51 +00:00
|
|
|
if s.positionType != types.PositionShort {
|
|
|
|
log.Errorf("funding long position type is not supported")
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 06:48:24 +00:00
|
|
|
if s.positionAction != PositionOpening {
|
|
|
|
return
|
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
if s.State.UsedQuoteInvestment.Compare(s.QuoteInvestment) >= 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 09:36:30 +00:00
|
|
|
_ = s.spotOrderExecutor.GracefulCancel(ctx)
|
|
|
|
|
|
|
|
ticker, err := s.spotSession.Exchange.QueryTicker(ctx, s.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not query ticker")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
leftQuota := s.QuoteInvestment.Sub(s.State.UsedQuoteInvestment)
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
orderPrice := ticker.Buy
|
|
|
|
orderQuantity := fixedpoint.Min(s.IncrementalQuoteQuantity, leftQuota).Div(orderPrice)
|
2023-03-23 09:36:30 +00:00
|
|
|
|
|
|
|
log.Infof("initial spot order quantity %s", orderQuantity.String())
|
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
orderQuantity = fixedpoint.Max(orderQuantity, s.spotMarket.MinQuantity)
|
2023-03-23 09:36:30 +00:00
|
|
|
orderQuantity = s.spotMarket.AdjustQuantityByMinNotional(orderQuantity, orderPrice)
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 09:36:30 +00:00
|
|
|
if s.spotMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
submitOrder := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Quantity: orderQuantity,
|
|
|
|
Price: orderPrice,
|
|
|
|
Market: s.spotMarket,
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
log.Infof("placing spot order: %+v", submitOrder)
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 06:48:24 +00:00
|
|
|
createdOrders, err := s.spotOrderExecutor.SubmitOrders(ctx, submitOrder)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not submit order")
|
|
|
|
return
|
2023-03-22 16:23:51 +00:00
|
|
|
}
|
2023-03-23 06:48:24 +00:00
|
|
|
|
|
|
|
log.Infof("created orders: %+v", createdOrders)
|
2023-03-22 16:23:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) (changed bool) {
|
|
|
|
fundingRate := premiumIndex.LastFundingRate
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
log.Infof("last %s funding rate: %s", s.Symbol, fundingRate.Percentage())
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-22 14:17:37 +00:00
|
|
|
if s.ShortFundingRate != nil {
|
|
|
|
if fundingRate.Compare(s.ShortFundingRate.High) >= 0 {
|
2023-03-23 09:36:30 +00:00
|
|
|
|
|
|
|
log.Infof("funding rate %s is higher than the High threshold %s, start opening position...",
|
|
|
|
fundingRate.Percentage(), s.ShortFundingRate.High.Percentage())
|
|
|
|
|
2023-03-22 14:17:37 +00:00
|
|
|
s.positionAction = PositionOpening
|
|
|
|
s.positionType = types.PositionShort
|
2023-03-23 04:58:10 +00:00
|
|
|
changed = true
|
2023-03-22 14:17:37 +00:00
|
|
|
} else if fundingRate.Compare(s.ShortFundingRate.Low) <= 0 {
|
|
|
|
s.positionAction = PositionClosing
|
2023-03-23 04:58:10 +00:00
|
|
|
changed = true
|
2023-03-22 14:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
|
|
|
return changed
|
2023-03-22 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
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)
|
2023-03-23 04:58:10 +00:00
|
|
|
orderExecutor.SetMaxRetries(0)
|
2023-03-22 13:36:42 +00:00
|
|
|
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
|
|
|
}
|