mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 23:16:53 +00:00
Currency package update (#247)
* Initial currency overhaul before service system implementation * Remove redundant currency string in orderbook.Base Unexport lastupdated field in orderbook.Base as it was being instantiated multiple times Add error handling for process orderbook * Remove redundant currency string in ticker.Price Unexport lastupdated field in ticker.Price Add error handling for process ticker function and fix tests * Phase Two Update * Update translations to use map type - thankyou to kempeng for spotting this * Change pair method name from Display -> Format for better readability * Fixes misspelling and tests * Implement requested changes from GloriousCode * Remove reduntant function and streamlined return in currency_translation.go * Revert pair method naming conventions * Change currency naming conventions * Changed code type to exported Item type with underlying string to reduce complexity * Added interim orderbook process method to orderbook.Base type * Changed feebuilder struct field to currency.Pair * Adds fall over system for backup fx providers * deprecate function and children and fix linter issue with btcmarkets * Fixed requested changes * Fix bug and move mtx for rates * Fixed after rebase oopsies * Fix linter issues * Fixes race conditions in testing functions * Final phase coinmarketcap update * fix linter issues * Implement requested changes * Adds configuration variables to increase/decrease time durations between updating currency file and fetching new currency rates * Add a collection of tests to improve codecov * After rebase oopsy fixes for btse * Fix requested changes * fix after rebase oopsies and add more efficient comparison checks within currency pair * Fix linter issues
This commit is contained in:
committed by
Adrian Gallagher
parent
ed760e184e
commit
0990f9d118
138
helpers.go
138
helpers.go
@@ -5,8 +5,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/currency"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
"github.com/thrasher-/gocryptotrader/currency/translation"
|
||||
exchange "github.com/thrasher-/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/stats"
|
||||
@@ -17,8 +15,8 @@ import (
|
||||
|
||||
// GetAllAvailablePairs returns a list of all available pairs on either enabled
|
||||
// or disabled exchanges
|
||||
func GetAllAvailablePairs(enabledExchangesOnly bool) []pair.CurrencyPair {
|
||||
var pairList []pair.CurrencyPair
|
||||
func GetAllAvailablePairs(enabledExchangesOnly bool) currency.Pairs {
|
||||
var pairList currency.Pairs
|
||||
for x := range bot.config.Exchanges {
|
||||
if enabledExchangesOnly && !bot.config.Exchanges[x].Enabled {
|
||||
continue
|
||||
@@ -31,7 +29,7 @@ func GetAllAvailablePairs(enabledExchangesOnly bool) []pair.CurrencyPair {
|
||||
}
|
||||
|
||||
for y := range pairs {
|
||||
if pair.Contains(pairList, pairs[y], false) {
|
||||
if pairList.Contains(pairs[y], false) {
|
||||
continue
|
||||
}
|
||||
pairList = append(pairList, pairs[y])
|
||||
@@ -42,24 +40,26 @@ func GetAllAvailablePairs(enabledExchangesOnly bool) []pair.CurrencyPair {
|
||||
|
||||
// GetSpecificAvailablePairs returns a list of supported pairs based on specific
|
||||
// parameters
|
||||
func GetSpecificAvailablePairs(enabledExchangesOnly, fiatPairs, includeUSDT, cryptoPairs bool) []pair.CurrencyPair {
|
||||
var pairList []pair.CurrencyPair
|
||||
func GetSpecificAvailablePairs(enabledExchangesOnly, fiatPairs, includeUSDT, cryptoPairs bool) currency.Pairs {
|
||||
var pairList currency.Pairs
|
||||
supportedPairs := GetAllAvailablePairs(enabledExchangesOnly)
|
||||
|
||||
for x := range supportedPairs {
|
||||
if fiatPairs {
|
||||
if currency.IsCryptoFiatPair(supportedPairs[x]) &&
|
||||
!pair.ContainsCurrency(supportedPairs[x], "USDT") ||
|
||||
(includeUSDT && pair.ContainsCurrency(supportedPairs[x], "USDT") && currency.IsCryptoPair(supportedPairs[x])) {
|
||||
if pair.Contains(pairList, supportedPairs[x], false) {
|
||||
if supportedPairs[x].IsCryptoFiatPair() &&
|
||||
!supportedPairs[x].ContainsCurrency(currency.USDT) ||
|
||||
(includeUSDT &&
|
||||
supportedPairs[x].ContainsCurrency(currency.USDT) &&
|
||||
supportedPairs[x].IsCryptoPair()) {
|
||||
if pairList.Contains(supportedPairs[x], false) {
|
||||
continue
|
||||
}
|
||||
pairList = append(pairList, supportedPairs[x])
|
||||
}
|
||||
}
|
||||
if cryptoPairs {
|
||||
if currency.IsCryptoPair(supportedPairs[x]) {
|
||||
if pair.Contains(pairList, supportedPairs[x], false) {
|
||||
if supportedPairs[x].IsCryptoPair() {
|
||||
if pairList.Contains(supportedPairs[x], false) {
|
||||
continue
|
||||
}
|
||||
pairList = append(pairList, supportedPairs[x])
|
||||
@@ -70,24 +70,25 @@ func GetSpecificAvailablePairs(enabledExchangesOnly, fiatPairs, includeUSDT, cry
|
||||
}
|
||||
|
||||
// IsRelatablePairs checks to see if the two pairs are relatable
|
||||
func IsRelatablePairs(p1, p2 pair.CurrencyPair, includeUSDT bool) bool {
|
||||
if p1.Equal(p2, false) {
|
||||
func IsRelatablePairs(p1, p2 currency.Pair, includeUSDT bool) bool {
|
||||
if p1.EqualIncludeReciprocal(p2) {
|
||||
return true
|
||||
}
|
||||
|
||||
var relatablePairs = GetRelatableCurrencies(p1, true, includeUSDT)
|
||||
if currency.IsCryptoFiatPair(p1) {
|
||||
if p1.IsCryptoFiatPair() {
|
||||
for x := range relatablePairs {
|
||||
relatablePairs = append(relatablePairs, GetRelatableFiatCurrencies(relatablePairs[x])...)
|
||||
relatablePairs = append(relatablePairs,
|
||||
GetRelatableFiatCurrencies(relatablePairs[x])...)
|
||||
}
|
||||
}
|
||||
return pair.Contains(relatablePairs, p2, false)
|
||||
return relatablePairs.Contains(p2, false)
|
||||
}
|
||||
|
||||
// MapCurrenciesByExchange returns a list of currency pairs mapped to an
|
||||
// exchange
|
||||
func MapCurrenciesByExchange(p []pair.CurrencyPair, enabledExchangesOnly bool) map[string][]pair.CurrencyPair {
|
||||
currencyExchange := make(map[string][]pair.CurrencyPair)
|
||||
func MapCurrenciesByExchange(p []currency.Pair, enabledExchangesOnly bool) map[string]currency.Pairs {
|
||||
currencyExchange := make(map[string]currency.Pairs)
|
||||
for x := range p {
|
||||
for y := range bot.config.Exchanges {
|
||||
if enabledExchangesOnly && !bot.config.Exchanges[y].Enabled {
|
||||
@@ -101,11 +102,11 @@ func MapCurrenciesByExchange(p []pair.CurrencyPair, enabledExchangesOnly bool) m
|
||||
|
||||
result, ok := currencyExchange[exchName]
|
||||
if !ok {
|
||||
var pairs []pair.CurrencyPair
|
||||
var pairs []currency.Pair
|
||||
pairs = append(pairs, p[x])
|
||||
currencyExchange[exchName] = pairs
|
||||
} else {
|
||||
if pair.Contains(result, p[x], false) {
|
||||
if result.Contains(p[x], false) {
|
||||
continue
|
||||
}
|
||||
result = append(result, p[x])
|
||||
@@ -118,7 +119,7 @@ func MapCurrenciesByExchange(p []pair.CurrencyPair, enabledExchangesOnly bool) m
|
||||
|
||||
// GetExchangeNamesByCurrency returns a list of exchanges supporting
|
||||
// a currency pair based on whether the exchange is enabled or not
|
||||
func GetExchangeNamesByCurrency(p pair.CurrencyPair, enabled bool) []string {
|
||||
func GetExchangeNamesByCurrency(p currency.Pair, enabled bool) []string {
|
||||
var exchanges []string
|
||||
for x := range bot.config.Exchanges {
|
||||
if enabled != bot.config.Exchanges[x].Enabled {
|
||||
@@ -140,14 +141,22 @@ func GetExchangeNamesByCurrency(p pair.CurrencyPair, enabled bool) []string {
|
||||
|
||||
// GetRelatableCryptocurrencies returns a list of currency pairs if it can find
|
||||
// any relatable currencies (e.g ETHBTC -> ETHLTC -> ETHUSDT -> ETHREP)
|
||||
// incOrig includes the supplied pair if desired
|
||||
func GetRelatableCryptocurrencies(p pair.CurrencyPair) []pair.CurrencyPair {
|
||||
var pairs []pair.CurrencyPair
|
||||
cryptocurrencies := currency.CryptoCurrencies
|
||||
func GetRelatableCryptocurrencies(p currency.Pair) currency.Pairs {
|
||||
var pairs currency.Pairs
|
||||
cryptocurrencies := currency.GetCryptocurrencies()
|
||||
|
||||
for x := range cryptocurrencies {
|
||||
newPair := pair.NewCurrencyPair(p.FirstCurrency.String(), cryptocurrencies[x])
|
||||
if pair.Contains(pairs, newPair, false) {
|
||||
newPair := currency.NewPair(p.Base, cryptocurrencies[x])
|
||||
if newPair.IsInvalid() {
|
||||
continue
|
||||
}
|
||||
|
||||
if newPair.Base.Upper() == p.Base.Upper() &&
|
||||
newPair.Quote.Upper() == p.Quote.Upper() {
|
||||
continue
|
||||
}
|
||||
|
||||
if pairs.Contains(newPair, false) {
|
||||
continue
|
||||
}
|
||||
pairs = append(pairs, newPair)
|
||||
@@ -157,14 +166,22 @@ func GetRelatableCryptocurrencies(p pair.CurrencyPair) []pair.CurrencyPair {
|
||||
|
||||
// GetRelatableFiatCurrencies returns a list of currency pairs if it can find
|
||||
// any relatable currencies (e.g ETHUSD -> ETHAUD -> ETHGBP -> ETHJPY)
|
||||
// incOrig includes the supplied pair if desired
|
||||
func GetRelatableFiatCurrencies(p pair.CurrencyPair) []pair.CurrencyPair {
|
||||
var pairs []pair.CurrencyPair
|
||||
fiatCurrencies := currency.FiatCurrencies
|
||||
func GetRelatableFiatCurrencies(p currency.Pair) currency.Pairs {
|
||||
var pairs currency.Pairs
|
||||
fiatCurrencies := currency.GetFiatCurrencies()
|
||||
|
||||
for x := range fiatCurrencies {
|
||||
newPair := pair.NewCurrencyPair(p.FirstCurrency.String(), fiatCurrencies[x])
|
||||
if pair.Contains(pairs, newPair, false) {
|
||||
newPair := currency.NewPair(p.Base, fiatCurrencies[x])
|
||||
if newPair.Base.Upper() == newPair.Quote.Upper() {
|
||||
continue
|
||||
}
|
||||
|
||||
if newPair.Base.Upper() == p.Base.Upper() &&
|
||||
newPair.Quote.Upper() == p.Quote.Upper() {
|
||||
continue
|
||||
}
|
||||
|
||||
if pairs.Contains(newPair, false) {
|
||||
continue
|
||||
}
|
||||
pairs = append(pairs, newPair)
|
||||
@@ -175,38 +192,35 @@ func GetRelatableFiatCurrencies(p pair.CurrencyPair) []pair.CurrencyPair {
|
||||
// GetRelatableCurrencies returns a list of currency pairs if it can find
|
||||
// any relatable currencies (e.g BTCUSD -> BTC USDT -> XBT USDT -> XBT USD)
|
||||
// incOrig includes the supplied pair if desired
|
||||
func GetRelatableCurrencies(p pair.CurrencyPair, incOrig, incUSDT bool) []pair.CurrencyPair {
|
||||
var pairs []pair.CurrencyPair
|
||||
func GetRelatableCurrencies(p currency.Pair, incOrig, incUSDT bool) currency.Pairs {
|
||||
var pairs currency.Pairs
|
||||
|
||||
addPair := func(p pair.CurrencyPair) {
|
||||
if pair.Contains(pairs, p, true) {
|
||||
addPair := func(p currency.Pair) {
|
||||
if pairs.Contains(p, true) {
|
||||
return
|
||||
}
|
||||
pairs = append(pairs, p)
|
||||
}
|
||||
|
||||
buildPairs := func(p pair.CurrencyPair, incOrig bool) {
|
||||
buildPairs := func(p currency.Pair, incOrig bool) {
|
||||
if incOrig {
|
||||
addPair(p)
|
||||
}
|
||||
|
||||
first, err := translation.GetTranslation(p.FirstCurrency)
|
||||
if err == nil {
|
||||
addPair(pair.NewCurrencyPair(first.String(),
|
||||
p.SecondCurrency.String()))
|
||||
first, ok := currency.GetTranslation(p.Base)
|
||||
if ok {
|
||||
addPair(currency.NewPair(first, p.Quote))
|
||||
|
||||
var second pair.CurrencyItem
|
||||
second, err = translation.GetTranslation(p.SecondCurrency)
|
||||
if err == nil {
|
||||
addPair(pair.NewCurrencyPair(first.String(),
|
||||
second.String()))
|
||||
var second currency.Code
|
||||
second, ok = currency.GetTranslation(p.Quote)
|
||||
if ok {
|
||||
addPair(currency.NewPair(first, second))
|
||||
}
|
||||
}
|
||||
|
||||
second, err := translation.GetTranslation(p.SecondCurrency)
|
||||
if err == nil {
|
||||
addPair(pair.NewCurrencyPair(p.FirstCurrency.String(),
|
||||
second.String()))
|
||||
second, ok := currency.GetTranslation(p.Quote)
|
||||
if ok {
|
||||
addPair(currency.NewPair(p.Base, second))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,7 +228,7 @@ func GetRelatableCurrencies(p pair.CurrencyPair, incOrig, incUSDT bool) []pair.C
|
||||
buildPairs(p.Swap(), incOrig)
|
||||
|
||||
if !incUSDT {
|
||||
pairs = pair.RemovePairsByFilter(pairs, "USDT")
|
||||
pairs = pairs.RemovePairsByFilter(currency.USDT)
|
||||
}
|
||||
|
||||
return pairs
|
||||
@@ -229,7 +243,7 @@ func GetSpecificOrderbook(currencyPair, exchangeName, assetType string) (orderbo
|
||||
if bot.exchanges[x] != nil {
|
||||
if bot.exchanges[x].GetName() == exchangeName {
|
||||
specificOrderbook, err = bot.exchanges[x].GetOrderbookEx(
|
||||
pair.NewCurrencyPairFromString(currencyPair),
|
||||
currency.NewPairFromString(currencyPair),
|
||||
assetType,
|
||||
)
|
||||
break
|
||||
@@ -248,7 +262,7 @@ func GetSpecificTicker(currencyPair, exchangeName, assetType string) (ticker.Pri
|
||||
if bot.exchanges[x] != nil {
|
||||
if bot.exchanges[x].GetName() == exchangeName {
|
||||
specificTicker, err = bot.exchanges[x].GetTickerPrice(
|
||||
pair.NewCurrencyPairFromString(currencyPair),
|
||||
currency.NewPairFromString(currencyPair),
|
||||
assetType,
|
||||
)
|
||||
break
|
||||
@@ -261,8 +275,8 @@ func GetSpecificTicker(currencyPair, exchangeName, assetType string) (ticker.Pri
|
||||
// GetCollatedExchangeAccountInfoByCoin collates individual exchange account
|
||||
// information and turns into into a map string of
|
||||
// exchange.AccountCurrencyInfo
|
||||
func GetCollatedExchangeAccountInfoByCoin(exchAccounts []exchange.AccountInfo) map[string]exchange.AccountCurrencyInfo {
|
||||
result := make(map[string]exchange.AccountCurrencyInfo)
|
||||
func GetCollatedExchangeAccountInfoByCoin(exchAccounts []exchange.AccountInfo) map[currency.Code]exchange.AccountCurrencyInfo {
|
||||
result := make(map[currency.Code]exchange.AccountCurrencyInfo)
|
||||
for _, accounts := range exchAccounts {
|
||||
for _, account := range accounts.Accounts {
|
||||
for _, accountCurrencyInfo := range account.Currencies {
|
||||
@@ -297,7 +311,7 @@ func GetAccountCurrencyInfoByExchangeName(accounts []exchange.AccountInfo, excha
|
||||
|
||||
// GetExchangeHighestPriceByCurrencyPair returns the exchange with the highest
|
||||
// price for a given currency pair and asset type
|
||||
func GetExchangeHighestPriceByCurrencyPair(p pair.CurrencyPair, assetType string) (string, error) {
|
||||
func GetExchangeHighestPriceByCurrencyPair(p currency.Pair, assetType string) (string, error) {
|
||||
result := stats.SortExchangesByPrice(p, assetType, true)
|
||||
if len(result) == 0 {
|
||||
return "", fmt.Errorf("no stats for supplied currency pair and asset type")
|
||||
@@ -308,7 +322,7 @@ func GetExchangeHighestPriceByCurrencyPair(p pair.CurrencyPair, assetType string
|
||||
|
||||
// GetExchangeLowestPriceByCurrencyPair returns the exchange with the lowest
|
||||
// price for a given currency pair and asset type
|
||||
func GetExchangeLowestPriceByCurrencyPair(p pair.CurrencyPair, assetType string) (string, error) {
|
||||
func GetExchangeLowestPriceByCurrencyPair(p currency.Pair, assetType string) (string, error) {
|
||||
result := stats.SortExchangesByPrice(p, assetType, false)
|
||||
if len(result) == 0 {
|
||||
return "", fmt.Errorf("no stats for supplied currency pair and asset type")
|
||||
@@ -384,8 +398,8 @@ func SeedExchangeAccountInfo(data []exchange.AccountInfo) {
|
||||
port.RemoveExchangeAddress(exchangeName, currencyName)
|
||||
} else {
|
||||
balance, ok := port.GetAddressBalance(exchangeName,
|
||||
currencyName,
|
||||
portfolio.PortfolioAddressExchange)
|
||||
portfolio.PortfolioAddressExchange,
|
||||
currencyName)
|
||||
|
||||
if !ok {
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user