Skip to content

Commit

Permalink
xfunding: add mutex protection
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Mar 23, 2023
1 parent 8b87a87 commit 44850e4
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions pkg/strategy/xfunding/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"strings"
"sync"
"time"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -93,6 +94,7 @@ type Strategy struct {
FuturesPosition *types.Position `persistence:"futures_position"`

State *State `persistence:"state"`
mu sync.Mutex

spotSession, futuresSession *bbgo.ExchangeSession
spotOrderExecutor, futuresOrderExecutor *bbgo.GeneralOrderExecutor
Expand Down Expand Up @@ -264,7 +266,9 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
return
}

// TODO: add mutex lock for this modification
s.mu.Lock()
defer s.mu.Unlock()

s.State.UsedQuoteInvestment = s.State.UsedQuoteInvestment.Add(trade.QuoteQuantity)
if s.State.UsedQuoteInvestment.Compare(s.QuoteInvestment) >= 0 {
s.positionAction = PositionNoOp
Expand Down Expand Up @@ -492,43 +496,43 @@ func (s *Strategy) increaseSpotPosition(ctx context.Context) {
log.Errorf("funding long position type is not supported")
return
}
if s.positionAction != PositionOpening {
return
}

switch s.positionAction {
s.mu.Lock()
defer s.mu.Unlock()

case PositionClosing:
// TODO: compare with the futures position and reduce the position
if s.State.UsedQuoteInvestment.Compare(s.QuoteInvestment) >= 0 {
return
}

case PositionOpening:
if s.State.UsedQuoteInvestment.Compare(s.QuoteInvestment) >= 0 {
return
}
leftQuota := s.QuoteInvestment.Sub(s.State.UsedQuoteInvestment)

leftQuote := s.QuoteInvestment.Sub(s.State.UsedQuoteInvestment)
orderPrice := ticker.Buy
orderQuantity := fixedpoint.Min(s.IncrementalQuoteQuantity, leftQuote).Div(orderPrice)
orderQuantity = fixedpoint.Max(orderQuantity, s.spotMarket.MinQuantity)
orderPrice := ticker.Buy
orderQuantity := fixedpoint.Min(s.IncrementalQuoteQuantity, leftQuota).Div(orderPrice)
orderQuantity = fixedpoint.Max(orderQuantity, s.spotMarket.MinQuantity)

_ = s.spotOrderExecutor.GracefulCancel(ctx)
_ = s.spotOrderExecutor.GracefulCancel(ctx)

submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimitMaker,
Quantity: orderQuantity,
Price: orderPrice,
Market: s.spotMarket,
}

log.Infof("placing spot order: %+v", submitOrder)
submitOrder := types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimitMaker,
Quantity: orderQuantity,
Price: orderPrice,
Market: s.spotMarket,
}

createdOrders, err := s.spotOrderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil {
log.WithError(err).Errorf("can not submit order")
return
}
log.Infof("placing spot order: %+v", submitOrder)

log.Infof("created orders: %+v", createdOrders)
createdOrders, err := s.spotOrderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil {
log.WithError(err).Errorf("can not submit order")
return
}

log.Infof("created orders: %+v", createdOrders)
}

func (s *Strategy) detectPremiumIndex(premiumIndex *types.PremiumIndex) (changed bool) {
Expand Down

0 comments on commit 44850e4

Please sign in to comment.