add notification config

This commit is contained in:
c9s 2020-10-27 08:48:47 +08:00
parent 284a0676f7
commit c315b79bd7
4 changed files with 84 additions and 4 deletions

View File

@ -24,6 +24,27 @@ type ExchangeStrategyMount struct {
Strategy SingleExchangeStrategy Strategy SingleExchangeStrategy
} }
type SlackNotification struct {
DefaultChannel string `json:"defaultChannel,omitempty" yaml:"defaultChannel,omitempty"`
ErrorChannel string `json:"errorChannel,omitempty" yaml:"errorChannel,omitempty"`
}
type NotificationRouting struct {
Trade string `json:"trade,omitempty" yaml:"trade,omitempty"`
Order string `json:"order,omitempty" yaml:"order,omitempty"`
SubmitOrder string `json:"submitOrder,omitempty" yaml:"submitOrder,omitempty"`
PnL string `json:"pnL,omitempty" yaml:"pnL,omitempty"`
}
type Notifications struct {
Slack *SlackNotification `json:"slack,omitempty" yaml:"slack,omitempty"`
SymbolChannels map[string]string `json:"symbolChannels,omitempty" yaml:"symbolChannels,omitempty"`
SessionChannels map[string]string `json:"sessionChannels,omitempty" yaml:"sessionChannels,omitempty"`
Routing *NotificationRouting `json:"routing,omitempty" yaml:"routing,omitempty"`
}
type Session struct { type Session struct {
ExchangeName string `json:"exchange" yaml:"exchange"` ExchangeName string `json:"exchange" yaml:"exchange"`
EnvVarPrefix string `json:"envVarPrefix" yaml:"envVarPrefix"` EnvVarPrefix string `json:"envVarPrefix" yaml:"envVarPrefix"`
@ -32,6 +53,8 @@ type Session struct {
type Config struct { type Config struct {
Imports []string `json:"imports" yaml:"imports"` Imports []string `json:"imports" yaml:"imports"`
Notifications *Notifications `json:"notifications,omitempty" yaml:"notifications,omitempty"`
Sessions map[string]Session `json:"sessions,omitempty" yaml:"sessions,omitempty"` Sessions map[string]Session `json:"sessions,omitempty" yaml:"sessions,omitempty"`
RiskControls *RiskControls `json:"riskControls,omitempty" yaml:"riskControls,omitempty"` RiskControls *RiskControls `json:"riskControls,omitempty" yaml:"riskControls,omitempty"`

View File

@ -34,6 +34,23 @@ func TestLoadConfig(t *testing.T) {
wantErr bool wantErr bool
f func(t *testing.T, config *Config) f func(t *testing.T, config *Config)
}{ }{
{
name: "notification",
args: args{configFile: "testdata/notification.yaml"},
wantErr: false,
f: func(t *testing.T, config *Config) {
assert.NotNil(t, config.Notifications)
assert.NotNil(t, config.Notifications.SessionChannels)
assert.NotNil(t, config.Notifications.SymbolChannels)
assert.Equal(t, map[string]string{
"^BTC": "#btc",
"^ETH": "#eth",
}, config.Notifications.SymbolChannels)
assert.NotNil(t, config.Notifications.Routing)
assert.Equal(t, "#dev-bbgo", config.Notifications.Slack.DefaultChannel)
assert.Equal(t, "#error", config.Notifications.Slack.ErrorChannel)
},
},
{ {
name: "strategy", name: "strategy",
args: args{configFile: "testdata/strategy.yaml"}, args: args{configFile: "testdata/strategy.yaml"},

View File

@ -83,11 +83,11 @@ func (reporter *AverageCostPnLReporter) Run() {
} }
} }
type SymbolChannelRouter struct { type PatternChannelRouter struct {
routes map[*regexp.Regexp]string routes map[*regexp.Regexp]string
} }
func (router *SymbolChannelRouter) RouteSymbols(routes map[string]string) *SymbolChannelRouter { func (router *PatternChannelRouter) RouteSymbols(routes map[string]string) *PatternChannelRouter {
for pattern, channel := range routes { for pattern, channel := range routes {
router.routes[regexp.MustCompile(pattern)] = channel router.routes[regexp.MustCompile(pattern)] = channel
} }
@ -95,9 +95,9 @@ func (router *SymbolChannelRouter) RouteSymbols(routes map[string]string) *Symbo
return router return router
} }
func (router *SymbolChannelRouter) Dispatch(symbol string) (channel string, ok bool) { func (router *PatternChannelRouter) Dispatch(text string) (channel string, ok bool) {
for pattern, channel := range router.routes { for pattern, channel := range router.routes {
if pattern.MatchString(symbol) { if pattern.MatchString(text) {
ok = true ok = true
return channel, ok return channel, ok
} }

40
pkg/bbgo/testdata/notification.yaml vendored Normal file
View File

@ -0,0 +1,40 @@
---
notifications:
slack:
defaultChannel: "#dev-bbgo"
errorChannel: "#error"
# if you want to route channel by symbol
symbolChannels:
"^BTC": "#btc"
"^ETH": "#eth"
# if you want to route channel by exchange session
sessionChannels:
max: "#bbgo-max"
binance: "#bbgo-binance"
# routing rules
routing:
trade: bySymbol
order: bySymbol
submitOrder: byExchange
pnL: "#bbgo-pnl"
sessions:
max:
exchange: max
envVarPrefix: max
binance:
exchange: binance
envVarPrefix: binance
exchangeStrategies:
- on: binance
test:
symbol: "BTCUSDT"
interval: "1m"
baseQuantity: 0.1
minDropPercentage: -0.05