mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
backtest: rename BackTest to Backtest
This commit is contained in:
parent
30742bcf0b
commit
5c8d2a019a
|
@ -56,7 +56,7 @@ type Exchange struct {
|
||||||
currentTime time.Time
|
currentTime time.Time
|
||||||
|
|
||||||
account *types.Account
|
account *types.Account
|
||||||
config *bbgo.BackTest
|
config *bbgo.Backtest
|
||||||
|
|
||||||
MarketDataStream types.StandardStreamEmitter
|
MarketDataStream types.StandardStreamEmitter
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ type Exchange struct {
|
||||||
Src *ExchangeDataSource
|
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
|
ex := sourceExchange
|
||||||
|
|
||||||
markets, err := cache.LoadExchangeMarketsWithCache(context.Background(), ex)
|
markets, err := cache.LoadExchangeMarketsWithCache(context.Background(), ex)
|
||||||
|
|
|
@ -39,16 +39,16 @@ func feeModeFunctionQuote(order *types.Order, market *types.Market, feeRate fixe
|
||||||
return fee, feeCurrency
|
return fee, feeCurrency
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFeeModeFunction(feeMode bbgo.BackTestFeeMode) FeeModeFunction {
|
func getFeeModeFunction(feeMode bbgo.BacktestFeeMode) FeeModeFunction {
|
||||||
switch feeMode {
|
switch feeMode {
|
||||||
|
|
||||||
case bbgo.BackTestFeeModeNative:
|
case bbgo.BacktestFeeModeNative:
|
||||||
return feeModeFunctionNative
|
return feeModeFunctionNative
|
||||||
|
|
||||||
case bbgo.BackTestFeeModeQuote:
|
case bbgo.BacktestFeeModeQuote:
|
||||||
return feeModeFunctionQuote
|
return feeModeFunctionQuote
|
||||||
|
|
||||||
case bbgo.BackTestFeeModeToken:
|
case bbgo.BacktestFeeModeToken:
|
||||||
return feeModeFunctionToken
|
return feeModeFunctionToken
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
// Code generated by "enumer -type=BackTestFeeMode -transform=snake -trimprefix BackTestFeeMode -yaml -json"; DO NOT EDIT.
|
// Code generated by "enumer -type=BacktestFeeMode -transform=snake -trimprefix BacktestFeeMode -yaml -json"; DO NOT EDIT.
|
||||||
|
|
||||||
package bbgo
|
package bbgo
|
||||||
|
|
||||||
|
@ -8,73 +8,73 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const _BackTestFeeModeName = "quotenativetoken"
|
const _BacktestFeeModeName = "quotenativetoken"
|
||||||
|
|
||||||
var _BackTestFeeModeIndex = [...]uint8{0, 5, 11, 16}
|
var _BacktestFeeModeIndex = [...]uint8{0, 5, 11, 16}
|
||||||
|
|
||||||
const _BackTestFeeModeLowerName = "quotenativetoken"
|
const _BacktestFeeModeLowerName = "quotenativetoken"
|
||||||
|
|
||||||
func (i BackTestFeeMode) String() string {
|
func (i BacktestFeeMode) String() string {
|
||||||
if i < 0 || i >= BackTestFeeMode(len(_BackTestFeeModeIndex)-1) {
|
if i < 0 || i >= BacktestFeeMode(len(_BacktestFeeModeIndex)-1) {
|
||||||
return fmt.Sprintf("BackTestFeeMode(%d)", i)
|
return fmt.Sprintf("BacktestFeeMode(%d)", i)
|
||||||
}
|
}
|
||||||
return _BackTestFeeModeName[_BackTestFeeModeIndex[i]:_BackTestFeeModeIndex[i+1]]
|
return _BacktestFeeModeName[_BacktestFeeModeIndex[i]:_BacktestFeeModeIndex[i+1]]
|
||||||
}
|
}
|
||||||
|
|
||||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _BackTestFeeModeNoOp() {
|
func _BacktestFeeModeNoOp() {
|
||||||
var x [1]struct{}
|
var x [1]struct{}
|
||||||
_ = x[BackTestFeeModeQuote-(0)]
|
_ = x[BacktestFeeModeQuote-(0)]
|
||||||
_ = x[BackTestFeeModeNative-(1)]
|
_ = x[BacktestFeeModeNative-(1)]
|
||||||
_ = x[BackTestFeeModeToken-(2)]
|
_ = x[BacktestFeeModeToken-(2)]
|
||||||
}
|
}
|
||||||
|
|
||||||
var _BackTestFeeModeValues = []BackTestFeeMode{BackTestFeeModeQuote, BackTestFeeModeNative, BackTestFeeModeToken}
|
var _BacktestFeeModeValues = []BacktestFeeMode{BacktestFeeModeQuote, BacktestFeeModeNative, BacktestFeeModeToken}
|
||||||
|
|
||||||
var _BackTestFeeModeNameToValueMap = map[string]BackTestFeeMode{
|
var _BacktestFeeModeNameToValueMap = map[string]BacktestFeeMode{
|
||||||
_BackTestFeeModeName[0:5]: BackTestFeeModeQuote,
|
_BacktestFeeModeName[0:5]: BacktestFeeModeQuote,
|
||||||
_BackTestFeeModeLowerName[0:5]: BackTestFeeModeQuote,
|
_BacktestFeeModeLowerName[0:5]: BacktestFeeModeQuote,
|
||||||
_BackTestFeeModeName[5:11]: BackTestFeeModeNative,
|
_BacktestFeeModeName[5:11]: BacktestFeeModeNative,
|
||||||
_BackTestFeeModeLowerName[5:11]: BackTestFeeModeNative,
|
_BacktestFeeModeLowerName[5:11]: BacktestFeeModeNative,
|
||||||
_BackTestFeeModeName[11:16]: BackTestFeeModeToken,
|
_BacktestFeeModeName[11:16]: BacktestFeeModeToken,
|
||||||
_BackTestFeeModeLowerName[11:16]: BackTestFeeModeToken,
|
_BacktestFeeModeLowerName[11:16]: BacktestFeeModeToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
var _BackTestFeeModeNames = []string{
|
var _BacktestFeeModeNames = []string{
|
||||||
_BackTestFeeModeName[0:5],
|
_BacktestFeeModeName[0:5],
|
||||||
_BackTestFeeModeName[5:11],
|
_BacktestFeeModeName[5:11],
|
||||||
_BackTestFeeModeName[11:16],
|
_BacktestFeeModeName[11:16],
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackTestFeeModeString retrieves an enum value from the enum constants string name.
|
// BacktestFeeModeString retrieves an enum value from the enum constants string name.
|
||||||
// Throws an error if the param is not part of the enum.
|
// Throws an error if the param is not part of the enum.
|
||||||
func BackTestFeeModeString(s string) (BackTestFeeMode, error) {
|
func BacktestFeeModeString(s string) (BacktestFeeMode, error) {
|
||||||
if val, ok := _BackTestFeeModeNameToValueMap[s]; ok {
|
if val, ok := _BacktestFeeModeNameToValueMap[s]; ok {
|
||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if val, ok := _BackTestFeeModeNameToValueMap[strings.ToLower(s)]; ok {
|
if val, ok := _BacktestFeeModeNameToValueMap[strings.ToLower(s)]; ok {
|
||||||
return val, nil
|
return val, nil
|
||||||
}
|
}
|
||||||
return 0, fmt.Errorf("%s does not belong to BackTestFeeMode values", s)
|
return 0, fmt.Errorf("%s does not belong to BacktestFeeMode values", s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackTestFeeModeValues returns all values of the enum
|
// BacktestFeeModeValues returns all values of the enum
|
||||||
func BackTestFeeModeValues() []BackTestFeeMode {
|
func BacktestFeeModeValues() []BacktestFeeMode {
|
||||||
return _BackTestFeeModeValues
|
return _BacktestFeeModeValues
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackTestFeeModeStrings returns a slice of all String values of the enum
|
// BacktestFeeModeStrings returns a slice of all String values of the enum
|
||||||
func BackTestFeeModeStrings() []string {
|
func BacktestFeeModeStrings() []string {
|
||||||
strs := make([]string, len(_BackTestFeeModeNames))
|
strs := make([]string, len(_BacktestFeeModeNames))
|
||||||
copy(strs, _BackTestFeeModeNames)
|
copy(strs, _BacktestFeeModeNames)
|
||||||
return strs
|
return strs
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsABackTestFeeMode returns "true" if the value is listed in the enum definition. "false" otherwise
|
// IsABacktestFeeMode returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
func (i BackTestFeeMode) IsABackTestFeeMode() bool {
|
func (i BacktestFeeMode) IsABacktestFeeMode() bool {
|
||||||
for _, v := range _BackTestFeeModeValues {
|
for _, v := range _BacktestFeeModeValues {
|
||||||
if i == v {
|
if i == v {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -82,36 +82,36 @@ func (i BackTestFeeMode) IsABackTestFeeMode() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface for BackTestFeeMode
|
// MarshalJSON implements the json.Marshaler interface for BacktestFeeMode
|
||||||
func (i BackTestFeeMode) MarshalJSON() ([]byte, error) {
|
func (i BacktestFeeMode) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(i.String())
|
return json.Marshal(i.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface for BackTestFeeMode
|
// UnmarshalJSON implements the json.Unmarshaler interface for BacktestFeeMode
|
||||||
func (i *BackTestFeeMode) UnmarshalJSON(data []byte) error {
|
func (i *BacktestFeeMode) UnmarshalJSON(data []byte) error {
|
||||||
var s string
|
var s string
|
||||||
if err := json.Unmarshal(data, &s); err != nil {
|
if err := json.Unmarshal(data, &s); err != nil {
|
||||||
return fmt.Errorf("BackTestFeeMode should be a string, got %s", data)
|
return fmt.Errorf("BacktestFeeMode should be a string, got %s", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
*i, err = BackTestFeeModeString(s)
|
*i, err = BacktestFeeModeString(s)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalYAML implements a YAML Marshaler for BackTestFeeMode
|
// MarshalYAML implements a YAML Marshaler for BacktestFeeMode
|
||||||
func (i BackTestFeeMode) MarshalYAML() (interface{}, error) {
|
func (i BacktestFeeMode) MarshalYAML() (interface{}, error) {
|
||||||
return i.String(), nil
|
return i.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYAML implements a YAML Unmarshaler for BackTestFeeMode
|
// UnmarshalYAML implements a YAML Unmarshaler for BacktestFeeMode
|
||||||
func (i *BackTestFeeMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
func (i *BacktestFeeMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||||
var s string
|
var s string
|
||||||
if err := unmarshal(&s); err != nil {
|
if err := unmarshal(&s); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
*i, err = BackTestFeeModeString(s)
|
*i, err = BacktestFeeModeString(s)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,26 +101,26 @@ type Session struct {
|
||||||
IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty" yaml:"isolatedMarginSymbol,omitempty"`
|
IsolatedMarginSymbol string `json:"isolatedMarginSymbol,omitempty" yaml:"isolatedMarginSymbol,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:generate go run github.com/dmarkham/enumer -type=BackTestFeeMode -transform=snake -trimprefix BackTestFeeMode -yaml -json
|
//go:generate go run github.com/dmarkham/enumer -type=BacktestFeeMode -transform=snake -trimprefix BacktestFeeMode -yaml -json
|
||||||
type BackTestFeeMode int
|
type BacktestFeeMode int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// BackTestFeeModeQuoteFee is designed for clean position but which also counts the fee in the quote balance.
|
// BackTestFeeModeQuoteFee is designed for clean position but which also counts the fee in the quote balance.
|
||||||
// buy order = quote currency fee
|
// buy order = quote currency fee
|
||||||
// sell order = quote currency fee
|
// sell order = quote currency fee
|
||||||
BackTestFeeModeQuote BackTestFeeMode = iota // quote
|
BacktestFeeModeQuote BacktestFeeMode = iota // quote
|
||||||
|
|
||||||
// BackTestFeeModeNativeFee is the default crypto exchange fee mode.
|
// BackTestFeeModeNativeFee is the default crypto exchange fee mode.
|
||||||
// buy order = base currency fee
|
// buy order = base currency fee
|
||||||
// sell order = quote currency fee
|
// sell order = quote currency fee
|
||||||
BackTestFeeModeNative // BackTestFeeMode = "native"
|
BacktestFeeModeNative // BackTestFeeMode = "native"
|
||||||
|
|
||||||
// BackTestFeeModeFeeToken is the mode which calculates fee from the outside of the balances.
|
// 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.
|
// the fee will not be included in the balances nor the profit.
|
||||||
BackTestFeeModeToken // BackTestFeeMode = "token"
|
BacktestFeeModeToken // BackTestFeeMode = "token"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BackTest struct {
|
type Backtest struct {
|
||||||
StartTime types.LooseFormatTime `json:"startTime,omitempty" yaml:"startTime,omitempty"`
|
StartTime types.LooseFormatTime `json:"startTime,omitempty" yaml:"startTime,omitempty"`
|
||||||
EndTime *types.LooseFormatTime `json:"endTime,omitempty" yaml:"endTime,omitempty"`
|
EndTime *types.LooseFormatTime `json:"endTime,omitempty" yaml:"endTime,omitempty"`
|
||||||
|
|
||||||
|
@ -131,14 +131,14 @@ type BackTest struct {
|
||||||
// Account is deprecated, use Accounts instead
|
// Account is deprecated, use Accounts instead
|
||||||
Account map[string]BacktestAccount `json:"account" yaml:"account"`
|
Account map[string]BacktestAccount `json:"account" yaml:"account"`
|
||||||
|
|
||||||
FeeMode BackTestFeeMode `json:"feeMode" yaml:"feeMode"`
|
FeeMode BacktestFeeMode `json:"feeMode" yaml:"feeMode"`
|
||||||
|
|
||||||
Accounts map[string]BacktestAccount `json:"accounts" yaml:"accounts"`
|
Accounts map[string]BacktestAccount `json:"accounts" yaml:"accounts"`
|
||||||
Symbols []string `json:"symbols" yaml:"symbols"`
|
Symbols []string `json:"symbols" yaml:"symbols"`
|
||||||
Sessions []string `json:"sessions" yaml:"sessions"`
|
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]
|
accountConfig, ok := b.Accounts[n]
|
||||||
if ok {
|
if ok {
|
||||||
return accountConfig
|
return accountConfig
|
||||||
|
@ -309,7 +309,7 @@ type Config struct {
|
||||||
// Deprecated: use BuildConfig instead
|
// Deprecated: use BuildConfig instead
|
||||||
Imports []string `json:"imports,omitempty" yaml:"imports,omitempty"`
|
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"`
|
Sync *SyncConfig `json:"sync,omitempty" yaml:"sync,omitempty"`
|
||||||
|
|
||||||
|
|
|
@ -255,10 +255,10 @@ func TestSyncSymbol(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBackTestFeeMode(t *testing.T) {
|
func TestBackTestFeeMode(t *testing.T) {
|
||||||
var mode BackTestFeeMode
|
var mode BacktestFeeMode
|
||||||
var err = yaml.Unmarshal([]byte(`quote`), &mode)
|
var err = yaml.Unmarshal([]byte(`quote`), &mode)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, BackTestFeeModeQuote, mode)
|
assert.Equal(t, BacktestFeeModeQuote, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_categorizeSyncSymbol(t *testing.T) {
|
func Test_categorizeSyncSymbol(t *testing.T) {
|
||||||
|
|
|
@ -141,7 +141,7 @@ var BacktestCmd = &cobra.Command{
|
||||||
startTime = userConfig.Backtest.StartTime.Time().Local()
|
startTime = userConfig.Backtest.StartTime.Time().Local()
|
||||||
|
|
||||||
// set default start time to the past 6 months
|
// 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 {
|
if userConfig.Backtest.EndTime != nil {
|
||||||
endTime = userConfig.Backtest.EndTime.Time().Local()
|
endTime = userConfig.Backtest.EndTime.Time().Local()
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user