implement TradeReporter

This commit is contained in:
c9s 2020-10-21 19:52:55 +08:00
parent b1a9a66dba
commit a714af739a
4 changed files with 56 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package bbgo
import ( import (
"context" "context"
"fmt" "fmt"
"regexp"
"strings" "strings"
"time" "time"
@ -15,6 +16,7 @@ import (
"github.com/c9s/bbgo/pkg/service" "github.com/c9s/bbgo/pkg/service"
"github.com/c9s/bbgo/pkg/store" "github.com/c9s/bbgo/pkg/store"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy) var LoadedExchangeStrategies = make(map[string]SingleExchangeStrategy)
@ -29,6 +31,52 @@ func RegisterCrossExchangeStrategy(key string, configmap CrossExchangeStrategy)
LoadedCrossExchangeStrategies[key] = configmap LoadedCrossExchangeStrategies[key] = configmap
} }
type TradeReporter struct {
notifier Notifier
channel string
channelRoutes map[*regexp.Regexp]string
}
func NewTradeReporter(notifier Notifier) *TradeReporter {
return &TradeReporter{
notifier: notifier,
channelRoutes: make(map[*regexp.Regexp]string),
}
}
func (reporter *TradeReporter) Channel(channel string) *TradeReporter {
reporter.channel = channel
return reporter
}
func (reporter *TradeReporter) ChannelBySymbol(routes map[string]string) *TradeReporter {
for pattern, channel := range routes {
reporter.channelRoutes[regexp.MustCompile(pattern)] = channel
}
return reporter
}
func (reporter *TradeReporter) getChannel(symbol string) string {
for pattern, channel := range reporter.channelRoutes {
if pattern.MatchString(symbol) {
return channel
}
}
return reporter.channel
}
func (reporter *TradeReporter) Report(trade types.Trade) {
var channel = reporter.getChannel(trade.Symbol)
var text = util.Render(`:handshake: {{ .Symbol }} {{ .Side }} Trade Execution @ {{ .Price }}`, trade)
if err := reporter.notifier.Notify(channel, text, trade); err != nil {
log.WithError(err).Error("notifier error")
}
}
// Environment presents the real exchange data layer // Environment presents the real exchange data layer
type Environment struct { type Environment struct {
TradeService *service.TradeService TradeService *service.TradeService

View File

@ -1,17 +1,11 @@
package bbgo package bbgo
import (
"github.com/c9s/bbgo/pkg/accounting/pnl"
"github.com/c9s/bbgo/pkg/types"
)
type Notifier interface { type Notifier interface {
Notify(channel, format string, args ...interface{}) Notify(channel, format string, args ...interface{}) error
NotifyTrade(trade *types.Trade)
NotifyPnL(report *pnl.AverageCostPnlReport)
} }
type NullNotifier struct{} type NullNotifier struct{}
func (n *NullNotifier) Notify(format string, args ...interface{}) { func (n *NullNotifier) Notify(format string, args ...interface{}) error {
return nil
} }

View File

@ -240,11 +240,13 @@ func (trader *Trader) NotifyPnL(report *accounting.ProfitAndLossReport) {
} }
*/ */
/*
func (trader *Trader) NotifyTrade(trade *types.Trade) { func (trader *Trader) NotifyTrade(trade *types.Trade) {
for _, n := range trader.notifiers { for _, n := range trader.notifiers {
n.NotifyTrade(trade) n.NotifyTrade(trade)
} }
} }
*/
func (trader *Trader) SubmitOrder(ctx context.Context, order types.SubmitOrder) { func (trader *Trader) SubmitOrder(ctx context.Context, order types.SubmitOrder) {
trader.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order) trader.Notify(":memo: Submitting %s %s %s order with quantity: %s", order.Symbol, order.Type, order.Side, order.QuantityString, order)

View File

@ -32,7 +32,7 @@ func New(token string, options ...NotifyOption) *Notifier {
return notifier return notifier
} }
func (n *Notifier) Notify(channel, format string, args ...interface{}) { func (n *Notifier) Notify(channel, format string, args ...interface{}) error {
var slackAttachments []slack.Attachment var slackAttachments []slack.Attachment
var slackArgsOffset = -1 var slackArgsOffset = -1
@ -70,6 +70,8 @@ func (n *Notifier) Notify(channel, format string, args ...interface{}) {
if err != nil { if err != nil {
logrus.WithError(err).Errorf("slack error: %s", err.Error()) logrus.WithError(err).Errorf("slack error: %s", err.Error())
} }
return err
} }
/* /*