From 4617245cbf40a408cf77aec93534c6d43037d19d Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 11 Sep 2022 23:18:54 +0800 Subject: [PATCH] pnl: fix nil position point issue --- pkg/accounting/pnl/avg_cost.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/pkg/accounting/pnl/avg_cost.go b/pkg/accounting/pnl/avg_cost.go index f1c9b2182..f592719a7 100644 --- a/pkg/accounting/pnl/avg_cost.go +++ b/pkg/accounting/pnl/avg_cost.go @@ -12,6 +12,7 @@ import ( type AverageCostCalculator struct { TradingFeeCurrency string Market types.Market + ExchangeFee *types.ExchangeFee } func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, currentPrice fixedpoint.Value) *AverageCostPnLReport { @@ -22,12 +23,31 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c var grossProfit = fixedpoint.Zero var grossLoss = fixedpoint.Zero + var position = types.NewPositionFromMarket(c.Market) + + if c.ExchangeFee != nil { + position.SetFeeRate(*c.ExchangeFee) + } else { + makerFeeRate := 0.075 * 0.01 + + if c.Market.QuoteCurrency == "BUSD" { + makerFeeRate = 0 + } + + position.SetFeeRate(types.ExchangeFee{ + // binance vip 0 uses 0.075% + MakerFeeRate: fixedpoint.NewFromFloat(makerFeeRate), + TakerFeeRate: fixedpoint.NewFromFloat(0.075 * 0.01), + }) + } + if len(trades) == 0 { return &AverageCostPnLReport{ Symbol: symbol, Market: c.Market, LastPrice: currentPrice, NumTrades: 0, + Position: position, BuyVolume: bidVolume, SellVolume: askVolume, FeeInUSD: feeUSD, @@ -36,13 +56,6 @@ func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, c var currencyFees = map[string]fixedpoint.Value{} - var position = types.NewPositionFromMarket(c.Market) - position.SetFeeRate(types.ExchangeFee{ - // binance vip 0 uses 0.075% - MakerFeeRate: fixedpoint.NewFromFloat(0.075 * 0.01), - TakerFeeRate: fixedpoint.NewFromFloat(0.075 * 0.01), - }) - // TODO: configure the exchange fee rate here later // position.SetExchangeFeeRate() var totalProfit fixedpoint.Value