backtest: add fee mode config

This commit is contained in:
c9s 2022-09-01 13:18:09 +08:00
parent 38dbd17664
commit 3d32faff46
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 26 additions and 6 deletions

View File

@ -56,7 +56,7 @@ type Exchange struct {
currentTime time.Time
account *types.Account
config *bbgo.Backtest
config *bbgo.BackTest
MarketDataStream types.StandardStreamEmitter
@ -74,7 +74,7 @@ type Exchange struct {
Src *ExchangeDataSource
}
func NewExchange(sourceName types.ExchangeName, sourceExchange types.Exchange, srv *service.BacktestService, config *bbgo.Backtest) (*Exchange, error) {
func NewExchange(sourceName types.ExchangeName, sourceExchange types.Exchange, srv *service.BacktestService, config *bbgo.BackTest) (*Exchange, error) {
ex := sourceExchange
markets, err := cache.LoadExchangeMarketsWithCache(context.Background(), ex)

View File

@ -101,7 +101,25 @@ type Session struct {
IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty" yaml:"isolatedMarginSymbol,omitempty"`
}
type Backtest struct {
type BackTestFeeMode string
const (
// BackTestFeeModeQuoteFee is designed for clean position but which also counts the fee in the quote balance.
// buy order = quote currency fee
// sell order = quote currency fee
BackTestFeeModeQuoteFee BackTestFeeMode = "quote_fee"
// BackTestFeeModeNativeFee is the default crypto exchange fee mode.
// buy order = base currency fee
// sell order = quote currency fee
BackTestFeeModeNativeFee BackTestFeeMode = "native"
// BackTestFeeModeFeeToken is the mode which calculates fee from the outside of the balances.
// the fee will not be included in the balances nor the profit.
BackTestFeeModeFeeToken BackTestFeeMode = "fee_token"
)
type BackTest struct {
StartTime types.LooseFormatTime `json:"startTime,omitempty" yaml:"startTime,omitempty"`
EndTime *types.LooseFormatTime `json:"endTime,omitempty" yaml:"endTime,omitempty"`
@ -112,12 +130,14 @@ type Backtest struct {
// Account is deprecated, use Accounts instead
Account map[string]BacktestAccount `json:"account" yaml:"account"`
FeeMode BackTestFeeMode `json:"feeMode" yaml:"feeMode"`
Accounts map[string]BacktestAccount `json:"accounts" yaml:"accounts"`
Symbols []string `json:"symbols" yaml:"symbols"`
Sessions []string `json:"sessions" yaml:"sessions"`
}
func (b *Backtest) GetAccount(n string) BacktestAccount {
func (b *BackTest) GetAccount(n string) BacktestAccount {
accountConfig, ok := b.Accounts[n]
if ok {
return accountConfig
@ -288,7 +308,7 @@ type Config struct {
// Deprecated: use BuildConfig instead
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
Backtest *Backtest `json:"backtest,omitempty" yaml:"backtest,omitempty"`
Backtest *BackTest `json:"backtest,omitempty" yaml:"backtest,omitempty"`
Sync *SyncConfig `json:"sync,omitempty" yaml:"sync,omitempty"`

View File

@ -141,7 +141,7 @@ var BacktestCmd = &cobra.Command{
startTime = userConfig.Backtest.StartTime.Time().Local()
// set default start time to the past 6 months
// userConfig.Backtest.StartTime = now.AddDate(0, -6, 0).Format("2006-01-02")
// userConfig.BackTest.StartTime = now.AddDate(0, -6, 0).Format("2006-01-02")
if userConfig.Backtest.EndTime != nil {
endTime = userConfig.Backtest.EndTime.Time().Local()
} else {