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

@@ -12,7 +12,7 @@ import (
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/symbol"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/request"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
@@ -149,9 +149,9 @@ func (b *Bitmex) Setup(exch config.ExchangeConfig) {
b.RESTPollingDelay = exch.RESTPollingDelay
b.Verbose = exch.Verbose
b.Websocket.SetWsStatusAndConnection(exch.Websocket)
b.BaseCurrencies = common.SplitStrings(exch.BaseCurrencies, ",")
b.AvailablePairs = common.SplitStrings(exch.AvailablePairs, ",")
b.EnabledPairs = common.SplitStrings(exch.EnabledPairs, ",")
b.BaseCurrencies = exch.BaseCurrencies
b.AvailablePairs = exch.AvailablePairs
b.EnabledPairs = exch.EnabledPairs
err := b.SetCurrencyPairFormat()
if err != nil {
log.Fatal(err)
@@ -705,13 +705,13 @@ func (b *Bitmex) ConfirmWithdrawal(token string) (TransactionInfo, error) {
}
// GetCryptoDepositAddress returns a deposit address for a cryptocurency
func (b *Bitmex) GetCryptoDepositAddress(currency string) (string, error) {
func (b *Bitmex) GetCryptoDepositAddress(cryptoCurrency string) (string, error) {
var address string
if !strings.EqualFold(currency, symbol.XBT) {
if !strings.EqualFold(cryptoCurrency, currency.XBT.String()) {
return "",
fmt.Errorf("cryptocurrency %s deposits are not supported by exchange only bitcoin",
currency)
cryptoCurrency)
}
return address, b.SendAuthenticatedHTTPRequest(http.MethodGet,

View File

@@ -7,8 +7,7 @@ import (
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency/symbol"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
)
@@ -363,13 +362,10 @@ func TestGetPreviousTrades(t *testing.T) {
func setFeeBuilder() exchange.FeeBuilder {
return exchange.FeeBuilder{
Amount: 1,
Delimiter: "",
FeeType: exchange.CryptocurrencyTradeFee,
FirstCurrency: symbol.BTC,
SecondCurrency: symbol.LTC,
IsMaker: false,
PurchasePrice: 1,
Amount: 1,
FeeType: exchange.CryptocurrencyTradeFee,
Pair: currency.NewPair(currency.BTC, currency.LTC),
PurchasePrice: 1,
}
}
@@ -429,7 +425,7 @@ func TestGetFee(t *testing.T) {
// InternationalBankDepositFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.InternationalBankDepositFee
feeBuilder.CurrencyItem = symbol.HKD
feeBuilder.FiatCurrency = currency.HKD
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Received: %f", float64(0), resp)
t.Error(err)
@@ -438,7 +434,7 @@ func TestGetFee(t *testing.T) {
// InternationalBankWithdrawalFee Basic
feeBuilder = setFeeBuilder()
feeBuilder.FeeType = exchange.InternationalBankWithdrawalFee
feeBuilder.CurrencyItem = symbol.HKD
feeBuilder.FiatCurrency = currency.HKD
if resp, err := b.GetFee(feeBuilder); resp != float64(0) || err != nil {
t.Errorf("Test Failed - GetFee() error. Expected: %f, Received: %f", float64(0), resp)
t.Error(err)
@@ -478,8 +474,9 @@ func TestGetOrderHistory(t *testing.T) {
TestSetup(t)
var getOrdersRequest = exchange.GetOrdersRequest{
OrderType: exchange.AnyOrderType,
Currencies: []pair.CurrencyPair{pair.NewCurrencyPair(symbol.LTC, symbol.BTC)},
OrderType: exchange.AnyOrderType,
Currencies: []currency.Pair{currency.NewPair(currency.LTC,
currency.BTC)},
}
_, err := b.GetOrderHistory(getOrdersRequest)
@@ -508,10 +505,10 @@ func TestSubmitOrder(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
var p = pair.CurrencyPair{
Delimiter: "",
FirstCurrency: symbol.XBT,
SecondCurrency: symbol.USD,
var p = currency.Pair{
Delimiter: "",
Base: currency.XBT,
Quote: currency.USD,
}
response, err := b.SubmitOrder(p, exchange.BuyOrderSide, exchange.MarketOrderType, 1, 1, "clientId")
if areTestAPIKeysSet() && (err != nil || !response.IsOrderPlaced) {
@@ -529,7 +526,7 @@ func TestCancelExchangeOrder(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
var orderCancellation = exchange.OrderCancellation{
OrderID: "123456789012345678901234567890123456",
@@ -555,7 +552,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
currencyPair := pair.NewCurrencyPair(symbol.LTC, symbol.BTC)
currencyPair := currency.NewPair(currency.LTC, currency.BTC)
var orderCancellation = exchange.OrderCancellation{
OrderID: "123456789012345678901234567890123456",
@@ -604,7 +601,7 @@ func TestWithdraw(t *testing.T) {
TestSetup(t)
var withdrawCryptoRequest = exchange.WithdrawRequest{
Amount: 100,
Currency: "XBt",
Currency: currency.XBT,
Address: "1F5zVDgNjorJ51oGebSvNCrSAHpwGkUdDB",
Description: "WITHDRAW IT ALL",
OneTimePassword: 000000,
@@ -657,12 +654,12 @@ func TestWithdrawInternationalBank(t *testing.T) {
func TestGetDepositAddress(t *testing.T) {
if areTestAPIKeysSet() {
_, err := b.GetDepositAddress(symbol.BTC, "")
_, err := b.GetDepositAddress(currency.BTC, "")
if err != nil {
t.Error("Test Failed - GetDepositAddress() error", err)
}
} else {
_, err := b.GetDepositAddress(symbol.BTC, "")
_, err := b.GetDepositAddress(currency.BTC, "")
if err == nil {
t.Error("Test Failed - GetDepositAddress() error cannot be nil")
}

View File

@@ -10,7 +10,7 @@ import (
"github.com/gorilla/websocket"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
log "github.com/thrasher-/gocryptotrader/logger"
@@ -236,7 +236,7 @@ func (b *Bitmex) wsHandleIncomingData() {
continue
}
p := pair.NewCurrencyPairFromString(orderbooks.Data[0].Symbol)
p := currency.NewPairFromString(orderbooks.Data[0].Symbol)
// TODO: update this to support multiple asset types
err = b.processOrderbook(orderbooks.Data, orderbooks.Action, p, "CONTRACT")
if err != nil {
@@ -269,7 +269,7 @@ func (b *Bitmex) wsHandleIncomingData() {
Timestamp: timestamp,
Price: trade.Price,
Amount: float64(trade.Size),
CurrencyPair: pair.NewCurrencyPairFromString(trade.Symbol),
CurrencyPair: currency.NewPairFromString(trade.Symbol),
Exchange: b.GetName(),
AssetType: "CONTRACT",
Side: trade.Side,
@@ -300,10 +300,10 @@ func (b *Bitmex) wsHandleIncomingData() {
}
}
var snapshotloaded = make(map[pair.CurrencyPair]map[string]bool)
var snapshotloaded = make(map[currency.Pair]map[string]bool)
// ProcessOrderbook processes orderbook updates
func (b *Bitmex) processOrderbook(data []OrderBookL2, action string, currencyPair pair.CurrencyPair, assetType string) error { // nolint: unparam
func (b *Bitmex) processOrderbook(data []OrderBookL2, action string, currencyPair currency.Pair, assetType string) error { // nolint: unparam
if len(data) < 1 {
return errors.New("bitmex_websocket.go error - no orderbook data")
}
@@ -345,8 +345,6 @@ func (b *Bitmex) processOrderbook(data []OrderBookL2, action string, currencyPai
newOrderbook.Asks = asks
newOrderbook.Bids = bids
newOrderbook.AssetType = assetType
newOrderbook.CurrencyPair = currencyPair.Pair().String()
newOrderbook.LastUpdated = time.Now()
newOrderbook.Pair = currencyPair
err := b.Websocket.Orderbook.LoadSnapshot(newOrderbook, b.GetName(), false)
@@ -415,8 +413,8 @@ func (b *Bitmex) websocketSubscribe() error {
// Orderbook and Trade subscribe
// NOTE more added here in future
subscriber.Arguments = append(subscriber.Arguments,
bitmexWSOrderbookL2+":"+contract.Pair().String(),
bitmexWSTrade+":"+contract.Pair().String())
bitmexWSOrderbookL2+":"+contract.String(),
bitmexWSTrade+":"+contract.String())
}
return b.WebsocketConn.WriteJSON(subscriber)

View File

@@ -8,7 +8,7 @@ import (
"time"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
@@ -42,7 +42,13 @@ func (b *Bitmex) Run() {
exchangeProducts = append(exchangeProducts, info.Symbol)
}
err = b.UpdateCurrencies(exchangeProducts, false, false)
var NewExchangeProducts currency.Pairs
for _, p := range exchangeProducts {
NewExchangeProducts = append(NewExchangeProducts,
currency.NewPairFromString(p))
}
err = b.UpdateCurrencies(NewExchangeProducts, false, false)
if err != nil {
log.Errorf("%s Failed to update available currencies.\n", b.GetName())
}
@@ -50,7 +56,7 @@ func (b *Bitmex) Run() {
}
// UpdateTicker updates and returns the ticker for a currency pair
func (b *Bitmex) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
func (b *Bitmex) UpdateTicker(p currency.Pair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
currency := exchange.FormatExchangeCurrency(b.Name, p)
@@ -68,18 +74,13 @@ func (b *Bitmex) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pri
}
tickerPrice.Pair = p
tickerPrice.LastUpdated = time.Now()
tickerPrice.CurrencyPair = tick[0].Symbol
tickerPrice.Last = tick[0].Price
tickerPrice.Volume = float64(tick[0].Size)
ticker.ProcessTicker(b.Name, p, tickerPrice, assetType)
return tickerPrice, nil
return tickerPrice, ticker.ProcessTicker(b.Name, tickerPrice, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (b *Bitmex) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
func (b *Bitmex) GetTickerPrice(p currency.Pair, assetType string) (ticker.Price, error) {
tickerNew, err := ticker.GetTicker(b.GetName(), p, assetType)
if err != nil {
return b.UpdateTicker(p, assetType)
@@ -88,8 +89,8 @@ func (b *Bitmex) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.P
}
// GetOrderbookEx returns orderbook base on the currency pair
func (b *Bitmex) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), currency, assetType)
func (b *Bitmex) GetOrderbookEx(currency currency.Pair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.Get(b.GetName(), currency, assetType)
if err != nil {
return b.UpdateOrderbook(currency, assetType)
}
@@ -97,7 +98,7 @@ func (b *Bitmex) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (o
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bitmex) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
func (b *Bitmex) UpdateOrderbook(p currency.Pair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetOrderbook(OrderBookGetL2Params{
@@ -119,9 +120,17 @@ func (b *Bitmex) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbo
continue
}
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
orderBook.Pair = p
orderBook.ExchangeName = b.GetName()
orderBook.AssetType = assetType
err = orderBook.Process()
if err != nil {
return orderBook, err
}
return orderbook.Get(b.Name, p, assetType)
}
// GetAccountInfo retrieves balances for all enabled currencies for the
@@ -138,7 +147,7 @@ func (b *Bitmex) GetAccountInfo() (exchange.AccountInfo, error) {
var balances []exchange.AccountCurrencyInfo
for _, data := range bal {
balances = append(balances, exchange.AccountCurrencyInfo{
CurrencyName: data.Currency,
CurrencyName: currency.NewCode(data.Currency),
TotalValue: float64(data.WalletBalance),
})
}
@@ -158,14 +167,14 @@ func (b *Bitmex) GetFundingHistory() ([]exchange.FundHistory, error) {
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (b *Bitmex) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
func (b *Bitmex) GetExchangeHistory(p currency.Pair, assetType string) ([]exchange.TradeHistory, error) {
var resp []exchange.TradeHistory
return resp, common.ErrNotYetImplemented
}
// SubmitOrder submits a new order
func (b *Bitmex) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, _ string) (exchange.SubmitOrderResponse, error) {
func (b *Bitmex) SubmitOrder(p currency.Pair, side exchange.OrderSide, orderType exchange.OrderType, amount, price float64, _ string) (exchange.SubmitOrderResponse, error) {
var submitOrderResponse exchange.SubmitOrderResponse
if math.Mod(amount, 1) != 0 {
@@ -175,7 +184,7 @@ func (b *Bitmex) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, order
var orderNewParams = OrderNewParams{
OrdType: side.ToString(),
Symbol: p.Pair().String(),
Symbol: p.String(),
OrderQty: amount,
Side: side.ToString(),
}
@@ -252,7 +261,7 @@ func (b *Bitmex) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
}
// GetDepositAddress returns a deposit address for a specified currency
func (b *Bitmex) GetDepositAddress(cryptocurrency pair.CurrencyItem, _ string) (string, error) {
func (b *Bitmex) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
return b.GetCryptoDepositAddress(cryptocurrency.String())
}
@@ -319,14 +328,16 @@ func (b *Bitmex) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([]
}
orderDetail := exchange.OrderDetail{
Price: order.Price,
Amount: float64(order.OrderQty),
Exchange: b.Name,
ID: order.OrderID,
OrderSide: orderSide,
OrderType: orderType,
Status: order.OrdStatus,
CurrencyPair: pair.NewCurrencyPairWithDelimiter(order.Symbol, order.SettlCurrency, b.ConfigCurrencyPairFormat.Delimiter),
Price: order.Price,
Amount: float64(order.OrderQty),
Exchange: b.Name,
ID: order.OrderID,
OrderSide: orderSide,
OrderType: orderType,
Status: order.OrdStatus,
CurrencyPair: currency.NewPairWithDelimiter(order.Symbol,
order.SettlCurrency,
b.ConfigCurrencyPairFormat.Delimiter),
}
orders = append(orders, orderDetail)
@@ -334,7 +345,8 @@ func (b *Bitmex) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([]
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
exchange.FilterOrdersByType(&orders, getOrdersRequest.OrderType)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
return orders, nil
@@ -359,14 +371,16 @@ func (b *Bitmex) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) ([]
}
orderDetail := exchange.OrderDetail{
Price: order.Price,
Amount: float64(order.OrderQty),
Exchange: b.Name,
ID: order.OrderID,
OrderSide: orderSide,
OrderType: orderType,
Status: order.OrdStatus,
CurrencyPair: pair.NewCurrencyPairWithDelimiter(order.Symbol, order.SettlCurrency, b.ConfigCurrencyPairFormat.Delimiter),
Price: order.Price,
Amount: float64(order.OrderQty),
Exchange: b.Name,
ID: order.OrderID,
OrderSide: orderSide,
OrderType: orderType,
Status: order.OrdStatus,
CurrencyPair: currency.NewPairWithDelimiter(order.Symbol,
order.SettlCurrency,
b.ConfigCurrencyPairFormat.Delimiter),
}
orders = append(orders, orderDetail)