Merge pull request #934 from c9s/fix/pnl-position

pnl: fix nil position point issue
This commit is contained in:
Yo-An Lin 2022-09-12 00:32:17 +08:00 committed by GitHub
commit 5db41f4be2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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