diff --git a/config/grid2-max.yaml b/config/grid2-max.yaml index 596d0069f..3c55294d8 100644 --- a/config/grid2-max.yaml +++ b/config/grid2-max.yaml @@ -11,7 +11,8 @@ notifications: sessions: max: exchange: max - envVarPrefix: max + envVarPrefix: MAX + # example command: # godotenv -f .env.local -- go run ./cmd/bbgo backtest --config config/grid2-max.yaml --base-asset-baseline diff --git a/pkg/strategy/xfunding/strategy.go b/pkg/strategy/xfunding/strategy.go index b0654e79e..977b2bdd1 100644 --- a/pkg/strategy/xfunding/strategy.go +++ b/pkg/strategy/xfunding/strategy.go @@ -108,6 +108,7 @@ type Strategy struct { type State struct { PendingBaseTransfer fixedpoint.Value `json:"pendingBaseTransfer"` + TotalBaseTransfer fixedpoint.Value `json:"totalBaseTransfer"` UsedQuoteInvestment fixedpoint.Value `json:"usedQuoteInvestment"` } @@ -238,6 +239,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order if s.State == nil { s.State = &State{ PendingBaseTransfer: fixedpoint.Zero, + TotalBaseTransfer: fixedpoint.Zero, UsedQuoteInvestment: fixedpoint.Zero, } } @@ -364,12 +366,25 @@ func (s *Strategy) transferIn(ctx context.Context, ex *binance.Exchange, trade t amount := s.State.PendingBaseTransfer.Add(trade.Quantity) + pos := s.SpotPosition.GetBase() + rest := pos.Sub(s.State.TotalBaseTransfer) + + if rest.Sign() < 0 { + return nil + } + + amount = fixedpoint.Min(rest, amount) + log.Infof("transfering futures account asset %s %s", amount, currency) if err := ex.TransferFuturesAccountAsset(ctx, currency, amount, types.TransferIn); err != nil { return err } + // reset pending transfer s.State.PendingBaseTransfer = fixedpoint.Zero + + // record the transfer in the total base transfer + s.State.TotalBaseTransfer = s.State.TotalBaseTransfer.Add(amount) return nil }