forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
197 lines (172 loc) · 4.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"errors"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
)
type Exchange struct {
anx ANX
btcc BTCC
bitstamp Bitstamp
bitfinex Bitfinex
brightonpeak BrightonPeak
btce BTCE
btcmarkets BTCMarkets
gdax GDAX
gemini Gemini
okcoinChina OKCoin
okcoinIntl OKCoin
itbit ItBit
lakebtc LakeBTC
localbitcoins LocalBitcoins
poloniex Poloniex
huobi HUOBI
kraken Kraken
}
type Bot struct {
config Config
exchange Exchange
exchanges []IBotExchange
tickers []Ticker
portfolio Portfolio
tickerChan chan Ticker
shutdown chan bool
}
var bot Bot
func setupBotExchanges() {
for _, exch := range bot.config.Exchanges {
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
if bot.exchanges[i].GetName() == exch.Name {
bot.exchanges[i].Setup(exch)
if bot.exchanges[i].IsEnabled() {
log.Printf("%s: Exchange support: %s (Authenticated API support: %s - Verbose mode: %s).\n", exch.Name, IsEnabled(exch.Enabled), IsEnabled(exch.AuthenticatedAPISupport), IsEnabled(exch.Verbose))
bot.exchanges[i].Start()
} else {
log.Printf("%s: Exchange support: %s\n", exch.Name, IsEnabled(exch.Enabled))
}
}
}
}
}
}
func main() {
HandleInterrupt()
log.Println("Loading config file config.json..")
err := errors.New("")
bot.config, err = ReadConfig()
if err != nil {
log.Printf("Fatal error opening config.json file. Error: %s", err)
return
}
log.Println("Config file loaded. Checking settings.. ")
err = CheckExchangeConfigValues()
if err != nil {
log.Println("Fatal error checking config values. Error:", err)
return
}
log.Printf("Bot '%s' started.\n", bot.config.Name)
AdjustGoMaxProcs()
if bot.config.SMS.Enabled {
err = CheckSMSGlobalConfigValues()
if err != nil {
log.Println(err) // non fatal event
bot.config.SMS.Enabled = false
} else {
log.Printf("SMS support enabled. Number of SMS contacts %d.\n", GetEnabledSMSContacts())
}
} else {
log.Println("SMS support disabled.")
}
log.Printf("Available Exchanges: %d. Enabled Exchanges: %d.\n", len(bot.config.Exchanges), GetEnabledExchanges())
log.Println("Bot Exchange support:")
bot.exchanges = []IBotExchange{
new(ANX),
new(Kraken),
new(BTCC),
new(Bitstamp),
new(BrightonPeak),
new(Bitfinex),
new(BTCE),
new(BTCMarkets),
new(GDAX),
new(Gemini),
new(OKCoin),
new(OKCoin),
new(ItBit),
new(LakeBTC),
new(LocalBitcoins),
new(Poloniex),
new(HUOBI),
}
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
bot.exchanges[i].SetDefaults()
log.Printf("Exchange %s successfully set default settings.\n", bot.exchanges[i].GetName())
}
}
setupBotExchanges()
err = RetrieveConfigCurrencyPairs(bot.config)
if err != nil {
log.Println("Fatal error retrieving config currency AvailablePairs. Error: ", err)
}
if bot.config.Webserver.Enabled {
err := CheckWebserverValues()
if err != nil {
log.Println(err) // non fatal event
//bot.config.Webserver.Enabled = false
} else {
listenAddr := bot.config.Webserver.ListenAddress
log.Printf("HTTP Webserver support enabled. Listen URL: http://%s:%d/\n", ExtractHost(listenAddr), ExtractPort(listenAddr))
router := NewRouter(bot.exchanges)
log.Fatal(http.ListenAndServe(listenAddr, router))
}
}
if !bot.config.Webserver.Enabled {
log.Println("HTTP Webserver support disabled.")
}
<-bot.shutdown
Shutdown()
}
func AdjustGoMaxProcs() {
log.Println("Adjusting bot runtime performance..")
maxProcsEnv := os.Getenv("GOMAXPROCS")
maxProcs := runtime.NumCPU()
log.Println("Number of CPU's detected:", maxProcs)
if maxProcsEnv != "" {
log.Println("GOMAXPROCS env =", maxProcsEnv)
env, err := strconv.Atoi(maxProcsEnv)
if err != nil {
log.Println("Unable to convert GOMAXPROCS to int, using", maxProcs)
} else {
maxProcs = env
}
}
log.Println("Set GOMAXPROCS to:", maxProcs)
runtime.GOMAXPROCS(maxProcs)
}
func HandleInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-c
log.Printf("Captured %v.", sig)
Shutdown()
}()
}
func Shutdown() {
log.Println("Bot shutting down..")
err := SaveConfig()
if err != nil {
log.Println("Unable to save config.")
} else {
log.Println("Config file saved successfully.")
}
log.Println("Exiting.")
os.Exit(1)
}