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"
|
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"
|
|
|
|
|
2023-03-26 06:54:27 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/batch"
|
2022-01-13 11:35:05 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance"
|
2023-03-26 06:54:27 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/exchange/binance/binanceapi"
|
2022-01-13 11:35:05 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2024-03-06 13:56:32 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/strategy/common"
|
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-25 17:21:20 +00:00
|
|
|
// WIP:
|
|
|
|
// - track fee token price for cost
|
|
|
|
// - buy enough BNB before creating positions
|
|
|
|
// - transfer the rest BNB into the futures account
|
|
|
|
// - add slack notification support
|
|
|
|
// - use neutral position to calculate the position cost
|
|
|
|
// - customize profit stats for this funding fee strategy
|
|
|
|
|
2023-03-22 13:17:33 +00:00
|
|
|
const ID = "xfunding"
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2023-03-23 16:36:28 +00:00
|
|
|
// Position State Transitions:
|
2023-03-23 16:52:36 +00:00
|
|
|
// NoOp -> Opening
|
|
|
|
// Opening -> Ready -> Closing
|
|
|
|
// Closing -> Closed -> Opening
|
2023-06-16 05:03:37 +00:00
|
|
|
//
|
2023-03-23 16:36:28 +00:00
|
|
|
//go:generate stringer -type=PositionState
|
|
|
|
type PositionState int
|
2023-03-22 13:42:06 +00:00
|
|
|
|
2023-03-22 13:38:56 +00:00
|
|
|
const (
|
2023-03-23 16:52:36 +00:00
|
|
|
PositionClosed PositionState = iota
|
2023-03-22 13:38:56 +00:00
|
|
|
PositionOpening
|
2023-03-23 16:52:36 +00:00
|
|
|
PositionReady
|
2023-03-22 13:38:56 +00:00
|
|
|
PositionClosing
|
|
|
|
)
|
|
|
|
|
2023-03-25 17:02:31 +00:00
|
|
|
type MovingAverageConfig 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"`
|
|
|
|
MovingAverageIntervalWindow types.IntervalWindow `json:"movingAverageIntervalWindow"`
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
var log = logrus.WithField("strategy", ID)
|
|
|
|
|
2023-03-25 17:16:54 +00:00
|
|
|
var errNotBinanceExchange = errors.New("not binance exchange, currently only support binance exchange")
|
|
|
|
|
2023-03-25 18:13:22 +00:00
|
|
|
var errDuplicatedFundingFeeTxnId = errors.New("duplicated funding fee txn id")
|
|
|
|
|
2022-01-13 11:35:05 +00:00
|
|
|
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 17:57:43 +00:00
|
|
|
type State struct {
|
|
|
|
PositionStartTime time.Time `json:"positionStartTime"`
|
|
|
|
|
|
|
|
// PositionState is default to NoOp
|
|
|
|
PositionState PositionState
|
|
|
|
|
|
|
|
PendingBaseTransfer fixedpoint.Value `json:"pendingBaseTransfer"`
|
|
|
|
TotalBaseTransfer fixedpoint.Value `json:"totalBaseTransfer"`
|
|
|
|
UsedQuoteInvestment fixedpoint.Value `json:"usedQuoteInvestment"`
|
|
|
|
}
|
|
|
|
|
2023-03-26 06:44:18 +00:00
|
|
|
func newState() *State {
|
|
|
|
return &State{
|
|
|
|
PositionState: PositionClosed,
|
|
|
|
PendingBaseTransfer: fixedpoint.Zero,
|
|
|
|
TotalBaseTransfer: fixedpoint.Zero,
|
|
|
|
UsedQuoteInvestment: fixedpoint.Zero,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:57:43 +00:00
|
|
|
func (s *State) Reset() {
|
|
|
|
s.PositionState = PositionClosed
|
|
|
|
s.PendingBaseTransfer = fixedpoint.Zero
|
|
|
|
s.TotalBaseTransfer = fixedpoint.Zero
|
|
|
|
s.UsedQuoteInvestment = fixedpoint.Zero
|
|
|
|
}
|
|
|
|
|
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-25 18:09:21 +00:00
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
Interval types.Interval `json:"interval"`
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
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-23 14:36:35 +00:00
|
|
|
MinHoldingPeriod types.Duration `json:"minHoldingPeriod"`
|
|
|
|
|
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
|
|
|
|
2023-03-23 05:14:59 +00:00
|
|
|
SpotSession string `json:"spotSession"`
|
|
|
|
FuturesSession string `json:"futuresSession"`
|
2023-03-29 09:46:54 +00:00
|
|
|
|
|
|
|
// Reset your position info
|
|
|
|
Reset bool `json:"reset"`
|
|
|
|
|
2024-03-06 13:56:32 +00:00
|
|
|
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer"`
|
|
|
|
|
2023-03-29 14:49:34 +00:00
|
|
|
// CloseFuturesPosition can be enabled to close the futures position and then transfer the collateral asset back to the spot account.
|
2023-03-29 09:46:54 +00:00
|
|
|
CloseFuturesPosition bool `json:"closeFuturesPosition"`
|
2023-03-22 13:36:42 +00:00
|
|
|
|
2023-03-25 17:54:39 +00:00
|
|
|
ProfitStats *ProfitStats `persistence:"profit_stats"`
|
2023-03-25 17:07:50 +00:00
|
|
|
|
|
|
|
// SpotPosition is used for the spot position (usually long position)
|
|
|
|
// so that we know how much spot we have bought and the average cost of the spot.
|
|
|
|
SpotPosition *types.Position `persistence:"spot_position"`
|
|
|
|
|
|
|
|
// FuturesPosition is used for the futures position
|
|
|
|
// this position is the reverse side of the spot position, when spot position is long, then the futures position will be short.
|
|
|
|
// but the base quantity should be the same as the spot position
|
|
|
|
FuturesPosition *types.Position `persistence:"futures_position"`
|
|
|
|
|
|
|
|
// NeutralPosition is used for sharing spot/futures position
|
|
|
|
// when creating the spot position and futures position, there will be a spread between the spot position and the futures position.
|
|
|
|
// this neutral position can calculate the spread cost between these two positions
|
|
|
|
NeutralPosition *types.Position `persistence:"neutral_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-26 06:54:27 +00:00
|
|
|
binanceFutures, binanceSpot *binance.Exchange
|
|
|
|
|
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-26 09:44:48 +00:00
|
|
|
|
|
|
|
minQuantity fixedpoint.Value
|
2023-03-23 05:14:59 +00:00
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
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]
|
|
|
|
|
2023-03-25 18:09:21 +00:00
|
|
|
spotSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
|
|
|
futuresSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
|
2023-03-22 13:17:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 16:52:36 +00:00
|
|
|
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {}
|
2022-01-13 11:35:05 +00:00
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
func (s *Strategy) Defaults() error {
|
2023-03-23 14:36:35 +00:00
|
|
|
if s.Leverage.IsZero() {
|
|
|
|
s.Leverage = fixedpoint.One
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.MinHoldingPeriod == 0 {
|
|
|
|
s.MinHoldingPeriod = types.Duration(3 * 24 * time.Hour)
|
|
|
|
}
|
|
|
|
|
2023-03-25 18:09:21 +00:00
|
|
|
if s.Interval == "" {
|
|
|
|
s.Interval = types.Interval1m
|
|
|
|
}
|
|
|
|
|
2023-03-24 06:37:18 +00:00
|
|
|
s.positionType = types.PositionShort
|
|
|
|
|
2023-03-23 04:58:10 +00:00
|
|
|
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 {
|
2023-03-25 17:03:07 +00:00
|
|
|
// standardIndicatorSet := session.StandardIndicatorSet(s.Symbol)
|
|
|
|
/*
|
|
|
|
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-14 08:12:35 +00:00
|
|
|
}
|
2023-03-25 17:03:07 +00:00
|
|
|
*/
|
2022-01-13 11:35:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-22 13:17:33 +00:00
|
|
|
|
2024-03-06 12:44:58 +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-26 06:54:27 +00:00
|
|
|
var ok bool
|
|
|
|
s.binanceFutures, ok = s.futuresSession.Exchange.(*binance.Exchange)
|
2023-03-26 06:44:18 +00:00
|
|
|
if !ok {
|
|
|
|
return errNotBinanceExchange
|
|
|
|
}
|
2023-03-26 06:54:27 +00:00
|
|
|
|
|
|
|
s.binanceSpot, ok = s.spotSession.Exchange.(*binance.Exchange)
|
2023-03-26 06:44:18 +00:00
|
|
|
if !ok {
|
|
|
|
return errNotBinanceExchange
|
|
|
|
}
|
|
|
|
|
2023-03-29 08:59:15 +00:00
|
|
|
if err := s.checkAndFixMarginMode(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:14:29 +00:00
|
|
|
if err := s.setInitialLeverage(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:57:43 +00:00
|
|
|
if s.ProfitStats == nil || s.Reset {
|
2023-03-25 17:54:39 +00:00
|
|
|
s.ProfitStats = &ProfitStats{
|
|
|
|
ProfitStats: types.NewProfitStats(s.Market),
|
|
|
|
// when receiving funding fee, the funding fee asset is the quote currency of that market.
|
|
|
|
FundingFeeCurrency: s.futuresMarket.QuoteCurrency,
|
2023-03-26 06:54:27 +00:00
|
|
|
TotalFundingFee: fixedpoint.Zero,
|
|
|
|
FundingFeeRecords: nil,
|
2023-03-25 17:54:39 +00:00
|
|
|
}
|
2023-03-22 13:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 09:44:48 +00:00
|
|
|
// common min quantity
|
|
|
|
s.minQuantity = fixedpoint.Max(s.futuresMarket.MinQuantity, s.spotMarket.MinQuantity)
|
|
|
|
|
2023-03-25 17:07:50 +00:00
|
|
|
if s.SpotPosition == nil || s.Reset {
|
|
|
|
s.SpotPosition = types.NewPositionFromMarket(s.spotMarket)
|
|
|
|
}
|
|
|
|
|
2023-03-23 17:57:43 +00:00
|
|
|
if s.FuturesPosition == nil || s.Reset {
|
2023-03-22 13:36:42 +00:00
|
|
|
s.FuturesPosition = types.NewPositionFromMarket(s.futuresMarket)
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:07:50 +00:00
|
|
|
if s.NeutralPosition == nil || s.Reset {
|
|
|
|
s.NeutralPosition = types.NewPositionFromMarket(s.futuresMarket)
|
2023-03-22 13:36:42 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 17:57:43 +00:00
|
|
|
if s.State == nil || s.Reset {
|
2023-03-26 06:44:18 +00:00
|
|
|
s.State = newState()
|
2023-03-23 05:14:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:56:32 +00:00
|
|
|
if s.ProfitFixerConfig != nil {
|
|
|
|
log.Infof("profitFixer is enabled, start fixing with config: %+v", s.ProfitFixerConfig)
|
|
|
|
|
|
|
|
s.SpotPosition = types.NewPositionFromMarket(s.spotMarket)
|
|
|
|
s.FuturesPosition = types.NewPositionFromMarket(s.futuresMarket)
|
|
|
|
s.ProfitStats.ProfitStats = types.NewProfitStats(s.Market)
|
|
|
|
|
|
|
|
since := s.ProfitFixerConfig.TradesSince.Time()
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
spotFixer := common.NewProfitFixer()
|
|
|
|
if ss, ok := s.spotSession.Exchange.(types.ExchangeTradeHistoryService); ok {
|
|
|
|
spotFixer.AddExchange(s.spotSession.Name, ss)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err2 := spotFixer.Fix(ctx, s.Symbol,
|
|
|
|
since, now,
|
|
|
|
s.ProfitStats.ProfitStats,
|
|
|
|
s.SpotPosition); err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
futuresFixer := common.NewProfitFixer()
|
|
|
|
if ss, ok := s.futuresSession.Exchange.(types.ExchangeTradeHistoryService); ok {
|
|
|
|
futuresFixer.AddExchange(s.futuresSession.Name, ss)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err2 := futuresFixer.Fix(ctx, s.Symbol,
|
|
|
|
since, now,
|
|
|
|
s.ProfitStats.ProfitStats,
|
|
|
|
s.FuturesPosition); err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
2024-03-06 13:58:58 +00:00
|
|
|
|
|
|
|
bbgo.Notify("Fixed spot position", s.SpotPosition)
|
|
|
|
bbgo.Notify("Fixed futures position", s.FuturesPosition)
|
|
|
|
bbgo.Notify("Fixed profit stats", s.ProfitStats.ProfitStats)
|
2024-03-06 13:56:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 13:41:49 +00:00
|
|
|
if err := s.syncPositionRisks(ctx); err != nil {
|
2023-03-29 10:28:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.CloseFuturesPosition && s.Reset {
|
|
|
|
return errors.New("reset and closeFuturesPosition can not be used together")
|
|
|
|
}
|
|
|
|
|
2023-03-26 08:24:33 +00:00
|
|
|
log.Infof("state: %+v", s.State)
|
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-26 06:54:27 +00:00
|
|
|
log.Infof("loaded neutral position: %s", s.NeutralPosition.String())
|
|
|
|
|
2023-03-29 16:46:41 +00:00
|
|
|
bbgo.Notify("Spot Position", s.SpotPosition)
|
|
|
|
bbgo.Notify("Futures Position", s.FuturesPosition)
|
|
|
|
bbgo.Notify("Neutral Position", s.NeutralPosition)
|
2024-03-06 12:44:58 +00:00
|
|
|
bbgo.Notify("State: %s", s.State.PositionState.String())
|
2023-03-29 16:46:41 +00:00
|
|
|
|
2023-03-26 06:54:27 +00:00
|
|
|
// sync funding fee txns
|
2024-03-07 05:32:50 +00:00
|
|
|
s.syncFundingFeeRecords(ctx, time.Now().Add(-5*24*time.Hour))
|
2023-03-26 07:04:12 +00:00
|
|
|
|
2024-03-07 05:41:06 +00:00
|
|
|
bbgo.Notify(s.ProfitStats)
|
|
|
|
|
2024-03-06 14:04:22 +00:00
|
|
|
switch s.State.PositionState {
|
|
|
|
case PositionClosed:
|
|
|
|
// adjust QuoteInvestment according to the available quote balance
|
|
|
|
// ONLY when the position is not opening
|
|
|
|
if b, ok := s.spotSession.Account.Balance(s.spotMarket.QuoteCurrency); ok {
|
|
|
|
originalQuoteInvestment := s.QuoteInvestment
|
|
|
|
|
|
|
|
// adjust available quote with the fee rate
|
|
|
|
spotFeeRate := 0.075
|
|
|
|
availableQuoteWithoutFee := b.Available.Mul(fixedpoint.NewFromFloat(1.0 - (spotFeeRate * 0.01)))
|
|
|
|
|
|
|
|
s.QuoteInvestment = fixedpoint.Min(availableQuoteWithoutFee, s.QuoteInvestment)
|
|
|
|
|
|
|
|
if originalQuoteInvestment.Compare(s.QuoteInvestment) != 0 {
|
|
|
|
log.Infof("adjusted quoteInvestment from %f to %f according to the balance",
|
|
|
|
originalQuoteInvestment.Float64(),
|
|
|
|
s.QuoteInvestment.Float64(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 14:07:00 +00:00
|
|
|
default:
|
2024-03-06 14:04:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 08:59:10 +00:00
|
|
|
switch s.State.PositionState {
|
2024-03-06 14:07:00 +00:00
|
|
|
case PositionReady:
|
|
|
|
|
2023-07-05 08:59:10 +00:00
|
|
|
case PositionOpening:
|
|
|
|
// transfer all base assets from the spot account into the spot account
|
|
|
|
if err := s.transferIn(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, fixedpoint.Zero); err != nil {
|
|
|
|
log.WithError(err).Errorf("futures asset transfer in error")
|
|
|
|
}
|
|
|
|
|
|
|
|
case PositionClosing, PositionClosed:
|
|
|
|
// transfer all base assets from the futures account back to the spot account
|
|
|
|
if err := s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, fixedpoint.Zero); err != nil {
|
|
|
|
log.WithError(err).Errorf("futures asset transfer out error")
|
|
|
|
}
|
2024-03-06 14:07:00 +00:00
|
|
|
|
2023-07-05 08:59:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
2023-03-23 14:54:42 +00:00
|
|
|
if s.positionType != types.PositionShort {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 16:36:28 +00:00
|
|
|
switch s.State.PositionState {
|
2023-03-23 14:54:42 +00:00
|
|
|
case PositionOpening:
|
|
|
|
if trade.Side != types.SideTypeBuy {
|
|
|
|
log.Errorf("unexpected trade side: %+v, expecting BUY trade", trade)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
s.State.UsedQuoteInvestment = s.State.UsedQuoteInvestment.Add(trade.QuoteQuantity)
|
2023-03-23 18:52:13 +00:00
|
|
|
s.mu.Unlock()
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-23 14:54:42 +00:00
|
|
|
// if we have trade, try to query the balance and transfer the balance to the futures wallet account
|
|
|
|
// TODO: handle missing trades here. If the process crashed during the transfer, how to recover?
|
|
|
|
if err := backoff.RetryGeneral(ctx, func() error {
|
2023-03-26 09:49:33 +00:00
|
|
|
return s.transferIn(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, trade.Quantity)
|
2023-03-23 14:54:42 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
|
|
|
|
return
|
2023-03-22 16:23:51 +00:00
|
|
|
}
|
2023-03-23 14:54:42 +00:00
|
|
|
|
|
|
|
case PositionClosing:
|
|
|
|
if trade.Side != types.SideTypeSell {
|
|
|
|
log.Errorf("unexpected trade side: %+v, expecting SELL trade", trade)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:23:51 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
s.futuresOrderExecutor = s.allocateOrderExecutor(ctx, s.futuresSession, instanceID, s.FuturesPosition)
|
2023-03-23 14:54:42 +00:00
|
|
|
s.futuresOrderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
|
|
|
|
if s.positionType != types.PositionShort {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:52:13 +00:00
|
|
|
switch s.getPositionState() {
|
2023-03-23 14:54:42 +00:00
|
|
|
case PositionClosing:
|
2023-06-16 09:26:14 +00:00
|
|
|
// de-leverage and get the collateral base quantity for transfer
|
|
|
|
quantity := trade.Quantity.Div(s.Leverage)
|
|
|
|
|
2023-03-23 14:54:42 +00:00
|
|
|
if err := backoff.RetryGeneral(ctx, func() error {
|
2023-06-16 09:26:14 +00:00
|
|
|
return s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, quantity)
|
2023-03-23 14:54:42 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
})
|
2023-03-22 14:15:01 +00:00
|
|
|
|
2023-03-25 18:09:21 +00:00
|
|
|
s.futuresSession.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
2023-03-26 06:54:27 +00:00
|
|
|
s.queryAndDetectPremiumIndex(ctx, s.binanceFutures)
|
2023-03-24 18:53:55 +00:00
|
|
|
}))
|
2023-03-24 18:48:27 +00:00
|
|
|
|
2023-03-29 09:46:54 +00:00
|
|
|
s.futuresSession.UserDataStream.OnStart(func() {
|
|
|
|
if s.CloseFuturesPosition {
|
2023-03-29 14:54:54 +00:00
|
|
|
|
|
|
|
openOrders, err := s.futuresSession.Exchange.QueryOpenOrders(ctx, s.Symbol)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("query open orders error")
|
|
|
|
} else {
|
|
|
|
// canceling open orders
|
|
|
|
if err = s.futuresSession.Exchange.CancelOrders(ctx, openOrders...); err != nil {
|
|
|
|
log.WithError(err).Errorf("query open orders error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:46:54 +00:00
|
|
|
if err := s.futuresOrderExecutor.ClosePosition(ctx, fixedpoint.One); err != nil {
|
|
|
|
log.WithError(err).Errorf("close position error")
|
|
|
|
}
|
2023-03-29 13:43:36 +00:00
|
|
|
|
|
|
|
if err := s.resetTransfer(ctx, s.binanceSpot, s.spotMarket.BaseCurrency); err != nil {
|
|
|
|
log.WithError(err).Errorf("transfer error")
|
|
|
|
}
|
2023-03-29 14:55:40 +00:00
|
|
|
|
|
|
|
if err := s.resetTransfer(ctx, s.binanceSpot, s.spotMarket.QuoteCurrency); err != nil {
|
|
|
|
log.WithError(err).Errorf("transfer error")
|
|
|
|
}
|
2023-03-29 09:46:54 +00:00
|
|
|
}
|
2023-03-29 13:43:36 +00:00
|
|
|
|
2023-03-29 09:46:54 +00:00
|
|
|
})
|
|
|
|
|
2023-03-24 18:48:27 +00:00
|
|
|
if binanceStream, ok := s.futuresSession.UserDataStream.(*binance.Stream); ok {
|
|
|
|
binanceStream.OnAccountUpdateEvent(func(e *binance.AccountUpdateEvent) {
|
2023-03-26 08:29:06 +00:00
|
|
|
s.handleAccountUpdate(ctx, e)
|
2023-03-24 18:48:27 +00:00
|
|
|
})
|
|
|
|
}
|
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:
|
2023-03-24 18:53:55 +00:00
|
|
|
s.syncSpotAccount(ctx)
|
2023-03-23 04:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-24 18:53:55 +00:00
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(10 * time.Second)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
s.syncFuturesAccount(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-22 13:36:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-26 08:29:06 +00:00
|
|
|
func (s *Strategy) handleAccountUpdate(ctx context.Context, e *binance.AccountUpdateEvent) {
|
|
|
|
switch e.AccountUpdate.EventReasonType {
|
|
|
|
case binance.AccountUpdateEventReasonDeposit:
|
|
|
|
case binance.AccountUpdateEventReasonWithdraw:
|
|
|
|
case binance.AccountUpdateEventReasonFundingFee:
|
|
|
|
// EventBase:{
|
|
|
|
// Event:ACCOUNT_UPDATE
|
|
|
|
// Time:1679760000932
|
|
|
|
// }
|
|
|
|
// Transaction:1679760000927
|
|
|
|
// AccountUpdate:{
|
|
|
|
// EventReasonType:FUNDING_FEE
|
|
|
|
// Balances:[{
|
|
|
|
// Asset:USDT
|
|
|
|
// WalletBalance:56.64251742
|
|
|
|
// CrossWalletBalance:56.64251742
|
|
|
|
// BalanceChange:-0.00037648
|
|
|
|
// }]
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
for _, b := range e.AccountUpdate.Balances {
|
|
|
|
if b.Asset != s.ProfitStats.FundingFeeCurrency {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-27 06:52:02 +00:00
|
|
|
txnTime := e.EventBase.Time.Time()
|
2023-03-26 08:31:25 +00:00
|
|
|
fee := FundingFee{
|
2023-03-26 08:29:06 +00:00
|
|
|
Asset: b.Asset,
|
|
|
|
Amount: b.BalanceChange,
|
|
|
|
Txn: e.Transaction,
|
|
|
|
Time: txnTime,
|
2023-03-26 08:31:25 +00:00
|
|
|
}
|
2023-09-27 06:52:02 +00:00
|
|
|
err := s.ProfitStats.AddFundingFee(fee)
|
2023-03-26 08:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("unable to add funding fee to profitStats")
|
2023-03-26 08:31:25 +00:00
|
|
|
continue
|
2023-03-26 08:29:06 +00:00
|
|
|
}
|
2023-03-26 08:31:25 +00:00
|
|
|
|
2023-03-29 16:13:02 +00:00
|
|
|
bbgo.Notify(&fee)
|
2023-03-26 08:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("total collected funding fee: %f %s", s.ProfitStats.TotalFundingFee.Float64(), s.ProfitStats.FundingFeeCurrency)
|
|
|
|
bbgo.Sync(ctx, s)
|
2023-03-26 08:31:25 +00:00
|
|
|
|
|
|
|
bbgo.Notify(s.ProfitStats)
|
2023-03-26 08:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-26 06:54:27 +00:00
|
|
|
func (s *Strategy) syncFundingFeeRecords(ctx context.Context, since time.Time) {
|
|
|
|
now := time.Now()
|
2023-03-26 07:04:12 +00:00
|
|
|
|
2023-07-06 08:02:37 +00:00
|
|
|
if since.IsZero() {
|
|
|
|
since = now.AddDate(0, -3, 0)
|
|
|
|
}
|
|
|
|
|
2023-03-26 07:04:12 +00:00
|
|
|
log.Infof("syncing funding fee records from the income history query: %s <=> %s", since, now)
|
|
|
|
|
|
|
|
defer log.Infof("sync funding fee records done")
|
|
|
|
|
2023-03-26 06:54:27 +00:00
|
|
|
q := batch.BinanceFuturesIncomeBatchQuery{
|
|
|
|
BinanceFuturesIncomeHistoryService: s.binanceFutures,
|
|
|
|
}
|
|
|
|
|
|
|
|
dataC, errC := q.Query(ctx, s.Symbol, binanceapi.FuturesIncomeFundingFee, since, now)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
|
2023-03-26 07:04:12 +00:00
|
|
|
case income, ok := <-dataC:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-26 06:54:27 +00:00
|
|
|
log.Infof("income: %+v", income)
|
2023-03-26 07:04:12 +00:00
|
|
|
switch income.IncomeType {
|
|
|
|
case binanceapi.FuturesIncomeFundingFee:
|
|
|
|
err := s.ProfitStats.AddFundingFee(FundingFee{
|
|
|
|
Asset: income.Asset,
|
|
|
|
Amount: income.Income,
|
|
|
|
Txn: income.TranId,
|
|
|
|
Time: income.Time.Time(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("can not add funding fee record to ProfitStats")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case err, ok := <-errC:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2023-03-26 06:54:27 +00:00
|
|
|
|
|
|
|
log.WithError(err).Errorf("unable to query futures income history")
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:09:49 +00:00
|
|
|
log.Info(premiumIndex)
|
2023-03-23 04:58:10 +00:00
|
|
|
|
|
|
|
if changed := s.detectPremiumIndex(premiumIndex); changed {
|
2023-03-23 18:09:49 +00:00
|
|
|
log.Infof("position state changed to -> %s %s", s.positionType, s.State.PositionState.String())
|
2023-03-23 04:58:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 18:53:55 +00:00
|
|
|
func (s *Strategy) syncSpotAccount(ctx context.Context) {
|
2023-03-23 18:52:13 +00:00
|
|
|
switch s.getPositionState() {
|
2023-03-23 04:58:10 +00:00
|
|
|
case PositionOpening:
|
2023-03-23 05:02:22 +00:00
|
|
|
s.increaseSpotPosition(ctx)
|
2023-03-24 18:53:55 +00:00
|
|
|
case PositionClosing:
|
|
|
|
s.syncSpotPosition(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) syncFuturesAccount(ctx context.Context) {
|
|
|
|
switch s.getPositionState() {
|
|
|
|
case PositionOpening:
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
func (s *Strategy) reduceFuturesPosition(ctx context.Context) {
|
2023-03-23 18:52:13 +00:00
|
|
|
if s.notPositionState(PositionClosing) {
|
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-06-16 09:13:42 +00:00
|
|
|
spotBase := s.SpotPosition.GetBase()
|
|
|
|
if !s.spotMarket.IsDustQuantity(spotBase, s.SpotPosition.AverageCost) {
|
|
|
|
if balance, ok := s.futuresSession.Account.Balance(s.futuresMarket.BaseCurrency); ok && balance.Available.Sign() > 0 {
|
|
|
|
if err := backoff.RetryGeneral(ctx, func() error {
|
|
|
|
return s.transferOut(ctx, s.binanceSpot, s.spotMarket.BaseCurrency, balance.Available)
|
|
|
|
}); err != nil {
|
|
|
|
log.WithError(err).Errorf("spot-to-futures transfer in retry failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
if futuresBase.Compare(fixedpoint.Zero) < 0 {
|
2023-03-23 18:52:13 +00:00
|
|
|
orderPrice := ticker.Buy
|
2023-03-23 10:09:16 +00:00
|
|
|
orderQuantity := futuresBase.Abs()
|
2023-06-16 08:18:17 +00:00
|
|
|
orderQuantity = fixedpoint.Max(orderQuantity, s.minQuantity)
|
2023-03-23 10:09:16 +00:00
|
|
|
orderQuantity = s.futuresMarket.AdjustQuantityByMinNotional(orderQuantity, orderPrice)
|
2023-06-16 08:18:17 +00:00
|
|
|
|
2023-03-23 10:09:16 +00:00
|
|
|
if s.futuresMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
2023-06-16 08:18:17 +00:00
|
|
|
submitOrder := types.SubmitOrder{
|
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Price: orderPrice,
|
|
|
|
Market: s.futuresMarket,
|
|
|
|
|
|
|
|
// quantity: Cannot be sent with closePosition=true(Close-All)
|
|
|
|
// reduceOnly: Cannot be sent with closePosition=true
|
|
|
|
ClosePosition: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s.futuresOrderExecutor.SubmitOrders(ctx, submitOrder); err != nil {
|
|
|
|
log.WithError(err).Errorf("can not submit futures order with close position: %+v", submitOrder)
|
|
|
|
}
|
2023-03-23 10:09:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-29 13:46:15 +00:00
|
|
|
submitOrder := types.SubmitOrder{
|
2023-03-23 10:09:16 +00:00
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeBuy,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Quantity: orderQuantity,
|
|
|
|
Price: orderPrice,
|
|
|
|
Market: s.futuresMarket,
|
|
|
|
ReduceOnly: true,
|
2023-03-29 13:46:15 +00:00
|
|
|
}
|
|
|
|
createdOrders, err := s.futuresOrderExecutor.SubmitOrders(ctx, submitOrder)
|
2023-03-23 10:09:16 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-03-29 13:46:15 +00:00
|
|
|
log.WithError(err).Errorf("can not submit futures order: %+v", submitOrder)
|
2023-03-23 10:09:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("created orders: %+v", createdOrders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// syncFuturesPosition syncs the futures position with the given spot position
|
2023-03-23 14:54:42 +00:00
|
|
|
// when the spot is transferred successfully, sync futures position
|
|
|
|
// compare spot position and futures position, increase the position size until they are the same size
|
2023-03-23 10:09:16 +00:00
|
|
|
func (s *Strategy) syncFuturesPosition(ctx context.Context) {
|
|
|
|
if s.positionType != types.PositionShort {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:52:13 +00:00
|
|
|
if s.notPositionState(PositionOpening) {
|
2023-03-23 10:09:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-06-16 09:57:53 +00:00
|
|
|
log.Infof("syncFuturesPosition: position comparision: %s (spot) <=> %s (futures)", spotBase.String(), futuresBase.String())
|
2023-03-23 05:02:22 +00:00
|
|
|
|
|
|
|
if futuresBase.Sign() > 0 {
|
|
|
|
// unexpected error
|
2024-03-06 13:41:49 +00:00
|
|
|
log.Errorf("unexpected futures position, got positive number (long), expecting negative number (short)")
|
2023-03-23 05:02:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-06 13:41:49 +00:00
|
|
|
// cancel the previous futures order
|
2023-03-23 10:09:16 +00:00
|
|
|
_ = s.futuresOrderExecutor.GracefulCancel(ctx)
|
|
|
|
|
2024-03-06 13:41:49 +00:00
|
|
|
// get the latest ticker price
|
2023-03-23 10:09:16 +00:00
|
|
|
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
|
|
|
|
}
|
2024-03-06 13:41:49 +00:00
|
|
|
|
2023-03-23 05:02:22 +00:00
|
|
|
log.Infof("calculated futures account quote value = %s", quoteValue.String())
|
2023-03-29 15:05:31 +00:00
|
|
|
if quoteValue.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
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))
|
|
|
|
|
2023-03-29 15:05:31 +00:00
|
|
|
if maxFuturesBasePosition.IsZero() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:36:30 +00:00
|
|
|
// if - futures position < max futures position, increase it
|
2023-06-16 08:18:17 +00:00
|
|
|
// posDiff := futuresBase.Abs().Sub(maxFuturesBasePosition)
|
|
|
|
if futuresBase.Abs().Compare(maxFuturesBasePosition) >= 0 {
|
2023-03-26 08:24:33 +00:00
|
|
|
s.setPositionState(PositionReady)
|
2023-03-23 09:36:30 +00:00
|
|
|
|
2023-03-27 05:34:24 +00:00
|
|
|
bbgo.Notify("Position Ready")
|
2023-03-26 08:31:25 +00:00
|
|
|
bbgo.Notify("SpotPosition", s.SpotPosition)
|
|
|
|
bbgo.Notify("FuturesPosition", s.FuturesPosition)
|
|
|
|
bbgo.Notify("NeutralPosition", s.NeutralPosition)
|
2023-03-26 08:24:33 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-26 08:24:33 +00:00
|
|
|
orderPrice := ticker.Sell
|
|
|
|
diffQuantity := maxFuturesBasePosition.Sub(futuresBase.Neg())
|
2023-03-23 05:02:22 +00:00
|
|
|
|
2023-03-26 08:24:33 +00:00
|
|
|
if diffQuantity.Sign() < 0 {
|
|
|
|
log.Errorf("unexpected negative position diff: %s", diffQuantity.String())
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-26 08:24:33 +00:00
|
|
|
log.Infof("position diff quantity: %s", diffQuantity.String())
|
2023-03-23 05:02:22 +00:00
|
|
|
|
2023-06-16 07:57:21 +00:00
|
|
|
orderQuantity := diffQuantity
|
2023-06-16 08:18:17 +00:00
|
|
|
orderQuantity = fixedpoint.Max(diffQuantity, s.minQuantity)
|
2023-03-26 08:24:33 +00:00
|
|
|
orderQuantity = s.futuresMarket.AdjustQuantityByMinNotional(orderQuantity, orderPrice)
|
2023-06-16 08:18:17 +00:00
|
|
|
|
2024-03-06 13:41:49 +00:00
|
|
|
if s.futuresMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
|
|
|
log.Warnf("unexpected dust quantity, skip futures order with dust quantity %s, market = %+v", orderQuantity.String(), s.futuresMarket)
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-29 13:46:15 +00:00
|
|
|
submitOrder := types.SubmitOrder{
|
2023-03-26 08:24:33 +00:00
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Quantity: orderQuantity,
|
|
|
|
Price: orderPrice,
|
|
|
|
Market: s.futuresMarket,
|
2023-03-29 13:46:15 +00:00
|
|
|
}
|
|
|
|
createdOrders, err := s.futuresOrderExecutor.SubmitOrders(ctx, submitOrder)
|
2023-03-26 08:24:33 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-03-06 13:41:49 +00:00
|
|
|
log.WithError(err).Errorf("can not submit futures order: %+v", submitOrder)
|
2023-03-26 08:24:33 +00:00
|
|
|
return
|
2023-03-23 04:58:10 +00:00
|
|
|
}
|
2023-03-26 08:24:33 +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 19:14:04 +00:00
|
|
|
if s.positionType != types.PositionShort {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.notPositionState(PositionClosing) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
spotBase := s.SpotPosition.GetBase() // should be positive base quantity here
|
|
|
|
futuresBase := s.FuturesPosition.GetBase() // should be negative base quantity here
|
|
|
|
|
|
|
|
if spotBase.IsZero() {
|
|
|
|
s.setPositionState(PositionClosed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip short spot position
|
|
|
|
if spotBase.Sign() < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-16 09:35:24 +00:00
|
|
|
log.Infof("syncSpotPosition: spot/futures positions: %s (spot) <=> %s (futures)", spotBase.String(), futuresBase.String())
|
2023-03-23 19:14:04 +00:00
|
|
|
|
|
|
|
if futuresBase.Sign() > 0 {
|
|
|
|
// unexpected error
|
|
|
|
log.Errorf("unexpected futures position (got positive, expecting negative)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-16 09:35:24 +00:00
|
|
|
_ = s.spotOrderExecutor.GracefulCancel(ctx)
|
2023-03-23 19:14:04 +00:00
|
|
|
|
|
|
|
ticker, err := s.spotSession.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
|
|
|
|
2023-03-23 19:14:04 +00:00
|
|
|
if s.SpotPosition.IsDust(ticker.Sell) {
|
|
|
|
dust := s.SpotPosition.GetBase().Abs()
|
|
|
|
cost := s.SpotPosition.AverageCost
|
|
|
|
|
|
|
|
log.Warnf("spot dust loss: %f %s (average cost = %f)", dust.Float64(), s.spotMarket.BaseCurrency, cost.Float64())
|
|
|
|
|
|
|
|
s.SpotPosition.Reset()
|
|
|
|
|
|
|
|
s.setPositionState(PositionClosed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// spot pos size > futures pos size ==> reduce spot position
|
|
|
|
if spotBase.Compare(futuresBase.Neg()) > 0 {
|
|
|
|
diffQuantity := spotBase.Sub(futuresBase.Neg())
|
|
|
|
|
|
|
|
if diffQuantity.Sign() < 0 {
|
|
|
|
log.Errorf("unexpected negative position diff: %s", diffQuantity.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
orderPrice := ticker.Sell
|
|
|
|
orderQuantity := diffQuantity
|
2023-06-16 08:21:25 +00:00
|
|
|
b, ok := s.spotSession.Account.Balance(s.spotMarket.BaseCurrency)
|
2023-06-16 09:41:47 +00:00
|
|
|
if !ok {
|
|
|
|
log.Warnf("%s balance not found, can not sync spot position", s.spotMarket.BaseCurrency)
|
|
|
|
return
|
2023-03-23 19:14:04 +00:00
|
|
|
}
|
|
|
|
|
2023-06-16 09:43:57 +00:00
|
|
|
log.Infof("spot balance: %+v", b)
|
|
|
|
|
2023-06-16 09:41:47 +00:00
|
|
|
orderQuantity = fixedpoint.Min(b.Available, orderQuantity)
|
|
|
|
|
2023-03-23 19:14:04 +00:00
|
|
|
// avoid increase the order size
|
|
|
|
if s.spotMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
2023-06-16 08:20:59 +00:00
|
|
|
log.Infof("skip spot order with dust quantity %s, market=%+v balance=%+v", orderQuantity.String(), s.spotMarket, b)
|
2023-03-23 19:14:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-16 09:37:18 +00:00
|
|
|
submitOrder := types.SubmitOrder{
|
2023-03-23 19:14:04 +00:00
|
|
|
Symbol: s.Symbol,
|
|
|
|
Side: types.SideTypeSell,
|
|
|
|
Type: types.OrderTypeLimitMaker,
|
|
|
|
Quantity: orderQuantity,
|
|
|
|
Price: orderPrice,
|
2023-06-16 09:44:27 +00:00
|
|
|
Market: s.spotMarket,
|
2023-06-16 09:37:18 +00:00
|
|
|
}
|
|
|
|
createdOrders, err := s.spotOrderExecutor.SubmitOrders(ctx, submitOrder)
|
2023-03-23 19:14:04 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2023-06-16 09:37:18 +00:00
|
|
|
log.WithError(err).Errorf("can not submit spot order: %+v", submitOrder)
|
2023-03-23 19:14:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("created spot orders: %+v", createdOrders)
|
|
|
|
}
|
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 16:36:28 +00:00
|
|
|
|
2023-03-23 18:52:13 +00:00
|
|
|
if s.notPositionState(PositionOpening) {
|
2023-03-23 06:48:24 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-22 16:23:51 +00:00
|
|
|
|
2023-03-24 06:28:36 +00:00
|
|
|
s.mu.Lock()
|
2023-03-24 18:53:55 +00:00
|
|
|
usedQuoteInvestment := s.State.UsedQuoteInvestment
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
if usedQuoteInvestment.Compare(s.QuoteInvestment) >= 0 {
|
2023-03-26 08:24:33 +00:00
|
|
|
// stop increase the stop position
|
2023-03-23 06:48:24 +00:00
|
|
|
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-24 18:53:55 +00:00
|
|
|
leftQuota := s.QuoteInvestment.Sub(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-26 09:44:48 +00:00
|
|
|
orderQuantity = fixedpoint.Max(orderQuantity, s.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 18:52:13 +00:00
|
|
|
func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) bool {
|
2023-03-23 04:58:10 +00:00
|
|
|
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-23 14:54:42 +00:00
|
|
|
if s.ShortFundingRate == nil {
|
2023-03-23 18:52:13 +00:00
|
|
|
return false
|
2023-03-23 14:54:42 +00:00
|
|
|
}
|
2023-03-23 09:36:30 +00:00
|
|
|
|
2023-03-23 18:52:13 +00:00
|
|
|
switch s.getPositionState() {
|
2023-03-23 10:18:30 +00:00
|
|
|
|
2023-03-23 16:52:36 +00:00
|
|
|
case PositionClosed:
|
2023-03-24 18:56:45 +00:00
|
|
|
if fundingRate.Compare(s.ShortFundingRate.High) < 0 {
|
|
|
|
return false
|
2023-03-23 16:52:36 +00:00
|
|
|
}
|
2023-03-23 14:36:35 +00:00
|
|
|
|
2023-03-24 18:56:45 +00:00
|
|
|
log.Infof("funding rate %s is higher than the High threshold %s, start opening position...",
|
|
|
|
fundingRate.Percentage(), s.ShortFundingRate.High.Percentage())
|
|
|
|
|
|
|
|
s.startOpeningPosition(types.PositionShort, premiumIndex.Time)
|
|
|
|
return true
|
|
|
|
|
2023-03-23 16:52:36 +00:00
|
|
|
case PositionReady:
|
2023-03-24 18:56:45 +00:00
|
|
|
if fundingRate.Compare(s.ShortFundingRate.Low) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("funding rate %s is lower than the Low threshold %s, start closing position...",
|
|
|
|
fundingRate.Percentage(), s.ShortFundingRate.Low.Percentage())
|
2023-03-23 14:54:42 +00:00
|
|
|
|
2023-06-16 05:03:37 +00:00
|
|
|
bbgo.Notify("%s funding rate %s is lower than the Low threshold %s, start closing position...",
|
|
|
|
s.Symbol, fundingRate.Percentage(), s.ShortFundingRate.Low.Percentage())
|
|
|
|
|
2023-03-24 18:56:45 +00:00
|
|
|
holdingPeriod := premiumIndex.Time.Sub(s.State.PositionStartTime)
|
|
|
|
if holdingPeriod < time.Duration(s.MinHoldingPeriod) {
|
|
|
|
log.Warnf("position holding period %s is less than %s, skip closing", holdingPeriod, s.MinHoldingPeriod.Duration())
|
|
|
|
return false
|
2023-03-23 16:52:36 +00:00
|
|
|
}
|
2023-03-24 18:56:45 +00:00
|
|
|
|
|
|
|
s.startClosingPosition()
|
|
|
|
return true
|
2023-03-22 14:17:37 +00:00
|
|
|
}
|
2023-03-23 04:58:10 +00:00
|
|
|
|
2023-03-23 18:52:13 +00:00
|
|
|
return false
|
2023-03-22 14:17:37 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 14:57:13 +00:00
|
|
|
func (s *Strategy) startOpeningPosition(pt types.PositionType, t time.Time) {
|
2023-03-23 16:52:36 +00:00
|
|
|
// only open a new position when there is no position
|
2023-03-23 18:52:13 +00:00
|
|
|
if s.notPositionState(PositionClosed) {
|
2023-03-23 14:58:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:09:49 +00:00
|
|
|
log.Infof("startOpeningPosition")
|
2023-03-23 18:52:13 +00:00
|
|
|
s.setPositionState(PositionOpening)
|
|
|
|
|
2023-03-23 14:57:13 +00:00
|
|
|
s.positionType = pt
|
|
|
|
|
|
|
|
// reset the transfer stats
|
|
|
|
s.State.PositionStartTime = t
|
|
|
|
s.State.PendingBaseTransfer = fixedpoint.Zero
|
|
|
|
s.State.TotalBaseTransfer = fixedpoint.Zero
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) startClosingPosition() {
|
2023-03-23 16:52:36 +00:00
|
|
|
// we can't close a position that is not ready
|
2023-03-23 18:52:13 +00:00
|
|
|
if s.notPositionState(PositionReady) {
|
2023-03-23 14:58:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:09:49 +00:00
|
|
|
log.Infof("startClosingPosition")
|
2023-06-16 05:03:37 +00:00
|
|
|
|
|
|
|
bbgo.Notify("Start to close position", s.FuturesPosition, s.SpotPosition)
|
|
|
|
|
2023-03-23 18:52:13 +00:00
|
|
|
s.setPositionState(PositionClosing)
|
2023-03-23 14:57:13 +00:00
|
|
|
|
|
|
|
// reset the transfer stats
|
|
|
|
s.State.PendingBaseTransfer = fixedpoint.Zero
|
2023-03-23 18:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) setPositionState(state PositionState) {
|
2023-03-24 06:28:36 +00:00
|
|
|
s.mu.Lock()
|
2023-03-23 18:52:13 +00:00
|
|
|
origState := s.State.PositionState
|
|
|
|
s.State.PositionState = state
|
2023-03-24 06:28:36 +00:00
|
|
|
s.mu.Unlock()
|
2023-03-23 18:52:13 +00:00
|
|
|
log.Infof("position state transition: %s -> %s", origState.String(), state.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) isPositionState(state PositionState) bool {
|
2023-03-24 06:28:36 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
ret := s.State.PositionState == state
|
|
|
|
s.mu.Unlock()
|
|
|
|
return ret
|
2023-03-23 18:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) getPositionState() PositionState {
|
|
|
|
return s.State.PositionState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Strategy) notPositionState(state PositionState) bool {
|
2023-03-24 06:28:36 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
ret := s.State.PositionState != state
|
|
|
|
s.mu.Unlock()
|
|
|
|
return ret
|
2023-03-23 14:57:13 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 12:44:58 +00:00
|
|
|
func (s *Strategy) allocateOrderExecutor(
|
|
|
|
ctx context.Context, session *bbgo.ExchangeSession, instanceID string, position *types.Position,
|
|
|
|
) *bbgo.GeneralOrderExecutor {
|
2023-03-22 13:36:42 +00:00
|
|
|
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().OnPositionUpdate(func(position *types.Position) {
|
|
|
|
bbgo.Sync(ctx, s)
|
|
|
|
})
|
2023-03-25 18:16:23 +00:00
|
|
|
orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, _ fixedpoint.Value, _ fixedpoint.Value) {
|
2023-03-25 18:32:21 +00:00
|
|
|
s.ProfitStats.AddTrade(trade)
|
|
|
|
|
2023-03-25 18:16:23 +00:00
|
|
|
if profit, netProfit, madeProfit := s.NeutralPosition.AddTrade(trade); madeProfit {
|
2023-03-25 18:32:21 +00:00
|
|
|
p := s.NeutralPosition.NewProfit(trade, profit, netProfit)
|
|
|
|
s.ProfitStats.AddProfit(p)
|
2023-03-25 18:16:23 +00:00
|
|
|
}
|
|
|
|
})
|
2023-03-22 13:36:42 +00:00
|
|
|
return orderExecutor
|
2023-03-22 13:17:33 +00:00
|
|
|
}
|
2023-03-29 08:59:15 +00:00
|
|
|
|
2023-03-29 09:14:29 +00:00
|
|
|
func (s *Strategy) setInitialLeverage(ctx context.Context) error {
|
2023-03-29 14:49:34 +00:00
|
|
|
log.Infof("setting futures leverage to %d", s.Leverage.Int()+1)
|
|
|
|
|
2023-03-29 09:14:29 +00:00
|
|
|
futuresClient := s.binanceFutures.GetFuturesClient()
|
|
|
|
req := futuresClient.NewFuturesChangeInitialLeverageRequest()
|
|
|
|
req.Symbol(s.Symbol)
|
|
|
|
req.Leverage(s.Leverage.Int() + 1)
|
|
|
|
resp, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("adjusted initial leverage: %+v", resp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 08:59:15 +00:00
|
|
|
func (s *Strategy) checkAndFixMarginMode(ctx context.Context) error {
|
|
|
|
futuresClient := s.binanceFutures.GetFuturesClient()
|
|
|
|
req := futuresClient.NewFuturesGetMultiAssetsModeRequest()
|
|
|
|
resp, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.MultiAssetsMargin {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fixReq := futuresClient.NewFuturesChangeMultiAssetsModeRequest()
|
|
|
|
fixReq.MultiAssetsMargin(binanceapi.MultiAssetsMarginModeOn)
|
|
|
|
fixResp, err := fixReq.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("changeMultiAssetsMode response: %+v", fixResp)
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-29 09:46:54 +00:00
|
|
|
|
2024-03-06 13:41:49 +00:00
|
|
|
func (s *Strategy) syncPositionRisks(ctx context.Context) error {
|
2023-03-29 09:46:54 +00:00
|
|
|
futuresClient := s.binanceFutures.GetFuturesClient()
|
|
|
|
req := futuresClient.NewFuturesGetPositionRisksRequest()
|
|
|
|
req.Symbol(s.Symbol)
|
2023-03-29 10:28:25 +00:00
|
|
|
positionRisks, err := req.Do(ctx)
|
2023-03-29 09:46:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-16 09:57:53 +00:00
|
|
|
log.Infof("fetched futures position risks: %+v", positionRisks)
|
|
|
|
|
2023-06-16 10:08:25 +00:00
|
|
|
if len(positionRisks) == 0 {
|
|
|
|
s.FuturesPosition.Reset()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:28:25 +00:00
|
|
|
for _, positionRisk := range positionRisks {
|
2023-03-29 13:36:29 +00:00
|
|
|
if positionRisk.Symbol != s.Symbol {
|
|
|
|
continue
|
2023-03-29 10:28:25 +00:00
|
|
|
}
|
2023-03-29 13:36:29 +00:00
|
|
|
|
|
|
|
if positionRisk.PositionAmount.IsZero() || positionRisk.EntryPrice.IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.FuturesPosition.Base = positionRisk.PositionAmount
|
|
|
|
s.FuturesPosition.AverageCost = positionRisk.EntryPrice
|
|
|
|
log.Infof("restored futures position from positionRisk: base=%s, average_cost=%s, position_risk=%+v",
|
|
|
|
s.FuturesPosition.Base.String(),
|
|
|
|
s.FuturesPosition.AverageCost.String(),
|
|
|
|
positionRisk)
|
2023-03-29 10:28:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 09:46:54 +00:00
|
|
|
return nil
|
|
|
|
}
|