Skip to content

Commit 403df84

Browse files
authored
Merge pull request #20 from saniales/refactoring
Refactoring and enhancing model
2 parents a4512a6 + d6e640e commit 403df84

23 files changed

+611
-608
lines changed

.bot_config.yaml.example

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
exchange_configs:
2+
- exchange: bittrex
3+
public_key: your_bittrex_public_key
4+
secret_key: your_bittrex_secret_key
5+
- exchange: binance
6+
public_key: your_binance_public_key
7+
secret_key: your_binance_secret_key
8+
- exchange: bitfinex
9+
public_key: your_bitfinex_public_key
10+
secret_key: your_bitfinex_secret_key
11+
strategies:
12+
- strategy: your_strategy_name
13+
markets:
14+
- market: market_logical_name
15+
bindings:
16+
- exchange: bittrex
17+
market_name: market_name_on_bittrex
18+
- exchange: binance
19+
market_name: market_name_on_binance
20+
- exchange: bitfinex
21+
market_name: market_name_on_bitfinex
22+
- market: another_market_logical_name
23+
bindings:
24+
- exchange: bittrex
25+
market_name: market_name_on_bittrex
26+
- exchange: binance
27+
market_name: market_name_on_binance
28+
- exchange: bitfinex
29+
market_name: market_name_on_bitfinex

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ _testmain.go
2323
*.test
2424
*.prof
2525

26-
golang-crypto-trading-bot
26+
golang-crypto-trading-bot
27+
28+
.bot_config.yaml
29+
.bot_config.yml

.gobot

Lines changed: 0 additions & 7 deletions
This file was deleted.

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: go
22

33
go:
4-
- 1.8
4+
- "1.10"
55

6-
before_install: go get ./...
6+
before_install: go get -t ./...
77
go_import_path: github.com/saniales/golang-crypto-trading-bot

README.md

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
11
# golang-crypto-trading-bot
2+
3+
[![Release](https://img.shields.io/github/downloads/saniales/golang-crypto-trading-bot/total.svg)](https://github.com/yangwenmai/how-to-add-badge-in-github-readme/releases)
4+
[![Documentation](https://godoc.org/github.com/saniales/golang-crypto-trading-bot?status.svg)](https://github.com/saniales/golang-crypto-trading-bot)
5+
[![Travis CI](https://img.shields.io/travis/saniales/golang-crypto-trading-bot.svg)]((https://github.com/saniales/golang-crypto-trading-bot))
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/saniales/golang-crypto-trading-bot.svg?branch=master)](https://github.com/saniales/golang-crypto-trading-bot)
7+
[![GitHub release](https://img.shields.io/github/release/saniales/golang-crypto-trading-bot.svg)](https://github.com/saniales/golang-crypto-trading-bot/releases)
8+
[![license](https://img.shields.io/github/license/saniales/golang-crypto-trading-bot.svg?maxAge=2592000)](https://github.com/saniales/golang-crypto-trading-bot/LICENSE)
9+
10+
211
A golang implementation of a console-based trading bot for cryptocurrency exchanges, can be deployed to heroku too.
312

4-
# Supported Exchanges
5-
Bittrex, Poloniex (both works in progress)
13+
## Supported Exchanges
14+
Bittrex, Poloniex, Binance, Bitfinex and Kraken, other in progress.
15+
16+
## Usage
617

7-
# Usage
8-
Create a strategy by implementing Strategy interface and add it to the bot, then compile and execute the bot specifying the --strategy flag and --exchange flag
18+
Download a release or directly build the code from this repository.
19+
``` bash
20+
$ go get github.com/saniales/golang-crypto-trading-bot
21+
```
922

10-
`go run gobot.go --exchange bittrex --strategy myCustomStrategyName`
23+
If you need to, you can create a strategy and bind it to the bot:
24+
``` go
25+
import bot "github.com/saniales/golang-crypto-trading-bot/cmd"
1126

12-
You can use the --simulate flag to avoid trade but just simulate them.
13-
There is also a --watch flag to print info about markets to the console continuously.
27+
bot.AddCustomStrategy(myStrategy)
28+
bot.Execute()
29+
```
1430

15-
Kill the bot by using CTRL+C or SIGINT signal (on linux, Command+C on MacOsX).
31+
For strategy reference see the [Godoc documentation](https://godoc.org/github.com/saniales/golang-crypto-trading-bot).
1632

17-
# Current version
18-
[pre-alpha 0.0.0.1]
33+
# Configuration file template
34+
Create a configuration file from this example or run the `init` command of the compiled executable.
35+
``` yaml
36+
exchange_configs:
37+
- exchange: bittrex
38+
public_key: your_bittrex_public_key
39+
secret_key: your_bittrex_secret_key
40+
- exchange: binance
41+
public_key: your_binance_public_key
42+
secret_key: your_binance_secret_key
43+
- exchange: bitfinex
44+
public_key: your_bitfinex_public_key
45+
secret_key: your_bitfinex_secret_key
46+
strategies:
47+
- strategy: your_strategy_name
48+
markets:
49+
- market: market_logical_name
50+
bindings:
51+
- exchange: bittrex
52+
market_name: market_name_on_bittrex
53+
- exchange: binance
54+
market_name: market_name_on_binance
55+
- exchange: bitfinex
56+
market_name: market_name_on_bitfinex
57+
- market: another_market_logical_name
58+
bindings:
59+
- exchange: bittrex
60+
market_name: market_name_on_bittrex
61+
- exchange: binance
62+
market_name: market_name_on_binance
63+
- exchange: bitfinex
64+
market_name: market_name_on_bitfinex
65+
```
1966
2067
# Donate
2168
Feel free to donate:
Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package botHelpers
1+
package helpers
22

33
import (
44
"github.com/saniales/golang-crypto-trading-bot/environment"
@@ -13,32 +13,12 @@ func InitExchange(exchangeConfig environment.ExchangeConfig) exchanges.ExchangeW
1313
case "bittrexV2":
1414
return exchanges.NewBittrexV2Wrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
1515
case "poloniex":
16-
return nil
17-
case "yobit":
18-
return nil
19-
case "cryptopia":
20-
return nil
16+
return exchanges.NewPoloniexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
17+
case "binance":
18+
return exchanges.NewBinanceWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
19+
case "bitfinex":
20+
return exchanges.NewBitfinexWrapper(exchangeConfig.PublicKey, exchangeConfig.SecretKey)
2121
default:
2222
return nil
2323
}
2424
}
25-
26-
//InitMarkets uses ExchangeWrapper to find info about markets and initialize them.
27-
func InitMarkets(exchange exchanges.ExchangeWrapper) (map[string]*environment.Market, error) {
28-
markets, err := exchange.GetMarkets()
29-
if err != nil {
30-
return nil, err
31-
}
32-
33-
marketMap := make(map[string]*environment.Market, len(markets))
34-
for _, market := range markets {
35-
marketMap[market.Name] = market
36-
}
37-
38-
err = exchange.GetMarketSummaries(marketMap)
39-
if err != nil {
40-
return nil, err
41-
}
42-
43-
return marketMap, nil
44-
}

cmd/init.go

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,42 @@ func initConfig() {
8282

8383
func generateInitFile() {
8484
configs := environment.BotConfig{}
85-
fmt.Println("Which exchange are you going to use?")
86-
fmt.Scanln(&configs.Exchange.ExchangeName)
87-
fmt.Println("Please provide Public Key for that exchange.")
88-
fmt.Scanln(&configs.Exchange.PublicKey)
89-
fmt.Println("Please provide Secret Key for that exchange.")
90-
fmt.Scanln(&configs.Exchange.SecretKey)
85+
for {
86+
var exchange environment.ExchangeConfig
87+
var YesNo string
88+
89+
fmt.Println("Which exchange are you going to add?")
90+
fmt.Scanln(&exchange.ExchangeName)
91+
92+
alreadyAdded := false
93+
for _, ex := range configs.ExchangeConfigs {
94+
if ex.ExchangeName == exchange.ExchangeName {
95+
alreadyAdded = true
96+
break
97+
}
98+
}
99+
100+
if alreadyAdded {
101+
fmt.Println("Exchange already added, retry.")
102+
continue
103+
}
104+
105+
fmt.Println("Please provide Public Key for that exchange.")
106+
fmt.Scanln(&exchange.PublicKey)
107+
fmt.Println("Please provide Secret Key for that exchange.")
108+
fmt.Scanln(&exchange.SecretKey)
109+
110+
configs.ExchangeConfigs = append(configs.ExchangeConfigs, exchange)
111+
112+
fmt.Println("Exchange Added")
113+
for YesNo != "Y" && YesNo != "n" {
114+
fmt.Println("Do you want to add another exchange? (Y/n)")
115+
fmt.Scanln(&YesNo)
116+
}
117+
if YesNo == "n" {
118+
break
119+
}
120+
}
91121

92122
for {
93123
var YesNo string
@@ -98,13 +128,46 @@ func generateInitFile() {
98128
if YesNo == "n" {
99129
break
100130
}
131+
101132
var tempStrategyAppliance environment.StrategyConfig
102-
fmt.Println("Please Enter Market Name using short notation " +
103-
"(e.g. BTC-ETH for a Bitcoin-Ethereum market).")
104-
fmt.Scanln(&tempStrategyAppliance.Market)
133+
105134
fmt.Println("Please Enter The Name of the strategy you want to use\n" +
106135
"in this market (must be in the system)")
107136
fmt.Scanln(&tempStrategyAppliance.Strategy)
137+
138+
for {
139+
var tmpMarketConf environment.MarketConfig
140+
fmt.Println("Please Enter Market Name using short notation " +
141+
"(e.g. BTC-ETH for a Bitcoin-Ethereum market).")
142+
fmt.Scanln(&tmpMarketConf.Name)
143+
for _, ex := range configs.ExchangeConfigs {
144+
var exMarketName string
145+
fmt.Printf("Please Enter %s exchange market ticker, or leave empty to skip this exchange\n", ex.ExchangeName)
146+
fmt.Scanln(&exMarketName)
147+
148+
if exMarketName != "" {
149+
tmpMarketConf.Exchanges = append(tmpMarketConf.Exchanges, environment.ExchangeBindingsConfig{
150+
Name: ex.ExchangeName,
151+
MarketName: exMarketName,
152+
})
153+
fmt.Printf("Exchange %s CONFIGURED with Market Name %s\n", ex.ExchangeName, exMarketName)
154+
} else {
155+
fmt.Printf("Exchange %s SKIPPED\n", ex.ExchangeName)
156+
}
157+
}
158+
159+
tempStrategyAppliance.Markets = append(tempStrategyAppliance.Markets, tmpMarketConf)
160+
161+
var YesNo string
162+
for YesNo != "Y" && YesNo != "n" {
163+
fmt.Println("Do you want to add another market binded to this strategy? (Y/n)")
164+
fmt.Scanln(&YesNo)
165+
}
166+
if YesNo == "n" {
167+
break
168+
}
169+
}
170+
108171
configs.Strategies = append(configs.Strategies, tempStrategyAppliance)
109172
}
110173

cmd/root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ const (
3030
// RootCmd represents the base command when called without any subcommands
3131
var RootCmd = &cobra.Command{
3232
Use: "gobot",
33-
Short: "USAGE gobot [OPTIONS].",
34-
Long: `USAGE gobot [OPTIONS] : see --help for details`,
33+
Short: fmt.Sprintf("USAGE %s [OPTIONS]", os.Args[0]),
34+
Long: fmt.Sprintf(`USAGE %s [OPTIONS] : see --help for details`, os.Args[0]),
3535
Run: executeRootCommand,
3636
}
3737

@@ -60,7 +60,7 @@ func init() {
6060
RootCmd.PersistentFlags().CountVarP(&GlobalFlags.Verbose, "verbose", "v", "show verbose information when trading : use multiple times to increase verbosity level.")
6161

6262
RootCmd.Flags().BoolVarP(&rootFlags.Version, "version", "V", false, "show version information.")
63-
RootCmd.PersistentFlags().StringVar(&GlobalFlags.ConfigFile, "config-file", "./.gobot", "Config file path (default : ./.gobot)")
63+
RootCmd.PersistentFlags().StringVar(&GlobalFlags.ConfigFile, "config-file", "./.bot_config.yaml", "Config file path (default : ./.bot_config.yaml)")
6464
}
6565

6666
func executeRootCommand(cmd *cobra.Command, args []string) {

0 commit comments

Comments
 (0)