Merge branch 'refactor/telegram-bot' into main

This commit is contained in:
c9s 2020-12-08 14:21:28 +08:00
commit bc888d8c90
6 changed files with 27 additions and 24 deletions

View File

@ -194,6 +194,7 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
// update last prices by the given kline
lastKLine := kLines[len(kLines)-1]
log.Infof("last kline: %+v", lastKLine)
if lastPriceTime == emptyTime {
session.lastPrices[symbol] = lastKLine.Close
lastPriceTime = lastKLine.EndTime

View File

@ -18,6 +18,7 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
tb "gopkg.in/tucnak/telebot.v2"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
@ -126,9 +127,20 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
telegramBotToken := viper.GetString("telegram-bot-token")
telegramAuthToken := viper.GetString("telegram-auth-token")
if len(telegramBotToken) > 0 && len(telegramAuthToken) > 0 {
bot, err := tb.NewBot(tb.Settings{
// You can also set custom API URL.
// If field is empty it equals to "https://api.telegram.org".
// URL: "http://195.129.111.17:8012",
Token: telegramBotToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
return err
}
log.Infof("adding telegram notifier")
var notifier = telegramnotifier.New(telegramBotToken, telegramAuthToken)
var notifier = telegramnotifier.New(bot, telegramAuthToken)
// start telegram bot
go notifier.Bot.Start()

View File

@ -19,6 +19,10 @@ type EWMA struct {
}
func (inc *EWMA) Last() float64 {
if len(inc.Values) == 0 {
return 0
}
return inc.Values[len(inc.Values)-1]
}

View File

@ -2,7 +2,6 @@ package telegramnotifier
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
@ -17,30 +16,16 @@ type Notifier struct {
type NotifyOption func(notifier *Notifier)
// start bot daemon
func New(botToken, authToken string, options ...NotifyOption) *Notifier {
func New(bot *tb.Bot, authToken string, options ...NotifyOption) *Notifier {
notifier := &Notifier{
chatUser: &tb.User{},
Bot: &tb.Bot{},
Bot: bot,
}
for _, o := range options {
o(notifier)
}
bot, err := tb.NewBot(tb.Settings{
// You can also set custom API URL.
// If field is empty it equals to "https://api.telegram.org".
// URL: "http://195.129.111.17:8012",
Token: botToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
panic(err)
}
bot.Handle("/help", func(m *tb.Message) {
helpMsg := `
help - print help message

View File

@ -202,7 +202,6 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
session.Stream.OnOrderUpdate(s.handleOrderUpdate)
// session.Stream.OnKLineClosed
sourceSession.Stream.OnKLineClosed(func(kline types.KLine) {
// skip k-lines from other symbols
@ -227,7 +226,7 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se
s.clear(ctx, session)
})
if lastPrice, ok := session.LastPrice(s.Symbol) ; ok {
if lastPrice, ok := session.LastPrice(s.Symbol); ok {
s.place(ctx, &orderExecutor, session, indicator, lastPrice)
}

View File

@ -169,7 +169,8 @@ func (k KLine) GetChange() float64 {
}
func (k KLine) String() string {
return fmt.Sprintf("%s %s %s Open: %.8f Close: %.8f High: %.8f Low: %.8f Volume: %.8f Change: %.4f Max Change: %.4f",
return fmt.Sprintf("%s %s %s %s Open: %.8f Close: %.8f High: %.8f Low: %.8f Volume: %.8f Change: %.4f Max Change: %.4f",
k.Exchange,
k.StartTime.Format("2006-01-02 15:04"),
k.Symbol, k.Interval, k.Open, k.Close, k.High, k.Low, k.Volume, k.GetChange(), k.GetMaxChange())
}
@ -346,14 +347,15 @@ func (k KLineWindow) Take(size int) KLineWindow {
}
func (k KLineWindow) Tail(size int) KLineWindow {
if len(k) <= size {
win := make(KLineWindow, len(k))
length := len(k)
if length <= size {
win := make(KLineWindow, length)
copy(win, k)
return win
}
win := make(KLineWindow, size)
copy(win, k[len(k)-size:])
copy(win, k[length-1-size:])
return win
}