bbgo_origin/pkg/backtest/fee.go

58 lines
1.6 KiB
Go
Raw Permalink Normal View History

package backtest
import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type FeeModeFunction func(order *types.Order, market *types.Market, feeRate fixedpoint.Value) (fee fixedpoint.Value, feeCurrency string)
func feeModeFunctionToken(order *types.Order, _ *types.Market, feeRate fixedpoint.Value) (fee fixedpoint.Value, feeCurrency string) {
quoteQuantity := order.Quantity.Mul(order.Price)
feeCurrency = FeeToken
fee = quoteQuantity.Mul(feeRate)
return fee, feeCurrency
}
func feeModeFunctionNative(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
}
func feeModeFunctionQuote(order *types.Order, market *types.Market, feeRate fixedpoint.Value) (fee fixedpoint.Value, feeCurrency string) {
feeCurrency = market.QuoteCurrency
quoteQuantity := order.Quantity.Mul(order.Price)
2022-09-01 11:11:32 +00:00
fee = quoteQuantity.Mul(feeRate)
return fee, feeCurrency
}
2022-09-01 11:17:41 +00:00
func getFeeModeFunction(feeMode bbgo.BacktestFeeMode) FeeModeFunction {
switch feeMode {
2022-09-01 11:17:41 +00:00
case bbgo.BacktestFeeModeNative:
return feeModeFunctionNative
2022-09-01 11:17:41 +00:00
case bbgo.BacktestFeeModeQuote:
return feeModeFunctionQuote
2022-09-01 11:17:41 +00:00
case bbgo.BacktestFeeModeToken:
return feeModeFunctionToken
default:
return feeModeFunctionQuote
}
}