mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* ALMOST THERE * more api wips * more api thingz * testing n more api wipz * more apiz * more wips * what is goin on * more wips * whip n testing * testing * testing no keys * remove log * kraken is broken ugh * still broken * fixing auth funcs + usdtm api docs * wip * api stuffs * whip * more wips * whip * more wip * api wip n testing * wip * wip * unsaved * wip n testing * wip * wip * wip * wip * wip * wip * wip * wip * wip * whip * wrapper authenticated functions * adding asset type and fixing dependencies * wip * binance auth wrapper start * wrapper functionality * wip * wip * wip * wrapper cancel functions * order submission for wrappers * wip * more error fixing and nits * websocket beginning n error fix * wip * WOW * glorious n shazzy nits * useless nits * wip * fixing things * merge stuffs * crapveyor * crapveyor rebuild * probably broke more things than he fixed * rm lns n other thangs * hope * please * stop it * done * ofcourse * rm vb * fix lbank * appveyor please * float lev * DONT ASK RYAN FOR HELP EVER * wip * wip * endpoint upgrades continued * path upgrade * NeeeNeeeNeeeNeeeNING * fix stuffs * fixing time issue * fixing broken funcs * glorious nits * shaz changes * fixing errors for fundmon * more error fixing for fundmon * test running past 30s * basic changes * THX AGAIN SHAZBERT * path system upgrade * config upgrade * unsaved stuffs * broken wip config upgrade * path system upgrade contd. * path system upgrade contd * path upgrade ready for review * testing verbose removed * linter stuffs * appveyor stuffs * appveyor stuff * fixed? * bugfix * wip * broken stuff * fix test * wierd hack fix * appveyor pls stop * error found * more useless nits * bitmex err * broken wip * broken wip path upgrade change to uint32 * changed url lookups to uint * WOW * ready4review * config fixed HOPEFULLY * config fix and glorious changes * efficient way of getting orders and open orders * binance wrapper logic fixing * testing, adding tests and fixing lot of errrrrs * merge master * appveyor stuffs * appveyor stuffs * fmt * test * octalLiteral issue fix? * octalLiteral fix? * rm vb * prnt ln to restart * adding testz * test fixzzz * READY FOR REVIEW * Actually ready now * FORMATTING * addressing shazzy n glorious nits * crapveyor * rm vb * small change * fixing err * shazbert nits * review changes * requested changes * more requested changes * noo * last nit fixes * restart appveyor * improving test cov * Update .golangci.yml * shazbert changes * moving pair formatting * format pair update wip * path upgrade complete * error fix * appveyor linters * more linters * remove testexch * more formatting changes * changes * shazbert changes * checking older requested changes to ensure completion * wip * fixing broken code * error fix * all fixed * additional changes * more changes * remove commented code * ftx margin api * appveyor fixes * more appveyor issues + test addition * more appveyor issues + test addition * remove unnecessary * testing * testing, fixing okex api, error fix * git merge fix * go sum * glorious changes and error fix * rm vb * more glorious changes and go mod tidy * fixed now * okex testing upgrade * old config migration and batch fetching fix * added test * glorious requested changes WIP * tested and fixed * go fmted * go fmt and test fix * additional funcs and tests for fundingRates * OKEX tested and fixed * appveyor fixes * ineff assign * 1 glorious change * error fix * typo * shazbert changes * glorious code changes and path fixing huobi WIP * adding assetType to accountinfo functions * fixing panic * panic fix and updating account info wrappers WIP * updateaccountinfo updated * testing WIP binance USDT n Coin Margined and Kraken Futures * auth functions tested and fixed * added test * config reverted * shazbert and glorious changes * shazbert and glorious changes * latest changes and portfolio update * go fmt change: * remove commented codes * improved error checking * index out of range fix * rm ln * critical nit * glorious latest changes * appveyor changes * shazbert change * easier readability * latest glorious changes * shadow dec * assetstore updated * last change * another last change * merge changes * go mod tidy * thrasher requested changes wip * improving struct layouts * appveyor go fmt * remove unnecessary code * shazbert changes * small change * oopsie * tidy * configtest reverted * error fix * oopsie * for what * test patch fix * insecurities * fixing tests * fix config
392 lines
10 KiB
Go
392 lines
10 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/binance"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/bitfinex"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/bitflyer"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/bithumb"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/bitmex"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/bitstamp"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/bittrex"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/btcmarkets"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/btse"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/coinbasepro"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/coinbene"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/coinut"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/exmo"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/ftx"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/gateio"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/gemini"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/hitbtc"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/huobi"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/itbit"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kraken"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/lakebtc"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/lbank"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/localbitcoins"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/okcoin"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/okex"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/poloniex"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/yobit"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/zb"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
)
|
|
|
|
// vars related to exchange functions
|
|
var (
|
|
ErrNoExchangesLoaded = errors.New("no exchanges have been loaded")
|
|
ErrExchangeNotFound = errors.New("exchange not found")
|
|
ErrExchangeAlreadyLoaded = errors.New("exchange already loaded")
|
|
ErrExchangeFailedToLoad = errors.New("exchange failed to load")
|
|
)
|
|
|
|
type exchangeManager struct {
|
|
m sync.Mutex
|
|
exchanges map[string]exchange.IBotExchange
|
|
}
|
|
|
|
func (bot *Engine) dryrunParamInteraction(param string) {
|
|
if !bot.Settings.CheckParamInteraction {
|
|
return
|
|
}
|
|
|
|
if !bot.Settings.EnableDryRun {
|
|
log.Warnf(log.Global,
|
|
"Command line argument '-%s' induces dry run mode."+
|
|
" Set -dryrun=false if you wish to override this.",
|
|
param)
|
|
bot.Settings.EnableDryRun = true
|
|
}
|
|
}
|
|
|
|
func (e *exchangeManager) add(exch exchange.IBotExchange) {
|
|
e.m.Lock()
|
|
if e.exchanges == nil {
|
|
e.exchanges = make(map[string]exchange.IBotExchange)
|
|
}
|
|
e.exchanges[strings.ToLower(exch.GetName())] = exch
|
|
e.m.Unlock()
|
|
}
|
|
|
|
func (e *exchangeManager) getExchanges() []exchange.IBotExchange {
|
|
if e.Len() == 0 {
|
|
return nil
|
|
}
|
|
|
|
e.m.Lock()
|
|
defer e.m.Unlock()
|
|
var exchs []exchange.IBotExchange
|
|
for x := range e.exchanges {
|
|
exchs = append(exchs, e.exchanges[x])
|
|
}
|
|
return exchs
|
|
}
|
|
|
|
func (e *exchangeManager) removeExchange(exchName string) error {
|
|
if e.Len() == 0 {
|
|
return ErrNoExchangesLoaded
|
|
}
|
|
exch := e.getExchangeByName(exchName)
|
|
if exch == nil {
|
|
return ErrExchangeNotFound
|
|
}
|
|
e.m.Lock()
|
|
defer e.m.Unlock()
|
|
delete(e.exchanges, strings.ToLower(exchName))
|
|
log.Infof(log.ExchangeSys, "%s exchange unloaded successfully.\n", exchName)
|
|
return nil
|
|
}
|
|
|
|
func (e *exchangeManager) getExchangeByName(exchangeName string) exchange.IBotExchange {
|
|
if e.Len() == 0 {
|
|
return nil
|
|
}
|
|
e.m.Lock()
|
|
defer e.m.Unlock()
|
|
exch, ok := e.exchanges[strings.ToLower(exchangeName)]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
return exch
|
|
}
|
|
|
|
func (e *exchangeManager) Len() int {
|
|
e.m.Lock()
|
|
defer e.m.Unlock()
|
|
return len(e.exchanges)
|
|
}
|
|
|
|
func (e *exchangeManager) unloadExchange(exchangeName string) error {
|
|
exchCfg, err := Bot.Config.GetExchangeConfig(exchangeName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = e.removeExchange(exchangeName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
exchCfg.Enabled = false
|
|
return nil
|
|
}
|
|
|
|
// GetExchangeByName returns an exchange given an exchange name
|
|
func (bot *Engine) GetExchangeByName(exchName string) exchange.IBotExchange {
|
|
return bot.exchangeManager.getExchangeByName(exchName)
|
|
}
|
|
|
|
// UnloadExchange unloads an exchange by name
|
|
func (bot *Engine) UnloadExchange(exchName string) error {
|
|
return bot.exchangeManager.unloadExchange(exchName)
|
|
}
|
|
|
|
// GetExchanges retrieves the loaded exchanges
|
|
func (bot *Engine) GetExchanges() []exchange.IBotExchange {
|
|
return bot.exchangeManager.getExchanges()
|
|
}
|
|
|
|
// LoadExchange loads an exchange by name
|
|
func (bot *Engine) LoadExchange(name string, useWG bool, wg *sync.WaitGroup) error {
|
|
nameLower := strings.ToLower(name)
|
|
var exch exchange.IBotExchange
|
|
|
|
if bot.exchangeManager.getExchangeByName(nameLower) != nil {
|
|
return ErrExchangeAlreadyLoaded
|
|
}
|
|
|
|
switch nameLower {
|
|
case "binance":
|
|
exch = new(binance.Binance)
|
|
case "bitfinex":
|
|
exch = new(bitfinex.Bitfinex)
|
|
case "bitflyer":
|
|
exch = new(bitflyer.Bitflyer)
|
|
case "bithumb":
|
|
exch = new(bithumb.Bithumb)
|
|
case "bitmex":
|
|
exch = new(bitmex.Bitmex)
|
|
case "bitstamp":
|
|
exch = new(bitstamp.Bitstamp)
|
|
case "bittrex":
|
|
exch = new(bittrex.Bittrex)
|
|
case "btc markets":
|
|
exch = new(btcmarkets.BTCMarkets)
|
|
case "btse":
|
|
exch = new(btse.BTSE)
|
|
case "coinbene":
|
|
exch = new(coinbene.Coinbene)
|
|
case "coinut":
|
|
exch = new(coinut.COINUT)
|
|
case "exmo":
|
|
exch = new(exmo.EXMO)
|
|
case "coinbasepro":
|
|
exch = new(coinbasepro.CoinbasePro)
|
|
case "ftx":
|
|
exch = new(ftx.FTX)
|
|
case "gateio":
|
|
exch = new(gateio.Gateio)
|
|
case "gemini":
|
|
exch = new(gemini.Gemini)
|
|
case "hitbtc":
|
|
exch = new(hitbtc.HitBTC)
|
|
case "huobi":
|
|
exch = new(huobi.HUOBI)
|
|
case "itbit":
|
|
exch = new(itbit.ItBit)
|
|
case "kraken":
|
|
exch = new(kraken.Kraken)
|
|
case "lakebtc":
|
|
exch = new(lakebtc.LakeBTC)
|
|
case "lbank":
|
|
exch = new(lbank.Lbank)
|
|
case "localbitcoins":
|
|
exch = new(localbitcoins.LocalBitcoins)
|
|
case "okcoin international":
|
|
exch = new(okcoin.OKCoin)
|
|
case "okex":
|
|
exch = new(okex.OKEX)
|
|
case "poloniex":
|
|
exch = new(poloniex.Poloniex)
|
|
case "yobit":
|
|
exch = new(yobit.Yobit)
|
|
case "zb":
|
|
exch = new(zb.ZB)
|
|
default:
|
|
return ErrExchangeNotFound
|
|
}
|
|
|
|
if exch == nil {
|
|
return ErrExchangeFailedToLoad
|
|
}
|
|
|
|
exch.SetDefaults()
|
|
exchCfg, err := bot.Config.GetExchangeConfig(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if bot.Settings.EnableAllPairs {
|
|
if exchCfg.CurrencyPairs != nil {
|
|
bot.dryrunParamInteraction("enableallpairs")
|
|
assets := exchCfg.CurrencyPairs.GetAssetTypes()
|
|
for x := range assets {
|
|
var pairs currency.Pairs
|
|
pairs, err = exchCfg.CurrencyPairs.GetPairs(assets[x], false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
exchCfg.CurrencyPairs.StorePairs(assets[x], pairs, true)
|
|
}
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableExchangeVerbose {
|
|
bot.dryrunParamInteraction("exchangeverbose")
|
|
exchCfg.Verbose = true
|
|
}
|
|
|
|
if bot.Settings.EnableExchangeWebsocketSupport {
|
|
bot.dryrunParamInteraction("exchangewebsocketsupport")
|
|
if exchCfg.Features != nil {
|
|
if exchCfg.Features.Supports.Websocket {
|
|
exchCfg.Features.Enabled.Websocket = true
|
|
}
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableExchangeAutoPairUpdates {
|
|
bot.dryrunParamInteraction("exchangeautopairupdates")
|
|
if exchCfg.Features != nil {
|
|
if exchCfg.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
|
exchCfg.Features.Enabled.AutoPairUpdates = true
|
|
}
|
|
}
|
|
}
|
|
|
|
if bot.Settings.DisableExchangeAutoPairUpdates {
|
|
bot.dryrunParamInteraction("exchangedisableautopairupdates")
|
|
if exchCfg.Features != nil {
|
|
if exchCfg.Features.Supports.RESTCapabilities.AutoPairUpdates {
|
|
exchCfg.Features.Enabled.AutoPairUpdates = false
|
|
}
|
|
}
|
|
}
|
|
|
|
if bot.Settings.HTTPUserAgent != "" {
|
|
bot.dryrunParamInteraction("httpuseragent")
|
|
exchCfg.HTTPUserAgent = bot.Settings.HTTPUserAgent
|
|
}
|
|
|
|
if bot.Settings.HTTPProxy != "" {
|
|
bot.dryrunParamInteraction("httpproxy")
|
|
exchCfg.ProxyAddress = bot.Settings.HTTPProxy
|
|
}
|
|
|
|
if bot.Settings.HTTPTimeout != exchange.DefaultHTTPTimeout {
|
|
bot.dryrunParamInteraction("httptimeout")
|
|
exchCfg.HTTPTimeout = bot.Settings.HTTPTimeout
|
|
}
|
|
|
|
if bot.Settings.EnableExchangeHTTPDebugging {
|
|
bot.dryrunParamInteraction("exchangehttpdebugging")
|
|
exchCfg.HTTPDebugging = bot.Settings.EnableExchangeHTTPDebugging
|
|
}
|
|
|
|
if bot.Settings.EnableAllExchanges {
|
|
bot.dryrunParamInteraction("enableallexchanges")
|
|
}
|
|
|
|
if !bot.Settings.EnableExchangeHTTPRateLimiter {
|
|
log.Warnf(log.ExchangeSys,
|
|
"Loaded exchange %s rate limiting has been turned off.\n",
|
|
exch.GetName(),
|
|
)
|
|
err = exch.DisableRateLimiter()
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys,
|
|
"Loaded exchange %s rate limiting cannot be turned off: %s.\n",
|
|
exch.GetName(),
|
|
err,
|
|
)
|
|
}
|
|
}
|
|
|
|
exchCfg.Enabled = true
|
|
err = exch.Setup(exchCfg)
|
|
if err != nil {
|
|
exchCfg.Enabled = false
|
|
return err
|
|
}
|
|
|
|
bot.exchangeManager.add(exch)
|
|
|
|
base := exch.GetBase()
|
|
if base.API.AuthenticatedSupport ||
|
|
base.API.AuthenticatedWebsocketSupport {
|
|
assetTypes := base.GetAssetTypes()
|
|
var useAsset asset.Item
|
|
for a := range assetTypes {
|
|
err = base.CurrencyPairs.IsAssetEnabled(assetTypes[a])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
useAsset = assetTypes[a]
|
|
break
|
|
}
|
|
err = exch.ValidateCredentials(useAsset)
|
|
if err != nil {
|
|
log.Warnf(log.ExchangeSys,
|
|
"%s: Cannot validate credentials, authenticated support has been disabled, Error: %s\n",
|
|
base.Name,
|
|
err)
|
|
base.API.AuthenticatedSupport = false
|
|
base.API.AuthenticatedWebsocketSupport = false
|
|
exchCfg.API.AuthenticatedSupport = false
|
|
exchCfg.API.AuthenticatedWebsocketSupport = false
|
|
}
|
|
}
|
|
|
|
if useWG {
|
|
exch.Start(wg)
|
|
} else {
|
|
tempWG := sync.WaitGroup{}
|
|
exch.Start(&tempWG)
|
|
tempWG.Wait()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetupExchanges sets up the exchanges used by the Bot
|
|
func (bot *Engine) SetupExchanges() {
|
|
var wg sync.WaitGroup
|
|
configs := bot.Config.GetAllExchangeConfigs()
|
|
for x := range configs {
|
|
if !configs[x].Enabled && !bot.Settings.EnableAllExchanges {
|
|
log.Debugf(log.ExchangeSys, "%s: Exchange support: Disabled\n", configs[x].Name)
|
|
continue
|
|
}
|
|
err := bot.LoadExchange(configs[x].Name, true, &wg)
|
|
if err != nil {
|
|
log.Errorf(log.ExchangeSys, "LoadExchange %s failed: %s\n", configs[x].Name, err)
|
|
continue
|
|
}
|
|
log.Debugf(log.ExchangeSys,
|
|
"%s: Exchange support: Enabled (Authenticated API support: %s - Verbose mode: %s).\n",
|
|
configs[x].Name,
|
|
common.IsEnabled(configs[x].API.AuthenticatedSupport),
|
|
common.IsEnabled(configs[x].Verbose),
|
|
)
|
|
}
|
|
wg.Wait()
|
|
}
|