From ea3e9e7d050a270457f3dc177b7253312feddbe3 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 22 Oct 2020 10:54:03 +0800 Subject: [PATCH] add per-session-based trade reporter --- config/bbgo.yaml | 30 +++++++++++++++++++++++++----- pkg/bbgo/environment.go | 6 +++++- pkg/bbgo/notifier.go | 14 ++++++++++++++ pkg/bbgo/session.go | 7 +++++++ pkg/bbgo/trader.go | 15 +-------------- 5 files changed, 52 insertions(+), 20 deletions(-) diff --git a/config/bbgo.yaml b/config/bbgo.yaml index 32e1f7853..98be53316 100644 --- a/config/bbgo.yaml +++ b/config/bbgo.yaml @@ -1,10 +1,30 @@ --- -notifier: +notifications: slack: + defaultChannel: "bbgo" errorChannel: "bbgo-error" -exchange: - binance: - target: binance +reportTrades: + channelBySymbol: + "btcusdt": "bbgo-btcusdt" + "ethusdt": "bbgo-ethusdt" + "bnbusdt": "bbgo-bnbusdt" + "sxpusdt": "bbgo-sxpusdt" + +sessions: max: - target: max + exchange: max + keyVar: MAX_API_KEY + secretVar: MAX_API_SECRET + binance: + exchange: binance + keyVar: BINANCE_API_KEY + secretVar: BINANCE_API_SECRET + +exchangeStrategies: + - on: binance + buyandhold: + symbol: "BTCUSDT" + interval: "1m" + baseQuantity: 0.1 + minDropPercentage: -0.05 diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 9a46565cd..cb661165c 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -146,7 +146,11 @@ func (environ *Environment) Init(ctx context.Context) (err error) { session.markets = markets - if environ.tradeReporter != nil { + if session.tradeReporter != nil { + session.Stream.OnTrade(func(trade types.Trade) { + session.tradeReporter.Report(trade) + }) + } else if environ.tradeReporter != nil { session.Stream.OnTrade(func(trade types.Trade) { environ.tradeReporter.Report(trade) }) diff --git a/pkg/bbgo/notifier.go b/pkg/bbgo/notifier.go index 5c01b9406..82317753e 100644 --- a/pkg/bbgo/notifier.go +++ b/pkg/bbgo/notifier.go @@ -14,3 +14,17 @@ func (n *NullNotifier) NotifyTo(channel, format string, args ...interface{}) err func (n *NullNotifier) Notify(format string, args ...interface{}) error { return nil } + +type Notifiability struct { + notifiers []Notifier +} + +func (m *Notifiability) AddNotifier(notifier Notifier) { + m.notifiers = append(m.notifiers, notifier) +} + +func (m *Notifiability) Notify(msg string, args ...interface{}) { + for _, n := range m.notifiers { + n.NotifyTo("", msg, args...) + } +} diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index ad55f2a1b..5b1b33f90 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -31,6 +31,8 @@ type ExchangeSession struct { Trades map[string][]types.Trade marketDataStores map[string]*store.MarketDataStore + + tradeReporter *TradeReporter } func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession { @@ -63,6 +65,11 @@ func (session *ExchangeSession) Market(symbol string) (market types.Market, ok b return market, ok } +func (session *ExchangeSession) ReportTrade(notifier Notifier) *TradeReporter { + session.tradeReporter = NewTradeReporter(notifier) + return session.tradeReporter +} + // Subscribe save the subscription info, later it will be assigned to the stream func (session *ExchangeSession) Subscribe(channel types.Channel, symbol string, options types.SubscribeOptions) *ExchangeSession { sub := types.Subscription{ diff --git a/pkg/bbgo/trader.go b/pkg/bbgo/trader.go index 073463a45..4d5ae20b9 100644 --- a/pkg/bbgo/trader.go +++ b/pkg/bbgo/trader.go @@ -31,22 +31,9 @@ type CrossExchangeStrategy interface { Run(ctx context.Context, orderExecutionRouter types.OrderExecutionRouter, sessions map[string]*ExchangeSession) error } -type Notifiability struct { - notifiers []Notifier -} - -func (m *Notifiability) AddNotifier(notifier Notifier) { - m.notifiers = append(m.notifiers, notifier) -} - -func (m *Notifiability) Notify(msg string, args ...interface{}) { - for _, n := range m.notifiers { - n.NotifyTo("", msg, args...) - } -} - type Trader struct { Notifiability + environment *Environment crossExchangeStrategies []CrossExchangeStrategy