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

@@ -7,7 +7,7 @@ import (
"time"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
)
// Const values for the ticker package
@@ -27,56 +27,55 @@ var (
// Price struct stores the currency pair and pricing information
type Price struct {
Pair pair.CurrencyPair `json:"Pair"`
LastUpdated time.Time `json:"LastUpdated"`
CurrencyPair string `json:"CurrencyPair"`
Last float64 `json:"Last"`
High float64 `json:"High"`
Low float64 `json:"Low"`
Bid float64 `json:"Bid"`
Ask float64 `json:"Ask"`
Volume float64 `json:"Volume"`
PriceATH float64 `json:"PriceATH"`
Pair currency.Pair `json:"Pair"`
Last float64 `json:"Last"`
High float64 `json:"High"`
Low float64 `json:"Low"`
Bid float64 `json:"Bid"`
Ask float64 `json:"Ask"`
Volume float64 `json:"Volume"`
PriceATH float64 `json:"PriceATH"`
LastUpdated time.Time
}
// Ticker struct holds the ticker information for a currency pair and type
type Ticker struct {
Price map[pair.CurrencyItem]map[pair.CurrencyItem]map[string]Price
Price map[string]map[string]map[string]Price
ExchangeName string
}
// PriceToString returns the string version of a stored price field
func (t *Ticker) PriceToString(p pair.CurrencyPair, priceType, tickerType string) string {
func (t *Ticker) PriceToString(p currency.Pair, priceType, tickerType string) string {
priceType = common.StringToLower(priceType)
switch priceType {
case "last":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].Last, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].Last, 'f', -1, 64)
case "high":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].High, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].High, 'f', -1, 64)
case "low":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].Low, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].Low, 'f', -1, 64)
case "bid":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].Bid, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].Bid, 'f', -1, 64)
case "ask":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].Ask, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].Ask, 'f', -1, 64)
case "volume":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].Volume, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].Volume, 'f', -1, 64)
case "ath":
return strconv.FormatFloat(t.Price[p.FirstCurrency][p.SecondCurrency][tickerType].PriceATH, 'f', -1, 64)
return strconv.FormatFloat(t.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType].PriceATH, 'f', -1, 64)
default:
return ""
}
}
// GetTicker checks and returns a requested ticker if it exists
func GetTicker(exchange string, p pair.CurrencyPair, tickerType string) (Price, error) {
func GetTicker(exchange string, p currency.Pair, tickerType string) (Price, error) {
ticker, err := GetTickerByExchange(exchange)
if err != nil {
return Price{}, err
}
if !FirstCurrencyExists(exchange, p.FirstCurrency) {
if !FirstCurrencyExists(exchange, p.Base) {
return Price{}, errors.New(ErrPrimaryCurrencyNotFound)
}
@@ -84,7 +83,7 @@ func GetTicker(exchange string, p pair.CurrencyPair, tickerType string) (Price,
return Price{}, errors.New(ErrSecondaryCurrencyNotFound)
}
return ticker.Price[p.FirstCurrency][p.SecondCurrency][tickerType], nil
return ticker.Price[p.Base.Upper().String()][p.Quote.Upper().String()][tickerType], nil
}
// GetTickerByExchange returns an exchange Ticker
@@ -101,12 +100,12 @@ func GetTickerByExchange(exchange string) (*Ticker, error) {
// FirstCurrencyExists checks to see if the first currency of the Price map
// exists
func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
func FirstCurrencyExists(exchange string, currency currency.Code) bool {
m.Lock()
defer m.Unlock()
for _, y := range Tickers {
if y.ExchangeName == exchange {
if _, ok := y.Price[currency]; ok {
if _, ok := y.Price[currency.Upper().String()]; ok {
return true
}
}
@@ -116,13 +115,13 @@ func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
// SecondCurrencyExists checks to see if the second currency of the Price map
// exists
func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
func SecondCurrencyExists(exchange string, p currency.Pair) bool {
m.Lock()
defer m.Unlock()
for _, y := range Tickers {
if y.ExchangeName == exchange {
if _, ok := y.Price[p.FirstCurrency]; ok {
if _, ok := y.Price[p.FirstCurrency][p.SecondCurrency]; ok {
if _, ok := y.Price[p.Base.Upper().String()]; ok {
if _, ok := y.Price[p.Base.Upper().String()][p.Quote.Upper().String()]; ok {
return true
}
}
@@ -132,52 +131,51 @@ func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
}
// CreateNewTicker creates a new Ticker
func CreateNewTicker(exchangeName string, p pair.CurrencyPair, tickerNew Price, tickerType string) Ticker {
func CreateNewTicker(exchangeName string, tickerNew Price, tickerType string) Ticker {
m.Lock()
defer m.Unlock()
ticker := Ticker{}
ticker.ExchangeName = exchangeName
ticker.Price = make(map[pair.CurrencyItem]map[pair.CurrencyItem]map[string]Price)
a := make(map[pair.CurrencyItem]map[string]Price)
ticker.Price = make(map[string]map[string]map[string]Price)
a := make(map[string]map[string]Price)
b := make(map[string]Price)
b[tickerType] = tickerNew
a[p.SecondCurrency] = b
ticker.Price[p.FirstCurrency] = a
a[tickerNew.Pair.Quote.Upper().String()] = b
ticker.Price[tickerNew.Pair.Base.Upper().String()] = a
Tickers = append(Tickers, ticker)
return ticker
}
// ProcessTicker processes incoming tickers, creating or updating the Tickers
// list
func ProcessTicker(exchangeName string, p pair.CurrencyPair, tickerNew Price, tickerType string) {
if tickerNew.Pair.Pair() == "" {
// set Pair if not set
tickerNew.Pair = p
func ProcessTicker(exchangeName string, tickerNew Price, tickerType string) error {
if tickerNew.Pair.String() == "" {
return errors.New("")
}
tickerNew.CurrencyPair = p.Pair().String()
tickerNew.LastUpdated = time.Now()
ticker, err := GetTickerByExchange(exchangeName)
if err != nil {
CreateNewTicker(exchangeName, p, tickerNew, tickerType)
return
CreateNewTicker(exchangeName, tickerNew, tickerType)
return nil
}
if FirstCurrencyExists(exchangeName, p.FirstCurrency) {
if FirstCurrencyExists(exchangeName, tickerNew.Pair.Base) {
m.Lock()
a := make(map[string]Price)
a[tickerType] = tickerNew
ticker.Price[p.FirstCurrency][p.SecondCurrency] = a
ticker.Price[tickerNew.Pair.Base.Upper().String()][tickerNew.Pair.Quote.Upper().String()] = a
m.Unlock()
return
return nil
}
m.Lock()
a := make(map[pair.CurrencyItem]map[string]Price)
a := make(map[string]map[string]Price)
b := make(map[string]Price)
b[tickerType] = tickerNew
a[p.SecondCurrency] = b
ticker.Price[p.FirstCurrency] = a
a[tickerNew.Pair.Quote.Upper().String()] = b
ticker.Price[tickerNew.Pair.Base.Upper().String()] = a
m.Unlock()
return nil
}

View File

@@ -8,24 +8,24 @@ import (
"testing"
"time"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
log "github.com/thrasher-/gocryptotrader/logger"
)
func TestPriceToString(t *testing.T) {
newPair := pair.NewCurrencyPair("BTC", "USD")
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
newTicker := CreateNewTicker("ANX", newPair, priceStruct, Spot)
newTicker := CreateNewTicker("ANX", priceStruct, Spot)
if newTicker.PriceToString(newPair, "last", Spot) != "1200" {
t.Error("Test Failed - ticker PriceToString last value is incorrect")
@@ -54,25 +54,28 @@ func TestPriceToString(t *testing.T) {
}
func TestGetTicker(t *testing.T) {
newPair := pair.NewCurrencyPair("BTC", "USD")
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
err := ProcessTicker("bitfinex", priceStruct, Spot)
if err != nil {
t.Fatal("Test failed. ProcessTicker error", err)
}
ProcessTicker("bitfinex", newPair, priceStruct, Spot)
tickerPrice, err := GetTicker("bitfinex", newPair, Spot)
if err != nil {
t.Errorf("Test Failed - Ticker GetTicker init error: %s", err)
}
if tickerPrice.CurrencyPair != "BTCUSD" {
if tickerPrice.Pair.String() != "BTCUSD" {
t.Error("Test Failed - ticker tickerPrice.CurrencyPair value is incorrect")
}
@@ -81,20 +84,25 @@ func TestGetTicker(t *testing.T) {
t.Fatal("Test Failed. TestGetTicker returned nil error on invalid exchange")
}
newPair.FirstCurrency = "ETH"
newPair.Base = currency.ETH
_, err = GetTicker("bitfinex", newPair, Spot)
if err == nil {
t.Fatal("Test Failed. TestGetTicker returned ticker for invalid first currency")
}
btcltcPair := pair.NewCurrencyPair("BTC", "LTC")
btcltcPair := currency.NewPairFromStrings("BTC", "LTC")
_, err = GetTicker("bitfinex", btcltcPair, Spot)
if err == nil {
t.Fatal("Test Failed. TestGetTicker returned ticker for invalid second currency")
}
priceStruct.PriceATH = 9001
ProcessTicker("bitfinex", newPair, priceStruct, "futures_3m")
priceStruct.Pair.Base = currency.ETH
err = ProcessTicker("bitfinex", priceStruct, "futures_3m")
if err != nil {
t.Fatal("Test failed. ProcessTicker error", err)
}
tickerPrice, err = GetTicker("bitfinex", newPair, "futures_3m")
if err != nil {
t.Errorf("Test Failed - Ticker GetTicker init error: %s", err)
@@ -106,20 +114,19 @@ func TestGetTicker(t *testing.T) {
}
func TestGetTickerByExchange(t *testing.T) {
newPair := pair.NewCurrencyPair("BTC", "USD")
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
anxTicker := CreateNewTicker("ANX", newPair, priceStruct, Spot)
anxTicker := CreateNewTicker("ANX", priceStruct, Spot)
Tickers = append(Tickers, anxTicker)
tickerPtr, err := GetTickerByExchange("ANX")
@@ -132,26 +139,25 @@ func TestGetTickerByExchange(t *testing.T) {
}
func TestFirstCurrencyExists(t *testing.T) {
newPair := pair.NewCurrencyPair("BTC", "USD")
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
alphaTicker := CreateNewTicker("alphapoint", newPair, priceStruct, Spot)
alphaTicker := CreateNewTicker("alphapoint", priceStruct, Spot)
Tickers = append(Tickers, alphaTicker)
if !FirstCurrencyExists("alphapoint", "BTC") {
if !FirstCurrencyExists("alphapoint", currency.BTC) {
t.Error("Test Failed - FirstCurrencyExists1 value return is incorrect")
}
if FirstCurrencyExists("alphapoint", "CATS") {
if FirstCurrencyExists("alphapoint", currency.NewCode("CATS")) {
t.Error("Test Failed - FirstCurrencyExists2 value return is incorrect")
}
}
@@ -159,48 +165,46 @@ func TestFirstCurrencyExists(t *testing.T) {
func TestSecondCurrencyExists(t *testing.T) {
t.Parallel()
newPair := pair.NewCurrencyPair("BTC", "USD")
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
bitstampTicker := CreateNewTicker("bitstamp", newPair, priceStruct, "SPOT")
bitstampTicker := CreateNewTicker("bitstamp", priceStruct, "SPOT")
Tickers = append(Tickers, bitstampTicker)
if !SecondCurrencyExists("bitstamp", newPair) {
t.Error("Test Failed - SecondCurrencyExists1 value return is incorrect")
}
newPair.SecondCurrency = "DOGS"
newPair.Quote = currency.NewCode("DOGS")
if SecondCurrencyExists("bitstamp", newPair) {
t.Error("Test Failed - SecondCurrencyExists2 value return is incorrect")
}
}
func TestCreateNewTicker(t *testing.T) {
newPair := pair.NewCurrencyPair("BTC", "USD")
const float64Type = "float64"
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
newTicker := CreateNewTicker("ANX", newPair, priceStruct, Spot)
const float64Type = "float64"
newTicker := CreateNewTicker("ANX", priceStruct, Spot)
if reflect.ValueOf(newTicker).NumField() != 2 {
t.Error("Test Failed - ticker CreateNewTicker struct change/or updated")
@@ -212,7 +216,7 @@ func TestCreateNewTicker(t *testing.T) {
t.Error("Test Failed - ticker CreateNewTicker.ExchangeName value is not ANX")
}
if newTicker.Price["BTC"]["USD"][Spot].Pair.Pair().String() != "BTCUSD" {
if newTicker.Price[currency.BTC.Upper().String()][currency.USD.Upper().String()][Spot].Pair.String() != "BTCUSD" {
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Pair.Pair().String() value is not expected 'BTCUSD'")
}
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][Spot].Ask).String() != float64Type {
@@ -221,8 +225,8 @@ func TestCreateNewTicker(t *testing.T) {
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][Spot].Bid).String() != float64Type {
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].Bid value is not a float64")
}
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][Spot].CurrencyPair).String() != "string" {
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].CurrencyPair value is not a string")
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][Spot].Pair).String() != "currency.Pair" {
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].CurrencyPair value is not a currency.Pair")
}
if reflect.TypeOf(newTicker.Price["BTC"]["USD"][Spot].High).String() != float64Type {
t.Error("Test Failed - ticker newTicker.Price[BTC][USD].High value is not a float64")
@@ -243,33 +247,43 @@ func TestCreateNewTicker(t *testing.T) {
func TestProcessTicker(t *testing.T) { // non-appending function to tickers
Tickers = []Ticker{}
newPair := pair.NewCurrencyPair("BTC", "USD")
newPair := currency.NewPairFromStrings("BTC", "USD")
priceStruct := Price{
Pair: newPair,
CurrencyPair: newPair.Pair().String(),
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
Pair: newPair,
Last: 1200,
High: 1298,
Low: 1148,
Bid: 1195,
Ask: 1220,
Volume: 5,
PriceATH: 1337,
}
ProcessTicker("btcc", newPair, priceStruct, Spot)
err := ProcessTicker("btcc", Price{}, Spot)
if err == nil {
t.Fatal("Test failed. ProcessTicker error cannot be nil")
}
err = ProcessTicker("btcc", priceStruct, Spot)
if err != nil {
t.Fatal("Test failed. ProcessTicker error", err)
}
result, err := GetTicker("btcc", newPair, Spot)
if err != nil {
t.Fatal("Test failed. TestProcessTicker failed to create and return a new ticker")
}
if result.Pair.Pair() != newPair.Pair() {
if result.Pair.String() != newPair.String() {
t.Fatal("Test failed. TestProcessTicker pair mismatch")
}
secondPair := pair.NewCurrencyPair("BTC", "AUD")
secondPair := currency.NewPairFromStrings("BTC", "AUD")
priceStruct.Pair = secondPair
ProcessTicker("btcc", secondPair, priceStruct, Spot)
err = ProcessTicker("btcc", priceStruct, Spot)
if err != nil {
t.Fatal("Test failed. ProcessTicker error", err)
}
result, err = GetTicker("btcc", secondPair, Spot)
if err != nil {
@@ -283,7 +297,7 @@ func TestProcessTicker(t *testing.T) { // non-appending function to tickers
type quick struct {
Name string
P pair.CurrencyPair
P currency.Pair
TP Price
}
@@ -294,26 +308,41 @@ func TestProcessTicker(t *testing.T) { // non-appending function to tickers
var wg sync.WaitGroup
var sm sync.Mutex
var catastrophicFailure bool
for i := 0; i < 500; i++ {
if catastrophicFailure {
break
}
wg.Add(1)
go func() {
newName := "Exchange" + strconv.FormatInt(rand.Int63(), 10)
newPairs := pair.NewCurrencyPair("BTC"+strconv.FormatInt(rand.Int63(), 10),
newPairs := currency.NewPairFromStrings("BTC"+strconv.FormatInt(rand.Int63(), 10),
"USD"+strconv.FormatInt(rand.Int63(), 10))
tp := Price{
Pair: newPairs,
CurrencyPair: newPairs.Pair().String(),
Last: rand.Float64(),
Pair: newPairs,
Last: rand.Float64(),
}
ProcessTicker(newName, newPairs, tp, Spot)
sm.Lock()
err = ProcessTicker(newName, tp, Spot)
if err != nil {
log.Error(err)
catastrophicFailure = true
return
}
testArray = append(testArray, quick{Name: newName, P: newPairs, TP: tp})
sm.Unlock()
wg.Done()
}()
}
if catastrophicFailure {
t.Fatal("Test failed. ProcessTicker error")
}
wg.Wait()
for _, test := range testArray {