mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-24 15:55:14 +00:00
backtest: add fee mode function tests
This commit is contained in:
parent
10ed706ed6
commit
45fb87f2b8
|
@ -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
124
pkg/backtest/fee_test.go
Normal 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)
|
||||
})
|
||||
}
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user