mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-13 02:23:51 +00:00
support specifying session in sync symbol
This commit is contained in:
parent
8d9e63671e
commit
b1873aa19b
|
@ -208,12 +208,53 @@ func GetNativeBuildTargetConfig() BuildTargetConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SyncSymbol struct {
|
||||||
|
Symbol string `json:"symbol" yaml:"symbol"`
|
||||||
|
Session string `json:"session" yaml:"session"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss *SyncSymbol) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) {
|
||||||
|
var s string
|
||||||
|
if err = unmarshal(&s); err == nil {
|
||||||
|
aa := strings.SplitN(s, ":", 2)
|
||||||
|
if len(aa) > 1 {
|
||||||
|
ss.Session = aa[0]
|
||||||
|
ss.Symbol = aa[1]
|
||||||
|
} else {
|
||||||
|
ss.Symbol = aa[0]
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type localSyncSymbol SyncSymbol
|
||||||
|
var ssNew localSyncSymbol
|
||||||
|
if err = unmarshal(&ssNew); err == nil {
|
||||||
|
*ss = SyncSymbol(ssNew)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func categorizeSyncSymbol(slice []SyncSymbol) (map[string][]string, []string) {
|
||||||
|
var rest []string
|
||||||
|
var m = make(map[string][]string)
|
||||||
|
for _, ss := range slice {
|
||||||
|
if len(ss.Session) > 0 {
|
||||||
|
m[ss.Session] = append(m[ss.Session], ss.Symbol)
|
||||||
|
} else {
|
||||||
|
rest = append(rest, ss.Symbol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, rest
|
||||||
|
}
|
||||||
|
|
||||||
type SyncConfig struct {
|
type SyncConfig struct {
|
||||||
// Sessions to sync, if ignored, all defined sessions will sync
|
// Sessions to sync, if ignored, all defined sessions will sync
|
||||||
Sessions []string `json:"sessions,omitempty" yaml:"sessions,omitempty"`
|
Sessions []string `json:"sessions,omitempty" yaml:"sessions,omitempty"`
|
||||||
|
|
||||||
// Symbols is the list of symbol to sync, if ignored, symbols wlll be discovered by your existing crypto balances
|
// Symbols is the list of symbol to sync, if ignored, symbols wlll be discovered by your existing crypto balances
|
||||||
Symbols []string `json:"symbols,omitempty" yaml:"symbols,omitempty"`
|
Symbols []SyncSymbol `json:"symbols,omitempty" yaml:"symbols,omitempty"`
|
||||||
|
|
||||||
// DepositHistory is for syncing deposit history
|
// DepositHistory is for syncing deposit history
|
||||||
DepositHistory bool `json:"depositHistory" yaml:"depositHistory"`
|
DepositHistory bool `json:"depositHistory" yaml:"depositHistory"`
|
||||||
|
|
|
@ -214,5 +214,52 @@ func TestLoadConfig(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSyncSymbol(t *testing.T) {
|
||||||
|
t.Run("symbol", func(t *testing.T) {
|
||||||
|
var ss []SyncSymbol
|
||||||
|
var err = yaml.Unmarshal([]byte(`- BTCUSDT`), &ss)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, []SyncSymbol{
|
||||||
|
{Symbol: "BTCUSDT"},
|
||||||
|
}, ss)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("session:symbol", func(t *testing.T) {
|
||||||
|
var ss []SyncSymbol
|
||||||
|
var err = yaml.Unmarshal([]byte(`- max:BTCUSDT`), &ss)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, []SyncSymbol{
|
||||||
|
{Session: "max", Symbol: "BTCUSDT"},
|
||||||
|
}, ss)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("object", func(t *testing.T) {
|
||||||
|
var ss []SyncSymbol
|
||||||
|
var err = yaml.Unmarshal([]byte(`- { session: "max", symbol: "BTCUSDT" }`), &ss)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, []SyncSymbol{
|
||||||
|
{Session: "max", Symbol: "BTCUSDT"},
|
||||||
|
}, ss)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_categorizeSyncSymbol(t *testing.T) {
|
||||||
|
var ss []SyncSymbol
|
||||||
|
var err = yaml.Unmarshal([]byte(`
|
||||||
|
- BTCUSDT
|
||||||
|
- ETHUSDT
|
||||||
|
- max:MAXUSDT
|
||||||
|
- max:USDTTWD
|
||||||
|
- binance:BNBUSDT
|
||||||
|
`), &ss)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, ss)
|
||||||
|
|
||||||
|
sm, rest := categorizeSyncSymbol(ss)
|
||||||
|
assert.NotEmpty(t, rest)
|
||||||
|
assert.NotEmpty(t, sm)
|
||||||
|
assert.Equal(t, []string{"MAXUSDT", "USDTTWD"}, sm["max"])
|
||||||
|
assert.Equal(t, []string{"BNBUSDT"}, sm["binance"])
|
||||||
}
|
}
|
||||||
|
|
|
@ -582,7 +582,6 @@ func (environ *Environment) setSyncing(status SyncStatus) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (environ *Environment) syncWithUserConfig(ctx context.Context, userConfig *Config) error {
|
func (environ *Environment) syncWithUserConfig(ctx context.Context, userConfig *Config) error {
|
||||||
syncSymbols := userConfig.Sync.Symbols
|
|
||||||
sessions := environ.sessions
|
sessions := environ.sessions
|
||||||
selectedSessions := userConfig.Sync.Sessions
|
selectedSessions := userConfig.Sync.Sessions
|
||||||
if len(selectedSessions) > 0 {
|
if len(selectedSessions) > 0 {
|
||||||
|
@ -594,7 +593,13 @@ func (environ *Environment) syncWithUserConfig(ctx context.Context, userConfig *
|
||||||
since = userConfig.Sync.Since.Time()
|
since = userConfig.Sync.Since.Time()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
syncSymbolMap, restSymbols := categorizeSyncSymbol(userConfig.Sync.Symbols)
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
|
syncSymbols := restSymbols
|
||||||
|
if ss, ok := syncSymbolMap[session.Name] ; ok {
|
||||||
|
syncSymbols = append(syncSymbols, ss...)
|
||||||
|
}
|
||||||
|
|
||||||
if err := environ.syncSession(ctx, session, syncSymbols...); err != nil {
|
if err := environ.syncSession(ctx, session, syncSymbols...); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,7 +89,9 @@ var SyncCmd = &cobra.Command{
|
||||||
|
|
||||||
if len(symbol) > 0 {
|
if len(symbol) > 0 {
|
||||||
if userConfig.Sync != nil && len(userConfig.Sync.Symbols) > 0 {
|
if userConfig.Sync != nil && len(userConfig.Sync.Symbols) > 0 {
|
||||||
userConfig.Sync.Symbols = []string{symbol}
|
userConfig.Sync.Symbols = []bbgo.SyncSymbol{
|
||||||
|
{Symbol: symbol},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user