backtest: add fee mode function tests

This commit is contained in:
c9s 2022-09-01 16:01:07 +08:00
parent 10ed706ed6
commit 45fb87f2b8
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 124 additions and 54 deletions

View File

@ -65,19 +65,3 @@ func getFeeModeFunction(feeMode bbgo.BackTestFeeMode) FeeModeFunction {
return feeModeFunctionNative
}
}
func calculateNativeOrderFee(order *types.Order, market types.Market, feeRate fixedpoint.Value) (fee fixedpoint.Value, feeCurrency string) {
switch order.Side {
case types.SideTypeBuy:
fee = order.Quantity.Mul(feeRate)
feeCurrency = market.BaseCurrency
case types.SideTypeSell:
quoteQuantity := order.Quantity.Mul(order.Price)
fee = quoteQuantity.Mul(feeRate)
feeCurrency = market.QuoteCurrency
}
return fee, feeCurrency
}

124
pkg/backtest/fee_test.go Normal file
View File

@ -0,0 +1,124 @@
package backtest
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func Test_feeModeFunctionToken(t *testing.T) {
market := getTestMarket()
t.Run("sellOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := feeModeFunctionToken(&order, &market, feeRate)
assert.Equal(t, "1.5", fee.String())
assert.Equal(t, "FEE", feeCurrency)
})
t.Run("buyOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := feeModeFunctionToken(&order, &market, feeRate)
assert.Equal(t, "1.5", fee.String())
assert.Equal(t, "FEE", feeCurrency)
})
}
func Test_feeModeFunctionQuote(t *testing.T) {
market := getTestMarket()
t.Run("sellOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := feeModeFunctionQuote(&order, &market, feeRate)
assert.Equal(t, "1.5", fee.String())
assert.Equal(t, "USDT", feeCurrency)
})
t.Run("buyOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := feeModeFunctionQuote(&order, &market, feeRate)
assert.Equal(t, "1.5", fee.String())
assert.Equal(t, "USDT", feeCurrency)
})
}
func Test_feeModeFunctionNative(t *testing.T) {
market := getTestMarket()
t.Run("sellOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := feeModeFunctionNative(&order, &market, feeRate)
assert.Equal(t, "1.5", fee.String())
assert.Equal(t, "USDT", feeCurrency)
})
t.Run("buyOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := feeModeFunctionNative(&order, &market, feeRate)
assert.Equal(t, "0.000075", fee.String())
assert.Equal(t, "BTC", feeCurrency)
})
}

View File

@ -497,44 +497,6 @@ func TestSimplePriceMatching_PlaceLimitOrder(t *testing.T) {
assert.Len(t, trades, 4)
}
func Test_calculateNativeOrderFee(t *testing.T) {
market := getTestMarket()
t.Run("sellOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeSell,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := calculateNativeOrderFee(&order, market, feeRate)
assert.Equal(t, "1.5", fee.String())
assert.Equal(t, "USDT", feeCurrency)
})
t.Run("buyOrder", func(t *testing.T) {
order := types.Order{
SubmitOrder: types.SubmitOrder{
Symbol: market.Symbol,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Quantity: fixedpoint.NewFromFloat(0.1),
Price: fixedpoint.NewFromFloat(20000.0),
TimeInForce: types.TimeInForceGTC,
},
}
feeRate := fixedpoint.MustNewFromString("0.075%")
fee, feeCurrency := calculateNativeOrderFee(&order, market, feeRate)
assert.Equal(t, "0.000075", fee.String())
assert.Equal(t, "BTC", feeCurrency)
})
}
func TestSimplePriceMatching_LimitTakerOrder(t *testing.T) {
account := getTestAccount()