From 6a4eec71d645199354a60473bec8f7fb5b1d995d Mon Sep 17 00:00:00 2001 From: zenix Date: Wed, 3 Aug 2022 17:34:51 +0900 Subject: [PATCH] feature: create simpleinteract and remove command in notification --- pkg/bbgo/interact.go | 21 +++++++++++++++++++++ pkg/bbgo/notification.go | 20 -------------------- pkg/notifier/slacknotifier/slack.go | 8 -------- pkg/notifier/telegramnotifier/telegram.go | 11 ----------- pkg/strategy/drift/strategy.go | 10 +++++++--- 5 files changed, 28 insertions(+), 42 deletions(-) diff --git a/pkg/bbgo/interact.go b/pkg/bbgo/interact.go index 3ace4cf72..820097219 100644 --- a/pkg/bbgo/interact.go +++ b/pkg/bbgo/interact.go @@ -51,6 +51,27 @@ func NewCoreInteraction(environment *Environment, trader *Trader) *CoreInteracti } } +type SimpleInteraction struct { + Command string + Description string + F interface{} + Cmd *interact.Command +} + +func (it *SimpleInteraction) Commands(i *interact.Interact) { + it.Cmd = i.PrivateCommand(it.Command, it.Description, it.F) +} + +func RegisterCommand(command, desc string, f interface{}) *interact.Command { + it := &SimpleInteraction{ + Command: command, + Description: desc, + F: f, + } + interact.AddCustomInteraction(it) + return it.Cmd +} + func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy) []string { var strategies []string for signature := range exchangeStrategies { diff --git a/pkg/bbgo/notification.go b/pkg/bbgo/notification.go index e1807a3d1..2d8420297 100644 --- a/pkg/bbgo/notification.go +++ b/pkg/bbgo/notification.go @@ -14,10 +14,6 @@ var Notification = &Notifiability{ ObjectChannelRouter: NewObjectChannelRouter(), } -func RegisterCommand(application, command string, handler func(string)) { - Notification.RegisterCommand(application, command, handler) -} - func Notify(obj interface{}, args ...interface{}) { Notification.Notify(obj, args...) } @@ -39,8 +35,6 @@ type Notifier interface { Notify(obj interface{}, args ...interface{}) SendPhotoTo(channel string, buffer *bytes.Buffer) SendPhoto(buffer *bytes.Buffer) - RegisterCommand(command string, handler func(string)) - ID() string } type NullNotifier struct{} @@ -53,12 +47,6 @@ func (n *NullNotifier) SendPhoto(buffer *bytes.Buffer) {} func (n *NullNotifier) SendPhotoTo(channel string, buffer *bytes.Buffer) {} -func (n *NullNotifier) RegisterCommand(command string, handler func(string)) {} - -func (n *NullNotifier) ID() string { - return "null" -} - type Notifiability struct { notifiers []Notifier SessionChannelRouter *PatternChannelRouter `json:"-"` @@ -123,11 +111,3 @@ func (m *Notifiability) SendPhotoTo(channel string, buffer *bytes.Buffer) { n.SendPhotoTo(channel, buffer) } } - -func (m *Notifiability) RegisterCommand(application, command string, handler func(string)) { - for _, n := range m.notifiers { - if application == n.ID() { - n.RegisterCommand(command, handler) - } - } -} diff --git a/pkg/notifier/slacknotifier/slack.go b/pkg/notifier/slacknotifier/slack.go index 529d214a7..333e3f1e5 100644 --- a/pkg/notifier/slacknotifier/slack.go +++ b/pkg/notifier/slacknotifier/slack.go @@ -159,14 +159,6 @@ func (n *Notifier) SendPhotoTo(channel string, buffer *bytes.Buffer) { // TODO } -func (n *Notifier) ID() string { - return "slack" -} - -func (n *Notifier) RegisterCommand(command string, simplehandler func(string)) { - // TODO -} - /* func (n *Notifier) NotifyTrade(trade *types.Trade) { _, _, err := n.client.PostMessageContext(context.Background(), n.TradeChannel, diff --git a/pkg/notifier/telegramnotifier/telegram.go b/pkg/notifier/telegramnotifier/telegram.go index a456bb30c..0afe4566f 100644 --- a/pkg/notifier/telegramnotifier/telegram.go +++ b/pkg/notifier/telegramnotifier/telegram.go @@ -50,17 +50,6 @@ func New(bot *telebot.Bot, options ...Option) *Notifier { return notifier } -func (n *Notifier) ID() string { - return "telegram" -} - -func (n *Notifier) RegisterCommand(command string, simplehandler func(string)) { - handler := func(msg *telebot.Message) { - simplehandler(msg.Text) - } - n.bot.Handle(command, handler) -} - func (n *Notifier) Notify(obj interface{}, args ...interface{}) { n.NotifyTo("", obj, args...) } diff --git a/pkg/strategy/drift/strategy.go b/pkg/strategy/drift/strategy.go index 611851c96..848beea8f 100644 --- a/pkg/strategy/drift/strategy.go +++ b/pkg/strategy/drift/strategy.go @@ -19,6 +19,7 @@ import ( "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/indicator" + "github.com/c9s/bbgo/pkg/interact" "github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/util" ) @@ -859,31 +860,34 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.TrailingStopLossType = "kline" } - bbgo.RegisterCommand("telegram", "/draw", func(msg string) { + bbgo.RegisterCommand("/draw", "Draw Indicators", func(reply interact.Reply) { canvas := s.DrawIndicators(dynamicKLine.StartTime, priceLine, zeroPoints) var buffer bytes.Buffer if err := canvas.Render(chart.PNG, &buffer); err != nil { log.WithError(err).Errorf("cannot render indicators in drift") + reply.Message(fmt.Sprintf("[error] cannot render indicators in drift: %v", err)) return } bbgo.SendPhoto(&buffer) }) - bbgo.RegisterCommand("telegram", "/pnl", func(msg string) { + bbgo.RegisterCommand("/pnl", "Draw PNL per trade", func(reply interact.Reply) { canvas := s.DrawPNL(&profit) var buffer bytes.Buffer if err := canvas.Render(chart.PNG, &buffer); err != nil { log.WithError(err).Errorf("cannot render pnl in drift") + reply.Message(fmt.Sprintf("[error] cannot render pnl in drift: %v", err)) return } bbgo.SendPhoto(&buffer) }) - bbgo.RegisterCommand("telegram", "/cumpnl", func(msg string) { + bbgo.RegisterCommand("/cumpnl", "Draw Cummulative PNL", func(reply interact.Reply) { canvas := s.DrawCumPNL(&cumProfit) var buffer bytes.Buffer if err := canvas.Render(chart.PNG, &buffer); err != nil { log.WithError(err).Errorf("cannot render cumpnl in drift") + reply.Message(fmt.Sprintf("[error] canot render cumpnl in drift: %v", err)) return } bbgo.SendPhoto(&buffer)