mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-16 15:09:57 +00:00
* 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
146 lines
3.3 KiB
Go
146 lines
3.3 KiB
Go
package stats
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/thrasher-/gocryptotrader/currency"
|
|
)
|
|
|
|
// Item holds various fields for storing currency pair stats
|
|
type Item struct {
|
|
Exchange string
|
|
Pair currency.Pair
|
|
AssetType string
|
|
Price float64
|
|
Volume float64
|
|
}
|
|
|
|
// Items var array
|
|
var Items []Item
|
|
|
|
// ByPrice allows sorting by price
|
|
type ByPrice []Item
|
|
|
|
func (b ByPrice) Len() int {
|
|
return len(b)
|
|
}
|
|
|
|
func (b ByPrice) Less(i, j int) bool {
|
|
return b[i].Price < b[j].Price
|
|
}
|
|
|
|
func (b ByPrice) Swap(i, j int) {
|
|
b[i], b[j] = b[j], b[i]
|
|
}
|
|
|
|
// ByVolume allows sorting by volume
|
|
type ByVolume []Item
|
|
|
|
func (b ByVolume) Len() int {
|
|
return len(b)
|
|
}
|
|
|
|
func (b ByVolume) Less(i, j int) bool {
|
|
return b[i].Volume < b[j].Volume
|
|
}
|
|
|
|
func (b ByVolume) Swap(i, j int) {
|
|
b[i], b[j] = b[j], b[i]
|
|
}
|
|
|
|
// Add adds or updates the item stats
|
|
func Add(exchange string, p currency.Pair, assetType string, price, volume float64) {
|
|
if exchange == "" ||
|
|
assetType == "" ||
|
|
price == 0 ||
|
|
volume == 0 ||
|
|
p.Base.IsEmpty() ||
|
|
p.Quote.IsEmpty() {
|
|
return
|
|
}
|
|
|
|
if p.Base == currency.XBT {
|
|
newPair := currency.NewPairFromStrings(currency.BTC.String(), p.Quote.String())
|
|
Append(exchange, newPair, assetType, price, volume)
|
|
}
|
|
|
|
if p.Quote == currency.USDT {
|
|
newPair := currency.NewPairFromStrings(p.Base.String(), currency.USD.String())
|
|
Append(exchange, newPair, assetType, price, volume)
|
|
}
|
|
|
|
Append(exchange, p, assetType, price, volume)
|
|
}
|
|
|
|
// Append adds or updates the item stats for a specific
|
|
// currency pair and asset type
|
|
func Append(exchange string, p currency.Pair, assetType string, price, volume float64) {
|
|
if AlreadyExists(exchange, p, assetType, price, volume) {
|
|
return
|
|
}
|
|
|
|
i := Item{
|
|
Exchange: exchange,
|
|
Pair: p,
|
|
AssetType: assetType,
|
|
Price: price,
|
|
Volume: volume,
|
|
}
|
|
|
|
Items = append(Items, i)
|
|
}
|
|
|
|
// AlreadyExists checks to see if item info already exists
|
|
// for a specific currency pair and asset type
|
|
func AlreadyExists(exchange string, p currency.Pair, assetType string, price, volume float64) bool {
|
|
for i := range Items {
|
|
if Items[i].Exchange == exchange &&
|
|
Items[i].Pair.EqualIncludeReciprocal(p) &&
|
|
Items[i].AssetType == assetType {
|
|
Items[i].Price, Items[i].Volume = price, volume
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// SortExchangesByVolume sorts item info by volume for a specific
|
|
// currency pair and asset type. Reverse will reverse the order from lowest to
|
|
// highest
|
|
func SortExchangesByVolume(p currency.Pair, assetType string, reverse bool) []Item {
|
|
var result []Item
|
|
for x := range Items {
|
|
if Items[x].Pair.EqualIncludeReciprocal(p) &&
|
|
Items[x].AssetType == assetType {
|
|
result = append(result, Items[x])
|
|
}
|
|
}
|
|
|
|
if reverse {
|
|
sort.Sort(sort.Reverse(ByVolume(result)))
|
|
} else {
|
|
sort.Sort(ByVolume(result))
|
|
}
|
|
return result
|
|
}
|
|
|
|
// SortExchangesByPrice sorts item info by volume for a specific
|
|
// currency pair and asset type. Reverse will reverse the order from lowest to
|
|
// highest
|
|
func SortExchangesByPrice(p currency.Pair, assetType string, reverse bool) []Item {
|
|
var result []Item
|
|
for x := range Items {
|
|
if Items[x].Pair.EqualIncludeReciprocal(p) &&
|
|
Items[x].AssetType == assetType {
|
|
result = append(result, Items[x])
|
|
}
|
|
}
|
|
|
|
if reverse {
|
|
sort.Sort(sort.Reverse(ByPrice(result)))
|
|
} else {
|
|
sort.Sort(ByPrice(result))
|
|
}
|
|
return result
|
|
}
|