add per-session-based trade reporter

This commit is contained in:
c9s 2020-10-22 10:54:03 +08:00
parent 678e4ef4ab
commit ea3e9e7d05
5 changed files with 52 additions and 20 deletions

View File

@ -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

View File

@ -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)
})

View File

@ -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...)
}
}

View File

@ -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{

View File

@ -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