mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-15 15:09:55 +00:00
* End of day commit moving packages and setting foundation into how trade processing will go * Conformity * tdd candle generation based on received trade data, renames orderbookbuffer back to buffer for now... * Formalises test functions and designs the trade processor * Theoretical amending old candles to allow any trades that were part of an old processed candle to be more accurate. Saving of candles will only occur on previous cycles, extending memory usage a bit longer * Figures out sqlboiler for sqlite. Updates websocket entries to process trade data * One more trade data * Adds more exchange support * Adds PSQL stuff * Begins creating sql implementation * End of day commit. Helper functions and understanding sql usage in GCT * Adds delete and cleans up table design * Finishes trades conceptually. Awaits candle data update in order to translate trades to candles * Initial handling of trades in coinbene * Proto * Fixing of some bugs, attempting to address coinbene asset type ws issues * Fixes up coinbene websocket implementation for the most part * finalises coinbene websocket implementation. Adds new ability to parse currencies without a delimiter * Implements rpc commands and adds testing * updates the following to be compatible with trade data update: Theoretical amending old candles to allow any trades that were part of an old processed candle to be more accurate. Saving of candles will only occur on previous cycles, extending memory usage a bit longer * Changes trade to be its own entity rather than attached to a websocket. * Adds coverage to trades. Changes signature of `AddTradesToBuffer` to return error. Now automatically shuts down without need for channel listening. Will automatically start up again if it gets data * Implements trade fetching at the wrapper level for a bunch of exchanges. Adds trade id to script updoot. Probably breaking change * Implements trade fetching for all wrappers hurray hurrah. Updates all the tests * Adds new interface func to get recent trades. Ensures GetExchangeHistory continues until conditions are met * Adds new readme, tests all new wrapper endpoints, updates exchange_wrapper_issues to test new endpoints. Updates exchange_wrapper_coverage with new coverage... Fixes lame bug causing wrapper tests to fail from being poorly setup. Adds loopy loop to ensure that all data is captured when requesting exchange history * Bugfix on psql migrations. Rebases latest changes, updates table design to use base and quote, updates trades to use exchange_name_id * Adds new config field for saving trades to the database per exchange. Now exits trade processing when trade saving is not enabled. Similarly for wrapper, does not save if not enabled * Minor bitfinex trade fixes. continues on buffer processing errors, now saves transactionid to the db * Adds support for generating candles from candlesextended. May extend it further, idk * Updates trade candles to be able to fill missing data with trades. Adds more tests. Also does a thing where you can forcefully override a candle based on internal trade data instead of API data * Fixes bug where force deletions did not follow up with insertions. Adds force to candle commands * Fixes specific exchange based issues. Extends recent trades to 24 hours where possible * Fixes issue with saved tests. Fixes tests for trades. Adds parallel to tests. Pre-fixes people's nits * Adds new GRPC functions to find out what data is missing from trades and candles. Fixes some assumptions from missing period code. * Adds unique constraint. Fixes up niggling issues for wrappers and websockets * Fixes issues with using unix times in the database trying to retrieve data via the CLI. Reduces save time to 15 seconds * Updates trades to use timestamps instead of int64 unix * Adds missing FTX wrapper implementation. Regens docs * Linting the linters. Updating readme * Adds new command to set whether an exchange can process trades * Doc update * Adds recent trades and historic trade endpoints to grpc * formats pair_test.go to appease linter gods * Addresses data race. Removes logging of missing intervals on unrelated function (now that it has its own rpc command). The buffer time isnt customisable, but I don't feel it needs to be at a config level at all really. * Fixes a few niterinos regarding spacing, type conversion, a weird Bitmex 0 trade value error, unsubscriptions and cli command references * Reduces map lookups. Adds base func and moves wrappers to use it * Uses better currency formatter. Adds time based validation to trade history. Reverts configtest.json * Reverts config and updates test names. Also WAYYYYY LESS SPAMMY * oopsie doopsie missed a whoopsie * mint flavoured lint * Fixes issues caused by rebase * Fixes issue with timestamps not converting properly from command to RPCServer. Adds new error type. Adds shorthand entries to some commands. Removes os.Exit from tests. Makes Gemini test rolling. Adds enabled exchange check to RPC function. Escapes timestamp on bitstamp. Renames var * fixes whoopsie oopsie doopsie I forgot to remove code shoopsie * missed a line * 🎉 🎉 :tada:Breaks everything in an end of day commit 🎉 🎉 🎉 * Modifies function 'createlocaloffset' to return a string instead. Uses strings for all time based start and end commands. Uses UTC times in RPC server and updates SQLITE to use formatted time based queries * Adds concurrency-safe way of changing SaveTradeData and checking it. Fixes embarrassing typo * End of day fix, adds bitfinex update to loop until either the return trades shows no new dates, or meets specifications. Fixes egregious typo * Improves testing and handling of historical trades function * Fixes tests after latest changes * Fix potential fatal err now that db is enabled in test config now * Fixes up some database settings to use a local engine instead of global var * DELICIOUS LINT CHOCOLATE FIXES * Fixes data race by slashing competitor's tyres * Adds mock test fixes to allow for live and stored data test * Removes verbosity in engine level tests. Adds new timezone format to highlight the timezone for RPC functions. Removes reference to Preix index fund * Oopsie doopsie, fixed a whoopsie * Loggers can no longer do data drag races on my lawn 👴 * Removes bad lock * Addresses command nits. End of day conceptual commit, trying to calculate spans of time in the context of missing periods. Tests will fail * Adds new stream response for retrieving trade history as it can take time to do. Unsuccessfully attempts to simplify time range calculation for missing trades response * Adds new timeperiods package to calculate time periods, time ranges and whether data is in those ranges. Removes kline basic implementation of same concept * Fixes lint issues. Fixes test. Moves trade cli commands to their own trade subcommands * Updates lakebtc to no longer have gethistorictrades as it is unsupported. Adds more validation to rpc functions * Removes requirement to have trades when testing trade wrapper functions. Doesn't really prove it works if there are no trades for a given currency in a time period. * Addresses nits, runs linting fix and ensures a test is consistent * Fix merge issues * Moves sort to timeperiods. Adds test coverage. Fixes typo * Removes log package in CLI * Fixes `GetTrades` url * Reorders all instances of validation occuring after settingup RPC connection * Fixes test to ensure that it is setup before testing that it is setup * Fixed issue with bool retrieval. Removes double append * Fixes Binance times, fixes bitfinex sell sides, fixes huobi times, sorts all responses * Fixes poloniex trade id consistency. Makes recent trade for poloniex consistent with others (15 minutes). Fixes coinbene. Fixes localbitcoins to use quote currency. Fixes coinut times. Updates huobi trade id, saves okgroup trades. Fixes bid and ask to buy and sell * Removes websocket trades for lakebtc as it did not meet our requirements for processing. Adds new constraints to the database to ensure we have uniqueness on trades where ID doesn't exist and doesn't trigger errors for trades where the tid does * Fixes migration for postgres to downscale properly * Really really fixes the psql index changes * Fixes broken tests * Now with working tests and no pocket lint * Makes the side column nullable with no more constraint for it. adds migrations and runs generation. comments lakebtc * Lint & Sprüngli * Updates zb to use more appropriate side * Fixes oopsie * Attempts to address a data race from globals * Fixes build * Fixes missed regen rpc files * Updates readme to point to trade readme. Fixes exchange_wrapper_coverage wrapper count and untested panics, tests bitfinex funding pair test for `fUSD`, adds shiny new param `tradeprocessinginterval` * mint flavoured lint * Uses the real default to set the default value by default * Fixes some extra tests surrounding email sending and number incompatibility * Reverts test config * re-adds gom2/usdt currency * Fixes typo, don't look! * Fixes minor codelingo pickups * Adds more precision to handling of trade data from Kraken. Expands test * interface christmas tree * lint
559 lines
21 KiB
Go
559 lines
21 KiB
Go
package engine
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/common"
|
|
"github.com/thrasher-corp/gocryptotrader/config"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/currency/coinmarketcap"
|
|
"github.com/thrasher-corp/gocryptotrader/dispatch"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
|
gctscript "github.com/thrasher-corp/gocryptotrader/gctscript/vm"
|
|
gctlog "github.com/thrasher-corp/gocryptotrader/log"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
|
|
"github.com/thrasher-corp/gocryptotrader/utils"
|
|
)
|
|
|
|
// Engine contains configuration, portfolio, exchange & ticker data and is the
|
|
// overarching type across this code base.
|
|
type Engine struct {
|
|
Config *config.Config
|
|
Portfolio *portfolio.Base
|
|
ExchangeCurrencyPairManager *ExchangeCurrencyPairSyncer
|
|
NTPManager ntpManager
|
|
ConnectionManager connectionManager
|
|
DatabaseManager databaseManager
|
|
GctScriptManager gctScriptManager
|
|
OrderManager orderManager
|
|
PortfolioManager portfolioManager
|
|
CommsManager commsManager
|
|
exchangeManager exchangeManager
|
|
DepositAddressManager *DepositAddressManager
|
|
Settings Settings
|
|
Uptime time.Time
|
|
ServicesWG sync.WaitGroup
|
|
}
|
|
|
|
// Vars for engine
|
|
var (
|
|
Bot *Engine
|
|
)
|
|
|
|
// New starts a new engine
|
|
func New() (*Engine, error) {
|
|
var b Engine
|
|
b.Config = &config.Cfg
|
|
|
|
err := b.Config.LoadConfig("", false)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to load config. Err: %s", err)
|
|
}
|
|
|
|
return &b, nil
|
|
}
|
|
|
|
// NewFromSettings starts a new engine based on supplied settings
|
|
func NewFromSettings(settings *Settings, flagSet map[string]bool) (*Engine, error) {
|
|
if settings == nil {
|
|
return nil, errors.New("engine: settings is nil")
|
|
}
|
|
|
|
var b Engine
|
|
var err error
|
|
|
|
b.Config, err = loadConfigWithSettings(settings, flagSet)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to load config. Err: %s", err)
|
|
}
|
|
|
|
if *b.Config.Logging.Enabled {
|
|
gctlog.SetupGlobalLogger()
|
|
gctlog.SetupSubLoggers(b.Config.Logging.SubLoggers)
|
|
gctlog.Infoln(gctlog.Global, "Logger initialised.")
|
|
}
|
|
|
|
b.Settings.ConfigFile = settings.ConfigFile
|
|
b.Settings.DataDir = b.Config.GetDataPath()
|
|
b.Settings.CheckParamInteraction = settings.CheckParamInteraction
|
|
|
|
err = utils.AdjustGoMaxProcs(settings.GoMaxProcs)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to adjust runtime GOMAXPROCS value. Err: %s", err)
|
|
}
|
|
|
|
validateSettings(&b, settings, flagSet)
|
|
return &b, nil
|
|
}
|
|
|
|
// loadConfigWithSettings creates configuration based on the provided settings
|
|
func loadConfigWithSettings(settings *Settings, flagSet map[string]bool) (*config.Config, error) {
|
|
filePath, err := config.GetAndMigrateDefaultPath(settings.ConfigFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
log.Printf("Loading config file %s..\n", filePath)
|
|
|
|
conf := &config.Cfg
|
|
err = conf.ReadConfig(filePath, settings.EnableDryRun)
|
|
if err != nil {
|
|
return nil, fmt.Errorf(config.ErrFailureOpeningConfig, filePath, err)
|
|
}
|
|
// Apply overrides from settings
|
|
if flagSet["datadir"] {
|
|
// warn if dryrun isn't enabled
|
|
if !settings.EnableDryRun {
|
|
log.Println("Command line argument '-datadir' induces dry run mode.")
|
|
}
|
|
settings.EnableDryRun = true
|
|
conf.DataDirectory = settings.DataDir
|
|
}
|
|
|
|
return conf, conf.CheckConfig()
|
|
}
|
|
|
|
// validateSettings validates and sets all bot settings
|
|
func validateSettings(b *Engine, s *Settings, flagSet map[string]bool) {
|
|
b.Settings.Verbose = s.Verbose
|
|
b.Settings.EnableDryRun = s.EnableDryRun
|
|
b.Settings.EnableAllExchanges = s.EnableAllExchanges
|
|
b.Settings.EnableAllPairs = s.EnableAllPairs
|
|
b.Settings.EnableCoinmarketcapAnalysis = s.EnableCoinmarketcapAnalysis
|
|
b.Settings.EnableDatabaseManager = s.EnableDatabaseManager
|
|
b.Settings.EnableGCTScriptManager = s.EnableGCTScriptManager
|
|
b.Settings.MaxVirtualMachines = s.MaxVirtualMachines
|
|
b.Settings.EnableDispatcher = s.EnableDispatcher
|
|
b.Settings.EnablePortfolioManager = s.EnablePortfolioManager
|
|
b.Settings.WithdrawCacheSize = s.WithdrawCacheSize
|
|
if b.Settings.EnablePortfolioManager {
|
|
if b.Settings.PortfolioManagerDelay != time.Duration(0) && s.PortfolioManagerDelay > 0 {
|
|
b.Settings.PortfolioManagerDelay = s.PortfolioManagerDelay
|
|
} else {
|
|
b.Settings.PortfolioManagerDelay = PortfolioSleepDelay
|
|
}
|
|
}
|
|
|
|
if flagSet["grpc"] {
|
|
b.Settings.EnableGRPC = s.EnableGRPC
|
|
} else {
|
|
b.Settings.EnableGRPC = b.Config.RemoteControl.GRPC.Enabled
|
|
}
|
|
|
|
if flagSet["grpcproxy"] {
|
|
b.Settings.EnableGRPCProxy = s.EnableGRPCProxy
|
|
} else {
|
|
b.Settings.EnableGRPCProxy = b.Config.RemoteControl.GRPC.GRPCProxyEnabled
|
|
}
|
|
|
|
if flagSet["websocketrpc"] {
|
|
b.Settings.EnableWebsocketRPC = s.EnableWebsocketRPC
|
|
} else {
|
|
b.Settings.EnableWebsocketRPC = b.Config.RemoteControl.WebsocketRPC.Enabled
|
|
}
|
|
|
|
if flagSet["deprecatedrpc"] {
|
|
b.Settings.EnableDeprecatedRPC = s.EnableDeprecatedRPC
|
|
} else {
|
|
b.Settings.EnableDeprecatedRPC = b.Config.RemoteControl.DeprecatedRPC.Enabled
|
|
}
|
|
|
|
if flagSet["gctscriptmanager"] {
|
|
gctscript.GCTScriptConfig.Enabled = s.EnableGCTScriptManager
|
|
}
|
|
|
|
if flagSet["maxvirtualmachines"] {
|
|
gctscript.GCTScriptConfig.MaxVirtualMachines = uint8(s.MaxVirtualMachines)
|
|
}
|
|
|
|
if flagSet["withdrawcachesize"] {
|
|
withdraw.CacheSize = s.WithdrawCacheSize
|
|
}
|
|
|
|
b.Settings.EnableCommsRelayer = s.EnableCommsRelayer
|
|
b.Settings.EnableEventManager = s.EnableEventManager
|
|
|
|
if b.Settings.EnableEventManager {
|
|
if b.Settings.EventManagerDelay != time.Duration(0) && s.EventManagerDelay > 0 {
|
|
b.Settings.EventManagerDelay = s.EventManagerDelay
|
|
} else {
|
|
b.Settings.EventManagerDelay = EventSleepDelay
|
|
}
|
|
}
|
|
|
|
b.Settings.EnableConnectivityMonitor = s.EnableConnectivityMonitor
|
|
b.Settings.EnableNTPClient = s.EnableNTPClient
|
|
b.Settings.EnableOrderManager = s.EnableOrderManager
|
|
b.Settings.EnableExchangeSyncManager = s.EnableExchangeSyncManager
|
|
b.Settings.EnableTickerSyncing = s.EnableTickerSyncing
|
|
b.Settings.EnableOrderbookSyncing = s.EnableOrderbookSyncing
|
|
b.Settings.EnableTradeSyncing = s.EnableTradeSyncing
|
|
b.Settings.SyncWorkers = s.SyncWorkers
|
|
b.Settings.SyncTimeout = s.SyncTimeout
|
|
b.Settings.SyncContinuously = s.SyncContinuously
|
|
b.Settings.EnableDepositAddressManager = s.EnableDepositAddressManager
|
|
b.Settings.EnableExchangeAutoPairUpdates = s.EnableExchangeAutoPairUpdates
|
|
b.Settings.EnableExchangeWebsocketSupport = s.EnableExchangeWebsocketSupport
|
|
b.Settings.EnableExchangeRESTSupport = s.EnableExchangeRESTSupport
|
|
b.Settings.EnableExchangeVerbose = s.EnableExchangeVerbose
|
|
b.Settings.EnableExchangeHTTPRateLimiter = s.EnableExchangeHTTPRateLimiter
|
|
b.Settings.EnableExchangeHTTPDebugging = s.EnableExchangeHTTPDebugging
|
|
b.Settings.DisableExchangeAutoPairUpdates = s.DisableExchangeAutoPairUpdates
|
|
b.Settings.ExchangePurgeCredentials = s.ExchangePurgeCredentials
|
|
b.Settings.EnableWebsocketRoutine = s.EnableWebsocketRoutine
|
|
|
|
// Checks if the flag values are different from the defaults
|
|
b.Settings.MaxHTTPRequestJobsLimit = s.MaxHTTPRequestJobsLimit
|
|
if b.Settings.MaxHTTPRequestJobsLimit != int(request.DefaultMaxRequestJobs) &&
|
|
s.MaxHTTPRequestJobsLimit > 0 {
|
|
request.MaxRequestJobs = int32(b.Settings.MaxHTTPRequestJobsLimit)
|
|
}
|
|
|
|
b.Settings.TradeBufferProcessingInterval = s.TradeBufferProcessingInterval
|
|
if b.Settings.TradeBufferProcessingInterval != trade.DefaultProcessorIntervalTime {
|
|
if b.Settings.TradeBufferProcessingInterval >= time.Second {
|
|
trade.BufferProcessorIntervalTime = b.Settings.TradeBufferProcessingInterval
|
|
} else {
|
|
b.Settings.TradeBufferProcessingInterval = trade.DefaultProcessorIntervalTime
|
|
gctlog.Warnf(gctlog.Global, "-tradeprocessinginterval must be >= to 1 second, using default value of %v",
|
|
trade.DefaultProcessorIntervalTime)
|
|
}
|
|
}
|
|
|
|
b.Settings.RequestMaxRetryAttempts = s.RequestMaxRetryAttempts
|
|
if b.Settings.RequestMaxRetryAttempts != request.DefaultMaxRetryAttempts && s.RequestMaxRetryAttempts > 0 {
|
|
request.MaxRetryAttempts = b.Settings.RequestMaxRetryAttempts
|
|
}
|
|
|
|
b.Settings.HTTPTimeout = s.HTTPTimeout
|
|
if s.HTTPTimeout != time.Duration(0) && s.HTTPTimeout > 0 {
|
|
b.Settings.HTTPTimeout = s.HTTPTimeout
|
|
} else {
|
|
b.Settings.HTTPTimeout = b.Config.GlobalHTTPTimeout
|
|
}
|
|
|
|
b.Settings.HTTPUserAgent = s.HTTPUserAgent
|
|
b.Settings.HTTPProxy = s.HTTPProxy
|
|
|
|
if s.GlobalHTTPTimeout != time.Duration(0) && s.GlobalHTTPTimeout > 0 {
|
|
b.Settings.GlobalHTTPTimeout = s.GlobalHTTPTimeout
|
|
} else {
|
|
b.Settings.GlobalHTTPTimeout = b.Config.GlobalHTTPTimeout
|
|
}
|
|
common.HTTPClient = common.NewHTTPClientWithTimeout(b.Settings.GlobalHTTPTimeout)
|
|
|
|
b.Settings.GlobalHTTPUserAgent = s.GlobalHTTPUserAgent
|
|
if b.Settings.GlobalHTTPUserAgent != "" {
|
|
common.HTTPUserAgent = b.Settings.GlobalHTTPUserAgent
|
|
}
|
|
|
|
b.Settings.GlobalHTTPProxy = s.GlobalHTTPProxy
|
|
b.Settings.DispatchMaxWorkerAmount = s.DispatchMaxWorkerAmount
|
|
b.Settings.DispatchJobsLimit = s.DispatchJobsLimit
|
|
}
|
|
|
|
// PrintSettings returns the engine settings
|
|
func PrintSettings(s *Settings) {
|
|
gctlog.Debugln(gctlog.Global)
|
|
gctlog.Debugf(gctlog.Global, "ENGINE SETTINGS")
|
|
gctlog.Debugf(gctlog.Global, "- CORE SETTINGS:")
|
|
gctlog.Debugf(gctlog.Global, "\t Verbose mode: %v", s.Verbose)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable dry run mode: %v", s.EnableDryRun)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable all exchanges: %v", s.EnableAllExchanges)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable all pairs: %v", s.EnableAllPairs)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable coinmarketcap analaysis: %v", s.EnableCoinmarketcapAnalysis)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable portfolio manager: %v", s.EnablePortfolioManager)
|
|
gctlog.Debugf(gctlog.Global, "\t Portfolio manager sleep delay: %v\n", s.PortfolioManagerDelay)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable gPRC: %v", s.EnableGRPC)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable gRPC Proxy: %v", s.EnableGRPCProxy)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable websocket RPC: %v", s.EnableWebsocketRPC)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable deprecated RPC: %v", s.EnableDeprecatedRPC)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable comms relayer: %v", s.EnableCommsRelayer)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable event manager: %v", s.EnableEventManager)
|
|
gctlog.Debugf(gctlog.Global, "\t Event manager sleep delay: %v", s.EventManagerDelay)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable order manager: %v", s.EnableOrderManager)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable exchange sync manager: %v", s.EnableExchangeSyncManager)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable deposit address manager: %v\n", s.EnableDepositAddressManager)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable websocket routine: %v\n", s.EnableWebsocketRoutine)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable NTP client: %v", s.EnableNTPClient)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable Database manager: %v", s.EnableDatabaseManager)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable dispatcher: %v", s.EnableDispatcher)
|
|
gctlog.Debugf(gctlog.Global, "\t Dispatch package max worker amount: %d", s.DispatchMaxWorkerAmount)
|
|
gctlog.Debugf(gctlog.Global, "\t Dispatch package jobs limit: %d", s.DispatchJobsLimit)
|
|
gctlog.Debugf(gctlog.Global, "- EXCHANGE SYNCER SETTINGS:\n")
|
|
gctlog.Debugf(gctlog.Global, "\t Exchange sync continuously: %v\n", s.SyncContinuously)
|
|
gctlog.Debugf(gctlog.Global, "\t Exchange sync workers: %v\n", s.SyncWorkers)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable ticker syncing: %v\n", s.EnableTickerSyncing)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable orderbook syncing: %v\n", s.EnableOrderbookSyncing)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable trade syncing: %v\n", s.EnableTradeSyncing)
|
|
gctlog.Debugf(gctlog.Global, "\t Exchange sync timeout: %v\n", s.SyncTimeout)
|
|
gctlog.Debugf(gctlog.Global, "- FOREX SETTINGS:")
|
|
gctlog.Debugf(gctlog.Global, "\t Enable currency conveter: %v", s.EnableCurrencyConverter)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable currency layer: %v", s.EnableCurrencyLayer)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable fixer: %v", s.EnableFixer)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable OpenExchangeRates: %v", s.EnableOpenExchangeRates)
|
|
gctlog.Debugf(gctlog.Global, "- EXCHANGE SETTINGS:")
|
|
gctlog.Debugf(gctlog.Global, "\t Enable exchange auto pair updates: %v", s.EnableExchangeAutoPairUpdates)
|
|
gctlog.Debugf(gctlog.Global, "\t Disable all exchange auto pair updates: %v", s.DisableExchangeAutoPairUpdates)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable exchange websocket support: %v", s.EnableExchangeWebsocketSupport)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable exchange verbose mode: %v", s.EnableExchangeVerbose)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable exchange HTTP rate limiter: %v", s.EnableExchangeHTTPRateLimiter)
|
|
gctlog.Debugf(gctlog.Global, "\t Enable exchange HTTP debugging: %v", s.EnableExchangeHTTPDebugging)
|
|
gctlog.Debugf(gctlog.Global, "\t Max HTTP request jobs: %v", s.MaxHTTPRequestJobsLimit)
|
|
gctlog.Debugf(gctlog.Global, "\t HTTP request max retry attempts: %v", s.RequestMaxRetryAttempts)
|
|
gctlog.Debugf(gctlog.Global, "\t Trade buffer processing interval: %v", s.TradeBufferProcessingInterval)
|
|
gctlog.Debugf(gctlog.Global, "\t HTTP timeout: %v", s.HTTPTimeout)
|
|
gctlog.Debugf(gctlog.Global, "\t HTTP user agent: %v", s.HTTPUserAgent)
|
|
gctlog.Debugf(gctlog.Global, "- GCTSCRIPT SETTINGS: ")
|
|
gctlog.Debugf(gctlog.Global, "\t Enable GCTScript manager: %v", s.EnableGCTScriptManager)
|
|
gctlog.Debugf(gctlog.Global, "\t GCTScript max virtual machines: %v", s.MaxVirtualMachines)
|
|
gctlog.Debugf(gctlog.Global, "- WITHDRAW SETTINGS: ")
|
|
gctlog.Debugf(gctlog.Global, "\t Withdraw Cache size: %v", s.WithdrawCacheSize)
|
|
gctlog.Debugf(gctlog.Global, "- COMMON SETTINGS:")
|
|
gctlog.Debugf(gctlog.Global, "\t Global HTTP timeout: %v", s.GlobalHTTPTimeout)
|
|
gctlog.Debugf(gctlog.Global, "\t Global HTTP user agent: %v", s.GlobalHTTPUserAgent)
|
|
gctlog.Debugf(gctlog.Global, "\t Global HTTP proxy: %v", s.GlobalHTTPProxy)
|
|
|
|
gctlog.Debugln(gctlog.Global)
|
|
}
|
|
|
|
// Start starts the engine
|
|
func (bot *Engine) Start() error {
|
|
if bot == nil {
|
|
return errors.New("engine instance is nil")
|
|
}
|
|
|
|
if bot.Settings.EnableDatabaseManager {
|
|
if err := bot.DatabaseManager.Start(bot); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Database manager unable to start: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableDispatcher {
|
|
if err := dispatch.Start(bot.Settings.DispatchMaxWorkerAmount, bot.Settings.DispatchJobsLimit); err != nil {
|
|
gctlog.Errorf(gctlog.DispatchMgr, "Dispatcher unable to start: %v", err)
|
|
}
|
|
}
|
|
|
|
// Sets up internet connectivity monitor
|
|
if bot.Settings.EnableConnectivityMonitor {
|
|
if err := bot.ConnectionManager.Start(&bot.Config.ConnectionMonitor); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Connection manager unable to start: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableNTPClient {
|
|
if err := bot.NTPManager.Start(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "NTP manager unable to start: %v", err)
|
|
}
|
|
}
|
|
|
|
bot.Uptime = time.Now()
|
|
gctlog.Debugf(gctlog.Global, "Bot '%s' started.\n", bot.Config.Name)
|
|
gctlog.Debugf(gctlog.Global, "Using data dir: %s\n", bot.Settings.DataDir)
|
|
if *bot.Config.Logging.Enabled && strings.Contains(bot.Config.Logging.Output, "file") {
|
|
gctlog.Debugf(gctlog.Global, "Using log file: %s\n",
|
|
filepath.Join(gctlog.LogPath, bot.Config.Logging.LoggerFileConfig.FileName))
|
|
}
|
|
gctlog.Debugf(gctlog.Global,
|
|
"Using %d out of %d logical processors for runtime performance\n",
|
|
runtime.GOMAXPROCS(-1), runtime.NumCPU())
|
|
|
|
enabledExchanges := bot.Config.CountEnabledExchanges()
|
|
if bot.Settings.EnableAllExchanges {
|
|
enabledExchanges = len(bot.Config.Exchanges)
|
|
}
|
|
|
|
gctlog.Debugln(gctlog.Global, "EXCHANGE COVERAGE")
|
|
gctlog.Debugf(gctlog.Global, "\t Available Exchanges: %d. Enabled Exchanges: %d.\n",
|
|
len(bot.Config.Exchanges), enabledExchanges)
|
|
|
|
if bot.Settings.ExchangePurgeCredentials {
|
|
gctlog.Debugln(gctlog.Global, "Purging exchange API credentials.")
|
|
bot.Config.PurgeExchangeAPICredentials()
|
|
}
|
|
|
|
gctlog.Debugln(gctlog.Global, "Setting up exchanges..")
|
|
bot.SetupExchanges()
|
|
if bot.exchangeManager.Len() == 0 {
|
|
return errors.New("no exchanges are loaded")
|
|
}
|
|
|
|
if bot.Settings.EnableCommsRelayer {
|
|
if err := bot.CommsManager.Start(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Communications manager unable to start: %v\n", err)
|
|
}
|
|
}
|
|
|
|
err := currency.RunStorageUpdater(currency.BotOverrides{
|
|
Coinmarketcap: bot.Settings.EnableCoinmarketcapAnalysis,
|
|
FxCurrencyConverter: bot.Settings.EnableCurrencyConverter,
|
|
FxCurrencyLayer: bot.Settings.EnableCurrencyLayer,
|
|
FxFixer: bot.Settings.EnableFixer,
|
|
FxOpenExchangeRates: bot.Settings.EnableOpenExchangeRates,
|
|
},
|
|
¤cy.MainConfiguration{
|
|
ForexProviders: bot.Config.GetForexProviders(),
|
|
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.Settings.DataDir)
|
|
if err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Currency updater system failed to start %v", err)
|
|
}
|
|
|
|
if bot.Settings.EnableGRPC {
|
|
go StartRPCServer(bot)
|
|
}
|
|
|
|
if bot.Settings.EnableDeprecatedRPC {
|
|
go StartRESTServer(bot)
|
|
}
|
|
|
|
if bot.Settings.EnableWebsocketRPC {
|
|
go StartWebsocketServer(bot)
|
|
StartWebsocketHandler()
|
|
}
|
|
|
|
if bot.Settings.EnablePortfolioManager {
|
|
if err = bot.PortfolioManager.Start(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Fund manager unable to start: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableDepositAddressManager {
|
|
bot.DepositAddressManager = new(DepositAddressManager)
|
|
go bot.DepositAddressManager.Sync()
|
|
}
|
|
|
|
if bot.Settings.EnableOrderManager {
|
|
if err = bot.OrderManager.Start(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Order manager unable to start: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableExchangeSyncManager {
|
|
exchangeSyncCfg := CurrencyPairSyncerConfig{
|
|
SyncTicker: bot.Settings.EnableTickerSyncing,
|
|
SyncOrderbook: bot.Settings.EnableOrderbookSyncing,
|
|
SyncTrades: bot.Settings.EnableTradeSyncing,
|
|
SyncContinuously: bot.Settings.SyncContinuously,
|
|
NumWorkers: bot.Settings.SyncWorkers,
|
|
Verbose: bot.Settings.Verbose,
|
|
SyncTimeout: bot.Settings.SyncTimeout,
|
|
}
|
|
|
|
bot.ExchangeCurrencyPairManager, err = NewCurrencyPairSyncer(exchangeSyncCfg)
|
|
if err != nil {
|
|
gctlog.Warnf(gctlog.Global, "Unable to initialise exchange currency pair syncer. Err: %s", err)
|
|
} else {
|
|
go bot.ExchangeCurrencyPairManager.Start()
|
|
}
|
|
}
|
|
|
|
if bot.Settings.EnableEventManager {
|
|
go EventManger()
|
|
}
|
|
|
|
if bot.Settings.EnableWebsocketRoutine {
|
|
go WebsocketRoutine()
|
|
}
|
|
|
|
if bot.Settings.EnableGCTScriptManager {
|
|
if bot.Config.GCTScript.Enabled {
|
|
if err := bot.GctScriptManager.Start(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "GCTScript manager unable to start: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Stop correctly shuts down engine saving configuration files
|
|
func (bot *Engine) Stop() {
|
|
gctlog.Debugln(gctlog.Global, "Engine shutting down..")
|
|
|
|
if len(portfolio.Portfolio.Addresses) != 0 {
|
|
bot.Config.Portfolio = portfolio.Portfolio
|
|
}
|
|
|
|
if bot.GctScriptManager.Started() {
|
|
if err := bot.GctScriptManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "GCTScript manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
if bot.OrderManager.Started() {
|
|
if err := bot.OrderManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Order manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.NTPManager.Started() {
|
|
if err := bot.NTPManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "NTP manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.CommsManager.Started() {
|
|
if err := bot.CommsManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Communication manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.PortfolioManager.Started() {
|
|
if err := bot.PortfolioManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Fund manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.ConnectionManager.Started() {
|
|
if err := bot.ConnectionManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Connection manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if bot.DatabaseManager.Started() {
|
|
if err := bot.DatabaseManager.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Database manager unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if dispatch.IsRunning() {
|
|
if err := dispatch.Stop(); err != nil {
|
|
gctlog.Errorf(gctlog.DispatchMgr, "Dispatch system unable to stop. Error: %v", err)
|
|
}
|
|
}
|
|
|
|
if err := currency.ShutdownStorageUpdater(); err != nil {
|
|
gctlog.Errorf(gctlog.Global, "Currency storage system. Error: %v", err)
|
|
}
|
|
|
|
if !bot.Settings.EnableDryRun {
|
|
err := bot.Config.SaveConfig(bot.Settings.ConfigFile, false)
|
|
if err != nil {
|
|
gctlog.Errorln(gctlog.Global, "Unable to save config.")
|
|
} else {
|
|
gctlog.Debugln(gctlog.Global, "Config file saved successfully.")
|
|
}
|
|
}
|
|
|
|
// Wait for services to gracefully shutdown
|
|
bot.ServicesWG.Wait()
|
|
err := gctlog.CloseLogger()
|
|
if err != nil {
|
|
log.Printf("Failed to close logger. Error: %v\n", err)
|
|
}
|
|
}
|