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:
Ryan O'Hara-Reid
2019-03-19 11:49:05 +11:00
committed by Adrian Gallagher
parent ed760e184e
commit 0990f9d118
189 changed files with 11982 additions and 8055 deletions

View File

@@ -3,13 +3,13 @@ package stats
import (
"sort"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
)
// Item holds various fields for storing currency pair stats
type Item struct {
Exchange string
Pair pair.CurrencyPair
Pair currency.Pair
AssetType string
Price float64
Volume float64
@@ -49,18 +49,23 @@ func (b ByVolume) Swap(i, j int) {
}
// Add adds or updates the item stats
func Add(exchange string, p pair.CurrencyPair, assetType string, price, volume float64) {
if exchange == "" || assetType == "" || price == 0 || volume == 0 || p.FirstCurrency == "" || p.SecondCurrency == "" {
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.FirstCurrency == "XBT" {
newPair := pair.NewCurrencyPair("BTC", p.SecondCurrency.String())
if p.Base == currency.XBT {
newPair := currency.NewPairFromStrings(currency.BTC.String(), p.Quote.String())
Append(exchange, newPair, assetType, price, volume)
}
if p.SecondCurrency == "USDT" {
newPair := pair.NewCurrencyPair(p.FirstCurrency.String(), "USD")
if p.Quote == currency.USDT {
newPair := currency.NewPairFromStrings(p.Base.String(), currency.USD.String())
Append(exchange, newPair, assetType, price, volume)
}
@@ -69,7 +74,7 @@ func Add(exchange string, p pair.CurrencyPair, assetType string, price, volume f
// Append adds or updates the item stats for a specific
// currency pair and asset type
func Append(exchange string, p pair.CurrencyPair, assetType string, price, volume float64) {
func Append(exchange string, p currency.Pair, assetType string, price, volume float64) {
if AlreadyExists(exchange, p, assetType, price, volume) {
return
}
@@ -87,9 +92,11 @@ func Append(exchange string, p pair.CurrencyPair, assetType string, price, volum
// AlreadyExists checks to see if item info already exists
// for a specific currency pair and asset type
func AlreadyExists(exchange string, p pair.CurrencyPair, assetType string, price, volume float64) bool {
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.Equal(p, false) && Items[i].AssetType == assetType {
if Items[i].Exchange == exchange &&
Items[i].Pair.EqualIncludeReciprocal(p) &&
Items[i].AssetType == assetType {
Items[i].Price, Items[i].Volume = price, volume
return true
}
@@ -100,10 +107,11 @@ func AlreadyExists(exchange string, p pair.CurrencyPair, assetType string, price
// 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 pair.CurrencyPair, assetType string, reverse bool) []Item {
func SortExchangesByVolume(p currency.Pair, assetType string, reverse bool) []Item {
var result []Item
for x := range Items {
if Items[x].Pair.Equal(p, false) && Items[x].AssetType == assetType {
if Items[x].Pair.EqualIncludeReciprocal(p) &&
Items[x].AssetType == assetType {
result = append(result, Items[x])
}
}
@@ -119,10 +127,11 @@ func SortExchangesByVolume(p pair.CurrencyPair, assetType string, reverse bool)
// 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 pair.CurrencyPair, assetType string, reverse bool) []Item {
func SortExchangesByPrice(p currency.Pair, assetType string, reverse bool) []Item {
var result []Item
for x := range Items {
if Items[x].Pair.Equal(p, false) && Items[x].AssetType == assetType {
if Items[x].Pair.EqualIncludeReciprocal(p) &&
Items[x].AssetType == assetType {
result = append(result, Items[x])
}
}

View File

@@ -3,11 +3,11 @@ package stats
import (
"testing"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
)
func TestLenByPrice(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
Items = []Item{
{
Exchange: "ANX",
@@ -24,7 +24,7 @@ func TestLenByPrice(t *testing.T) {
}
func TestLessByPrice(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
Items = []Item{
{
@@ -52,7 +52,7 @@ func TestLessByPrice(t *testing.T) {
}
func TestSwapByPrice(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
Items = []Item{
{
@@ -102,7 +102,7 @@ func TestSwapByVolume(t *testing.T) {
func TestAdd(t *testing.T) {
Items = Items[:0]
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
Add("ANX", p, "SPOT", 1200, 42)
if len(Items) < 1 {
@@ -115,23 +115,23 @@ func TestAdd(t *testing.T) {
t.Error("Test Failed - stats Add did not add exchange info.")
}
p.FirstCurrency = "XBT"
p.Base = currency.XBT
Add("ANX", p, "SPOT", 1201, 43)
if Items[1].Pair.Pair() != "XBTUSD" {
if Items[1].Pair.String() != "XBTUSD" {
t.Fatal("Test failed. stats Add did not add exchange info.")
}
p = pair.NewCurrencyPair("ETH", "USDT")
p = currency.NewPairFromStrings("ETH", "USDT")
Add("ANX", p, "SPOT", 300, 1000)
if Items[2].Pair.Pair() != "ETHUSD" {
if Items[2].Pair.String() != "ETHUSD" {
t.Fatal("Test failed. stats Add did not add exchange info.")
}
}
func TestAppend(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
Append("sillyexchange", p, "SPOT", 1234, 45)
if len(Items) < 2 {
t.Error("Test Failed - stats Append did not add exchange values.")
@@ -144,18 +144,18 @@ func TestAppend(t *testing.T) {
}
func TestAlreadyExists(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
if !AlreadyExists("ANX", p, "SPOT", 1200, 42) {
t.Error("Test Failed - stats AlreadyExists exchange does not exist.")
}
p.FirstCurrency = "dii"
p.Base = currency.NewCode("dii")
if AlreadyExists("bla", p, "SPOT", 1234, 123) {
t.Error("Test Failed - stats AlreadyExists found incorrect exchange.")
}
}
func TestSortExchangesByVolume(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
topVolume := SortExchangesByVolume(p, "SPOT", true)
if topVolume[0].Exchange != "sillyexchange" {
t.Error("Test Failed - stats SortExchangesByVolume incorrectly sorted values.")
@@ -168,7 +168,7 @@ func TestSortExchangesByVolume(t *testing.T) {
}
func TestSortExchangesByPrice(t *testing.T) {
p := pair.NewCurrencyPair("BTC", "USD")
p := currency.NewPairFromStrings("BTC", "USD")
topPrice := SortExchangesByPrice(p, "SPOT", true)
if topPrice[0].Exchange != "sillyexchange" {
t.Error("Test Failed - stats SortExchangesByPrice incorrectly sorted values.")