mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
fix fee calculation
This commit is contained in:
parent
c8849e76a1
commit
78be592fee
47
bbgo/pnl.go
47
bbgo/pnl.go
|
@ -31,10 +31,11 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
|||
var trades = c.Trades
|
||||
var bidVolume = 0.0
|
||||
var bidAmount = 0.0
|
||||
var bidFee = 0.0
|
||||
|
||||
var askVolume = 0.0
|
||||
var askFee = 0.0
|
||||
|
||||
var feeUSD = 0.0
|
||||
var bidFeeUSD = 0.0
|
||||
var feeRate = 0.0015
|
||||
|
||||
var currencyFees = map[string]float64{}
|
||||
|
@ -49,14 +50,22 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
|||
// since we use USDT as the quote currency, we simply check if it matches the currency symbol
|
||||
if strings.HasPrefix(trade.Symbol, trade.FeeCurrency) {
|
||||
bidVolume -= trade.Fee
|
||||
bidFee += trade.Price * trade.Fee
|
||||
feeUSD += trade.Price * trade.Fee
|
||||
if trade.IsBuyer {
|
||||
bidFeeUSD += trade.Price * trade.Fee
|
||||
}
|
||||
} else if trade.FeeCurrency == "USDT" {
|
||||
bidFee += trade.Fee
|
||||
feeUSD += trade.Fee
|
||||
if trade.IsBuyer {
|
||||
bidFeeUSD += trade.Fee
|
||||
}
|
||||
}
|
||||
|
||||
} else if trade.FeeCurrency == c.TradingFeeCurrency {
|
||||
} else {
|
||||
if trade.FeeCurrency == c.TradingFeeCurrency {
|
||||
bidVolume -= trade.Fee
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := currencyFees[trade.FeeCurrency]; !ok {
|
||||
currencyFees[trade.FeeCurrency] = 0.0
|
||||
|
@ -64,9 +73,9 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
|||
currencyFees[trade.FeeCurrency] += trade.Fee
|
||||
}
|
||||
|
||||
log.Infof("average bid price = (total amount %f + total fee %f) / volume %f", bidAmount, bidFee, bidVolume)
|
||||
log.Infof("average bid price = (total amount %f + total feeUSD %f) / volume %f", bidAmount, bidFeeUSD, bidVolume)
|
||||
profit := 0.0
|
||||
averageBidPrice := (bidAmount + bidFee) / bidVolume
|
||||
averageBidPrice := (bidAmount + bidFeeUSD) / bidVolume
|
||||
|
||||
for _, t := range trades {
|
||||
if t.Symbol != c.Symbol {
|
||||
|
@ -79,19 +88,11 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
|||
|
||||
profit += (t.Price - averageBidPrice) * t.Quantity
|
||||
askVolume += t.Quantity
|
||||
|
||||
// since we use USDT as the quote currency, we simply check if it matches the currency symbol
|
||||
if strings.HasPrefix(t.Symbol, t.FeeCurrency) {
|
||||
askFee += t.Price * t.Fee
|
||||
} else if t.FeeCurrency == "USDT" {
|
||||
askFee += t.Fee
|
||||
}
|
||||
}
|
||||
|
||||
profit -= askFee
|
||||
profit -= feeUSD
|
||||
|
||||
stock := bidVolume - askVolume
|
||||
futureFee := 0.0
|
||||
if stock > 0 {
|
||||
_ = feeRate
|
||||
// stockFee := c.CurrentPrice * feeRate * stock
|
||||
|
@ -99,8 +100,6 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
|||
// futureFee += stockFee
|
||||
}
|
||||
|
||||
fee := bidFee + askFee + futureFee
|
||||
|
||||
return &ProfitAndLossReport{
|
||||
Symbol: c.Symbol,
|
||||
StartTime: c.StartTime,
|
||||
|
@ -112,8 +111,8 @@ func (c *ProfitAndLossCalculator) Calculate() *ProfitAndLossReport {
|
|||
|
||||
Stock: stock,
|
||||
Profit: profit,
|
||||
AverageBidPrice: averageBidPrice,
|
||||
FeeUSD: fee,
|
||||
AverageBidCost: averageBidPrice,
|
||||
FeeUSD: feeUSD,
|
||||
CurrencyFees: currencyFees,
|
||||
}
|
||||
}
|
||||
|
@ -125,7 +124,7 @@ type ProfitAndLossReport struct {
|
|||
|
||||
NumTrades int
|
||||
Profit float64
|
||||
AverageBidPrice float64
|
||||
AverageBidCost float64
|
||||
BidVolume float64
|
||||
AskVolume float64
|
||||
FeeUSD float64
|
||||
|
@ -135,10 +134,11 @@ type ProfitAndLossReport struct {
|
|||
|
||||
func (report ProfitAndLossReport) Print() {
|
||||
log.Infof("trades since: %v", report.StartTime)
|
||||
log.Infof("average bid price: %s", USD.FormatMoneyFloat64(report.AverageBidPrice))
|
||||
log.Infof("average bid cost: %s", USD.FormatMoneyFloat64(report.AverageBidCost))
|
||||
log.Infof("total bid volume: %f", report.BidVolume)
|
||||
log.Infof("total ask volume: %f", report.AskVolume)
|
||||
log.Infof("stock: %f", report.Stock)
|
||||
log.Infof("fee (USD): %f", report.FeeUSD)
|
||||
log.Infof("current price: %s", USD.FormatMoneyFloat64(report.CurrentPrice))
|
||||
log.Infof("profit: %s", USD.FormatMoneyFloat64(report.Profit))
|
||||
log.Infof("currency fees:")
|
||||
|
@ -170,8 +170,9 @@ func (report ProfitAndLossReport) SlackAttachment() slack.Attachment {
|
|||
Fields: []slack.AttachmentField{
|
||||
{Title: "Symbol", Value: report.Symbol, Short: true},
|
||||
{Title: "Profit", Value: USD.FormatMoney(report.Profit), Short: true},
|
||||
{Title: "Fee (USD)", Value: USD.FormatMoney(report.FeeUSD), Short: true},
|
||||
{Title: "Current Price", Value: USD.FormatMoney(report.CurrentPrice), Short: true},
|
||||
{Title: "Average Bid Price", Value: USD.FormatMoney(report.AverageBidPrice), Short: true},
|
||||
{Title: "Average Bid Cost", Value: USD.FormatMoney(report.AverageBidCost), Short: true},
|
||||
{Title: "Number of Trades", Value: strconv.Itoa(report.NumTrades), Short: true},
|
||||
{Title: "Stock", Value: strconv.FormatFloat(report.Stock, 'f', 8, 64), Short: true},
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
Block a user