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

@@ -9,8 +9,7 @@ import (
"time"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/currency/symbol"
"github.com/thrasher-/gocryptotrader/currency"
exchange "github.com/thrasher-/gocryptotrader/exchanges"
"github.com/thrasher-/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
@@ -38,7 +37,13 @@ func (b *Bithumb) Run() {
if err != nil {
log.Errorf("%s Failed to get available symbols.\n", b.GetName())
} else {
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 symbols.\n", b.GetName())
}
@@ -60,7 +65,7 @@ func (b *Bithumb) GetTradingPairs() ([]string, error) {
}
// UpdateTicker updates and returns the ticker for a currency pair
func (b *Bithumb) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
func (b *Bithumb) UpdateTicker(p currency.Pair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
tickers, err := b.GetAllTickers()
@@ -69,7 +74,7 @@ func (b *Bithumb) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pr
}
for _, x := range b.GetEnabledCurrencies() {
currency := x.FirstCurrency.String()
currency := x.Base.String()
var tp ticker.Price
tp.Pair = x
tp.Ask = tickers[currency].SellPrice
@@ -78,13 +83,17 @@ func (b *Bithumb) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Pr
tp.Last = tickers[currency].ClosingPrice
tp.Volume = tickers[currency].Volume1Day
tp.High = tickers[currency].MaxPrice
ticker.ProcessTicker(b.Name, x, tp, assetType)
err = ticker.ProcessTicker(b.Name, tp, assetType)
if err != nil {
return tickerPrice, err
}
}
return ticker.GetTicker(b.Name, p, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (b *Bithumb) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
func (b *Bithumb) 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)
@@ -93,8 +102,8 @@ func (b *Bithumb) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.
}
// GetOrderbookEx returns orderbook base on the currency pair
func (b *Bithumb) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), currency, assetType)
func (b *Bithumb) 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)
}
@@ -102,9 +111,9 @@ func (b *Bithumb) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bithumb) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
func (b *Bithumb) UpdateOrderbook(p currency.Pair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
currency := p.FirstCurrency.String()
currency := p.Base.String()
orderbookNew, err := b.GetOrderBook(currency)
if err != nil {
@@ -119,8 +128,16 @@ func (b *Bithumb) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderb
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: asks.Quantity, Price: asks.Price})
}
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
@@ -141,7 +158,7 @@ func (b *Bithumb) GetAccountInfo() (exchange.AccountInfo, error) {
}
exchangeBalances = append(exchangeBalances, exchange.AccountCurrencyInfo{
CurrencyName: key,
CurrencyName: currency.NewCode(key),
TotalValue: totalAmount,
Hold: hold,
})
@@ -163,7 +180,7 @@ func (b *Bithumb) GetFundingHistory() ([]exchange.FundHistory, error) {
}
// GetExchangeHistory returns historic trade data since exchange opening.
func (b *Bithumb) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]exchange.TradeHistory, error) {
func (b *Bithumb) GetExchangeHistory(p currency.Pair, assetType string) ([]exchange.TradeHistory, error) {
var resp []exchange.TradeHistory
return resp, common.ErrNotYetImplemented
@@ -171,17 +188,17 @@ func (b *Bithumb) GetExchangeHistory(p pair.CurrencyPair, assetType string) ([]e
// SubmitOrder submits a new order
// TODO: Fill this out to support limit orders
func (b *Bithumb) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, _ exchange.OrderType, amount, _ float64, _ string) (exchange.SubmitOrderResponse, error) {
func (b *Bithumb) SubmitOrder(p currency.Pair, side exchange.OrderSide, _ exchange.OrderType, amount, _ float64, _ string) (exchange.SubmitOrderResponse, error) {
var submitOrderResponse exchange.SubmitOrderResponse
var err error
var orderID string
if side == exchange.BuyOrderSide {
var result MarketBuy
result, err = b.MarketBuyOrder(p.FirstCurrency.String(), amount)
result, err = b.MarketBuyOrder(p.Base.String(), amount)
orderID = result.OrderID
} else if side == exchange.SellOrderSide {
var result MarketSell
result, err = b.MarketSellOrder(p.FirstCurrency.String(), amount)
result, err = b.MarketSellOrder(p.Base.String(), amount)
orderID = result.OrderID
}
@@ -200,7 +217,7 @@ func (b *Bithumb) SubmitOrder(p pair.CurrencyPair, side exchange.OrderSide, _ ex
// market conversion
func (b *Bithumb) ModifyOrder(action exchange.ModifyOrder) (string, error) {
order, err := b.ModifyTrade(action.OrderID,
action.Currency.FirstCurrency.String(),
action.CurrencyPair.Base.String(),
common.StringToLower(action.OrderSide.ToString()),
action.Amount,
int64(action.Price))
@@ -214,7 +231,9 @@ func (b *Bithumb) ModifyOrder(action exchange.ModifyOrder) (string, error) {
// CancelOrder cancels an order by its corresponding ID number
func (b *Bithumb) CancelOrder(order exchange.OrderCancellation) error {
_, err := b.CancelTrade(order.Side.ToString(), order.OrderID, order.CurrencyPair.FirstCurrency.String())
_, err := b.CancelTrade(order.Side.ToString(),
order.OrderID,
order.CurrencyPair.Base.String())
return err
}
@@ -226,7 +245,11 @@ func (b *Bithumb) CancelAllOrders(orderCancellation exchange.OrderCancellation)
var allOrders []OrderData
for _, currency := range b.GetEnabledCurrencies() {
orders, err := b.GetOrders("", orderCancellation.Side.ToString(), "100", "", currency.FirstCurrency.String())
orders, err := b.GetOrders("",
orderCancellation.Side.ToString(),
"100",
"",
currency.Base.String())
if err != nil {
return cancelAllOrdersResponse, err
}
@@ -234,7 +257,9 @@ func (b *Bithumb) CancelAllOrders(orderCancellation exchange.OrderCancellation)
}
for _, order := range allOrders {
_, err := b.CancelTrade(orderCancellation.Side.ToString(), order.OrderID, orderCancellation.CurrencyPair.FirstCurrency.String())
_, err := b.CancelTrade(orderCancellation.Side.ToString(),
order.OrderID,
orderCancellation.CurrencyPair.Base.String())
if err != nil {
cancelAllOrdersResponse.OrderStatus[order.OrderID] = err.Error()
}
@@ -250,7 +275,7 @@ func (b *Bithumb) GetOrderInfo(orderID string) (exchange.OrderDetail, error) {
}
// GetDepositAddress returns a deposit address for a specified currency
func (b *Bithumb) GetDepositAddress(cryptocurrency pair.CurrencyItem, _ string) (string, error) {
func (b *Bithumb) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
addr, err := b.GetWalletAddress(cryptocurrency.String())
if err != nil {
return "", err
@@ -272,7 +297,7 @@ func (b *Bithumb) WithdrawFiatFunds(withdrawRequest exchange.WithdrawRequest) (s
if math.Mod(withdrawRequest.Amount, 1) != 0 {
return "", errors.New("currency KRW does not support decimal places")
}
if withdrawRequest.Currency.String() != symbol.KRW {
if withdrawRequest.Currency != currency.KRW {
return "", errors.New("only KRW is supported")
}
bankDetails := fmt.Sprintf("%v_%v", withdrawRequest.BankCode, withdrawRequest.BankName)
@@ -326,7 +351,9 @@ func (b *Bithumb) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([
Price: order.Price,
RemainingAmount: order.UnitsRemaining,
Status: string(exchange.ActiveOrderStatus),
CurrencyPair: pair.NewCurrencyPairWithDelimiter(order.OrderCurrency, order.PaymentCurrency, b.ConfigCurrencyPairFormat.Delimiter),
CurrencyPair: currency.NewPairWithDelimiter(order.OrderCurrency,
order.PaymentCurrency,
b.ConfigCurrencyPairFormat.Delimiter),
}
if order.Type == "bid" {
@@ -339,7 +366,8 @@ func (b *Bithumb) GetActiveOrders(getOrdersRequest exchange.GetOrdersRequest) ([
}
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
return orders, nil
@@ -367,7 +395,9 @@ func (b *Bithumb) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) ([
OrderDate: orderDate,
Price: order.Price,
RemainingAmount: order.UnitsRemaining,
CurrencyPair: pair.NewCurrencyPairWithDelimiter(order.OrderCurrency, order.PaymentCurrency, b.ConfigCurrencyPairFormat.Delimiter),
CurrencyPair: currency.NewPairWithDelimiter(order.OrderCurrency,
order.PaymentCurrency,
b.ConfigCurrencyPairFormat.Delimiter),
}
if order.Type == "bid" {
@@ -380,7 +410,8 @@ func (b *Bithumb) GetOrderHistory(getOrdersRequest exchange.GetOrdersRequest) ([
}
exchange.FilterOrdersBySide(&orders, getOrdersRequest.OrderSide)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks, getOrdersRequest.EndTicks)
exchange.FilterOrdersByTickRange(&orders, getOrdersRequest.StartTicks,
getOrdersRequest.EndTicks)
exchange.FilterOrdersByCurrencies(&orders, getOrdersRequest.Currencies)
return orders, nil