From 2e8f9c3ad8fd89193048618ad506d6fb05dfaab4 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 12 Jun 2022 03:45:47 +0800 Subject: [PATCH] backtest: fix fee calculation --- pkg/backtest/matching.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index 875a82164..de6857ce2 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -201,11 +201,15 @@ func (m *SimplePriceMatching) executeTrade(trade types.Trade) { var err error // execute trade, update account balances if trade.IsBuyer { - err = m.Account.UseLockedBalance(m.Market.QuoteCurrency, trade.Price.Mul(trade.Quantity)) - m.Account.AddBalance(m.Market.BaseCurrency, trade.Quantity.Sub(trade.Fee.Div(trade.Price))) + err = m.Account.UseLockedBalance(m.Market.QuoteCurrency, trade.QuoteQuantity) + + // here the fee currency is the base currency + m.Account.AddBalance(m.Market.BaseCurrency, trade.Quantity.Sub(trade.Fee)) } else { err = m.Account.UseLockedBalance(m.Market.BaseCurrency, trade.Quantity) - m.Account.AddBalance(m.Market.QuoteCurrency, trade.Quantity.Mul(trade.Price).Sub(trade.Fee)) + + // here the fee currency is the quote currency + m.Account.AddBalance(m.Market.QuoteCurrency, trade.QuoteQuantity.Sub(trade.Fee)) } if err != nil { @@ -237,6 +241,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order *types.Order, isMaker bool price = m.LastPrice } + var quoteQuantity = order.Quantity.Mul(price) var fee fixedpoint.Value var feeCurrency string @@ -247,7 +252,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order *types.Order, isMaker bool feeCurrency = m.Market.BaseCurrency case types.SideTypeSell: - fee = order.Quantity.Mul(price).Mul(feeRate) + fee = quoteQuantity.Mul(feeRate) feeCurrency = m.Market.QuoteCurrency } @@ -262,7 +267,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order *types.Order, isMaker bool Exchange: "backtest", Price: price, Quantity: order.Quantity, - QuoteQuantity: order.Quantity.Mul(price), + QuoteQuantity: quoteQuantity, Symbol: order.Symbol, Side: order.Side, IsBuyer: order.Side == types.SideTypeBuy,