Skip to content

Commit

Permalink
Add currency pair support
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Apr 18, 2017
1 parent 7457a7a commit d526e9f
Show file tree
Hide file tree
Showing 23 changed files with 484 additions and 320 deletions.
63 changes: 63 additions & 0 deletions currency/pair/pair.go
Original file line number Diff line number Diff line change
@@ -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:])
}
113 changes: 113 additions & 0 deletions currency/pair/pair_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
18 changes: 9 additions & 9 deletions exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}
20 changes: 10 additions & 10 deletions exchanges/anx/anx_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
29 changes: 14 additions & 15 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
Loading

0 comments on commit d526e9f

Please sign in to comment.