From 890ae86c3db42804a5120bd0187bd3e606582636 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 7 Mar 2024 13:32:50 +0800 Subject: [PATCH] xfunding: improve funding fee guard --- pkg/strategy/xfunding/profitstats.go | 22 ++++++++++++++++------ pkg/strategy/xfunding/strategy.go | 6 +----- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/strategy/xfunding/profitstats.go b/pkg/strategy/xfunding/profitstats.go index 6ae2780b5..0a7700cf2 100644 --- a/pkg/strategy/xfunding/profitstats.go +++ b/pkg/strategy/xfunding/profitstats.go @@ -17,8 +17,13 @@ type ProfitStats struct { FundingFeeCurrency string `json:"fundingFeeCurrency"` TotalFundingFee fixedpoint.Value `json:"totalFundingFee"` FundingFeeRecords []FundingFee `json:"fundingFeeRecords"` - LastFundingFeeTxn int64 `json:"lastFundingFeeTxn"` - LastFundingFeeTime time.Time `json:"lastFundingFeeTime"` + + // Fees map[string] + Last *FundingFee `json:"last"` + + LastFundingFeeTime time.Time `json:"lastFundingFeeTime"` + + txns map[int64]struct{} } func (s *ProfitStats) SlackAttachment() slack.Attachment { @@ -31,24 +36,29 @@ func (s *ProfitStats) SlackAttachment() slack.Attachment { // Pretext: "", // Text: text, Fields: fields, - Footer: fmt.Sprintf("Last Funding Fee Transation ID: %d Last Funding Fee Time %s", s.LastFundingFeeTxn, s.LastFundingFeeTime.Format(time.RFC822)), + Footer: fmt.Sprintf("Last Funding Fee Transation ID: %d Last Funding Fee Time %s", s.Last.Txn, s.Last.Time.Format(time.RFC822)), } } func (s *ProfitStats) AddFundingFee(fee FundingFee) error { + if s.txns == nil { + s.txns = make(map[int64]struct{}) + } + if s.FundingFeeCurrency == "" { s.FundingFeeCurrency = fee.Asset } else if s.FundingFeeCurrency != fee.Asset { return fmt.Errorf("unexpected error, funding fee currency is not matched, given: %s, wanted: %s", fee.Asset, s.FundingFeeCurrency) } - if s.LastFundingFeeTxn == fee.Txn { + if _, ok := s.txns[fee.Txn]; ok { return errDuplicatedFundingFeeTxnId } s.FundingFeeRecords = append(s.FundingFeeRecords, fee) s.TotalFundingFee = s.TotalFundingFee.Add(fee.Amount) - s.LastFundingFeeTxn = fee.Txn - s.LastFundingFeeTime = fee.Time + s.Last = &fee + + s.txns[fee.Txn] = struct{}{} return nil } diff --git a/pkg/strategy/xfunding/strategy.go b/pkg/strategy/xfunding/strategy.go index 50d1b392b..7cb444a75 100644 --- a/pkg/strategy/xfunding/strategy.go +++ b/pkg/strategy/xfunding/strategy.go @@ -298,7 +298,6 @@ func (s *Strategy) CrossRun( FundingFeeCurrency: s.futuresMarket.QuoteCurrency, TotalFundingFee: fixedpoint.Zero, FundingFeeRecords: nil, - LastFundingFeeTime: time.Time{}, } } @@ -379,10 +378,7 @@ func (s *Strategy) CrossRun( bbgo.Notify("State: %s", s.State.PositionState.String()) // sync funding fee txns - s.syncFundingFeeRecords(ctx, s.ProfitStats.LastFundingFeeTime) - - // TEST CODE: - // s.syncFundingFeeRecords(ctx, time.Now().Add(-3*24*time.Hour)) + s.syncFundingFeeRecords(ctx, time.Now().Add(-5*24*time.Hour)) switch s.State.PositionState { case PositionClosed: