mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 15:09:51 +00:00
* Initial codes for a trade tracker * Moving everything in a broken fashion * Removes tradetracker. Removes some errors for subsystems * Cleans up some subsystems, renames stuttering types. Removes some global Bot usage * More basic subsystem renaming and file moving * Removes engine dependency from events,ntpserver,ordermanager,comms manager * Exports eventManager, fixes rpcserver. puts rpcserver back for now * Removes redundant error message, further removes engine dependencies * experimental end of day interface usage * adds ability to build the application * Withdraw and event manager handling * cleans up apiserver and communications manager * Cleans up some start/setup processes. Though should separate * More consistency with Setup Start Stop IsRunning funcs * Final consistency pass before testing phase * Fixes engine tests. Fixes stop nil issue * api server tests * Communications manager testing * Connection manager tests and nilsubsystem error * End of day currencypairsyncer tests * Adds databaseconnection/databaseconnection_test.go * Adds withdrawal manager tests * Deposit address testing. Moved orderbook sync first as its more important * Adds test for event manager * More full eventmanager testing * Adds testfile. Enables skipped test. * ntp manager tests * Adds ordermanager tests, Extracts a whole new subsystem from engine and fanangles import cycles * Adds websocket routine manager tests * Basic portfolio manager testing * Fixes issue with currency pair sync startup * Fixes issue with event manager startup * Starts the order manager before backtester starts * Fixes fee tests. Expands testing. Doesnt fix races * Fixes most test races * Resolves data races * Fixes subsystem test issues * currency pair syncer coverage tests * Refactors portfolio. Fixes tests. Withdraw validation Portfolio didn't need to exist with a portfolio manager. Now the porfolio manager is in charge how the portfolio is handled and all portfolio functions are attached to the base instead of just exported at the package level Withdrawal validation occurred at the exchange level when it can just be run at the withdrawal manager level. All withdrawal requests go through that endpoint * lint -fix * golang lint fixes * lints and comments everything * Updates GCT logo, adds documentation for some subsystems * More documentation and more logo updates * Fixes backtesting and apiserver errors encountered * Fixes errors and typos from reviewing * More minor fixes * Changes %h verb to %w * reverbs to %s * Humbly begins reverting to more flat engine package The main reasoning for this is that the subsystem split doesn't make sense in a golang environment. The subsystems are only meant to be used with engine and so by placing them in a non-engine area, it does not work and is inconsistent with the rest of the application's package layout. This will begin salvaging the changes made by reverting to a flat engine package, but maintaining the consistent designs introduced. Further, I will look to remove any TestMains and decrease the scope of testing to be more local and decrease the issues that have been caused from our style of testing. * Manages to re-flatten things. Everything is within its own file * mini fixes * Fixes tests and data races and lints * Updates docs tool for engine to create filename readmes * os -> ioutil * remove err * Appveyor version increase test * Removes tCleanup as its unsupported on appveyor * Adds stuff that I thought was in previous merge master commit * Removes cancel from test * Fixes really fun test-exclusive data race * minor nit fixes * niterinos * docs gen * rm;rf test * Remove typoline. expands startstop helper. Splits apiserver * Removes accidental folder * Uses update instead of replace for order upsert * addresses nits. Renames files. Regenerates documentation. * lint and removal of comments * Add new test for default scenario * Fixes typo * regen docs
328 lines
9.1 KiB
Go
328 lines
9.1 KiB
Go
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
|
|
"github.com/thrasher-corp/gocryptotrader/log"
|
|
"github.com/thrasher-corp/gocryptotrader/portfolio"
|
|
)
|
|
|
|
// PortfolioManagerName is an exported subsystem name
|
|
const PortfolioManagerName = "portfolio"
|
|
|
|
var (
|
|
// PortfolioSleepDelay defines the default sleep time between portfolio manager runs
|
|
PortfolioSleepDelay = time.Minute
|
|
)
|
|
|
|
// portfolioManager routinely retrieves a user's holdings through exchange APIs as well
|
|
// as through addresses provided in the config
|
|
type portfolioManager struct {
|
|
started int32
|
|
processing int32
|
|
portfolioManagerDelay time.Duration
|
|
exchangeManager *ExchangeManager
|
|
shutdown chan struct{}
|
|
base *portfolio.Base
|
|
}
|
|
|
|
// setupPortfolioManager creates a new portfolio manager
|
|
func setupPortfolioManager(e *ExchangeManager, portfolioManagerDelay time.Duration, cfg *portfolio.Base) (*portfolioManager, error) {
|
|
if e == nil {
|
|
return nil, errNilExchangeManager
|
|
}
|
|
if portfolioManagerDelay <= 0 {
|
|
portfolioManagerDelay = PortfolioSleepDelay
|
|
}
|
|
if cfg == nil {
|
|
cfg = &portfolio.Base{Addresses: []portfolio.Address{}}
|
|
}
|
|
m := &portfolioManager{
|
|
portfolioManagerDelay: portfolioManagerDelay,
|
|
exchangeManager: e,
|
|
shutdown: make(chan struct{}),
|
|
base: cfg,
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// IsRunning safely checks whether the subsystem is running
|
|
func (m *portfolioManager) IsRunning() bool {
|
|
if m == nil {
|
|
return false
|
|
}
|
|
return atomic.LoadInt32(&m.started) == 1
|
|
}
|
|
|
|
// Start runs the subsystem
|
|
func (m *portfolioManager) Start(wg *sync.WaitGroup) error {
|
|
if m == nil {
|
|
return fmt.Errorf("portfolio manager %w", ErrNilSubsystem)
|
|
}
|
|
if wg == nil {
|
|
return errNilWaitGroup
|
|
}
|
|
if !atomic.CompareAndSwapInt32(&m.started, 0, 1) {
|
|
return fmt.Errorf("portfolio manager %w", ErrSubSystemAlreadyStarted)
|
|
}
|
|
|
|
log.Debugf(log.PortfolioMgr, "Portfolio manager %s", MsgSubSystemStarting)
|
|
m.shutdown = make(chan struct{})
|
|
go m.run(wg)
|
|
return nil
|
|
}
|
|
|
|
// Stop attempts to shutdown the subsystem
|
|
func (m *portfolioManager) Stop() error {
|
|
if m == nil {
|
|
return fmt.Errorf("portfolio manager %w", ErrNilSubsystem)
|
|
}
|
|
if !atomic.CompareAndSwapInt32(&m.started, 1, 0) {
|
|
return fmt.Errorf("portfolio manager %w", ErrSubSystemNotStarted)
|
|
}
|
|
defer func() {
|
|
atomic.CompareAndSwapInt32(&m.started, 1, 0)
|
|
}()
|
|
|
|
log.Debugf(log.PortfolioMgr, "Portfolio manager %s", MsgSubSystemShuttingDown)
|
|
close(m.shutdown)
|
|
return nil
|
|
}
|
|
|
|
// run periodically will check and update portfolio holdings
|
|
func (m *portfolioManager) run(wg *sync.WaitGroup) {
|
|
log.Debugln(log.PortfolioMgr, "Portfolio manager started.")
|
|
wg.Add(1)
|
|
tick := time.NewTicker(m.portfolioManagerDelay)
|
|
defer func() {
|
|
tick.Stop()
|
|
wg.Done()
|
|
log.Debugf(log.PortfolioMgr, "Portfolio manager shutdown.")
|
|
}()
|
|
|
|
go m.processPortfolio()
|
|
for {
|
|
select {
|
|
case <-m.shutdown:
|
|
return
|
|
case <-tick.C:
|
|
go m.processPortfolio()
|
|
}
|
|
}
|
|
}
|
|
|
|
// processPortfolio updates portfolio holdings
|
|
func (m *portfolioManager) processPortfolio() {
|
|
if !atomic.CompareAndSwapInt32(&m.processing, 0, 1) {
|
|
return
|
|
}
|
|
data := m.base.GetPortfolioGroupedCoin()
|
|
for key, value := range data {
|
|
err := m.base.UpdatePortfolio(value, key)
|
|
if err != nil {
|
|
log.Errorf(log.PortfolioMgr,
|
|
"PortfolioWatcher error %s for currency %s\n",
|
|
err,
|
|
key)
|
|
continue
|
|
}
|
|
|
|
log.Debugf(log.PortfolioMgr,
|
|
"Portfolio manager: Successfully updated address balance for %s address(es) %s\n",
|
|
key,
|
|
value)
|
|
}
|
|
|
|
d := m.getExchangeAccountInfo(m.exchangeManager.GetExchanges())
|
|
m.seedExchangeAccountInfo(d)
|
|
atomic.CompareAndSwapInt32(&m.processing, 1, 0)
|
|
}
|
|
|
|
// seedExchangeAccountInfo seeds account info
|
|
func (m *portfolioManager) seedExchangeAccountInfo(accounts []account.Holdings) {
|
|
if len(accounts) == 0 {
|
|
return
|
|
}
|
|
for x := range accounts {
|
|
exchangeName := accounts[x].Exchange
|
|
var currencies []account.Balance
|
|
for y := range accounts[x].Accounts {
|
|
for z := range accounts[x].Accounts[y].Currencies {
|
|
var update bool
|
|
for i := range currencies {
|
|
if accounts[x].Accounts[y].Currencies[z].CurrencyName == currencies[i].CurrencyName {
|
|
currencies[i].Hold += accounts[x].Accounts[y].Currencies[z].Hold
|
|
currencies[i].TotalValue += accounts[x].Accounts[y].Currencies[z].TotalValue
|
|
update = true
|
|
}
|
|
}
|
|
|
|
if update {
|
|
continue
|
|
}
|
|
|
|
currencies = append(currencies, account.Balance{
|
|
CurrencyName: accounts[x].Accounts[y].Currencies[z].CurrencyName,
|
|
TotalValue: accounts[x].Accounts[y].Currencies[z].TotalValue,
|
|
Hold: accounts[x].Accounts[y].Currencies[z].Hold,
|
|
})
|
|
}
|
|
}
|
|
|
|
for x := range currencies {
|
|
currencyName := currencies[x].CurrencyName
|
|
total := currencies[x].TotalValue
|
|
|
|
if !m.base.ExchangeAddressExists(exchangeName, currencyName) {
|
|
if total <= 0 {
|
|
continue
|
|
}
|
|
|
|
log.Debugf(log.PortfolioMgr, "Portfolio: Adding new exchange address: %s, %s, %f, %s\n",
|
|
exchangeName,
|
|
currencyName,
|
|
total,
|
|
portfolio.ExchangeAddress)
|
|
|
|
m.base.Addresses = append(
|
|
m.base.Addresses,
|
|
portfolio.Address{Address: exchangeName,
|
|
CoinType: currencyName,
|
|
Balance: total,
|
|
Description: portfolio.ExchangeAddress})
|
|
} else {
|
|
if total <= 0 {
|
|
log.Debugf(log.PortfolioMgr, "Portfolio: Removing %s %s entry.\n",
|
|
exchangeName,
|
|
currencyName)
|
|
m.base.RemoveExchangeAddress(exchangeName, currencyName)
|
|
} else {
|
|
balance, ok := m.base.GetAddressBalance(exchangeName,
|
|
portfolio.ExchangeAddress,
|
|
currencyName)
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
if balance != total {
|
|
log.Debugf(log.PortfolioMgr, "Portfolio: Updating %s %s entry with balance %f.\n",
|
|
exchangeName,
|
|
currencyName,
|
|
total)
|
|
m.base.UpdateExchangeAddressBalance(exchangeName,
|
|
currencyName,
|
|
total)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// getExchangeAccountInfo returns all the current enabled exchanges
|
|
func (m *portfolioManager) getExchangeAccountInfo(exchanges []exchange.IBotExchange) []account.Holdings {
|
|
var response []account.Holdings
|
|
for x := range exchanges {
|
|
if exchanges[x] == nil || !exchanges[x].IsEnabled() {
|
|
continue
|
|
}
|
|
if !exchanges[x].GetAuthenticatedAPISupport(exchange.RestAuthentication) {
|
|
if m.base.Verbose {
|
|
log.Debugf(log.PortfolioMgr,
|
|
"skipping %s due to disabled authenticated API support.\n",
|
|
exchanges[x].GetName())
|
|
}
|
|
continue
|
|
}
|
|
assetTypes := exchanges[x].GetAssetTypes()
|
|
var exchangeHoldings account.Holdings
|
|
for y := range assetTypes {
|
|
accountHoldings, err := exchanges[x].FetchAccountInfo(assetTypes[y])
|
|
if err != nil {
|
|
log.Errorf(log.PortfolioMgr,
|
|
"Error encountered retrieving exchange account info for %s. Error %s\n",
|
|
exchanges[x].GetName(),
|
|
err)
|
|
continue
|
|
}
|
|
for z := range accountHoldings.Accounts {
|
|
accountHoldings.Accounts[z].AssetType = assetTypes[y]
|
|
}
|
|
exchangeHoldings.Exchange = exchanges[x].GetName()
|
|
exchangeHoldings.Accounts = append(exchangeHoldings.Accounts, accountHoldings.Accounts...)
|
|
}
|
|
response = append(response, exchangeHoldings)
|
|
}
|
|
return response
|
|
}
|
|
|
|
// AddAddress adds a new portfolio address for the portfolio manager to track
|
|
func (m *portfolioManager) AddAddress(address, description string, coinType currency.Code, balance float64) error {
|
|
if m == nil {
|
|
return fmt.Errorf("portfolio manager %w", ErrNilSubsystem)
|
|
}
|
|
if !m.IsRunning() {
|
|
return fmt.Errorf("portfolio manager %w", ErrSubSystemNotStarted)
|
|
}
|
|
return m.base.AddAddress(address, description, coinType, balance)
|
|
}
|
|
|
|
// RemoveAddress removes a portfolio address
|
|
func (m *portfolioManager) RemoveAddress(address, description string, coinType currency.Code) error {
|
|
if m == nil {
|
|
return fmt.Errorf("portfolio manager %w", ErrNilSubsystem)
|
|
}
|
|
if !m.IsRunning() {
|
|
return fmt.Errorf("portfolio manager %w", ErrSubSystemNotStarted)
|
|
}
|
|
return m.base.RemoveAddress(address, description, coinType)
|
|
}
|
|
|
|
// GetPortfolioSummary returns a summary of all portfolio holdings
|
|
func (m *portfolioManager) GetPortfolioSummary() portfolio.Summary {
|
|
if m == nil || !m.IsRunning() {
|
|
return portfolio.Summary{}
|
|
}
|
|
return m.base.GetPortfolioSummary()
|
|
}
|
|
|
|
// GetAddresses returns all addresses
|
|
func (m *portfolioManager) GetAddresses() []portfolio.Address {
|
|
if m == nil || !m.IsRunning() {
|
|
return nil
|
|
}
|
|
return m.base.Addresses
|
|
}
|
|
|
|
// GetPortfolio returns a copy of the internal portfolio base for
|
|
// saving addresses to the config
|
|
func (m *portfolioManager) GetPortfolio() *portfolio.Base {
|
|
if m == nil || !m.IsRunning() {
|
|
return nil
|
|
}
|
|
resp := m.base
|
|
return resp
|
|
}
|
|
|
|
// IsWhiteListed checks if an address is whitelisted to withdraw to
|
|
func (m *portfolioManager) IsWhiteListed(address string) bool {
|
|
if m == nil || !m.IsRunning() {
|
|
return false
|
|
}
|
|
return m.base.IsWhiteListed(address)
|
|
}
|
|
|
|
// IsExchangeSupported checks if an exchange is supported
|
|
func (m *portfolioManager) IsExchangeSupported(exchange, address string) bool {
|
|
if m == nil || !m.IsRunning() {
|
|
return false
|
|
}
|
|
return m.base.IsExchangeSupported(exchange, address)
|
|
}
|