forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
297 lines (253 loc) · 9.06 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"strconv"
"sync"
"syscall"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/communications"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/connchecker"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/currency/coinmarketcap"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
log "github.com/thrasher-corp/gocryptotrader/logger"
"github.com/thrasher-corp/gocryptotrader/ntpclient"
"github.com/thrasher-corp/gocryptotrader/portfolio"
)
// Bot contains configuration, portfolio, exchange & ticker data and is the
// overarching type across this code base.
type Bot struct {
config *config.Config
portfolio *portfolio.Base
exchanges []exchange.IBotExchange
comms *communications.Communications
shutdown chan bool
dryRun bool
configFile string
dataDir string
connectivity *connchecker.Checker
sync.Mutex
}
const banner = `
______ ______ __ ______ __
/ ____/____ / ____/_____ __ __ ____ / /_ ____ /_ __/_____ ______ ____/ /___ _____
/ / __ / __ \ / / / ___// / / // __ \ / __// __ \ / / / ___// __ // __ // _ \ / ___/
/ /_/ // /_/ // /___ / / / /_/ // /_/ // /_ / /_/ // / / / / /_/ // /_/ // __// /
\____/ \____/ \____//_/ \__, // .___/ \__/ \____//_/ /_/ \__,_/ \__,_/ \___//_/
/____//_/
`
var bot Bot
func main() {
bot.shutdown = make(chan bool)
HandleInterrupt()
defaultPath, err := config.GetFilePath("")
if err != nil {
log.Fatal(err)
}
// Handle flags
flag.StringVar(&bot.configFile, "config", defaultPath, "config file to load")
flag.StringVar(&bot.dataDir, "datadir", common.GetDefaultDataDir(runtime.GOOS), "default data directory for GoCryptoTrader files")
dryrun := flag.Bool("dryrun", false, "dry runs bot, doesn't save config file")
version := flag.Bool("version", false, "retrieves current GoCryptoTrader version")
verbosity := flag.Bool("verbose", false, "increases logging verbosity for GoCryptoTrader")
Coinmarketcap := flag.Bool("c", false, "overrides config and runs currency analaysis")
FxCurrencyConverter := flag.Bool("fxa", false, "overrides config and sets up foreign exchange Currency Converter")
FxCurrencyLayer := flag.Bool("fxb", false, "overrides config and sets up foreign exchange Currency Layer")
FxFixer := flag.Bool("fxc", false, "overrides config and sets up foreign exchange Fixer.io")
FxOpenExchangeRates := flag.Bool("fxd", false, "overrides config and sets up foreign exchange Open Exchange Rates")
flag.Parse()
if *version {
fmt.Print(BuildVersion(true))
os.Exit(0)
}
if *dryrun {
bot.dryRun = true
}
fmt.Println(banner)
fmt.Println(BuildVersion(false))
bot.config = &config.Cfg
log.Debugf("Loading config file %s..\n", bot.configFile)
err = bot.config.LoadConfig(bot.configFile)
if err != nil {
log.Fatalf("Failed to load config. Err: %s", err)
}
err = common.CreateDir(bot.dataDir)
if err != nil {
log.Fatalf("Failed to open/create data directory: %s. Err: %s", bot.dataDir, err)
}
log.Debugf("Using data directory: %s.\n", bot.dataDir)
err = bot.config.CheckLoggerConfig()
if err != nil {
log.Errorf("Failed to configure logger reason: %s", err)
}
err = log.SetupLogger()
if err != nil {
log.Errorf("Failed to setup logger reason: %s", err)
}
ActivateNTP()
ActivateConnectivityMonitor()
AdjustGoMaxProcs()
log.Debugf("Bot '%s' started.\n", bot.config.Name)
log.Debugf("Bot dry run mode: %v.\n", common.IsEnabled(bot.dryRun))
log.Debugf("Available Exchanges: %d. Enabled Exchanges: %d.\n",
len(bot.config.Exchanges),
bot.config.CountEnabledExchanges())
common.HTTPClient = common.NewHTTPClientWithTimeout(bot.config.GlobalHTTPTimeout)
log.Debugf("Global HTTP request timeout: %v.\n", common.HTTPClient.Timeout)
SetupExchanges()
if len(bot.exchanges) == 0 {
log.Fatal("No exchanges were able to be loaded. Exiting")
}
log.Debugf("Starting communication mediums..")
cfg := bot.config.GetCommunicationsConfig()
bot.comms = communications.NewComm(&cfg)
bot.comms.GetEnabledCommunicationMediums()
var newFxSettings []currency.FXSettings
for _, d := range bot.config.Currency.ForexProviders {
newFxSettings = append(newFxSettings, currency.FXSettings(d))
}
err = currency.RunStorageUpdater(currency.BotOverrides{
Coinmarketcap: *Coinmarketcap,
FxCurrencyConverter: *FxCurrencyConverter,
FxCurrencyLayer: *FxCurrencyLayer,
FxFixer: *FxFixer,
FxOpenExchangeRates: *FxOpenExchangeRates,
},
¤cy.MainConfiguration{
ForexProviders: newFxSettings,
CryptocurrencyProvider: coinmarketcap.Settings(bot.config.Currency.CryptocurrencyProvider),
Cryptocurrencies: bot.config.Currency.Cryptocurrencies,
FiatDisplayCurrency: bot.config.Currency.FiatDisplayCurrency,
CurrencyDelay: bot.config.Currency.CurrencyFileUpdateDuration,
FxRateDelay: bot.config.Currency.ForeignExchangeUpdateDuration,
},
bot.dataDir,
*verbosity)
if err != nil {
log.Fatalf("currency updater system failed to start %v", err)
}
bot.portfolio = &portfolio.Portfolio
bot.portfolio.SeedPortfolio(bot.config.Portfolio)
SeedExchangeAccountInfo(GetAllEnabledExchangeAccountInfo().Data)
ActivateWebServer()
go portfolio.StartPortfolioWatcher()
go TickerUpdaterRoutine()
go OrderbookUpdaterRoutine()
go WebsocketRoutine(*verbosity)
<-bot.shutdown
Shutdown()
}
// ActivateWebServer Sets up a local web server
func ActivateWebServer() {
if bot.config.Webserver.Enabled {
listenAddr := bot.config.Webserver.ListenAddress
log.Debugf(
"HTTP Webserver support enabled. Listen URL: http://%s:%d/\n",
common.ExtractHost(listenAddr), common.ExtractPort(listenAddr),
)
router := NewRouter()
go func() {
err := http.ListenAndServe(listenAddr, router)
if err != nil {
log.Fatal(err)
}
}()
log.Debugln("HTTP Webserver started successfully.")
log.Debugln("Starting websocket handler.")
StartWebsocketHandler()
} else {
log.Debugln("HTTP RESTful Webserver support disabled.")
}
}
// ActivateConnectivityMonitor Sets up internet connectivity monitor
func ActivateConnectivityMonitor() {
var err error
bot.connectivity, err = connchecker.New(bot.config.ConnectionMonitor.DNSList,
bot.config.ConnectionMonitor.PublicDomainList,
bot.config.ConnectionMonitor.CheckInterval)
if err != nil {
log.Fatalf("Connectivity checker failure: %s", err)
}
}
// ActivateNTP Sets up NTP client
func ActivateNTP() {
if bot.config.NTPClient.Level != -1 {
bot.config.CheckNTPConfig()
NTPTime, errNTP := ntpclient.NTPClient(bot.config.NTPClient.Pool)
currentTime := time.Now()
if errNTP != nil {
log.Warnf("NTPClient failed to create: %v", errNTP)
} else {
NTPcurrentTimeDifference := NTPTime.Sub(currentTime)
configNTPTime := *bot.config.NTPClient.AllowedDifference
configNTPNegativeTime := (*bot.config.NTPClient.AllowedNegativeDifference - (*bot.config.NTPClient.AllowedNegativeDifference * 2))
if NTPcurrentTimeDifference > configNTPTime || NTPcurrentTimeDifference < configNTPNegativeTime {
log.Warnf("Time out of sync (NTP): %v | (time.Now()): %v | (Difference): %v | (Allowed): +%v / %v", NTPTime, currentTime, NTPcurrentTimeDifference, configNTPTime, configNTPNegativeTime)
if *bot.config.Logging.Enabled && bot.config.NTPClient.Level == 0 {
disable, errNTP := bot.config.DisableNTPCheck(os.Stdin)
if errNTP != nil {
log.Errorf("failed to disable ntp time check reason: %v", errNTP)
} else {
log.Info(disable)
}
}
}
}
}
}
// AdjustGoMaxProcs adjusts the maximum processes that the CPU can handle.
func AdjustGoMaxProcs() {
log.Debugln("Adjusting bot runtime performance..")
maxProcsEnv := os.Getenv("GOMAXPROCS")
maxProcs := runtime.NumCPU()
log.Debugln("Number of CPU's detected:", maxProcs)
if maxProcsEnv != "" {
log.Debugln("GOMAXPROCS env =", maxProcsEnv)
env, err := strconv.Atoi(maxProcsEnv)
if err != nil {
log.Debugf("Unable to convert GOMAXPROCS to int, using %d", maxProcs)
} else {
maxProcs = env
}
}
if i := runtime.GOMAXPROCS(maxProcs); i != maxProcs {
log.Error("Go Max Procs were not set correctly.")
}
log.Debugln("Set GOMAXPROCS to:", maxProcs)
}
// HandleInterrupt monitors and captures the SIGTERM in a new goroutine then
// shuts down bot
func HandleInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-c
log.Debugf("Captured %v, shutdown requested.", sig)
close(bot.shutdown)
}()
}
// Shutdown correctly shuts down bot saving configuration files
func Shutdown() {
log.Debugln("Bot shutting down..")
if len(portfolio.Portfolio.Addresses) != 0 {
bot.config.Portfolio = portfolio.Portfolio
}
if !bot.dryRun {
err := bot.config.SaveConfig(bot.configFile)
if err != nil {
log.Warn("Unable to save config.")
} else {
log.Debugln("Config file saved successfully.")
}
}
log.Debugln("Exiting.")
log.CloseLogFile()
os.Exit(0)
}