diff --git a/currency/pair/pair.go b/currency/pair/pair.go new file mode 100644 index 00000000..e1a24e98 --- /dev/null +++ b/currency/pair/pair.go @@ -0,0 +1,63 @@ +package pair + +import "strings" + +type CurrencyItem string + +func (c CurrencyItem) Lower() CurrencyItem { + return CurrencyItem(strings.ToLower(string(c))) +} + +func (c CurrencyItem) Upper() CurrencyItem { + return CurrencyItem(strings.ToUpper(string(c))) +} + +func (c CurrencyItem) String() string { + return string(c) +} + +type CurrencyPair struct { + Delimiter string `json:"delimiter"` + FirstCurrency CurrencyItem `json:"first_currency"` + SecondCurrency CurrencyItem `json:"second_currency"` +} + +func (c CurrencyPair) GetFirstCurrency() CurrencyItem { + return c.FirstCurrency +} + +func (c CurrencyPair) GetSecondCurrency() CurrencyItem { + return c.SecondCurrency +} + +func (c CurrencyPair) Pair() CurrencyItem { + return c.FirstCurrency + CurrencyItem(c.Delimiter) + c.SecondCurrency +} + +func NewCurrencyPairDelimiter(currency, delimiter string) CurrencyPair { + result := strings.Split(currency, delimiter) + return CurrencyPair{ + Delimiter: delimiter, + FirstCurrency: CurrencyItem(result[0]), + SecondCurrency: CurrencyItem(result[1]), + } +} + +func NewCurrencyPair(firstCurrency, secondCurrency string) CurrencyPair { + return CurrencyPair{ + FirstCurrency: CurrencyItem(firstCurrency), + SecondCurrency: CurrencyItem(secondCurrency), + } +} + +func NewCurrencyPairFromString(currency string) CurrencyPair { + delmiters := []string{"_", "-"} + var delimiter string + for _, x := range delmiters { + if strings.Contains(currency, x) { + delimiter = x + return NewCurrencyPairDelimiter(currency, delimiter) + } + } + return NewCurrencyPair(currency[0:3], currency[3:]) +} diff --git a/currency/pair/pair_test.go b/currency/pair/pair_test.go new file mode 100644 index 00000000..227ef872 --- /dev/null +++ b/currency/pair/pair_test.go @@ -0,0 +1,113 @@ +package pair + +import ( + "testing" +) + +func TestLower(t *testing.T) { + t.Parallel() + pair := CurrencyItem("BTCUSD") + actual := pair.Lower() + expected := CurrencyItem("btcusd") + if actual != expected { + t.Errorf("Test failed. Lower(): %s was not equal to expected value: %s", + actual, expected) + } +} + +func TestUpper(t *testing.T) { + t.Parallel() + pair := CurrencyItem("btcusd") + actual := pair.Upper() + expected := CurrencyItem("BTCUSD") + if actual != expected { + t.Errorf("Test failed. Upper(): %s was not equal to expected value: %s", + actual, expected) + } +} + +func TestString(t *testing.T) { + t.Parallel() + pair := NewCurrencyPair("BTC", "USD") + actual := "BTCUSD" + expected := pair.Pair().String() + if actual != expected { + t.Errorf("Test failed. String(): %s was not equal to expected value: %s", + actual, expected) + } +} + +func TestGetFirstCurrency(t *testing.T) { + t.Parallel() + pair := NewCurrencyPair("BTC", "USD") + actual := pair.GetFirstCurrency() + expected := CurrencyItem("BTC") + if actual != expected { + t.Errorf("Test failed. GetFirstCurrency(): %s was not equal to expected value: %s", actual, expected) + } +} + +func TestGetSecondCurrency(t *testing.T) { + t.Parallel() + pair := NewCurrencyPair("BTC", "USD") + actual := pair.GetSecondCurrency() + expected := CurrencyItem("USD") + if actual != expected { + t.Errorf("Test failed. GetSecondCurrency(): %s was not equal to expected value: %s", actual, expected) + } +} + +func TestPair(t *testing.T) { + t.Parallel() + pair := NewCurrencyPair("BTC", "USD") + actual := pair.Pair() + expected := CurrencyItem("BTCUSD") + if actual != expected { + t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + } +} + +func TestNewCurrencyPair(t *testing.T) { + t.Parallel() + pair := NewCurrencyPair("BTC", "USD") + actual := pair.Pair() + expected := CurrencyItem("BTCUSD") + if actual != expected { + t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + } +} + +func TestNewCurrencyPairDelimiter(t *testing.T) { + t.Parallel() + pair := NewCurrencyPairDelimiter("BTC-USD", "-") + actual := pair.Pair() + expected := CurrencyItem("BTC-USD") + if actual != expected { + t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + } + + actual = CurrencyItem(pair.Delimiter) + expected = "-" + if actual != expected { + t.Errorf("Test failed. Delmiter: %s was not equal to expected value: %s", actual, expected) + } +} + +func TestNewCurrencyPairFromString(t *testing.T) { + t.Parallel() + pairStr := "BTC-USD" + pair := NewCurrencyPairFromString(pairStr) + actual := pair.Pair() + expected := CurrencyItem("BTC-USD") + if actual != expected { + t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + } + + pairStr = "BTCUSD" + pair = NewCurrencyPairFromString(pairStr) + actual = pair.Pair() + expected = CurrencyItem("BTCUSD") + if actual != expected { + t.Errorf("Test failed. Pair(): %s was not equal to expected value: %s", actual, expected) + } +} diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index 85b026d7..4548c6ad 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -3,6 +3,7 @@ package alphapoint import ( "log" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/ticker" @@ -28,27 +29,27 @@ func (e *Alphapoint) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, err return response, nil } -func (a *Alphapoint) GetTickerPrice(currency string) ticker.TickerPrice { +func (a *Alphapoint) GetTickerPrice(p pair.CurrencyPair) ticker.TickerPrice { var tickerPrice ticker.TickerPrice - tick, err := a.GetTicker(currency) + tick, err := a.GetTicker(p.Pair().String()) if err != nil { log.Println(err) return ticker.TickerPrice{} } + tickerPrice.Pair = p tickerPrice.Ask = tick.Ask tickerPrice.Bid = tick.Bid - return tickerPrice } -func (a *Alphapoint) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(a.GetName(), currency[0:3], currency[3:]) +func (a *Alphapoint) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(a.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := a.GetOrderbook(currency) + orderbookNew, err := a.GetOrderbook(p.Pair().String()) if err != nil { return orderBook, err } @@ -63,8 +64,7 @@ func (a *Alphapoint) GetOrderbookEx(currency string) (orderbook.OrderbookBase, e orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Quantity, Price: data.Price}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(a.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(a.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/anx/anx_wrapper.go b/exchanges/anx/anx_wrapper.go index 7ae55171..8e00e11c 100644 --- a/exchanges/anx/anx_wrapper.go +++ b/exchanges/anx/anx_wrapper.go @@ -4,6 +4,7 @@ import ( "log" "time" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -22,46 +23,45 @@ func (a *ANX) Run() { for a.Enabled { for _, x := range a.EnabledPairs { - currency := x + currency := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { ticker, err := a.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("ANX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(a.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) + log.Printf("ANX %s: Last %f High %f Low %f Volume %f\n", currency.Pair(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(a.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * a.RESTPollingDelay) } } -func (a *ANX) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(a.GetName(), currency[0:3], currency[3:]) +func (a *ANX) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(a.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := a.GetTicker(currency) + tick, err := a.GetTicker(p.Pair().String()) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tick.Data.Buy.Value tickerPrice.Bid = tick.Data.Sell.Value - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] tickerPrice.Low = tick.Data.Low.Value tickerPrice.Last = tick.Data.Last.Value tickerPrice.Volume = tick.Data.Vol.Value tickerPrice.High = tick.Data.High.Value - ticker.ProcessTicker(a.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(a.GetName(), p, tickerPrice) return tickerPrice, nil } -func (e *ANX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { +func (e *ANX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { return orderbook.OrderbookBase{}, nil } diff --git a/exchanges/bitfinex/bitfinex_wrapper.go b/exchanges/bitfinex/bitfinex_wrapper.go index 156bc455..7702c700 100644 --- a/exchanges/bitfinex/bitfinex_wrapper.go +++ b/exchanges/bitfinex/bitfinex_wrapper.go @@ -6,6 +6,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -39,51 +40,50 @@ func (b *Bitfinex) Run() { for b.Enabled { for _, x := range b.EnabledPairs { - currency := x + currency := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { ticker, err := b.GetTickerPrice(currency) if err != nil { return } - log.Printf("Bitfinex %s Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) + log.Printf("Bitfinex %s Last %f High %f Low %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(b.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * b.RESTPollingDelay) } } -func (b *Bitfinex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tick, err := ticker.GetTicker(b.GetName(), currency[0:3], currency[3:]) +func (b *Bitfinex) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tick, err := ticker.GetTicker(b.GetName(), p) if err == nil { return tick, nil } var tickerPrice ticker.TickerPrice - tickerNew, err := b.GetTicker(currency, nil) + tickerNew, err := b.GetTicker(p.Pair().String(), nil) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tickerNew.Ask tickerPrice.Bid = tickerNew.Bid - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] tickerPrice.Low = tickerNew.Low tickerPrice.Last = tickerNew.Last tickerPrice.Volume = tickerNew.Volume tickerPrice.High = tickerNew.High - ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(b.GetName(), p, tickerPrice) return tickerPrice, nil } -func (b *Bitfinex) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:]) +func (b *Bitfinex) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := b.GetOrderbook(currency, nil) + orderbookNew, err := b.GetOrderbook(p.Pair().String(), nil) if err != nil { return orderBook, err } @@ -100,9 +100,8 @@ func (b *Bitfinex) GetOrderbookEx(currency string) (orderbook.OrderbookBase, err orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Price: price, Amount: amount}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(b.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/bitstamp/bitstamp_wrapper.go b/exchanges/bitstamp/bitstamp_wrapper.go index ce6116ab..c824a6a1 100644 --- a/exchanges/bitstamp/bitstamp_wrapper.go +++ b/exchanges/bitstamp/bitstamp_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -28,53 +29,52 @@ func (b *Bitstamp) Run() { for b.Enabled { for _, x := range b.EnabledPairs { - currency := x + currency := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { ticker, err := b.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("Bitstamp %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) + log.Printf("Bitstamp %s: Last %f High %f Low %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(b.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * b.RESTPollingDelay) } } -func (b *Bitstamp) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], currency[3:]) +func (b *Bitstamp) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(b.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := b.GetTicker(currency, false) + tick, err := b.GetTicker(p.Pair().String(), false) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tick.Ask tickerPrice.Bid = tick.Bid - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Volume tickerPrice.High = tick.High - ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(b.GetName(), p, tickerPrice) return tickerPrice, nil } -func (b *Bitstamp) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:]) +func (b *Bitstamp) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := b.GetOrderbook(currency) + orderbookNew, err := b.GetOrderbook(p.Pair().String()) if err != nil { return orderBook, err } @@ -89,9 +89,8 @@ func (b *Bitstamp) GetOrderbookEx(currency string) (orderbook.OrderbookBase, err orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(b.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/btcc/btcc_wrapper.go b/exchanges/btcc/btcc_wrapper.go index a1f2ee6c..ed634df6 100644 --- a/exchanges/btcc/btcc_wrapper.go +++ b/exchanges/btcc/btcc_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -28,49 +29,48 @@ func (b *BTCC) Run() { for b.Enabled { for _, x := range b.EnabledPairs { - currency := x + currency := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { - ticker, err := b.GetTickerPrice(common.StringToLower(currency)) + ticker, err := b.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("BTCC %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) + log.Printf("BTCC %s: Last %f High %f Low %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(b.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * b.RESTPollingDelay) } } -func (b *BTCC) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], currency[3:]) +func (b *BTCC) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(b.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := b.GetTicker(currency) + tick, err := b.GetTicker(p.Pair().Lower().String()) + tickerPrice.Pair = p tickerPrice.Ask = tick.Sell tickerPrice.Bid = tick.Buy - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol tickerPrice.High = tick.High - ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(b.GetName(), p, tickerPrice) return tickerPrice, nil } -func (b *BTCC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:]) +func (b *BTCC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := b.GetOrderBook(currency, 100) + orderbookNew, err := b.GetOrderBook(p.Pair().Lower().String(), 100) if err != nil { return orderBook, err } @@ -85,9 +85,8 @@ func (b *BTCC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) orderBook.Asks = append(ob.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(b.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/btce/btce_wrapper.go b/exchanges/btce/btce_wrapper.go index e612cb79..5f4433e0 100644 --- a/exchanges/btce/btce_wrapper.go +++ b/exchanges/btce/btce_wrapper.go @@ -6,6 +6,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -48,32 +49,31 @@ func (b *BTCE) Run() { } } -func (b *BTCE) GetTickerPrice(currency string) (ticker.TickerPrice, error) { +func (b *BTCE) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice - tick, ok := b.Ticker[currency] + tick, ok := b.Ticker[p.Pair().Lower().String()] if !ok { return tickerPrice, errors.New("Unable to get currency.") } + tickerPrice.Pair = p tickerPrice.Ask = tick.Buy tickerPrice.Bid = tick.Sell - tickerPrice.FirstCurrency = common.StringToUpper(currency[0:3]) - tickerPrice.SecondCurrency = common.StringToUpper(currency[4:]) tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol_cur tickerPrice.High = tick.High - ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(b.GetName(), p, tickerPrice) return tickerPrice, nil } -func (b *BTCE) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(b.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:])) +func (b *BTCE) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := b.GetDepth(currency) + orderbookNew, err := b.GetDepth(p.Pair().Lower().String()) if err != nil { return orderBook, err } @@ -88,9 +88,8 @@ func (b *BTCE) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) orderBook.Asks = append(ob.Asks, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) } - orderBook.FirstCurrency = common.StringToUpper(currency[0:3]) - orderBook.SecondCurrency = common.StringToUpper(currency[4:]) - orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(b.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/btcmarkets/btcmarkets_wrapper.go b/exchanges/btcmarkets/btcmarkets_wrapper.go index 924df389..99ebe701 100644 --- a/exchanges/btcmarkets/btcmarkets_wrapper.go +++ b/exchanges/btcmarkets/btcmarkets_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/currency" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -23,7 +24,7 @@ func (b *BTCMarkets) Run() { for b.Enabled { for _, x := range b.EnabledPairs { - curr := x + curr := pair.NewCurrencyPair(x, "AUD") go func() { ticker, err := b.GetTickerPrice(curr) if err != nil { @@ -32,43 +33,42 @@ func (b *BTCMarkets) Run() { BTCMarketsLastUSD, _ := currency.ConvertCurrency(ticker.Last, "AUD", "USD") BTCMarketsBestBidUSD, _ := currency.ConvertCurrency(ticker.Bid, "AUD", "USD") BTCMarketsBestAskUSD, _ := currency.ConvertCurrency(ticker.Ask, "AUD", "USD") - log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", curr, BTCMarketsLastUSD, ticker.Last, BTCMarketsBestBidUSD, ticker.Bid, BTCMarketsBestAskUSD, ticker.Ask) - stats.AddExchangeInfo(b.GetName(), curr[0:3], curr[3:], ticker.Last, 0) - stats.AddExchangeInfo(b.GetName(), curr[0:3], "USD", BTCMarketsLastUSD, 0) + log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", curr.Pair().String(), BTCMarketsLastUSD, ticker.Last, BTCMarketsBestBidUSD, ticker.Bid, BTCMarketsBestAskUSD, ticker.Ask) + stats.AddExchangeInfo(b.GetName(), curr.GetFirstCurrency().String(), curr.GetSecondCurrency().String(), ticker.Last, 0) + stats.AddExchangeInfo(b.GetName(), curr.GetFirstCurrency().String(), "USD", BTCMarketsLastUSD, 0) }() } time.Sleep(time.Second * b.RESTPollingDelay) } } -func (b *BTCMarkets) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], "AUD") +func (b *BTCMarkets) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(b.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := b.GetTicker(currency) + tick, err := b.GetTicker(p.GetFirstCurrency().String()) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tick.BestAsk tickerPrice.Bid = tick.BestBID - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = "AUD" tickerPrice.Last = tick.LastPrice - ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(b.GetName(), p, tickerPrice) return tickerPrice, nil } -func (b *BTCMarkets) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], "AUD") +func (b *BTCMarkets) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := b.GetOrderbook(currency) + orderbookNew, err := b.GetOrderbook(p.GetFirstCurrency().String()) if err != nil { return orderBook, err } @@ -83,9 +83,8 @@ func (b *BTCMarkets) GetOrderbookEx(currency string) (orderbook.OrderbookBase, e orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = "AUD" - orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(b.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/exchange.go b/exchanges/exchange.go index 9fc44e74..0f555aa8 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/config" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -51,8 +52,8 @@ type IBotExchange interface { SetDefaults() GetName() string IsEnabled() bool - GetTickerPrice(currency string) (ticker.TickerPrice, error) - GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) + GetTickerPrice(currency pair.CurrencyPair) (ticker.TickerPrice, error) + GetOrderbookEx(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) GetEnabledCurrencies() []string GetExchangeAccountInfo() (ExchangeAccountInfo, error) } diff --git a/exchanges/gdax/gdax_wrapper.go b/exchanges/gdax/gdax_wrapper.go index 831ee06d..861b389a 100644 --- a/exchanges/gdax/gdax_wrapper.go +++ b/exchanges/gdax/gdax_wrapper.go @@ -1,11 +1,11 @@ package gdax import ( - "fmt" "log" "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -45,7 +45,8 @@ func (g *GDAX) Run() { for g.Enabled { for _, x := range g.EnabledPairs { - currency := x[0:3] + "-" + x[3:] + currency := pair.NewCurrencyPair(x[0:3], x[3:]) + currency.Delimiter = "-" go func() { ticker, err := g.GetTickerPrice(currency) @@ -53,8 +54,8 @@ func (g *GDAX) Run() { log.Println(err) return } - log.Printf("GDAX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(g.GetName(), currency[0:3], currency[4:], ticker.Last, ticker.Volume) + log.Printf("GDAX %s: Last %f High %f Low %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(g.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * g.RESTPollingDelay) @@ -80,45 +81,41 @@ func (e *GDAX) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { return response, nil } -func (g *GDAX) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - if len(currency) == 6 { - currency = fmt.Sprintf("%s-%s", currency[0:3], currency[3:]) - } - tickerNew, err := ticker.GetTicker(g.GetName(), currency[0:3], currency[4:]) +func (g *GDAX) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(g.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := g.GetTicker(currency) + tick, err := g.GetTicker(p.Pair().String()) if err != nil { return ticker.TickerPrice{}, err } - stats, err := g.GetStats(currency) + stats, err := g.GetStats(p.Pair().String()) if err != nil { return ticker.TickerPrice{}, err } - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[4:] + tickerPrice.Pair = p tickerPrice.Volume = stats.Volume tickerPrice.Last = tick.Price tickerPrice.High = stats.High tickerPrice.Low = stats.Low - ticker.ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(g.GetName(), p, tickerPrice) return tickerPrice, nil } -func (g *GDAX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(g.GetName(), currency[0:3], currency[4:]) +func (g *GDAX) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(g.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := g.GetOrderbook(currency, 2) + orderbookNew, err := g.GetOrderbook(p.Pair().String(), 2) if err != nil { return orderBook, err } @@ -132,8 +129,7 @@ func (g *GDAX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) for x, _ := range obNew.Asks { orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: obNew.Bids[x].Amount, Price: obNew.Bids[x].Price}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[4:] - orderbook.ProcessOrderbook(g.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(g.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/gemini/gemini_wrapper.go b/exchanges/gemini/gemini_wrapper.go index e80b89f9..a14d156a 100644 --- a/exchanges/gemini/gemini_wrapper.go +++ b/exchanges/gemini/gemini_wrapper.go @@ -5,6 +5,7 @@ import ( "net/url" "time" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -33,15 +34,15 @@ func (g *Gemini) Run() { for g.Enabled { for _, x := range g.EnabledPairs { - currency := x + currency := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { ticker, err := g.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("Gemini %s Last %f Bid %f Ask %f Volume %f\n", currency, ticker.Last, ticker.Bid, ticker.Ask, ticker.Volume) - stats.AddExchangeInfo(g.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) + log.Printf("Gemini %s Last %f Bid %f Ask %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.Bid, ticker.Ask, ticker.Volume) + stats.AddExchangeInfo(g.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * g.RESTPollingDelay) @@ -67,35 +68,34 @@ func (e *Gemini) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) return response, nil } -func (g *Gemini) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(g.GetName(), currency[0:3], currency[3:]) +func (g *Gemini) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(g.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := g.GetTicker(currency) + tick, err := g.GetTicker(p.Pair().String()) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tick.Ask tickerPrice.Bid = tick.Bid - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Volume.USD - ticker.ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(g.GetName(), p, tickerPrice) return tickerPrice, nil } -func (g *Gemini) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(g.GetName(), currency[0:3], currency[3:]) +func (g *Gemini) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(g.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := g.GetOrderbook(currency, url.Values{}) + orderbookNew, err := g.GetOrderbook(p.Pair().String(), url.Values{}) if err != nil { return orderBook, err } @@ -108,8 +108,7 @@ func (g *Gemini) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(g.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(g.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index 65f08ae3..23056756 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/currency" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -29,7 +30,7 @@ func (h *HUOBI) Run() { for h.Enabled { for _, x := range h.EnabledPairs { - curr := common.StringToLower(x[0:3]) + curr := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { ticker, err := h.GetTickerPrice(curr) if err != nil { @@ -39,46 +40,45 @@ func (h *HUOBI) Run() { HuobiLastUSD, _ := currency.ConvertCurrency(ticker.Last, "CNY", "USD") HuobiHighUSD, _ := currency.ConvertCurrency(ticker.High, "CNY", "USD") HuobiLowUSD, _ := currency.ConvertCurrency(ticker.Low, "CNY", "USD") - log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", curr, HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(h.GetName(), common.StringToUpper(curr[0:3]), common.StringToUpper(curr[3:]), ticker.Last, ticker.Volume) - stats.AddExchangeInfo(h.GetName(), common.StringToUpper(curr[0:3]), "USD", HuobiLastUSD, ticker.Volume) + log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", curr.Pair().String(), HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(h.GetName(), curr.GetFirstCurrency().String(), curr.GetSecondCurrency().String(), ticker.Last, ticker.Volume) + stats.AddExchangeInfo(h.GetName(), curr.GetFirstCurrency().String(), "USD", HuobiLastUSD, ticker.Volume) }() } time.Sleep(time.Second * h.RESTPollingDelay) } } -func (h *HUOBI) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:])) +func (h *HUOBI) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(h.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := h.GetTicker(currency) + tick, err := h.GetTicker(p.GetFirstCurrency().Lower().String()) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tick.Sell tickerPrice.Bid = tick.Buy - tickerPrice.FirstCurrency = common.StringToUpper(currency[0:3]) - tickerPrice.SecondCurrency = common.StringToUpper(currency[3:]) tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol tickerPrice.High = tick.High - ticker.ProcessTicker(h.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(h.GetName(), p, tickerPrice) return tickerPrice, nil } -func (h *HUOBI) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(h.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[3:])) +func (h *HUOBI) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(h.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := h.GetOrderBook(currency) + orderbookNew, err := h.GetOrderBook(p.GetFirstCurrency().Lower().String()) if err != nil { return orderBook, err } @@ -92,9 +92,8 @@ func (h *HUOBI) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } - orderBook.FirstCurrency = common.StringToUpper(currency[0:3]) - orderBook.SecondCurrency = common.StringToUpper(currency[3:]) - orderbook.ProcessOrderbook(h.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(h.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/itbit/itbit_wrapper.go b/exchanges/itbit/itbit_wrapper.go index a51b0bae..a73df974 100644 --- a/exchanges/itbit/itbit_wrapper.go +++ b/exchanges/itbit/itbit_wrapper.go @@ -5,6 +5,7 @@ import ( "strconv" "time" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -22,53 +23,52 @@ func (i *ItBit) Run() { for i.Enabled { for _, x := range i.EnabledPairs { - currency := x + currency := pair.NewCurrencyPair(x[0:3], x[3:]) go func() { ticker, err := i.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("ItBit %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(i.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume) + log.Printf("ItBit %s: Last %f High %f Low %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(i.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * i.RESTPollingDelay) } } -func (i *ItBit) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(i.GetName(), currency[0:3], currency[3:]) +func (i *ItBit) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(i.GetName(), p) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := i.GetTicker(currency) + tick, err := i.GetTicker(p.Pair().String()) if err != nil { return tickerPrice, err } + tickerPrice.Pair = p tickerPrice.Ask = tick.Ask tickerPrice.Bid = tick.Bid - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] tickerPrice.Last = tick.LastPrice tickerPrice.High = tick.High24h tickerPrice.Low = tick.Low24h tickerPrice.Volume = tick.Volume24h - ticker.ProcessTicker(i.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(i.GetName(), p, tickerPrice) return tickerPrice, nil } -func (i *ItBit) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(i.GetName(), currency[0:3], currency[3:]) +func (i *ItBit) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(i.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := i.GetOrderbook(currency) + orderbookNew, err := i.GetOrderbook(p.Pair().String()) if err != nil { return orderBook, err } @@ -98,9 +98,8 @@ func (i *ItBit) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) } orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: amount, Price: price}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(i.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(i.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index 4b747148..64fb1780 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -51,7 +52,7 @@ func (k *Kraken) Run() { } //This will return the TickerPrice struct when tickers are completed here.. -func (k *Kraken) GetTickerPrice(currency string) (ticker.TickerPrice, error) { +func (k *Kraken) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice /* ticker, err := i.GetTicker(currency) @@ -65,7 +66,7 @@ func (k *Kraken) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } -func (k *Kraken) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { +func (k *Kraken) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { return orderbook.OrderbookBase{}, nil } diff --git a/exchanges/lakebtc/lakebtc_wrapper.go b/exchanges/lakebtc/lakebtc_wrapper.go index ca70857a..09c0d4c3 100644 --- a/exchanges/lakebtc/lakebtc_wrapper.go +++ b/exchanges/lakebtc/lakebtc_wrapper.go @@ -6,6 +6,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -23,20 +24,21 @@ func (l *LakeBTC) Run() { for l.Enabled { for _, x := range l.EnabledPairs { - ticker, err := l.GetTickerPrice(x) + currency := pair.NewCurrencyPair(x[0:3], x[3:]) + ticker, err := l.GetTickerPrice(currency) if err != nil { log.Println(err) continue } log.Printf("LakeBTC BTC %s: Last %f High %f Low %f Volume %f\n", x[3:], ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.Last, ticker.Volume) + stats.AddExchangeInfo(l.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) } time.Sleep(time.Second * l.RESTPollingDelay) } } -func (l *LakeBTC) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(l.GetName(), currency[0:3], currency[3:]) +func (l *LakeBTC) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(l.GetName(), p) if err == nil { return tickerNew, nil } @@ -46,32 +48,31 @@ func (l *LakeBTC) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return ticker.TickerPrice{}, err } - result, ok := tick[currency] + result, ok := tick[p.Pair().String()] if !ok { return ticker.TickerPrice{}, err } var tickerPrice ticker.TickerPrice + tickerPrice.Pair = p tickerPrice.Ask = result.Ask tickerPrice.Bid = result.Bid tickerPrice.Volume = result.Volume tickerPrice.High = result.High tickerPrice.Low = result.Low tickerPrice.Last = result.Last - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] - ticker.ProcessTicker(l.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(l.GetName(), p, tickerPrice) return tickerPrice, nil } -func (l *LakeBTC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(l.GetName(), currency[0:3], currency[3:]) +func (l *LakeBTC) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(l.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := l.GetOrderBook(currency) + orderbookNew, err := l.GetOrderBook(p.Pair().String()) if err != nil { return orderBook, err } @@ -84,9 +85,8 @@ func (l *LakeBTC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, erro orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: orderbookNew.Asks[x].Amount, Price: orderbookNew.Asks[x].Price}) } - orderBook.FirstCurrency = currency[0:3] - orderBook.SecondCurrency = currency[3:] - orderbook.ProcessOrderbook(l.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(l.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/liqui/liqui_wrapper.go b/exchanges/liqui/liqui_wrapper.go index dd1f5aa0..eef40b9c 100644 --- a/exchanges/liqui/liqui_wrapper.go +++ b/exchanges/liqui/liqui_wrapper.go @@ -6,6 +6,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -50,45 +51,41 @@ func (l *Liqui) Run() { return } for x, y := range ticker { - currencies := common.SplitStrings(x, "_") - x = common.StringToUpper(x) - log.Printf("Liqui %s: Last %f High %f Low %f Volume %f\n", x, y.Last, y.High, y.Low, y.Vol_cur) + currency := pair.NewCurrencyPairDelimiter(common.StringToUpper(x), "_") + log.Printf("Liqui %s: Last %f High %f Low %f Volume %f\n", currency.Pair().String(), y.Last, y.High, y.Low, y.Vol_cur) l.Ticker[x] = y - stats.AddExchangeInfo(l.GetName(), common.StringToUpper(currencies[0]), common.StringToUpper(currencies[1]), y.Last, y.Vol_cur) + stats.AddExchangeInfo(l.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), y.Last, y.Vol_cur) } }() time.Sleep(time.Second * l.RESTPollingDelay) } } -func (l *Liqui) GetTickerPrice(currency string) (ticker.TickerPrice, error) { +func (l *Liqui) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { var tickerPrice ticker.TickerPrice - tick, ok := l.Ticker[currency] + tick, ok := l.Ticker[p.Pair().Lower().String()] if !ok { return tickerPrice, errors.New("Unable to get currency.") } + tickerPrice.Pair = p tickerPrice.Ask = tick.Buy tickerPrice.Bid = tick.Sell - currencies := common.SplitStrings(currency, "_") - tickerPrice.FirstCurrency = currencies[0] - tickerPrice.SecondCurrency = currencies[1] tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol_cur tickerPrice.High = tick.High - ticker.ProcessTicker(l.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(l.GetName(), p, tickerPrice) return tickerPrice, nil } -func (l *Liqui) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - currencies := common.SplitStrings(currency, "_") - ob, err := orderbook.GetOrderbook(l.GetName(), currencies[0], currencies[1]) +func (l *Liqui) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(l.GetName(), p) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := l.GetDepth(currency) + orderbookNew, err := l.GetDepth(p.Pair().Lower().String()) if err != nil { return orderBook, err } @@ -102,9 +99,8 @@ func (l *Liqui) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } - orderBook.FirstCurrency = currencies[0] - orderBook.SecondCurrency = currencies[1] - orderbook.ProcessOrderbook(l.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = p + orderbook.ProcessOrderbook(l.GetName(), p, orderBook) return orderBook, nil } diff --git a/exchanges/localbitcoins/localbitcoins_wrapper.go b/exchanges/localbitcoins/localbitcoins_wrapper.go index c0ec30ad..d8ad0517 100644 --- a/exchanges/localbitcoins/localbitcoins_wrapper.go +++ b/exchanges/localbitcoins/localbitcoins_wrapper.go @@ -4,6 +4,7 @@ import ( "log" "time" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -22,23 +23,23 @@ func (l *LocalBitcoins) Run() { for l.Enabled { for _, x := range l.EnabledPairs { - currency := x[3:] - ticker, err := l.GetTickerPrice("BTC" + currency) + currency := pair.NewCurrencyPair("BTC", x[3:]) + ticker, err := l.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("LocalBitcoins BTC %s: Last %f Volume %f\n", currency, ticker.Last, ticker.Volume) - stats.AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.Last, ticker.Volume) + log.Printf("LocalBitcoins BTC %s: Last %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.Volume) + stats.AddExchangeInfo(l.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) } time.Sleep(time.Second * l.RESTPollingDelay) } } -func (l *LocalBitcoins) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(l.GetName(), currency[0:3], currency[3:]) +func (l *LocalBitcoins) GetTickerPrice(p pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(l.GetName(), p) if err == nil { return tickerNew, nil } @@ -50,16 +51,16 @@ func (l *LocalBitcoins) GetTickerPrice(currency string) (ticker.TickerPrice, err var tickerPrice ticker.TickerPrice for key, value := range tick { + tickerPrice.Pair = p tickerPrice.Last = value.Rates.Last - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = key + tickerPrice.Pair.SecondCurrency = pair.CurrencyItem(key) tickerPrice.Volume = value.VolumeBTC - ticker.ProcessTicker(l.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(l.GetName(), p, tickerPrice) } return tickerPrice, nil } -func (l *LocalBitcoins) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { +func (l *LocalBitcoins) GetOrderbookEx(p pair.CurrencyPair) (orderbook.OrderbookBase, error) { return orderbook.OrderbookBase{}, nil } diff --git a/exchanges/okcoin/okcoin_wrapper.go b/exchanges/okcoin/okcoin_wrapper.go index 7f64f456..153e831a 100644 --- a/exchanges/okcoin/okcoin_wrapper.go +++ b/exchanges/okcoin/okcoin_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/currency" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -29,18 +30,19 @@ func (o *OKCoin) Run() { for o.Enabled { for _, x := range o.EnabledPairs { - curr := common.StringToLower(x[0:3] + "_" + x[3:]) + curr := pair.NewCurrencyPair(x[0:3], x[3:]) + curr.Delimiter = "_" if o.APIUrl == OKCOIN_API_URL { for _, y := range o.FuturesValues { futuresValue := y go func() { - ticker, err := o.GetFuturesTicker(curr, futuresValue) + ticker, err := o.GetFuturesTicker(curr.Pair().Lower().String(), futuresValue) if err != nil { log.Println(err) return } - log.Printf("OKCoin Intl Futures %s (%s): Last %f High %f Low %f Volume %f\n", curr, futuresValue, ticker.Last, ticker.High, ticker.Low, ticker.Vol) - stats.AddExchangeInfo(o.GetName(), common.StringToUpper(curr[0:3]), common.StringToUpper(curr[4:]), ticker.Last, ticker.Vol) + log.Printf("OKCoin Intl Futures %s (%s): Last %f High %f Low %f Volume %f\n", curr.Pair().String(), futuresValue, ticker.Last, ticker.High, ticker.Low, ticker.Vol) + stats.AddExchangeInfo(o.GetName(), curr.GetFirstCurrency().String(), curr.GetSecondCurrency().String(), ticker.Last, ticker.Vol) }() } go func() { @@ -49,8 +51,8 @@ func (o *OKCoin) Run() { log.Println(err) return } - log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", curr, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(o.GetName(), common.StringToUpper(curr[0:3]), common.StringToUpper(curr[4:]), ticker.Last, ticker.Volume) + log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", curr.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(o.GetName(), curr.GetFirstCurrency().String(), curr.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } else { go func() { @@ -62,9 +64,9 @@ func (o *OKCoin) Run() { tickerLastUSD, _ := currency.ConvertCurrency(ticker.Last, "CNY", "USD") tickerHighUSD, _ := currency.ConvertCurrency(ticker.High, "CNY", "USD") tickerLowUSD, _ := currency.ConvertCurrency(ticker.Low, "CNY", "USD") - log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", curr, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Volume) - stats.AddExchangeInfo(o.GetName(), common.StringToUpper(curr[0:3]), common.StringToUpper(curr[4:]), ticker.Last, ticker.Volume) - stats.AddExchangeInfo(o.GetName(), common.StringToUpper(curr[0:3]), "USD", tickerLastUSD, ticker.Volume) + log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", curr.Pair().String(), tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(o.GetName(), curr.GetFirstCurrency().String(), curr.GetSecondCurrency().String(), ticker.Last, ticker.Volume) + stats.AddExchangeInfo(o.GetName(), curr.GetFirstCurrency().String(), "USD", tickerLastUSD, ticker.Volume) }() } } @@ -72,37 +74,36 @@ func (o *OKCoin) Run() { } } -func (o *OKCoin) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:])) +func (o *OKCoin) GetTickerPrice(currency pair.CurrencyPair) (ticker.TickerPrice, error) { + tickerNew, err := ticker.GetTicker(o.GetName(), currency) if err == nil { return tickerNew, nil } var tickerPrice ticker.TickerPrice - tick, err := o.GetTicker(currency) + tick, err := o.GetTicker(currency.Pair().Lower().String()) if err != nil { return tickerPrice, err } + tickerPrice.Pair = currency tickerPrice.Ask = tick.Sell tickerPrice.Bid = tick.Buy - tickerPrice.FirstCurrency = common.StringToUpper(currency[0:3]) - tickerPrice.SecondCurrency = common.StringToUpper(currency[4:]) tickerPrice.Low = tick.Low tickerPrice.Last = tick.Last tickerPrice.Volume = tick.Vol tickerPrice.High = tick.High - ticker.ProcessTicker(o.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(o.GetName(), currency, tickerPrice) return tickerPrice, nil } -func (o *OKCoin) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - ob, err := orderbook.GetOrderbook(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:])) +func (o *OKCoin) GetOrderbookEx(currency pair.CurrencyPair) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(o.GetName(), currency) if err == nil { return ob, nil } var orderBook orderbook.OrderbookBase - orderbookNew, err := o.GetOrderBook(currency, 200, false) + orderbookNew, err := o.GetOrderBook(currency.Pair().Lower().String(), 200, false) if err != nil { return orderBook, err } @@ -116,9 +117,8 @@ func (o *OKCoin) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data[1], Price: data[0]}) } - orderBook.FirstCurrency = common.StringToUpper(currency[0:3]) - orderBook.SecondCurrency = common.StringToUpper(currency[4:]) - orderbook.ProcessOrderbook(o.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = currency + orderbook.ProcessOrderbook(o.GetName(), currency, orderBook) return orderBook, nil } diff --git a/exchanges/orderbook/orderbook.go b/exchanges/orderbook/orderbook.go index 29d3bf9e..d9391a58 100644 --- a/exchanges/orderbook/orderbook.go +++ b/exchanges/orderbook/orderbook.go @@ -3,6 +3,8 @@ package orderbook import ( "errors" "time" + + "github.com/thrasher-/gocryptotrader/currency/pair" ) var ( @@ -19,16 +21,15 @@ type OrderbookItem struct { } type OrderbookBase struct { - FirstCurrency string `json:"first_currency"` - SecondCurrency string `json:"second_currency"` - CurrencyPair string `json:"currency_pair"` - Bids []OrderbookItem `json:"bids"` - Asks []OrderbookItem `json:"asks"` - LastUpdated time.Time `json:"last_updated"` + Pair pair.CurrencyPair `json:"pair"` + CurrencyPair string `json:"CurrencyPair"` + Bids []OrderbookItem `json:"bids"` + Asks []OrderbookItem `json:"asks"` + LastUpdated time.Time `json:"last_updated"` } type Orderbook struct { - Orderbook map[string]map[string]OrderbookBase + Orderbook map[pair.CurrencyItem]map[pair.CurrencyItem]OrderbookBase ExchangeName string } @@ -58,21 +59,21 @@ func (o *OrderbookBase) Update(Bids, Asks []OrderbookItem) { o.LastUpdated = time.Now() } -func GetOrderbook(exchange, firstCurrency, secondCurrency string) (OrderbookBase, error) { +func GetOrderbook(exchange string, p pair.CurrencyPair) (OrderbookBase, error) { orderbook, err := GetOrderbookByExchange(exchange) if err != nil { return OrderbookBase{}, err } - if !FirstCurrencyExists(exchange, firstCurrency) { + if !FirstCurrencyExists(exchange, p.GetFirstCurrency()) { return OrderbookBase{}, errors.New(ErrPrimaryCurrencyNotFound) } - if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) { + if !SecondCurrencyExists(exchange, p) { return OrderbookBase{}, errors.New(ErrSecondaryCurrencyNotFound) } - return orderbook.Orderbook[firstCurrency][secondCurrency], nil + return orderbook.Orderbook[p.GetFirstCurrency()][p.GetSecondCurrency()], nil } func GetOrderbookByExchange(exchange string) (*Orderbook, error) { @@ -84,7 +85,7 @@ func GetOrderbookByExchange(exchange string) (*Orderbook, error) { return nil, errors.New(ErrOrderbookForExchangeNotFound) } -func FirstCurrencyExists(exchange, currency string) bool { +func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool { for _, y := range Orderbooks { if y.ExchangeName == exchange { if _, ok := y.Orderbook[currency]; ok { @@ -95,11 +96,11 @@ func FirstCurrencyExists(exchange, currency string) bool { return false } -func SecondCurrencyExists(exchange, primary, secondary string) bool { +func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool { for _, y := range Orderbooks { if y.ExchangeName == exchange { - if _, ok := y.Orderbook[primary]; ok { - if _, ok := y.Orderbook[primary][secondary]; ok { + if _, ok := y.Orderbook[p.GetFirstCurrency()]; ok { + if _, ok := y.Orderbook[p.GetFirstCurrency()][p.GetSecondCurrency()]; ok { return true } } @@ -108,39 +109,39 @@ func SecondCurrencyExists(exchange, primary, secondary string) bool { return false } -func CreateNewOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) Orderbook { +func CreateNewOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew OrderbookBase) Orderbook { orderbook := Orderbook{} orderbook.ExchangeName = exchangeName - orderbook.Orderbook = make(map[string]map[string]OrderbookBase) - sMap := make(map[string]OrderbookBase) - sMap[secondCurrency] = orderbookNew - orderbook.Orderbook[firstCurrency] = sMap + 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 + Orderbooks = append(Orderbooks, orderbook) return orderbook } -func ProcessOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) { - orderbookNew.CurrencyPair = orderbookNew.FirstCurrency + orderbookNew.SecondCurrency - +func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew OrderbookBase) { + orderbookNew.CurrencyPair = p.Pair().String() if len(Orderbooks) == 0 { - CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew) + CreateNewOrderbook(exchangeName, p, orderbookNew) return } else { orderbook, err := GetOrderbookByExchange(exchangeName) if err != nil { - CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew) + CreateNewOrderbook(exchangeName, p, orderbookNew) return } - if FirstCurrencyExists(exchangeName, firstCurrency) { - if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) { - second := orderbook.Orderbook[firstCurrency] - second[secondCurrency] = orderbookNew - orderbook.Orderbook[firstCurrency] = second + 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[string]OrderbookBase) - sMap[secondCurrency] = orderbookNew - orderbook.Orderbook[firstCurrency] = sMap + sMap := make(map[pair.CurrencyItem]OrderbookBase) + sMap[p.GetSecondCurrency()] = orderbookNew + orderbook.Orderbook[p.GetFirstCurrency()] = sMap } } diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 4962173d..f85c2a38 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/common" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges" "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" @@ -28,25 +29,24 @@ func (p *Poloniex) Run() { for p.Enabled { for _, x := range p.EnabledPairs { - currency := x + currency := pair.NewCurrencyPairDelimiter(x, "_") go func() { ticker, err := p.GetTickerPrice(currency) if err != nil { log.Println(err) return } - log.Printf("Poloniex %s Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume) - currencyPair := common.SplitStrings(currency, "_") - stats.AddExchangeInfo(p.GetName(), currencyPair[0], currencyPair[1], ticker.Last, ticker.Volume) + log.Printf("Poloniex %s Last %f High %f Low %f Volume %f\n", currency.Pair().String(), ticker.Last, ticker.High, ticker.Low, ticker.Volume) + stats.AddExchangeInfo(p.GetName(), currency.GetFirstCurrency().String(), currency.GetSecondCurrency().String(), ticker.Last, ticker.Volume) }() } time.Sleep(time.Second * p.RESTPollingDelay) } } -func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - currencyPair := common.SplitStrings(currency, "_") - tickerNew, err := ticker.GetTicker(p.GetName(), currencyPair[0], currencyPair[1]) +func (p *Poloniex) GetTickerPrice(currencyPair pair.CurrencyPair) (ticker.TickerPrice, error) { + currency := currencyPair.Pair().String() + tickerNew, err := ticker.GetTicker(p.GetName(), currencyPair) if err == nil { return tickerNew, nil } @@ -57,21 +57,20 @@ func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, err } - tickerPrice.FirstCurrency = currencyPair[0] - tickerPrice.SecondCurrency = currencyPair[1] + tickerPrice.Pair = currencyPair tickerPrice.Ask = tick[currency].Last tickerPrice.Bid = tick[currency].HighestBid tickerPrice.High = tick[currency].HighestBid tickerPrice.Last = tick[currency].Last tickerPrice.Low = tick[currency].LowestAsk tickerPrice.Volume = tick[currency].BaseVolume - ticker.ProcessTicker(p.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) + ticker.ProcessTicker(p.GetName(), currencyPair, tickerPrice) return tickerPrice, nil } -func (p *Poloniex) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { - currencyPair := common.SplitStrings(currency, "_") - ob, err := orderbook.GetOrderbook(p.GetName(), currencyPair[0], currencyPair[1]) +func (p *Poloniex) GetOrderbookEx(currencyPair pair.CurrencyPair) (orderbook.OrderbookBase, error) { + currency := currencyPair.Pair().String() + ob, err := orderbook.GetOrderbook(p.GetName(), currencyPair) if err == nil { return ob, nil } @@ -91,9 +90,8 @@ func (p *Poloniex) GetOrderbookEx(currency string) (orderbook.OrderbookBase, err data := orderbookNew.Asks[x] orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Amount: data.Amount, Price: data.Price}) } - orderBook.FirstCurrency = currencyPair[0] - orderBook.SecondCurrency = currencyPair[1] - orderbook.ProcessOrderbook(p.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + orderBook.Pair = currencyPair + orderbook.ProcessOrderbook(p.GetName(), currencyPair, orderBook) return orderBook, nil } diff --git a/exchanges/ticker/ticker.go b/exchanges/ticker/ticker.go index f2cd99e8..366dc8be 100644 --- a/exchanges/ticker/ticker.go +++ b/exchanges/ticker/ticker.go @@ -3,6 +3,8 @@ package ticker import ( "errors" "strconv" + + "github.com/thrasher-/gocryptotrader/currency/pair" ) var ( @@ -14,59 +16,58 @@ var ( ) type TickerPrice struct { - FirstCurrency string `json:"FirstCurrency"` - SecondCurrency string `json:"SecondCurrency"` - CurrencyPair string `json:"CurrencyPair"` - Last float64 `json:"Last"` - High float64 `json:"High"` - Low float64 `json:"Low"` - Bid float64 `json:"Bid"` - Ask float64 `json:"Ask"` - Volume float64 `json:"Volume"` - PriceATH float64 `json:"PriceATH"` + Pair pair.CurrencyPair `json:"Pair"` + CurrencyPair string `json:"CurrencyPair"` + Last float64 `json:"Last"` + High float64 `json:"High"` + Low float64 `json:"Low"` + Bid float64 `json:"Bid"` + Ask float64 `json:"Ask"` + Volume float64 `json:"Volume"` + PriceATH float64 `json:"PriceATH"` } type Ticker struct { - Price map[string]map[string]TickerPrice + Price map[pair.CurrencyItem]map[pair.CurrencyItem]TickerPrice ExchangeName string } -func (t *Ticker) PriceToString(firstCurrency, secondCurrency, priceType string) string { +func (t *Ticker) PriceToString(p pair.CurrencyPair, priceType string) string { switch priceType { case "last": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Last, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].Last, 'f', -1, 64) case "high": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].High, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].High, 'f', -1, 64) case "low": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Low, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].Low, 'f', -1, 64) case "bid": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Bid, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].Bid, 'f', -1, 64) case "ask": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Ask, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].Ask, 'f', -1, 64) case "volume": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Volume, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].Volume, 'f', -1, 64) case "ath": - return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].PriceATH, 'f', -1, 64) + return strconv.FormatFloat(t.Price[p.GetFirstCurrency()][p.GetSecondCurrency()].PriceATH, 'f', -1, 64) default: return "" } } -func GetTicker(exchange, firstCurrency, secondCurrency string) (TickerPrice, error) { +func GetTicker(exchange string, p pair.CurrencyPair) (TickerPrice, error) { ticker, err := GetTickerByExchange(exchange) if err != nil { return TickerPrice{}, err } - if !FirstCurrencyExists(exchange, firstCurrency) { + if !FirstCurrencyExists(exchange, p.GetFirstCurrency()) { return TickerPrice{}, errors.New(ErrPrimaryCurrencyNotFound) } - if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) { + if !SecondCurrencyExists(exchange, p) { return TickerPrice{}, errors.New(ErrSecondaryCurrencyNotFound) } - return ticker.Price[firstCurrency][secondCurrency], nil + return ticker.Price[p.GetFirstCurrency()][p.GetSecondCurrency()], nil } func GetTickerByExchange(exchange string) (*Ticker, error) { @@ -78,7 +79,7 @@ func GetTickerByExchange(exchange string) (*Ticker, error) { return nil, errors.New(ErrTickerForExchangeNotFound) } -func FirstCurrencyExists(exchange, currency string) bool { +func FirstCurrencyExists(exchange string, currency pair.CurrencyItem) bool { for _, y := range Tickers { if y.ExchangeName == exchange { if _, ok := y.Price[currency]; ok { @@ -89,11 +90,11 @@ func FirstCurrencyExists(exchange, currency string) bool { return false } -func SecondCurrencyExists(exchange, primary, secondary string) bool { +func SecondCurrencyExists(exchange string, p pair.CurrencyPair) bool { for _, y := range Tickers { if y.ExchangeName == exchange { - if _, ok := y.Price[primary]; ok { - if _, ok := y.Price[primary][secondary]; ok { + if _, ok := y.Price[p.GetFirstCurrency()]; ok { + if _, ok := y.Price[p.GetFirstCurrency()][p.GetSecondCurrency()]; ok { return true } } @@ -102,39 +103,39 @@ func SecondCurrencyExists(exchange, primary, secondary string) bool { return false } -func CreateNewTicker(exchangeName string, firstCurrency, secondCurrency string, tickerNew TickerPrice) Ticker { +func CreateNewTicker(exchangeName string, p pair.CurrencyPair, tickerNew TickerPrice) Ticker { ticker := Ticker{} ticker.ExchangeName = exchangeName - ticker.Price = make(map[string]map[string]TickerPrice) - sMap := make(map[string]TickerPrice) - sMap[secondCurrency] = tickerNew - ticker.Price[firstCurrency] = sMap + ticker.Price = make(map[pair.CurrencyItem]map[pair.CurrencyItem]TickerPrice) + sMap := make(map[pair.CurrencyItem]TickerPrice) + sMap[p.GetSecondCurrency()] = tickerNew + ticker.Price[p.GetFirstCurrency()] = sMap + Tickers = append(Tickers, ticker) return ticker } -func ProcessTicker(exchangeName string, firstCurrency, secondCurrency string, tickerNew TickerPrice) { - tickerNew.CurrencyPair = tickerNew.FirstCurrency + tickerNew.SecondCurrency - +func ProcessTicker(exchangeName string, p pair.CurrencyPair, tickerNew TickerPrice) { + tickerNew.CurrencyPair = p.Pair().String() if len(Tickers) == 0 { - CreateNewTicker(exchangeName, firstCurrency, secondCurrency, tickerNew) + CreateNewTicker(exchangeName, p, tickerNew) return } else { ticker, err := GetTickerByExchange(exchangeName) if err != nil { - CreateNewTicker(exchangeName, firstCurrency, secondCurrency, tickerNew) + CreateNewTicker(exchangeName, p, tickerNew) return } - if FirstCurrencyExists(exchangeName, firstCurrency) { - if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) { - second := ticker.Price[firstCurrency] - second[secondCurrency] = tickerNew - ticker.Price[firstCurrency] = second + if FirstCurrencyExists(exchangeName, p.GetFirstCurrency()) { + if !SecondCurrencyExists(exchangeName, p) { + second := ticker.Price[p.GetFirstCurrency()] + second[p.GetSecondCurrency()] = tickerNew + ticker.Price[p.GetFirstCurrency()] = second return } } - sMap := make(map[string]TickerPrice) - sMap[secondCurrency] = tickerNew - ticker.Price[firstCurrency] = sMap + sMap := make(map[pair.CurrencyItem]TickerPrice) + sMap[p.GetSecondCurrency()] = tickerNew + ticker.Price[p.GetFirstCurrency()] = sMap } } diff --git a/ticker_routes.go b/ticker_routes.go index 38c63faa..44d764a6 100644 --- a/ticker_routes.go +++ b/ticker_routes.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/thrasher-/gocryptotrader/currency/pair" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -18,7 +19,7 @@ func jsonTickerResponse(w http.ResponseWriter, r *http.Request) { for i := 0; i < len(bot.exchanges); i++ { if bot.exchanges[i] != nil { if bot.exchanges[i].IsEnabled() && bot.exchanges[i].GetName() == exchangeName { - response, err = bot.exchanges[i].GetTickerPrice(currency) + response, err = bot.exchanges[i].GetTickerPrice(pair.NewCurrencyPairFromString(currency)) if err != nil { log.Println(err) continue @@ -56,7 +57,7 @@ func getAllActiveTickersResponse(w http.ResponseWriter, r *http.Request) { currencies := individualBot.GetEnabledCurrencies() log.Println(currencies) for _, currency := range currencies { - tickerPrice, err := individualBot.GetTickerPrice(currency) + tickerPrice, err := individualBot.GetTickerPrice(pair.NewCurrencyPairFromString(currency)) if err != nil { continue }