58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
package backtest
|
|
|
|
import (
|
|
"git.qtrade.icu/lychiyu/qbtrade/pkg/fixedpoint"
|
|
"git.qtrade.icu/lychiyu/qbtrade/pkg/qbtrade"
|
|
"git.qtrade.icu/lychiyu/qbtrade/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)
|
|
fee = quoteQuantity.Mul(feeRate)
|
|
return fee, feeCurrency
|
|
}
|
|
|
|
func getFeeModeFunction(feeMode qbtrade.BacktestFeeMode) FeeModeFunction {
|
|
switch feeMode {
|
|
|
|
case qbtrade.BacktestFeeModeNative:
|
|
return feeModeFunctionNative
|
|
|
|
case qbtrade.BacktestFeeModeQuote:
|
|
return feeModeFunctionQuote
|
|
|
|
case qbtrade.BacktestFeeModeToken:
|
|
return feeModeFunctionToken
|
|
|
|
default:
|
|
return feeModeFunctionQuote
|
|
}
|
|
}
|