The Golang Crypto Trading Bot by @saniales (Part I)

dev-challenges
thebotguys-tavern

#1

Hello,

Today I am going to show you the awesomeness of a trading bot framework @saniales made.

Meet the Golang Crypto Trading Bot Framework

Github repository: https://github.com/saniales/golang-crypto-trading-bot

A golang implementation of a console-based trading bot for cryptocurrency exchanges.

The objective of this challenge is composed of multiple phases:

  1. Read carefully the Wiki section (https://github.com/saniales/golang-crypto-trading-bot/wiki)
  2. Learn how to use the framework
  3. Create a strategy as follows
  4. Share a feedback aboutthe framework itself
  5. [OPTIONAL] Open an issue if you have a feature request or you find a bug

The strategy

The strategy you have to implement is a simple price alert for slack:

  • You assume to have some environment variables set:
    SLACK_TOKEN : the Slack Bot API Token
    SLACK_CHANNEL: The Slack channel to post into your messages (Assuming you have permission to post)
    PRICE_THRESHOLD: The price as float64 number
  • When threshold price is reached you have to throw a message of your choice containing the price and an explication message (like “OMG, price {PRICE} reached!!!”) winner will also have best message format, Using slack format is allowed.

Hope you are having fun,

The Bot Guy

PS: Additional Tips to this challenge may come if this thread has a lot of views :smiley:


#2

First TIP: There is an example section in the repository, check them to see if something helps you :smiley:


#3

Hi. This is my solution. I explain it in the below reply

package main

import (
        "fmt"
	"os"
	"time"

	"github.com/nlopes/slack"
	bot "github.com/saniales/golang-crypto-trading-bot/cmd"
	"github.com/saniales/golang-crypto-trading-bot/environment"
	"github.com/saniales/golang-crypto-trading-bot/exchanges"
	"github.com/saniales/golang-crypto-trading-bot/strategies"
	"github.com/shopspring/decimal"
	"github.com/sirupsen/logrus"
)

func main() {
	slackStrategy := loadStrategy()

	strategies.AddCustomStrategy(slackStrategy)
	bot.Execute()
}

func loadStrategy() strategies.Strategy {

	slackToken := os.Getenv("SLACK_TOKEN")
	slackChannel := os.Getenv("SLACK_CHANNEL")
	priceThreshold := os.Getenv("PRICE_THRESHOLD")
	priceThresholdDecimal, err := decimal.NewFromString(priceThreshold)
	if err != nil {
		logrus.Error(err)
		return nil
	}

	var slackClient *slack.Client

	// slackSyscoinChallenge send messages to Slack as a strategy.
	var slackSyscoinChallenge = strategies.IntervalStrategy{
		Model: strategies.StrategyModel{
			Name: "SlackSyscoinChallenge",
			Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
				slackClient = slack.New(slackToken)
				return nil
			},
			OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
				marketSummary, err := wrappers[0].GetMarketSummary(markets[0])
				if err != nil {
					return err
				}
				if marketSummary.Last.GreaterThanOrEqual(priceThresholdDecimal) {
					message := fmt.Sprintf("OMG, price %v reached!!!", priceThresholdDecimal)
					_, _, err = slackClient.PostMessage(slackChannel, slack.MsgOptionText(message, false), slack.MsgOptionAttachments(slack.Attachment{}))				}
				return err
			},
			OnError: func(err error) {
				logrus.Errorf("I Got an error %s", err)
			},
		},
		Interval: time.Second * 10,
	}

	return slackSyscoinChallenge
}

#4

Explanation:

Following the wiki, in the loadStrategy function I create a custom strategy, I add it in the list of strategies and I execute the bot.

I read the environment variables using “os” library, converting the PRICE_THRESHOLD from string to Decimal (GetMarketSummary returns a struct in which Last is a Decimal).

In “Setup” I create a new Slack client using “github.com/nlopes/slack” library.
In “OnUpdate” I read the current market status from first Exchange (supposing I added only one Exchange and only one market). If the Last price is greater or equal then PRICE_THRESHOLD, I send a message to SLACK_CHANNEL.

Using IntervalStrategy, I check the price every 10 seconds


#5

Well done, 100 SYS as payout tipped to you :smiley:


#6

#7

Awesome stuff guys, keep it up!