feature: create simpleinteract and remove command in notification

This commit is contained in:
zenix 2022-08-03 17:34:51 +09:00
parent 90e596f463
commit 6a4eec71d6
5 changed files with 28 additions and 42 deletions

View File

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

View File

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

View File

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

View File

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

View File

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