Skip to content

Commit

Permalink
new way to start with a new strategy (#81)
Browse files Browse the repository at this point in the history
Added examples refactoring
  • Loading branch information
marioarranzr authored and saniales committed Nov 26, 2018
1 parent 0eb2040 commit 130d742
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 171 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ If you need to, you can create a strategy and bind it to the bot:
import bot "github.com/saniales/golang-crypto-trading-bot/cmd"

func main() {
bot.AddCustomStrategy(myStrategy)
bot.AddCustomStrategy(examples.MyStrategy)
bot.Execute()
}
```
Expand Down
137 changes: 137 additions & 0 deletions examples/interval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright © 2017 Alessandro Sanino <saninoale@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package examples

import (
"fmt"
"log"
"time"

"github.com/nlopes/slack"
"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/shomali11/slacker"
"github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)

// Watch5Sec prints out the info of the market every 5 seconds.
var Watch5Sec = strategies.IntervalStrategy{
Model: strategies.StrategyModel{
Name: "Watch5Sec",
Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
fmt.Println("Watch5Sec starting")
return nil
},
OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
_, err := wrappers[0].GetMarketSummary(markets[0])
if err != nil {
return err
}
logrus.Info(markets)
logrus.Info(wrappers)
return nil
},
OnError: func(err error) {
fmt.Println(err)
},
TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
fmt.Println("Watch5Sec exited")
return nil
},
},
Interval: time.Second * 5,
}

var slackBot *slacker.Slacker

// SlackIntegrationExample send messages to Slack as a strategy.
// RTM not supported (and usually not requested when trading, this is an automated slackBot).
var SlackIntegrationExample = strategies.IntervalStrategy{
Model: strategies.StrategyModel{
Name: "SlackIntegrationExample",
Setup: func([]exchanges.ExchangeWrapper, []*environment.Market) error {
// connect slack token
slackBot = slacker.NewClient("YOUR-TOKEN-HERE")
slackBot.Init(func() {
log.Println("Slack BOT Connected")
})
slackBot.Err(func(err string) {
log.Println("Error during slack slackBot connection: ", err)
})
go func() {
err := slackBot.Listen()
if err != nil {
log.Fatal(err)
}
}()
return nil
},
OnUpdate: func([]exchanges.ExchangeWrapper, []*environment.Market) error {
//if updates has requirements
_, _, err := slackBot.Client.PostMessage("DESIRED-CHANNEL", "OMG something happening!!!!!", slack.PostMessageParameters{})
return err
},
OnError: func(err error) {
logrus.Errorf("I Got an error %s", err)
},
},
Interval: time.Second * 10,
}

var telegramBot *tb.Bot

// TelegramIntegrationExample send messages to Telegram as a strategy.
var TelegramIntegrationExample = strategies.IntervalStrategy{
Model: strategies.StrategyModel{
Name: "TelegramIntegrationExample",
Setup: func([]exchanges.ExchangeWrapper, []*environment.Market) error {
telegramBot, err := tb.NewBot(tb.Settings{
Token: "YOUR-TELEGRAM-TOKEN",
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})

if err != nil {
return err
}

telegramBot.Start()
return nil
},
OnUpdate: func([]exchanges.ExchangeWrapper, []*environment.Market) error {
telegramBot.Send(&tb.User{
Username: "YOUR-USERNAME-GROUP-OR-USER",
}, "OMG SOMETHING HAPPENING!!!!!", tb.SendOptions{})

/*
// Optionally it can have options
telegramBot.Send(tb.User{
Username: "YOUR-JOINED-GROUP-USERNAME",
}, "OMG SOMETHING HAPPENING!!!!!", tb.SendOptions{})
*/
return nil
},
OnError: func(err error) {
logrus.Errorf("I Got an error %s", err)
telegramBot.Stop()
},
TearDown: func([]exchanges.ExchangeWrapper, []*environment.Market) error {
telegramBot.Stop()
return nil
},
},
}
17 changes: 17 additions & 0 deletions examples/package-info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright © 2017 Alessandro Sanino <saninoale@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Package examples contains different implementations of the strategies.
package examples
64 changes: 0 additions & 64 deletions examples/slack_integration.go

This file was deleted.

53 changes: 0 additions & 53 deletions examples/telegram_integration.go

This file was deleted.

34 changes: 16 additions & 18 deletions examples/watch.go → examples/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,38 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package strategies
package examples

import (
"fmt"
"time"

"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/sirupsen/logrus"
)

// Watch5Min prints out the info of the market every 5 minutes.
var Watch5Min = strategies.IntervalStrategy{
// Websocket strategy
var Websocket = strategies.WebsocketStrategy{
Model: strategies.StrategyModel{
Name: "Watch5Min",
Name: "Websocket",
Setup: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
fmt.Println("Watch5Min starting")
return nil
},
OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
_, err := wrappers[0].GetMarketSummary(markets[0])
if err != nil {
for _, wrapper := range wrappers {
err := wrapper.FeedConnect(markets)
if err == exchanges.ErrWebsocketNotSupported || err == nil {
continue
}
return err
}
fmt.Println(markets)
return nil
},
OnError: func(err error) {
fmt.Println(err)
OnUpdate: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
// do something
return nil
},
TearDown: func(wrappers []exchanges.ExchangeWrapper, markets []*environment.Market) error {
fmt.Println("Watch5Min exited")
return nil
},
OnError: func(err error) {
logrus.Error(err)
},
},
Interval: time.Minute * 5,
}
1 change: 1 addition & 0 deletions strategies/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (is IntervalStrategy) Apply(wrappers []exchanges.ExchangeWrapper, markets [
is.Model.OnError(err)
}
}

if !hasUpdateFunc {
_err := errors.New("OnUpdate func cannot be empty")
if hasErrorFunc {
Expand Down
Loading

0 comments on commit 130d742

Please sign in to comment.