bbgo_origin/pkg/accounting/pnl/avg_cost.go

130 lines
3.0 KiB
Go
Raw Permalink Normal View History

package pnl
import (
2021-02-06 04:32:21 +00:00
"time"
log "github.com/sirupsen/logrus"
2021-12-04 18:16:48 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type AverageCostCalculator struct {
TradingFeeCurrency string
2021-12-04 18:16:48 +00:00
Market types.Market
2022-09-11 15:18:54 +00:00
ExchangeFee *types.ExchangeFee
}
func (c *AverageCostCalculator) Calculate(symbol string, trades []types.Trade, currentPrice fixedpoint.Value) *AverageCostPnLReport {
// copy trades, so that we can truncate it.
var bidVolume = fixedpoint.Zero
var askVolume = fixedpoint.Zero
var feeUSD = fixedpoint.Zero
var grossProfit = fixedpoint.Zero
var grossLoss = fixedpoint.Zero
2022-09-11 15:18:54 +00:00
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),
})
}
2020-10-24 08:32:54 +00:00
if len(trades) == 0 {
return &AverageCostPnLReport{
2021-12-05 17:05:33 +00:00
Symbol: symbol,
Market: c.Market,
LastPrice: currentPrice,
NumTrades: 0,
2022-09-11 15:18:54 +00:00
Position: position,
2021-12-05 17:05:33 +00:00
BuyVolume: bidVolume,
SellVolume: askVolume,
FeeInUSD: feeUSD,
2020-10-24 08:32:54 +00:00
}
}
var currencyFees = map[string]fixedpoint.Value{}
2021-12-04 18:16:48 +00:00
// TODO: configure the exchange fee rate here later
// position.SetExchangeFeeRate()
var totalProfit fixedpoint.Value
var totalNetProfit fixedpoint.Value
var tradeIDs = map[uint64]types.Trade{}
for _, trade := range trades {
if _, exists := tradeIDs[trade.ID]; exists {
log.Warnf("duplicated trade: %+v", trade)
continue
}
if trade.Symbol != symbol {
continue
}
profit, netProfit, madeProfit := position.AddTrade(trade)
if madeProfit {
totalProfit = totalProfit.Add(profit)
totalNetProfit = totalNetProfit.Add(netProfit)
}
if profit.Sign() > 0 {
grossProfit = grossProfit.Add(profit)
} else if profit.Sign() < 0 {
grossLoss = grossLoss.Add(profit)
}
if trade.IsBuyer {
bidVolume = bidVolume.Add(trade.Quantity)
} else {
askVolume = askVolume.Add(trade.Quantity)
}
if _, ok := currencyFees[trade.FeeCurrency]; !ok {
2021-12-04 18:16:48 +00:00
currencyFees[trade.FeeCurrency] = trade.Fee
} else {
currencyFees[trade.FeeCurrency] = currencyFees[trade.FeeCurrency].Add(trade.Fee)
}
tradeIDs[trade.ID] = trade
}
unrealizedProfit := currentPrice.Sub(position.AverageCost).
Mul(position.GetBase())
2022-05-10 10:27:23 +00:00
return &AverageCostPnLReport{
2021-12-05 17:05:33 +00:00
Symbol: symbol,
Market: c.Market,
LastPrice: currentPrice,
NumTrades: len(trades),
StartTime: time.Time(trades[0].Time),
Position: position,
2020-11-10 06:18:27 +00:00
BuyVolume: bidVolume,
SellVolume: askVolume,
2022-06-08 06:51:31 +00:00
BaseAssetPosition: position.GetBase(),
Profit: totalProfit,
NetProfit: totalNetProfit,
UnrealizedProfit: unrealizedProfit,
GrossProfit: grossProfit,
GrossLoss: grossLoss,
AverageCost: position.AverageCost,
FeeInUSD: totalProfit.Sub(totalNetProfit),
CurrencyFees: currencyFees,
}
}