mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-15 07:26:49 +00:00
Separates bot configuration to its own method to allow for repeat use. Also main.go should be small Now reloads bot configuration on the save post. Active changes!
211 lines
4.9 KiB
Go
211 lines
4.9 KiB
Go
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
|
|
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)
|
|
}
|
|
|
|
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))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|