exchanges/kraken,bittrex,gemini: Resolve Kraken panic, lint corrections, Bittrex batch tickers, set Gemini order limits and update tradable pairs (#1372)

* fix kraken, batch bittrex, fix lint

* surprise gemini!

* thought this happened automatically

* fix before shazbert sees

* fixes annoying atoi bug

* rm futures from gemini

* lint

* bittrex UpdatedAt, gemini Limits, stats relook

* STATS used HARDEN!(improve stats package)

* Whoopsies in your Daisies

* rm RWMutex, json stringeroo

* fixes additional index issues 😆 😭
This commit is contained in:
Scott
2023-10-23 17:06:25 +10:00
committed by GitHub
parent f8e943ea8e
commit 2ea3083468
14 changed files with 1855 additions and 120 deletions

View File

@@ -8,49 +8,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
// Item holds various fields for storing currency pair stats
type Item struct {
Exchange string
Pair currency.Pair
AssetType asset.Item
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
// Add adds or updates the Item stats
func Add(exchange string, p currency.Pair, a asset.Item, price, volume float64) error {
if exchange == "" ||
a == asset.Empty ||
@@ -82,13 +40,14 @@ func Add(exchange string, p currency.Pair, a asset.Item, price, volume float64)
return nil
}
// Append adds or updates the item stats for a specific
// currency pair and asset type
// Append adds the Item stats for a specific currency pair and asset type
// if it doesn't exist
func Append(exchange string, p currency.Pair, a asset.Item, price, volume float64) {
if AlreadyExists(exchange, p, a, price, volume) {
statMutex.Lock()
defer statMutex.Unlock()
if alreadyExistsRequiresLock(exchange, p, a, price, volume) {
return
}
i := Item{
Exchange: exchange,
Pair: p,
@@ -97,59 +56,95 @@ func Append(exchange string, p currency.Pair, a asset.Item, price, volume float6
Volume: volume,
}
Items = append(Items, i)
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 asset.Item, 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
// alreadyExistsRequiresLock checks to see if Item info already exists
// requires a locking beforehand because of globals
func alreadyExistsRequiresLock(exchange string, p currency.Pair, assetType asset.Item, 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
// 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 asset.Item, price, volume float64) bool {
statMutex.Lock()
defer statMutex.Unlock()
return alreadyExistsRequiresLock(exchange, p, assetType, price, volume)
}
// 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 asset.Item, 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])
statMutex.Lock()
defer statMutex.Unlock()
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)))
sort.Sort(sort.Reverse(byVolume(result)))
} else {
sort.Sort(ByVolume(result))
sort.Sort(byVolume(result))
}
return result
}
// SortExchangesByPrice sorts item info by volume for a specific
// 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 asset.Item, 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])
statMutex.Lock()
defer statMutex.Unlock()
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)))
sort.Sort(sort.Reverse(byPrice(result)))
} else {
sort.Sort(ByPrice(result))
sort.Sort(byPrice(result))
}
return result
}
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]
}
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]
}

View File

@@ -12,11 +12,12 @@ const (
)
func TestLenByPrice(t *testing.T) {
t.Parallel()
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
Items = []Item{
localItems := []Item{
{
Exchange: testExchange,
Pair: p,
@@ -26,17 +27,18 @@ func TestLenByPrice(t *testing.T) {
},
}
if ByPrice.Len(Items) < 1 {
if byPrice.Len(localItems) < 1 {
t.Error("stats LenByPrice() length not correct.")
}
}
func TestLessByPrice(t *testing.T) {
t.Parallel()
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
Items = []Item{
localItems := []Item{
{
Exchange: "alphapoint",
Pair: p,
@@ -53,20 +55,21 @@ func TestLessByPrice(t *testing.T) {
},
}
if !ByPrice.Less(Items, 1, 0) {
if !byPrice.Less(localItems, 1, 0) {
t.Error("stats LessByPrice() incorrect return.")
}
if ByPrice.Less(Items, 0, 1) {
if byPrice.Less(localItems, 0, 1) {
t.Error("stats LessByPrice() incorrect return.")
}
}
func TestSwapByPrice(t *testing.T) {
t.Parallel()
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
Items = []Item{
localItems := []Item{
{
Exchange: "bitstamp",
Pair: p,
@@ -83,37 +86,97 @@ func TestSwapByPrice(t *testing.T) {
},
}
ByPrice.Swap(Items, 0, 1)
if Items[0].Exchange != "bitfinex" || Items[1].Exchange != "bitstamp" {
byPrice.Swap(localItems, 0, 1)
if localItems[0].Exchange != "bitfinex" || localItems[1].Exchange != "bitstamp" {
t.Error("stats SwapByPrice did not swap values.")
}
}
func TestLenByVolume(t *testing.T) {
if ByVolume.Len(Items) != 2 {
t.Parallel()
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
localItems := []Item{
{
Exchange: "bitstamp",
Pair: p,
AssetType: asset.Spot,
Price: 1324,
Volume: 5,
},
{
Exchange: "bitfinex",
Pair: p,
AssetType: asset.Spot,
Price: 7863,
Volume: 20,
},
}
if byVolume.Len(localItems) != 2 {
t.Error("stats lenByVolume did not swap values.")
}
}
func TestLessByVolume(t *testing.T) {
if !ByVolume.Less(Items, 1, 0) {
t.Error("stats LessByVolume() incorrect return.")
t.Parallel()
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
if ByVolume.Less(Items, 0, 1) {
t.Error("stats LessByVolume() incorrect return.")
localItems := []Item{
{
Exchange: "bitstamp",
Pair: p,
AssetType: asset.Spot,
Price: 1324,
Volume: 5,
},
{
Exchange: "bitfinex",
Pair: p,
AssetType: asset.Spot,
Price: 7863,
Volume: 20,
},
}
if !byVolume.Less(localItems, 0, 1) {
t.Error("localItems[0].Volume should be less than localItems[1].Volume")
}
}
func TestSwapByVolume(t *testing.T) {
ByPrice.Swap(Items, 0, 1)
if Items[1].Exchange != "bitfinex" || Items[0].Exchange != "bitstamp" {
t.Parallel()
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
}
localItems := []Item{
{
Exchange: "bitstamp",
Pair: p,
AssetType: asset.Spot,
Price: 1324,
Volume: 5,
},
{
Exchange: "bitfinex",
Pair: p,
AssetType: asset.Spot,
Price: 7863,
Volume: 20,
},
}
byVolume.Swap(localItems, 0, 1)
if localItems[0].Exchange != "bitfinex" || localItems[1].Exchange != "bitstamp" {
t.Error("stats SwapByVolume did not swap values.")
}
}
func TestAdd(t *testing.T) {
Items = Items[:0]
items = items[:0]
p, err := currency.NewPairFromStrings("BTC", "USD")
if err != nil {
t.Fatal(err)
@@ -123,7 +186,7 @@ func TestAdd(t *testing.T) {
t.Fatal(err)
}
if len(Items) < 1 {
if len(items) < 1 {
t.Error("stats Add did not add exchange info.")
}
@@ -132,7 +195,7 @@ func TestAdd(t *testing.T) {
t.Fatal("error cannot be nil")
}
if len(Items) != 1 {
if len(items) != 1 {
t.Error("stats Add did not add exchange info.")
}
@@ -142,7 +205,7 @@ func TestAdd(t *testing.T) {
t.Fatal(err)
}
if Items[1].Pair.String() != "XBTUSD" {
if items[1].Pair.String() != "XBTUSD" {
t.Fatal("stats Add did not add exchange info.")
}
@@ -156,7 +219,7 @@ func TestAdd(t *testing.T) {
t.Fatal(err)
}
if Items[2].Pair.String() != "ETHUSD" {
if items[2].Pair.String() != "ETHUSD" {
t.Fatal("stats Add did not add exchange info.")
}
}
@@ -167,12 +230,12 @@ func TestAppend(t *testing.T) {
t.Fatal(err)
}
Append("sillyexchange", p, asset.Spot, 1234, 45)
if len(Items) < 2 {
if len(items) < 2 {
t.Error("stats AppendResults did not add exchange values.")
}
Append("sillyexchange", p, asset.Spot, 1234, 45)
if len(Items) == 3 {
if len(items) == 3 {
t.Error("stats AppendResults added exchange values")
}
}

View File

@@ -0,0 +1,29 @@
package stats
import (
"sync"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
var (
// items holds stat items
items []Item
statMutex sync.Mutex
)
// Item holds various fields for storing currency pair stats
type Item struct {
Exchange string
Pair currency.Pair
AssetType asset.Item
Price float64
Volume float64
}
// byPrice allows sorting by price
type byPrice []Item
// byVolume allows sorting by volume
type byVolume []Item