Link up orderbook websocket code, improve exchange test coverage and various other fixes

This commit is contained in:
Adrian Gallagher
2017-09-02 16:31:08 +10:00
parent 913c104d09
commit 87633c2142
34 changed files with 1215 additions and 354 deletions

View File

@@ -29,7 +29,7 @@ func (a *Alphapoint) GetExchangeAccountInfo() (exchange.AccountInfo, error) {
}
// UpdateTicker updates and returns the ticker for a currency pair
func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair) (ticker.Price, error) {
func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
var tickerPrice ticker.Price
tick, err := a.GetTicker(p.Pair().String())
if err != nil {
@@ -43,22 +43,22 @@ func (a *Alphapoint) UpdateTicker(p pair.CurrencyPair) (ticker.Price, error) {
tickerPrice.High = tick.High
tickerPrice.Volume = tick.Volume
tickerPrice.Last = tick.Last
ticker.ProcessTicker(a.GetName(), p, tickerPrice, ticker.Spot)
return ticker.GetTicker(a.Name, p, ticker.Spot)
ticker.ProcessTicker(a.GetName(), p, tickerPrice, assetType)
return ticker.GetTicker(a.Name, p, assetType)
}
// GetTickerPrice returns the ticker for a currency pair
func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair) (ticker.Price, error) {
tick, err := ticker.GetTicker(a.GetName(), p, ticker.Spot)
func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Price, error) {
tick, err := ticker.GetTicker(a.GetName(), p, assetType)
if err != nil {
return a.UpdateTicker(p)
return a.UpdateTicker(p, assetType)
}
return tick, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *Alphapoint) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (a *Alphapoint) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := a.GetOrderbook(p.Pair().String())
if err != nil {
return orderBook, err
@@ -66,23 +66,23 @@ func (a *Alphapoint) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBa
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data.Quantity, Price: data.Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Quantity, Price: data.Price})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Quantity, Price: data.Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Quantity, Price: data.Price})
}
orderbook.ProcessOrderbook(a.GetName(), p, orderBook)
return orderbook.GetOrderbook(a.Name, p)
orderbook.ProcessOrderbook(a.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(a.Name, p, assetType)
}
// GetOrderbookEx returns the orderbook for a currency pair
func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p)
func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p, assetType)
if err == nil {
return a.UpdateOrderbook(p)
return a.UpdateOrderbook(p, assetType)
}
return ob, nil
}

View File

@@ -100,17 +100,17 @@ func (a *ANX) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pric
}
// GetOrderbookEx returns the orderbook for a currency pair
func (a *ANX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p)
func (a *ANX) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(a.GetName(), p, assetType)
if err == nil {
return a.UpdateOrderbook(p)
return a.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (a *ANX) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (a *ANX) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
return orderBook, nil
}

View File

@@ -67,32 +67,32 @@ func (b *Bitfinex) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker
}
// GetOrderbookEx returns the orderbook for a currency pair
func (b *Bitfinex) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p)
func (b *Bitfinex) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
if err == nil {
return b.UpdateOrderbook(p)
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bitfinex) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (b *Bitfinex) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetOrderbook(p.Pair().String(), nil)
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: orderbookNew.Asks[x].Price, Amount: orderbookNew.Asks[x].Amount})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Price: orderbookNew.Asks[x].Price, Amount: orderbookNew.Asks[x].Amount})
}
for x := range orderbookNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: orderbookNew.Bids[x].Price, Amount: orderbookNew.Bids[x].Amount})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Price: orderbookNew.Bids[x].Price, Amount: orderbookNew.Bids[x].Amount})
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook)
return orderbook.GetOrderbook(b.Name, p)
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies on the

View File

@@ -19,7 +19,8 @@ func TestRun(t *testing.T) {
func TestGetTickerPrice(t *testing.T) {
getTickerPrice := Bitfinex{}
_, err := getTickerPrice.GetTickerPrice(pair.NewCurrencyPair("BTC", "USD"), ticker.Spot)
_, err := getTickerPrice.GetTickerPrice(pair.NewCurrencyPair("BTC", "USD"),
ticker.Spot)
if err != nil {
t.Errorf("Test Failed - Bitfinex GetTickerPrice() error: %s", err)
}
@@ -27,7 +28,8 @@ func TestGetTickerPrice(t *testing.T) {
func TestGetOrderbookEx(t *testing.T) {
getOrderBookEx := Bitfinex{}
_, err := getOrderBookEx.GetOrderbookEx(pair.NewCurrencyPair("BTC", "USD"))
_, err := getOrderBookEx.GetOrderbookEx(pair.NewCurrencyPair("BTC", "USD"),
ticker.Spot)
if err != nil {
t.Errorf("Test Failed - Bitfinex GetOrderbookEx() error: %s", err)
}

View File

@@ -57,17 +57,17 @@ func (b *Bitstamp) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker
}
// GetOrderbookEx returns the orderbook for a currency pair
func (b *Bitstamp) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p)
func (b *Bitstamp) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
if err == nil {
return b.UpdateOrderbook(p)
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bitstamp) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (b *Bitstamp) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetOrderbook(p.Pair().String())
if err != nil {
return orderBook, err
@@ -75,16 +75,16 @@ func (b *Bitstamp) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Amount, Price: data.Price})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Amount, Price: data.Price})
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook)
return orderbook.GetOrderbook(b.Name, p)
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -100,17 +100,17 @@ func (b *Bittrex) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.
}
// GetOrderbookEx returns the orderbook for a currency pair
func (b *Bittrex) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p)
func (b *Bittrex) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
if err == nil {
return b.UpdateOrderbook(p)
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *Bittrex) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (b *Bittrex) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetOrderbook(exchange.FormatExchangeCurrency(b.GetName(), p).String())
if err != nil {
return orderBook, err
@@ -118,7 +118,7 @@ func (b *Bittrex) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase,
for x := range orderbookNew.Buy {
orderBook.Bids = append(orderBook.Bids,
orderbook.OrderbookItem{
orderbook.Item{
Amount: orderbookNew.Buy[x].Quantity,
Price: orderbookNew.Buy[x].Rate,
},
@@ -127,13 +127,13 @@ func (b *Bittrex) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase,
for x := range orderbookNew.Sell {
orderBook.Asks = append(orderBook.Asks,
orderbook.OrderbookItem{
orderbook.Item{
Amount: orderbookNew.Sell[x].Quantity,
Price: orderbookNew.Sell[x].Rate,
},
)
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook)
return orderbook.GetOrderbook(b.Name, p)
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
}

View File

@@ -56,17 +56,17 @@ func (b *BTCC) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pri
}
// GetOrderbookEx returns the orderbook for a currency pair
func (b *BTCC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p)
func (b *BTCC) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
if err == nil {
return b.UpdateOrderbook(p)
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *BTCC) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (b *BTCC) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetOrderBook(exchange.FormatExchangeCurrency(b.GetName(), p).String(), 100)
if err != nil {
return orderBook, err
@@ -74,16 +74,16 @@ func (b *BTCC) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, er
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Price: data[0], Amount: data[1]})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Price: data[0], Amount: data[1]})
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook)
return orderbook.GetOrderbook(b.Name, p)
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
}
// GetExchangeAccountInfo : Retrieves balances for all enabled currencies for

View File

@@ -85,17 +85,17 @@ func (b *BTCE) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pri
}
// GetOrderbookEx returns the orderbook for a currency pair
func (b *BTCE) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p)
func (b *BTCE) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
if err == nil {
return b.UpdateOrderbook(p)
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *BTCE) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (b *BTCE) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetDepth(exchange.FormatExchangeCurrency(b.Name, p).String())
if err != nil {
return orderBook, err
@@ -103,16 +103,16 @@ func (b *BTCE) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, er
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Price: data[0], Amount: data[1]})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Price: data[0], Amount: data[1]})
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook)
return orderbook.GetOrderbook(b.Name, p)
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -75,17 +75,17 @@ func (b *BTCMarkets) GetTickerPrice(p pair.CurrencyPair, assetType string) (tick
}
// GetOrderbookEx returns orderbook base on the currency pair
func (b *BTCMarkets) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p)
func (b *BTCMarkets) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(b.GetName(), p, assetType)
if err == nil {
return b.UpdateOrderbook(p)
return b.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (b *BTCMarkets) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (b *BTCMarkets) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := b.GetOrderbook(p.GetFirstCurrency().String())
if err != nil {
return orderBook, err
@@ -93,16 +93,16 @@ func (b *BTCMarkets) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBa
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
}
orderbook.ProcessOrderbook(b.GetName(), p, orderBook)
return orderbook.GetOrderbook(b.Name, p)
orderbook.ProcessOrderbook(b.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(b.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -96,30 +96,30 @@ func (c *COINUT) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.P
}
// GetOrderbookEx returns orderbook base on the currency pair
func (c *COINUT) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(c.GetName(), p)
func (c *COINUT) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(c.GetName(), p, assetType)
if err == nil {
return c.UpdateOrderbook(p)
return c.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (c *COINUT) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (c *COINUT) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := c.GetInstrumentOrderbook(c.InstrumentMap[p.Pair().String()], 200)
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Buy {
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: orderbookNew.Buy[x].Quantity, Price: orderbookNew.Buy[x].Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: orderbookNew.Buy[x].Quantity, Price: orderbookNew.Buy[x].Price})
}
for x := range orderbookNew.Sell {
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Sell[x].Quantity, Price: orderbookNew.Sell[x].Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: orderbookNew.Sell[x].Quantity, Price: orderbookNew.Sell[x].Price})
}
orderbook.ProcessOrderbook(c.GetName(), p, orderBook)
return orderbook.GetOrderbook(c.Name, p)
orderbook.ProcessOrderbook(c.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(c.Name, p, assetType)
}

View File

@@ -66,8 +66,8 @@ type IBotExchange interface {
IsEnabled() bool
GetTickerPrice(currency pair.CurrencyPair, assetType string) (ticker.Price, error)
UpdateTicker(currency pair.CurrencyPair, assetType string) (ticker.Price, error)
GetOrderbookEx(currency pair.CurrencyPair) (orderbook.OrderbookBase, error)
UpdateOrderbook(currency pair.CurrencyPair) (orderbook.OrderbookBase, error)
GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error)
UpdateOrderbook(currency pair.CurrencyPair, assetType string) (orderbook.Base, error)
GetEnabledCurrencies() []pair.CurrencyPair
GetExchangeAccountInfo() (AccountInfo, error)
GetAuthenticatedAPISupport() bool

View File

@@ -3,10 +3,157 @@ package exchange
import (
"testing"
"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency/pair"
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
)
func TestSetAssetTypes(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
if err != nil {
t.Fatalf("Test failed. TestSetAssetTypes failed to load config file. Error: %s", err)
}
b := Base{
Name: "TESTNAME",
}
err = b.SetAssetTypes()
if err == nil {
t.Fatal("Test failed. TestSetAssetTypes returned nil error for a non-existant exchange")
}
b.Name = "ANX"
err = b.SetAssetTypes()
if err != nil {
t.Fatalf("Test failed. TestSetAssetTypes. Error %s", err)
}
exch, err := cfg.GetExchangeConfig(b.Name)
if err != nil {
t.Fatalf("Test failed. TestSetAssetTypes load config failed. Error %s", err)
}
exch.AssetTypes = ""
err = cfg.UpdateExchangeConfig(exch)
if err != nil {
t.Fatalf("Test failed. TestSetAssetTypes update config failed. Error %s", err)
}
exch, err = cfg.GetExchangeConfig(b.Name)
if err != nil {
t.Fatalf("Test failed. TestSetAssetTypes load config failed. Error %s", err)
}
if exch.AssetTypes != "" {
t.Fatal("Test failed. TestSetAssetTypes assetTypes != ''")
}
err = b.SetAssetTypes()
if err != nil {
t.Fatalf("Test failed. TestSetAssetTypes. Error %s", err)
}
if !common.DataContains(b.AssetTypes, ticker.Spot) {
t.Fatal("Test failed. TestSetAssetTypes assetTypes is not set")
}
}
func TestGetExchangeAssetTypes(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
if err != nil {
t.Fatalf("Failed to load config file. Error: %s", err)
}
result, err := GetExchangeAssetTypes("Bitfinex")
if err != nil {
t.Fatal("Test failed. Unable to obtain Bitfinex asset types")
}
if !common.DataContains(result, ticker.Spot) {
t.Fatal("Test failed. Bitfinex does not contain default asset type 'SPOT'")
}
_, err = GetExchangeAssetTypes("non-existant-exchange")
if err == nil {
t.Fatal("Test failed. Got asset types for non-existant exchange")
}
}
func TestSetCurrencyPairFormat(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat failed to load config file. Error: %s", err)
}
b := Base{
Name: "TESTNAME",
}
err = b.SetCurrencyPairFormat()
if err == nil {
t.Fatal("Test failed. TestSetCurrencyPairFormat returned nil error for a non-existant exchange")
}
b.Name = "ANX"
err = b.SetCurrencyPairFormat()
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat. Error %s", err)
}
exch, err := cfg.GetExchangeConfig(b.Name)
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat load config failed. Error %s", err)
}
exch.ConfigCurrencyPairFormat = nil
exch.RequestCurrencyPairFormat = nil
err = cfg.UpdateExchangeConfig(exch)
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat update config failed. Error %s", err)
}
exch, err = cfg.GetExchangeConfig(b.Name)
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat load config failed. Error %s", err)
}
if exch.ConfigCurrencyPairFormat != nil && exch.RequestCurrencyPairFormat != nil {
t.Fatal("Test failed. TestSetCurrencyPairFormat exch values are not nil")
}
err = b.SetCurrencyPairFormat()
if err != nil {
t.Fatalf("Test failed. TestSetCurrencyPairFormat. Error %s", err)
}
if b.ConfigCurrencyPairFormat.Delimiter != "" &&
b.ConfigCurrencyPairFormat.Index != "BTC" &&
b.ConfigCurrencyPairFormat.Uppercase {
t.Fatal("Test failed. TestSetCurrencyPairFormat ConfigCurrencyPairFormat values are incorrect")
}
if b.RequestCurrencyPairFormat.Delimiter != "" &&
b.RequestCurrencyPairFormat.Index != "BTC" &&
b.RequestCurrencyPairFormat.Uppercase {
t.Fatal("Test failed. TestSetCurrencyPairFormat RequestCurrencyPairFormat values are incorrect")
}
}
func TestGetAuthenticatedAPISupport(t *testing.T) {
base := Base{
AuthenticatedAPISupport: false,
}
if base.GetAuthenticatedAPISupport() {
t.Fatal("Test failed. TestGetAuthenticatedAPISupport returned true when it should of been false.")
}
}
func TestGetName(t *testing.T) {
GetName := Base{
Name: "TESTNAME",
@@ -18,51 +165,132 @@ func TestGetName(t *testing.T) {
}
}
func TestSetCurrencyPairFormat(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
if err != nil {
t.Fatalf("Failed to load config file. Error: %s", err)
}
exch, err := cfg.GetExchangeConfig("GDAX")
if err != nil {
t.Fatalf("Failed to load GDAX exchange config. Error: %s", err)
}
exch.RequestCurrencyPairFormat = nil
exch.ConfigCurrencyPairFormat = nil
err = cfg.UpdateExchangeConfig(exch)
if err != nil {
t.Fatalf("Failed to update GDAX config. Error: %s", err)
}
// to-do
}
func TestGetEnabledCurrencies(t *testing.T) {
enabledPairs := []string{"BTCUSD", "BTCAUD", "LTCUSD", "LTCAUD"}
GetEnabledCurrencies := Base{
Name: "TESTNAME",
EnabledPairs: enabledPairs,
b := Base{
Name: "TESTNAME",
}
enCurr := GetEnabledCurrencies.GetEnabledCurrencies()
if enCurr[0].Pair().String() != "BTCUSD" {
t.Error("Test Failed - Exchange GetEnabledCurrencies() incorrect string")
b.EnabledPairs = []string{"BTC-USD"}
format := config.CurrencyPairFormatConfig{
Delimiter: "-",
Index: "",
}
b.RequestCurrencyPairFormat = format
b.ConfigCurrencyPairFormat = format
c := b.GetEnabledCurrencies()
if c[0].Pair().String() != "BTC-USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
format.Delimiter = "~"
b.RequestCurrencyPairFormat = format
c = b.GetEnabledCurrencies()
if c[0].Pair().String() != "BTC-USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
format.Delimiter = ""
b.ConfigCurrencyPairFormat = format
c = b.GetEnabledCurrencies()
if c[0].Pair().String() != "BTC-USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.EnabledPairs = []string{"BTCDOGE"}
format.Index = "BTC"
b.ConfigCurrencyPairFormat = format
c = b.GetEnabledCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "DOGE" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.EnabledPairs = []string{"BTC_USD"}
b.RequestCurrencyPairFormat.Delimiter = ""
b.ConfigCurrencyPairFormat.Delimiter = "_"
c = b.GetEnabledCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.EnabledPairs = []string{"BTCDOGE"}
b.RequestCurrencyPairFormat.Delimiter = ""
b.ConfigCurrencyPairFormat.Delimiter = ""
b.ConfigCurrencyPairFormat.Index = "BTC"
c = b.GetEnabledCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "DOGE" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.EnabledPairs = []string{"BTCUSD"}
b.ConfigCurrencyPairFormat.Index = ""
c = b.GetEnabledCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
}
func TestGetAvailableCurrencies(t *testing.T) {
availablePairs := []string{"BTCUSD", "BTCAUD", "LTCUSD", "LTCAUD"}
GetEnabledCurrencies := Base{
Name: "TESTNAME",
AvailablePairs: availablePairs,
b := Base{
Name: "TESTNAME",
}
enCurr := GetEnabledCurrencies.GetAvailableCurrencies()
if enCurr[0].Pair().String() != "BTCUSD" {
b.AvailablePairs = []string{"BTC-USD"}
format := config.CurrencyPairFormatConfig{
Delimiter: "-",
Index: "",
}
b.RequestCurrencyPairFormat = format
b.ConfigCurrencyPairFormat = format
c := b.GetAvailableCurrencies()
if c[0].Pair().String() != "BTC-USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
format.Delimiter = "~"
b.RequestCurrencyPairFormat = format
c = b.GetAvailableCurrencies()
if c[0].Pair().String() != "BTC-USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
format.Delimiter = ""
b.ConfigCurrencyPairFormat = format
c = b.GetAvailableCurrencies()
if c[0].Pair().String() != "BTC-USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.AvailablePairs = []string{"BTCDOGE"}
format.Index = "BTC"
b.ConfigCurrencyPairFormat = format
c = b.GetAvailableCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "DOGE" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.AvailablePairs = []string{"BTC_USD"}
b.RequestCurrencyPairFormat.Delimiter = ""
b.ConfigCurrencyPairFormat.Delimiter = "_"
c = b.GetAvailableCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.AvailablePairs = []string{"BTCDOGE"}
b.RequestCurrencyPairFormat.Delimiter = ""
b.ConfigCurrencyPairFormat.Delimiter = ""
b.ConfigCurrencyPairFormat.Index = "BTC"
c = b.GetAvailableCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "DOGE" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
b.AvailablePairs = []string{"BTCUSD"}
b.ConfigCurrencyPairFormat.Index = ""
c = b.GetAvailableCurrencies()
if c[0].FirstCurrency.String() != "BTC" && c[0].SecondCurrency.String() != "USD" {
t.Error("Test Failed - Exchange GetAvailableCurrencies() incorrect string")
}
}
@@ -89,6 +317,14 @@ func TestGetExchangeFormatCurrencySeperator(t *testing.T) {
t.Errorf("Test failed - TestGetExchangeFormatCurrencySeperator expected %v != actual %v",
expected, actual)
}
expected = false
actual = GetExchangeFormatCurrencySeperator("blah")
if expected != actual {
t.Errorf("Test failed - TestGetExchangeFormatCurrencySeperator expected %v != actual %v",
expected, actual)
}
}
func TestGetAndFormatExchangeCurrencies(t *testing.T) {
@@ -112,6 +348,11 @@ func TestGetAndFormatExchangeCurrencies(t *testing.T) {
t.Errorf("Test failed - Exchange TestGetAndFormatExchangeCurrencies %s != %s",
actual, expected)
}
_, err = GetAndFormatExchangeCurrencies("non-existant", pairs)
if err == nil {
t.Errorf("Test failed - Exchange TestGetAndFormatExchangeCurrencies returned nil error on non-existant exchange")
}
}
func TestFormatExchangeCurrency(t *testing.T) {
@@ -193,33 +434,77 @@ func TestSetAPIKeys(t *testing.T) {
func TestUpdateEnabledCurrencies(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
UAC := Base{Name: "ANX"}
enabledCurrencies := []string{"ltc", "btc", "usd", "aud"}
if err != nil {
t.Error(
"Test Failed - Exchange UpdateEnabledCurrencies() did not set correct values",
)
t.Fatal("Test failed. TestUpdateEnabledCurrencies failed to load config")
}
err2 := UAC.UpdateEnabledCurrencies(enabledCurrencies, false)
if err2 != nil {
t.Errorf("Test Failed - Exchange UpdateEnabledCurrencies() error: %s", err2)
UAC := Base{Name: "ANX"}
exchangeProducts := []string{"ltc", "btc", "usd", "aud"}
// Test updating exchange products for an exchange which doesn't exist
UAC.Name = "Blah"
err = UAC.UpdateEnabledCurrencies(exchangeProducts, false)
if err == nil {
t.Errorf("Test Failed - Exchange TestUpdateEnabledCurrencies succeeded on an exchange which doesn't exist")
}
// Test updating exchange products
UAC.Name = "ANX"
err = UAC.UpdateEnabledCurrencies(exchangeProducts, false)
if err != nil {
t.Errorf("Test Failed - Exchange TestUpdateEnabledCurrencies error: %s", err)
}
// Test updating the same new products, diff should be 0
UAC.Name = "ANX"
err = UAC.UpdateEnabledCurrencies(exchangeProducts, false)
if err != nil {
t.Errorf("Test Failed - Exchange TestUpdateEnabledCurrencies error: %s", err)
}
// Test force updating to only one product
exchangeProducts = []string{"btc"}
err = UAC.UpdateEnabledCurrencies(exchangeProducts, true)
if err != nil {
t.Errorf("Test Failed - Forced Exchange TestUpdateEnabledCurrencies error: %s", err)
}
}
func TestUpdateAvailableCurrencies(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig(config.ConfigTestFile)
if err != nil {
t.Fatal("Test failed. TestUpdateAvailableCurrencies failed to load config")
}
UAC := Base{Name: "ANX"}
exchangeProducts := []string{"ltc", "btc", "usd", "aud"}
if err != nil {
t.Error(
"Test Failed - Exchange UpdateAvailableCurrencies() did not set correct values",
)
// Test updating exchange products for an exchange which doesn't exist
UAC.Name = "Blah"
err = UAC.UpdateAvailableCurrencies(exchangeProducts, false)
if err == nil {
t.Errorf("Test Failed - Exchange UpdateAvailableCurrencies() succeeded on an exchange which doesn't exist")
}
err2 := UAC.UpdateAvailableCurrencies(exchangeProducts, false)
if err2 != nil {
t.Errorf("Test Failed - Exchange UpdateAvailableCurrencies() error: %s", err2)
// Test updating exchange products
UAC.Name = "ANX"
err = UAC.UpdateAvailableCurrencies(exchangeProducts, false)
if err != nil {
t.Errorf("Test Failed - Exchange UpdateAvailableCurrencies() error: %s", err)
}
// Test updating the same new products, diff should be 0
UAC.Name = "ANX"
err = UAC.UpdateAvailableCurrencies(exchangeProducts, false)
if err != nil {
t.Errorf("Test Failed - Exchange UpdateAvailableCurrencies() error: %s", err)
}
// Test force updating to only one product
exchangeProducts = []string{"btc"}
err = UAC.UpdateAvailableCurrencies(exchangeProducts, true)
if err != nil {
t.Errorf("Test Failed - Forced Exchange UpdateAvailableCurrencies() error: %s", err)
}
}

View File

@@ -97,17 +97,17 @@ func (g *GDAX) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pri
}
// GetOrderbookEx returns orderbook base on the currency pair
func (g *GDAX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(g.GetName(), p)
func (g *GDAX) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(g.GetName(), p, assetType)
if err == nil {
return g.UpdateOrderbook(p)
return g.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (g *GDAX) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (g *GDAX) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := g.GetOrderbook(exchange.FormatExchangeCurrency(g.Name, p).String(), 2)
if err != nil {
return orderBook, err
@@ -116,13 +116,13 @@ func (g *GDAX) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, er
obNew := orderbookNew.(GDAXOrderbookL1L2)
for x := range obNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
}
for x := range obNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price})
}
orderbook.ProcessOrderbook(g.GetName(), p, orderBook)
return orderbook.GetOrderbook(g.Name, p)
orderbook.ProcessOrderbook(g.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(g.Name, p, assetType)
}

View File

@@ -78,30 +78,30 @@ func (g *Gemini) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.P
}
// GetOrderbookEx returns orderbook base on the currency pair
func (g *Gemini) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(g.GetName(), p)
func (g *Gemini) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(g.GetName(), p, assetType)
if err == nil {
return g.UpdateOrderbook(p)
return g.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (g *Gemini) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (g *Gemini) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := g.GetOrderbook(p.Pair().String(), url.Values{})
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
}
for x := range orderbookNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
}
orderbook.ProcessOrderbook(g.GetName(), p, orderBook)
return orderbook.GetOrderbook(g.Name, p)
orderbook.ProcessOrderbook(g.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(g.Name, p, assetType)
}

View File

@@ -56,17 +56,17 @@ func (h *HUOBI) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pr
}
// GetOrderbookEx returns orderbook base on the currency pair
func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(h.GetName(), p)
func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(h.GetName(), p, assetType)
if err == nil {
return h.UpdateOrderbook(p)
return h.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (h *HUOBI) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (h *HUOBI) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := h.GetOrderBook(p.GetFirstCurrency().Lower().String())
if err != nil {
return orderBook, err
@@ -74,16 +74,16 @@ func (h *HUOBI) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, e
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
}
orderbook.ProcessOrderbook(h.GetName(), p, orderBook)
return orderbook.GetOrderbook(h.Name, p)
orderbook.ProcessOrderbook(h.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(h.Name, p, assetType)
}
//GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -53,17 +53,17 @@ func (i *ItBit) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pr
}
// GetOrderbookEx returns orderbook base on the currency pair
func (i *ItBit) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(i.GetName(), p)
func (i *ItBit) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(i.GetName(), p, assetType)
if err == nil {
return i.UpdateOrderbook(p)
return i.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (i *ItBit) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (i *ItBit) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := i.GetOrderbook(exchange.FormatExchangeCurrency(i.Name,
p).String())
if err != nil {
@@ -80,7 +80,7 @@ func (i *ItBit) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, e
if err != nil {
log.Println(err)
}
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: amount, Price: price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: amount, Price: price})
}
for x := range orderbookNew.Asks {
@@ -93,11 +93,11 @@ func (i *ItBit) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, e
if err != nil {
log.Println(err)
}
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: amount, Price: price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: amount, Price: price})
}
orderbook.ProcessOrderbook(i.GetName(), p, orderBook)
return orderbook.GetOrderbook(i.Name, p)
orderbook.ProcessOrderbook(i.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(i.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -78,32 +78,32 @@ func (k *Kraken) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.P
}
// GetOrderbookEx returns orderbook base on the currency pair
func (k *Kraken) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(k.GetName(), p)
func (k *Kraken) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(k.GetName(), p, assetType)
if err == nil {
return k.UpdateOrderbook(p)
return k.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (k *Kraken) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (k *Kraken) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := k.GetDepth(exchange.FormatExchangeCurrency(k.GetName(), p).String())
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
}
for x := range orderbookNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
}
orderbook.ProcessOrderbook(k.GetName(), p, orderBook)
return orderbook.GetOrderbook(k.Name, p)
orderbook.ProcessOrderbook(k.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(k.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -56,32 +56,32 @@ func (l *LakeBTC) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.
}
// GetOrderbookEx returns orderbook base on the currency pair
func (l *LakeBTC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(l.GetName(), p)
func (l *LakeBTC) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(l.GetName(), p, assetType)
if err == nil {
return l.UpdateOrderbook(p)
return l.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (l *LakeBTC) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (l *LakeBTC) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := l.GetOrderBook(p.Pair().String())
if err != nil {
return orderBook, err
}
for x := range orderbookNew.Bids {
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: orderbookNew.Bids[x].Amount, Price: orderbookNew.Bids[x].Price})
}
for x := range orderbookNew.Asks {
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price})
}
orderbook.ProcessOrderbook(l.GetName(), p, orderBook)
return orderbook.GetOrderbook(l.Name, p)
orderbook.ProcessOrderbook(l.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(l.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -75,17 +75,17 @@ func (l *Liqui) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.Pr
}
// GetOrderbookEx returns orderbook base on the currency pair
func (l *Liqui) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(l.Name, p)
func (l *Liqui) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(l.Name, p, assetType)
if err == nil {
return l.UpdateOrderbook(p)
return l.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (l *Liqui) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (l *Liqui) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := l.GetDepth(exchange.FormatExchangeCurrency(l.Name, p).String())
if err != nil {
return orderBook, err
@@ -93,16 +93,16 @@ func (l *Liqui) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, e
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
}
orderbook.ProcessOrderbook(l.Name, p, orderBook)
return orderbook.GetOrderbook(l.Name, p)
orderbook.ProcessOrderbook(l.Name, p, orderBook, assetType)
return orderbook.GetOrderbook(l.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -52,17 +52,17 @@ func (l *LocalBitcoins) GetTickerPrice(p pair.CurrencyPair, assetType string) (t
}
// GetOrderbookEx returns orderbook base on the currency pair
func (l *LocalBitcoins) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(l.GetName(), p)
func (l *LocalBitcoins) GetOrderbookEx(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(l.GetName(), p, assetType)
if err == nil {
return l.UpdateOrderbook(p)
return l.UpdateOrderbook(p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (l *LocalBitcoins) UpdateOrderbook(p pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (l *LocalBitcoins) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := l.GetOrderbook(p.GetSecondCurrency().String())
if err != nil {
return orderBook, err
@@ -70,16 +70,16 @@ func (l *LocalBitcoins) UpdateOrderbook(p pair.CurrencyPair) (orderbook.Orderboo
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Amount, Price: data.Price})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Amount, Price: data.Price})
}
orderbook.ProcessOrderbook(l.GetName(), p, orderBook)
return orderbook.GetOrderbook(l.Name, p)
orderbook.ProcessOrderbook(l.GetName(), p, orderBook, assetType)
return orderbook.GetOrderbook(l.Name, p, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -74,17 +74,17 @@ func (o *OKCoin) GetTickerPrice(p pair.CurrencyPair, assetType string) (ticker.P
}
// GetOrderbookEx returns orderbook base on the currency pair
func (o *OKCoin) GetOrderbookEx(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(o.GetName(), currency)
func (o *OKCoin) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(o.GetName(), currency, assetType)
if err == nil {
return o.UpdateOrderbook(currency)
return o.UpdateOrderbook(currency, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (o *OKCoin) UpdateOrderbook(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (o *OKCoin) UpdateOrderbook(currency pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := o.GetOrderBook(exchange.FormatExchangeCurrency(o.Name, currency).String(), 200, false)
if err != nil {
return orderBook, err
@@ -92,16 +92,16 @@ func (o *OKCoin) UpdateOrderbook(currency pair.CurrencyPair) (orderbook.Orderboo
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data[1], Price: data[0]})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data[1], Price: data[0]})
}
orderbook.ProcessOrderbook(o.GetName(), currency, orderBook)
return orderbook.GetOrderbook(o.Name, currency)
orderbook.ProcessOrderbook(o.GetName(), currency, orderBook, assetType)
return orderbook.GetOrderbook(o.Name, currency, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -7,33 +7,44 @@ import (
"github.com/thrasher-/gocryptotrader/currency/pair"
)
var (
// Const values for orderbook package
const (
ErrOrderbookForExchangeNotFound = "Ticker for exchange does not exist."
ErrPrimaryCurrencyNotFound = "Error primary currency for orderbook not found."
ErrSecondaryCurrencyNotFound = "Error secondary currency for orderbook not found."
Spot = "SPOT"
)
// Vars for the orderbook package
var (
Orderbooks []Orderbook
)
type OrderbookItem struct {
// Item stores the amount and price values
type Item struct {
Amount float64
Price float64
}
type OrderbookBase struct {
// Base holds the fields for the orderbook base
type Base struct {
Pair pair.CurrencyPair `json:"pair"`
CurrencyPair string `json:"CurrencyPair"`
Bids []OrderbookItem `json:"bids"`
Asks []OrderbookItem `json:"asks"`
Bids []Item `json:"bids"`
Asks []Item `json:"asks"`
LastUpdated time.Time `json:"last_updated"`
}
// Orderbook holds the orderbook information for a currency pair and type
type Orderbook struct {
Orderbook map[pair.CurrencyItem]map[pair.CurrencyItem]OrderbookBase
Orderbook map[pair.CurrencyItem]map[pair.CurrencyItem]map[string]Base
ExchangeName string
}
func (o *OrderbookBase) CalculateTotalBids() (float64, float64) {
// CalculateTotalBids returns the total amount of bids and the total orderbook
// bids value
func (o *Base) CalculateTotalBids() (float64, float64) {
amountCollated := float64(0)
total := float64(0)
for _, x := range o.Bids {
@@ -43,7 +54,9 @@ func (o *OrderbookBase) CalculateTotalBids() (float64, float64) {
return amountCollated, total
}
func (o *OrderbookBase) CalculateTotalAsks() (float64, float64) {
// CalculateTotalAsks returns the total amount of asks and the total orderbook
// asks value
func (o *Base) CalculateTotalAsks() (float64, float64) {
amountCollated := float64(0)
total := float64(0)
for _, x := range o.Asks {
@@ -53,29 +66,33 @@ func (o *OrderbookBase) CalculateTotalAsks() (float64, float64) {
return amountCollated, total
}
func (o *OrderbookBase) Update(Bids, Asks []OrderbookItem) {
// Update updates the bids and asks
func (o *Base) Update(Bids, Asks []Item) {
o.Bids = Bids
o.Asks = Asks
o.LastUpdated = time.Now()
}
func GetOrderbook(exchange string, p pair.CurrencyPair) (OrderbookBase, error) {
// GetOrderbook checks and returns the orderbook given an exchange name and
// currency pair if it exists
func GetOrderbook(exchange string, p pair.CurrencyPair, orderbookType string) (Base, error) {
orderbook, err := GetOrderbookByExchange(exchange)
if err != nil {
return OrderbookBase{}, err
return Base{}, err
}
if !FirstCurrencyExists(exchange, p.GetFirstCurrency()) {
return OrderbookBase{}, errors.New(ErrPrimaryCurrencyNotFound)
return Base{}, errors.New(ErrPrimaryCurrencyNotFound)
}
if !SecondCurrencyExists(exchange, p) {
return OrderbookBase{}, errors.New(ErrSecondaryCurrencyNotFound)
return Base{}, errors.New(ErrSecondaryCurrencyNotFound)
}
return orderbook.Orderbook[p.GetFirstCurrency()][p.GetSecondCurrency()], nil
return orderbook.Orderbook[p.GetFirstCurrency()][p.GetSecondCurrency()][orderbookType], nil
}
// GetOrderbookByExchange returns an exchange orderbook
func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
for _, y := range Orderbooks {
if y.ExchangeName == exchange {
@@ -85,6 +102,8 @@ func GetOrderbookByExchange(exchange string) (*Orderbook, error) {
return nil, errors.New(ErrOrderbookForExchangeNotFound)
}
// FirstCurrencyExists checks to see if the first currency of the orderbook map
// exists
func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
for _, y := range Orderbooks {
if y.ExchangeName == exchange {
@@ -96,6 +115,8 @@ func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool {
return false
}
// SecondCurrencyExists checks to see if the second currency of the orderbook
// map exists
func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
for _, y := range Orderbooks {
if y.ExchangeName == exchange {
@@ -109,40 +130,51 @@ func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool {
return false
}
func CreateNewOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew OrderbookBase) Orderbook {
// CreateNewOrderbook creates a new orderbook
func CreateNewOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew Base, orderbookType string) Orderbook {
orderbook := Orderbook{}
orderbook.ExchangeName = exchangeName
orderbook.Orderbook = make(map[pair.CurrencyItem]map[pair.CurrencyItem]OrderbookBase)
sMap := make(map[pair.CurrencyItem]OrderbookBase)
sMap[p.GetSecondCurrency()] = orderbookNew
orderbook.Orderbook[p.GetFirstCurrency()] = sMap
orderbook.Orderbook = make(map[pair.CurrencyItem]map[pair.CurrencyItem]map[string]Base)
a := make(map[pair.CurrencyItem]map[string]Base)
b := make(map[string]Base)
b[orderbookType] = orderbookNew
a[p.SecondCurrency] = b
orderbook.Orderbook[p.FirstCurrency] = a
Orderbooks = append(Orderbooks, orderbook)
return orderbook
}
func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew OrderbookBase) {
// ProcessOrderbook processes incoming orderbooks, creating or updating the
// Orderbook list
func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew Base, orderbookType string) {
orderbookNew.CurrencyPair = p.Pair().String()
orderbookNew.LastUpdated = time.Now()
if len(Orderbooks) == 0 {
CreateNewOrderbook(exchangeName, p, orderbookNew)
CreateNewOrderbook(exchangeName, p, orderbookNew, orderbookType)
return
} else {
orderbook, err := GetOrderbookByExchange(exchangeName)
if err != nil {
CreateNewOrderbook(exchangeName, p, orderbookNew)
}
orderbook, err := GetOrderbookByExchange(exchangeName)
if err != nil {
CreateNewOrderbook(exchangeName, p, orderbookNew, orderbookType)
return
}
if FirstCurrencyExists(exchangeName, p.GetFirstCurrency()) {
if !SecondCurrencyExists(exchangeName, p) {
a := orderbook.Orderbook[p.FirstCurrency]
b := make(map[string]Base)
b[orderbookType] = orderbookNew
a[p.SecondCurrency] = b
orderbook.Orderbook[p.FirstCurrency] = a
return
}
if FirstCurrencyExists(exchangeName, p.GetFirstCurrency()) {
if !SecondCurrencyExists(exchangeName, p) {
second := orderbook.Orderbook[p.GetFirstCurrency()]
second[p.GetSecondCurrency()] = orderbookNew
orderbook.Orderbook[p.GetFirstCurrency()] = second
return
}
}
sMap := make(map[pair.CurrencyItem]OrderbookBase)
sMap[p.GetSecondCurrency()] = orderbookNew
orderbook.Orderbook[p.GetFirstCurrency()] = sMap
}
a := make(map[pair.CurrencyItem]map[string]Base)
b := make(map[string]Base)
b[orderbookType] = orderbookNew
a[p.SecondCurrency] = b
orderbook.Orderbook[p.FirstCurrency] = a
}

View File

@@ -0,0 +1,266 @@
package orderbook
import (
"testing"
"time"
"github.com/thrasher-/gocryptotrader/currency/pair"
)
func TestCalculateTotalBids(t *testing.T) {
t.Parallel()
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Bids: []Item{Item{Price: 100, Amount: 10}},
LastUpdated: time.Now(),
}
a, b := base.CalculateTotalBids()
if a != 10 && b != 1000 {
t.Fatal("Test failed. TestCalculateTotalBids expected a = 10 and b = 1000")
}
}
func TestCalculateTotaAsks(t *testing.T) {
t.Parallel()
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
LastUpdated: time.Now(),
}
a, b := base.CalculateTotalAsks()
if a != 10 && b != 1000 {
t.Fatal("Test failed. TestCalculateTotalAsks expected a = 10 and b = 1000")
}
}
func TestUpdate(t *testing.T) {
t.Parallel()
currency := pair.NewCurrencyPair("BTC", "USD")
timeNow := time.Now()
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
LastUpdated: timeNow,
}
asks := []Item{Item{Price: 200, Amount: 101}}
bids := []Item{Item{Price: 201, Amount: 100}}
time.Sleep(time.Millisecond * 50)
base.Update(bids, asks)
if !base.LastUpdated.After(timeNow) {
t.Fatal("test failed. TestUpdate expected LastUpdated to be greater then original time")
}
a, b := base.CalculateTotalAsks()
if a != 100 && b != 20200 {
t.Fatal("Test failed. TestUpdate expected a = 100 and b = 20100")
}
a, b = base.CalculateTotalBids()
if a != 100 && b != 20100 {
t.Fatal("Test failed. TestUpdate expected a = 100 and b = 20100")
}
}
func TestGetOrderbook(t *testing.T) {
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
}
CreateNewOrderbook("Exchange", currency, base, Spot)
result, err := GetOrderbook("Exchange", currency, Spot)
if err != nil {
t.Fatalf("Test failed. TestGetOrderbook failed to get orderbook. Error %s",
err)
}
if result.Pair.Pair() != currency.Pair() {
t.Fatal("Test failed. TestGetOrderbook failed. Mismatched pairs")
}
_, err = GetOrderbook("nonexistant", currency, Spot)
if err == nil {
t.Fatal("Test failed. TestGetOrderbook retrieved non-existant orderbook")
}
currency.FirstCurrency = "blah"
_, err = GetOrderbook("Exchange", currency, Spot)
if err == nil {
t.Fatal("Test failed. TestGetOrderbook retrieved non-existant orderbook using invalid first currency")
}
newCurrency := pair.NewCurrencyPair("BTC", "AUD")
_, err = GetOrderbook("Exchange", newCurrency, Spot)
if err == nil {
t.Fatal("Test failed. TestGetOrderbook retrieved non-existant orderbook using invalid second currency")
}
}
func TestGetOrderbookByExchange(t *testing.T) {
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
}
CreateNewOrderbook("Exchange", currency, base, Spot)
_, err := GetOrderbookByExchange("Exchange")
if err != nil {
t.Fatalf("Test failed. TestGetOrderbookByExchange failed to get orderbook. Error %s",
err)
}
_, err = GetOrderbookByExchange("nonexistant")
if err == nil {
t.Fatal("Test failed. TestGetOrderbookByExchange retrieved non-existant orderbook")
}
}
func TestFirstCurrencyExists(t *testing.T) {
currency := pair.NewCurrencyPair("BTC", "AUD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
}
CreateNewOrderbook("Exchange", currency, base, Spot)
if !FirstCurrencyExists("Exchange", currency.FirstCurrency) {
t.Fatal("Test failed. TestFirstCurrencyExists expected first currency doesn't exist")
}
var item pair.CurrencyItem = "blah"
if FirstCurrencyExists("Exchange", item) {
t.Fatal("Test failed. TestFirstCurrencyExists unexpected first currency exists")
}
}
func TestSecondCurrencyExists(t *testing.T) {
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
}
CreateNewOrderbook("Exchange", currency, base, Spot)
if !SecondCurrencyExists("Exchange", currency) {
t.Fatal("Test failed. TestSecondCurrencyExists expected first currency doesn't exist")
}
currency.SecondCurrency = "blah"
if SecondCurrencyExists("Exchange", currency) {
t.Fatal("Test failed. TestSecondCurrencyExists unexpected first currency exists")
}
}
func TestCreateNewOrderbook(t *testing.T) {
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
}
CreateNewOrderbook("Exchange", currency, base, Spot)
result, err := GetOrderbook("Exchange", currency, Spot)
if err != nil {
t.Fatal("Test failed. TestCreateNewOrderbook failed to create new orderbook")
}
if result.Pair.Pair() != currency.Pair() {
t.Fatal("Test failed. TestCreateNewOrderbook result pair is incorrect")
}
a, b := result.CalculateTotalAsks()
if a != 10 && b != 1000 {
t.Fatal("Test failed. TestCreateNewOrderbook CalculateTotalAsks value is incorrect")
}
a, b = result.CalculateTotalBids()
if a != 10 && b != 2000 {
t.Fatal("Test failed. TestCreateNewOrderbook CalculateTotalBids value is incorrect")
}
}
func TestProcessOrderbook(t *testing.T) {
Orderbooks = []Orderbook{}
currency := pair.NewCurrencyPair("BTC", "USD")
base := Base{
Pair: currency,
CurrencyPair: currency.Pair().String(),
Asks: []Item{Item{Price: 100, Amount: 10}},
Bids: []Item{Item{Price: 200, Amount: 10}},
}
ProcessOrderbook("Exchange", currency, base, Spot)
result, err := GetOrderbook("Exchange", currency, Spot)
if err != nil {
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
}
if result.Pair.Pair() != currency.Pair() {
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
}
currency = pair.NewCurrencyPair("BTC", "GBP")
base.Pair = currency
ProcessOrderbook("Exchange", currency, base, Spot)
result, err = GetOrderbook("Exchange", currency, Spot)
if err != nil {
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
}
if result.Pair.Pair() != currency.Pair() {
t.Fatal("Test failed. TestProcessOrderbook result pair is incorrect")
}
base.Asks = []Item{Item{Price: 200, Amount: 200}}
ProcessOrderbook("Exchange", currency, base, "monthly")
result, err = GetOrderbook("Exchange", currency, "monthly")
if err != nil {
t.Fatal("Test failed. TestProcessOrderbook failed to retrieve new orderbook")
}
a, b := result.CalculateTotalAsks()
if a != 200 && b != 40000 {
t.Fatal("Test failed. TestProcessOrderbook CalculateTotalsAsks incorrect values")
}
base.Bids = []Item{Item{Price: 420, Amount: 200}}
ProcessOrderbook("Blah", currency, base, "quarterly")
result, err = GetOrderbook("Blah", currency, "quarterly")
if err != nil {
t.Fatal("Test failed. TestProcessOrderbook failed to create new orderbook")
}
if a != 200 && b != 84000 {
t.Fatal("Test failed. TestProcessOrderbook CalculateTotalsBids incorrect values")
}
}

View File

@@ -61,17 +61,17 @@ func (p *Poloniex) GetTickerPrice(currencyPair pair.CurrencyPair, assetType stri
}
// GetOrderbookEx returns orderbook base on the currency pair
func (p *Poloniex) GetOrderbookEx(currencyPair pair.CurrencyPair) (orderbook.OrderbookBase, error) {
ob, err := orderbook.GetOrderbook(p.GetName(), currencyPair)
func (p *Poloniex) GetOrderbookEx(currencyPair pair.CurrencyPair, assetType string) (orderbook.Base, error) {
ob, err := orderbook.GetOrderbook(p.GetName(), currencyPair, assetType)
if err == nil {
return p.UpdateOrderbook(currencyPair)
return p.UpdateOrderbook(currencyPair, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (p *Poloniex) UpdateOrderbook(currencyPair pair.CurrencyPair) (orderbook.OrderbookBase, error) {
var orderBook orderbook.OrderbookBase
func (p *Poloniex) UpdateOrderbook(currencyPair pair.CurrencyPair, assetType string) (orderbook.Base, error) {
var orderBook orderbook.Base
orderbookNew, err := p.GetOrderbook(exchange.FormatExchangeCurrency(p.GetName(), currencyPair).String(), 1000)
if err != nil {
return orderBook, err
@@ -79,16 +79,16 @@ func (p *Poloniex) UpdateOrderbook(currencyPair pair.CurrencyPair) (orderbook.Or
for x := range orderbookNew.Bids {
data := orderbookNew.Bids[x]
orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Amount, Price: data.Price})
}
for x := range orderbookNew.Asks {
data := orderbookNew.Asks[x]
orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price})
orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Amount, Price: data.Price})
}
orderbook.ProcessOrderbook(p.GetName(), currencyPair, orderBook)
return orderbook.GetOrderbook(p.Name, currencyPair)
orderbook.ProcessOrderbook(p.GetName(), currencyPair, orderBook, assetType)
return orderbook.GetOrderbook(p.Name, currencyPair, assetType)
}
// GetExchangeAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -72,6 +72,23 @@ func TestGetTicker(t *testing.T) {
t.Error("Test Failed - ticker tickerPrice.CurrencyPair value is incorrect")
}
_, err = GetTicker("blah", newPair, Spot)
if err == nil {
t.Fatal("Test Failed. TestGetTicker returned nil error on invalid exchange")
}
newPair.FirstCurrency = "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")
_, 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")
tickerPrice, err = GetTicker("bitfinex", newPair, "futures_3m")
@@ -220,6 +237,7 @@ func TestCreateNewTicker(t *testing.T) {
}
func TestProcessTicker(t *testing.T) { //non-appending function to tickers
Tickers = []Ticker{}
newPair := pair.NewCurrencyPair("BTC", "USD")
priceStruct := Price{
Pair: newPair,
@@ -234,4 +252,27 @@ func TestProcessTicker(t *testing.T) { //non-appending function to tickers
}
ProcessTicker("btcc", newPair, priceStruct, Spot)
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() {
t.Fatal("Test failed. TestProcessTicker pair mismatch")
}
secondPair := pair.NewCurrencyPair("BTC", "AUD")
priceStruct.Pair = secondPair
ProcessTicker("btcc", secondPair, priceStruct, Spot)
result, err = GetTicker("btcc", secondPair, Spot)
if err != nil {
t.Fatal("Test failed. TestProcessTicker failed to create and return a new ticker")
}
result, err = GetTicker("btcc", newPair, Spot)
if err != nil {
t.Fatal("Test failed. TestProcessTicker failed to return an existing ticker")
}
}