add trade reporter

This commit is contained in:
c9s 2020-10-22 10:47:54 +08:00
parent a714af739a
commit 678e4ef4ab
6 changed files with 38 additions and 28 deletions

View File

@ -56,8 +56,8 @@ var runCmd = &cobra.Command{
log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel")))
var notifier = slacknotifier.New(slackToken)
_ = notifier
// TODO: load channel from config file
var notifier = slacknotifier.New(slackToken, viper.GetString("slack-channel"))
db, err := cmdutil.ConnectMySQL()
if err != nil {
@ -65,6 +65,8 @@ var runCmd = &cobra.Command{
}
environ := bbgo.NewDefaultEnvironment(db)
environ.ReportTrade(notifier)
trader := bbgo.NewTrader(environ)
for _, entry := range userConfig.ExchangeStrategies {

View File

@ -72,7 +72,7 @@ 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 {
if err := reporter.notifier.NotifyTo(channel, text, trade); err != nil {
log.WithError(err).Error("notifier error")
}
}
@ -84,6 +84,8 @@ type Environment struct {
tradeScanTime time.Time
sessions map[string]*ExchangeSession
tradeReporter *TradeReporter
}
// NewDefaultEnvironment prepares the exchange sessions from the viper settings.
@ -122,6 +124,11 @@ func (environ *Environment) AddExchange(name string, exchange types.Exchange) (s
return session
}
func (environ *Environment) ReportTrade(notifier Notifier) *TradeReporter {
environ.tradeReporter = NewTradeReporter(notifier)
return environ.tradeReporter
}
func (environ *Environment) Init(ctx context.Context) (err error) {
for _, session := range environ.sessions {
var markets types.MarketMap
@ -138,6 +145,12 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
}
session.markets = markets
if environ.tradeReporter != nil {
session.Stream.OnTrade(func(trade types.Trade) {
environ.tradeReporter.Report(trade)
})
}
}
return nil

View File

@ -1,11 +1,16 @@
package bbgo
type Notifier interface {
Notify(channel, format string, args ...interface{}) error
NotifyTo(channel, format string, args ...interface{}) error
Notify(format string, args ...interface{}) error
}
type NullNotifier struct{}
func (n *NullNotifier) NotifyTo(channel, format string, args ...interface{}) error {
return nil
}
func (n *NullNotifier) Notify(format string, args ...interface{}) error {
return nil
}

View File

@ -41,7 +41,7 @@ func (m *Notifiability) AddNotifier(notifier Notifier) {
func (m *Notifiability) Notify(msg string, args ...interface{}) {
for _, n := range m.notifiers {
n.Notify("", msg, args...)
n.NotifyTo("", msg, args...)
}
}
@ -164,7 +164,7 @@ func (trader *OrderExecutor) RunStrategyWithHotReload(ctx context.Context, strat
log.WithError(err).Error("error load config file")
}
trader.Notify("config reloaded, restarting trader")
trader.NotifyTo("config reloaded, restarting trader")
traderDone, err = trader.RunStrategy(strategyContext, strategy)
if err != nil {
@ -232,22 +232,6 @@ func (trader *Trader) reportPnL() {
}
*/
/*
func (trader *Trader) NotifyPnL(report *accounting.ProfitAndLossReport) {
for _, n := range trader.notifiers {
n.NotifyPnL(report)
}
}
*/
/*
func (trader *Trader) NotifyTrade(trade *types.Trade) {
for _, n := range trader.notifiers {
n.NotifyTrade(trade)
}
}
*/
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)

View File

@ -282,7 +282,7 @@ func (s *Stream) read(ctx context.Context) {
continue
}
// log.Notify("[binance] event: %+v", e)
// log.NotifyTo("[binance] event: %+v", e)
switch e := e.(type) {
case *OutboundAccountInfoEvent:

View File

@ -13,16 +13,18 @@ type SlackAttachmentCreator interface {
}
type Notifier struct {
client *slack.Client
client *slack.Client
channel string
}
type NotifyOption func(notifier *Notifier)
func New(token string, options ...NotifyOption) *Notifier {
func New(token, channel string, options ...NotifyOption) *Notifier {
var client = slack.New(token, slack.OptionDebug(true))
notifier := &Notifier{
client: client,
channel: channel,
client: client,
}
for _, o := range options {
@ -32,7 +34,11 @@ func New(token string, options ...NotifyOption) *Notifier {
return notifier
}
func (n *Notifier) Notify(channel, format string, args ...interface{}) error {
func (n *Notifier) Notify(format string, args ...interface{}) error {
return n.NotifyTo(n.channel, format, args...)
}
func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) error {
var slackAttachments []slack.Attachment
var slackArgsOffset = -1
@ -84,7 +90,7 @@ func (n *Notifier) NotifyTrade(trade *types.Trade) {
logrus.WithError(err).Error("slack send error")
}
}
*/
*/
/*
func (n *Notifier) NotifyPnL(report *pnl.AverageCostPnlReport) {