Skip to content

Commit

Permalink
feat(telegram): include start, stop, and status commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-brito committed Aug 10, 2021
1 parent f646b0f commit f14b22f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkg/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (d *DataFeedSubscription) Preload(pair, timeframe string, candles []model.C
}

func (d *DataFeedSubscription) Connect() {
log.Infof("[SETUP] connecting to exchange")
log.Infof("Connecting to the exchange.")
for _, feed := range d.Feeds {
pair, timeframe := d.pairTimeframeFromKey(feed)
ccandle, cerr := d.exchange.CandlesSubscription(pair, timeframe)
Expand Down Expand Up @@ -112,6 +112,6 @@ func (d *DataFeedSubscription) Start() {
}(key, feed)
}

log.Infof("Bot started.")
log.Infof("Data feed connected.")
wg.Wait()
}
50 changes: 43 additions & 7 deletions pkg/notification/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func NewTelegram(controller *order.Controller, settings model.Settings, options
statusBtn = menu.Text("/status")
profitBtn = menu.Text("/profit")
balanceBtn = menu.Text("/balance")
startBtn = menu.Text("/start")
stopBtn = menu.Text("/stop")
buyBtn = menu.Text("/buy")
sellBtn = menu.Text("/sell")
Expand All @@ -77,7 +78,7 @@ func NewTelegram(controller *order.Controller, settings model.Settings, options

menu.Reply(
menu.Row(statusBtn, balanceBtn, profitBtn),
menu.Row(stopBtn, buyBtn, sellBtn),
menu.Row(startBtn, stopBtn, buyBtn, sellBtn),
)

bot := &telegram{
Expand Down Expand Up @@ -105,6 +106,12 @@ func NewTelegram(controller *order.Controller, settings model.Settings, options

func (t telegram) Start() {
go t.client.Start()
for _, id := range t.settings.Telegram.Users {
_, err := t.client.Send(&tb.User{ID: id}, "Bot initialized.", t.defaultMenu)
if err != nil {
log.Error(err)
}
}
}

func (t telegram) Notify(text string) {
Expand Down Expand Up @@ -159,9 +166,19 @@ func (t telegram) HelpHandle(m *tb.Message) {
}

func (t telegram) ProfitHandle(m *tb.Message) {
_, err := t.client.Send(m.Sender, "not implemented yet")
if err != nil {
log.Error(err)
if len(t.orderController.Results) == 0 {
_, err := t.client.Send(m.Sender, "No trades registered.")
if err != nil {
log.Error(err)
}
return
}

for _, summary := range t.orderController.Results {
_, err := t.client.Send(m.Sender, fmt.Sprintf("`%s`", summary.String()))
if err != nil {
log.Error(err)
}
}
}

Expand All @@ -173,7 +190,8 @@ func (t telegram) BuyHandle(m *tb.Message) {
}

func (t telegram) StatusHandle(m *tb.Message) {
_, err := t.client.Send(m.Sender, "not implemented yet")
status := t.orderController.Status()
_, err := t.client.Send(m.Sender, fmt.Sprintf("Status: `%s`", status))
if err != nil {
log.Error(err)
}
Expand All @@ -187,14 +205,32 @@ func (t telegram) SellHandle(m *tb.Message) {
}

func (t telegram) StartHandle(m *tb.Message) {
_, err := t.client.Send(m.Sender, "Bot started", t.defaultMenu)
if t.orderController.Status() == order.StatusRunning {
_, err := t.client.Send(m.Sender, "Bot is already running.", t.defaultMenu)
if err != nil {
log.Error(err)
}
return
}

t.orderController.Start()
_, err := t.client.Send(m.Sender, "Bot started.", t.defaultMenu)
if err != nil {
log.Error(err)
}
}

func (t telegram) StopHandle(m *tb.Message) {
_, err := t.client.Send(m.Sender, "Bot stopped. To start again, use /start", t.defaultMenu)
if t.orderController.Status() == order.StatusStopped {
_, err := t.client.Send(m.Sender, "Bot is already stopped.", t.defaultMenu)
if err != nil {
log.Error(err)
}
return
}

t.orderController.Stop()
_, err := t.client.Send(m.Sender, "Bot stopped.", t.defaultMenu)
if err != nil {
log.Error(err)
}
Expand Down
53 changes: 37 additions & 16 deletions pkg/order/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (s summary) String() string {
return tableString.String()
}

type Status string

const (
StatusRunning Status = "running"
StatusStopped Status = "stopped"
StatusError Status = "error"
)

type Controller struct {
mtx sync.Mutex
ctx context.Context
Expand All @@ -82,6 +90,7 @@ type Controller struct {
Results map[string]*summary
tickerInterval time.Duration
finish chan bool
status Status
}

func NewController(ctx context.Context, exchange service.Exchange, storage *ent.Client,
Expand Down Expand Up @@ -168,11 +177,6 @@ func (c *Controller) processTrade(order *model.Order) {
c.notify(fmt.Sprintf("[PROFIT] %f %s (%f %%)\n%s", profitValue, quote, profit*100, c.Results[order.Symbol].String()))
}

func (c *Controller) Stop() {
c.updateOrders()
c.finish <- true
}

func (c *Controller) updateOrders() {
c.mtx.Lock()
defer c.mtx.Unlock()
Expand Down Expand Up @@ -229,19 +233,36 @@ func (c *Controller) updateOrders() {
}
}

func (c *Controller) Status() Status {
return c.status
}

func (c *Controller) Start() {
go func() {
ticker := time.NewTicker(c.tickerInterval)
for {
select {
case <-ticker.C:
c.updateOrders()
case <-c.finish:
ticker.Stop()
return
if c.status != StatusRunning {
c.status = StatusRunning
go func() {
ticker := time.NewTicker(c.tickerInterval)
for {
select {
case <-ticker.C:
c.updateOrders()
case <-c.finish:
ticker.Stop()
return
}
}
}
}()
}()
log.Info("Bot started.")
}
}

func (c *Controller) Stop() {
if c.status == StatusRunning {
c.status = StatusStopped
c.updateOrders()
c.finish <- true
log.Info("Bot stopped.")
}
}

func (c *Controller) createOrder(order *model.Order) error {
Expand Down

0 comments on commit f14b22f

Please sign in to comment.