From a900c72032de784385ad49e883d390ab856e51ea Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 22 Aug 2024 11:15:42 +0800 Subject: [PATCH] types/position: drop approximateAverageCost --- pkg/exchange/binance/convert_futures.go | 12 +++++------ pkg/strategy/drift/strategy.go | 4 ++-- pkg/types/position.go | 28 +++++++------------------ 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/pkg/exchange/binance/convert_futures.go b/pkg/exchange/binance/convert_futures.go index 9811e62d2..f40b53255 100644 --- a/pkg/exchange/binance/convert_futures.go +++ b/pkg/exchange/binance/convert_futures.go @@ -2,9 +2,10 @@ package binance import ( "fmt" - "github.com/c9s/bbgo/pkg/exchange/binance/binanceapi" "time" + "github.com/c9s/bbgo/pkg/exchange/binance/binanceapi" + "github.com/adshao/go-binance/v2/futures" "github.com/pkg/errors" @@ -42,11 +43,10 @@ func toGlobalFuturesPositions(futuresPositions []*binanceapi.FuturesAccountPosit retFuturesPositions := make(types.FuturesPositionMap) for _, futuresPosition := range futuresPositions { retFuturesPositions[futuresPosition.Symbol] = types.FuturesPosition{ // TODO: types.FuturesPosition - Isolated: futuresPosition.Isolated, - AverageCost: fixedpoint.MustNewFromString(futuresPosition.EntryPrice), - ApproximateAverageCost: fixedpoint.MustNewFromString(futuresPosition.EntryPrice), - Base: fixedpoint.MustNewFromString(futuresPosition.PositionAmt), - Quote: fixedpoint.MustNewFromString(futuresPosition.Notional), + Isolated: futuresPosition.Isolated, + AverageCost: fixedpoint.MustNewFromString(futuresPosition.EntryPrice), + Base: fixedpoint.MustNewFromString(futuresPosition.PositionAmt), + Quote: fixedpoint.MustNewFromString(futuresPosition.Notional), PositionRisk: &types.PositionRisk{ Leverage: fixedpoint.MustNewFromString(futuresPosition.Leverage), diff --git a/pkg/strategy/drift/strategy.go b/pkg/strategy/drift/strategy.go index cf9c53e8e..fe8e9a19d 100644 --- a/pkg/strategy/drift/strategy.go +++ b/pkg/strategy/drift/strategy.go @@ -854,12 +854,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.highestPrice = 0 s.lowestPrice = 0 } else if s.Position.IsLong() { - s.buyPrice = s.Position.ApproximateAverageCost.Float64() + s.buyPrice = s.Position.AverageCost.Float64() s.sellPrice = 0 s.highestPrice = math.Max(s.buyPrice, s.highestPrice) s.lowestPrice = s.buyPrice } else if s.Position.IsShort() { - s.sellPrice = s.Position.ApproximateAverageCost.Float64() + s.sellPrice = s.Position.AverageCost.Float64() s.buyPrice = 0 s.highestPrice = s.sellPrice if s.lowestPrice == 0 { diff --git a/pkg/types/position.go b/pkg/types/position.go index a573732e5..52cdc8617 100644 --- a/pkg/types/position.go +++ b/pkg/types/position.go @@ -44,10 +44,6 @@ type Position struct { Quote fixedpoint.Value `json:"quote" db:"quote"` AverageCost fixedpoint.Value `json:"averageCost" db:"average_cost"` - // ApproximateAverageCost adds the computed fee in quote in the average cost - // This is used for calculating net profit - ApproximateAverageCost fixedpoint.Value `json:"approximateAverageCost"` - FeeRate *ExchangeFee `json:"feeRate,omitempty"` ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"` @@ -282,10 +278,6 @@ type FuturesPosition struct { Quote fixedpoint.Value `json:"quote"` AverageCost fixedpoint.Value `json:"averageCost"` - // ApproximateAverageCost adds the computed fee in quote in the average cost - // This is used for calculating net profit - ApproximateAverageCost fixedpoint.Value `json:"approximateAverageCost"` - FeeRate *ExchangeFee `json:"feeRate,omitempty"` ExchangeFeeRates map[ExchangeName]ExchangeFee `json:"exchangeFeeRates"` @@ -583,11 +575,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp // convert short position to long position if p.Base.Add(quantity).Sign() > 0 { profit = p.AverageCost.Sub(price).Mul(p.Base.Neg()) - netProfit = p.ApproximateAverageCost.Sub(price).Mul(p.Base.Neg()).Sub(feeInQuote) + netProfit = p.AverageCost.Sub(price).Mul(p.Base.Neg()).Sub(feeInQuote) p.Base = p.Base.Add(quantity) p.Quote = p.Quote.Sub(quoteQuantity) p.AverageCost = price - p.ApproximateAverageCost = price p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) p.OpenedAt = td.Time.Time() return profit, netProfit, true @@ -596,7 +587,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp p.Base = p.Base.Add(quantity) p.Quote = p.Quote.Sub(quoteQuantity) profit = p.AverageCost.Sub(price).Mul(quantity) - netProfit = p.ApproximateAverageCost.Sub(price).Mul(quantity).Sub(feeInQuote) + netProfit = p.AverageCost.Sub(price).Mul(quantity).Sub(feeInQuote) p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) return profit, netProfit, true } @@ -610,11 +601,12 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp // here the case is: base == 0 or base > 0 divisor := p.Base.Add(quantity) - p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base). + + p.AverageCost = p.AverageCost.Mul(p.Base). Add(quoteQuantity). Add(feeInQuote). Div(divisor) - p.AverageCost = p.AverageCost.Mul(p.Base).Add(quoteQuantity).Div(divisor) + p.Base = p.Base.Add(quantity) p.Quote = p.Quote.Sub(quoteQuantity) return fixedpoint.Zero, fixedpoint.Zero, false @@ -625,11 +617,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp // convert long position to short position if p.Base.Compare(quantity) < 0 { profit = price.Sub(p.AverageCost).Mul(p.Base) - netProfit = price.Sub(p.ApproximateAverageCost).Mul(p.Base).Sub(feeInQuote) + netProfit = price.Sub(p.AverageCost).Mul(p.Base).Sub(feeInQuote) p.Base = p.Base.Sub(quantity) p.Quote = p.Quote.Add(quoteQuantity) p.AverageCost = price - p.ApproximateAverageCost = price p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) p.OpenedAt = td.Time.Time() return profit, netProfit, true @@ -637,7 +628,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp p.Base = p.Base.Sub(quantity) p.Quote = p.Quote.Add(quoteQuantity) profit = price.Sub(p.AverageCost).Mul(quantity) - netProfit = price.Sub(p.ApproximateAverageCost).Mul(quantity).Sub(feeInQuote) + netProfit = price.Sub(p.AverageCost).Mul(quantity).Sub(feeInQuote) p.AccumulatedProfit = p.AccumulatedProfit.Add(profit) return profit, netProfit, true } @@ -651,13 +642,10 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp // handling short position, since Base here is negative we need to reverse the sign divisor := quantity.Sub(p.Base) - p.ApproximateAverageCost = p.ApproximateAverageCost.Mul(p.Base.Neg()). - Add(quoteQuantity). - Sub(feeInQuote). - Div(divisor) p.AverageCost = p.AverageCost.Mul(p.Base.Neg()). Add(quoteQuantity). + Sub(feeInQuote). Div(divisor) p.Base = p.Base.Sub(quantity) p.Quote = p.Quote.Add(quoteQuantity)