diff --git a/exchanges/alphapoint/alphapoint_wrapper.go b/exchanges/alphapoint/alphapoint_wrapper.go index 0f33ff19..85b026d7 100644 --- a/exchanges/alphapoint/alphapoint_wrapper.go +++ b/exchanges/alphapoint/alphapoint_wrapper.go @@ -4,6 +4,7 @@ import ( "log" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -39,3 +40,31 @@ func (a *Alphapoint) GetTickerPrice(currency string) ticker.TickerPrice { return tickerPrice } + +func (a *Alphapoint) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(a.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := a.GetOrderbook(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = currency[0:3] + orderBook.SecondCurrency = currency[3:] + orderbook.ProcessOrderbook(a.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} diff --git a/exchanges/anx/anx_wrapper.go b/exchanges/anx/anx_wrapper.go index ec6b36b1..7ae55171 100644 --- a/exchanges/anx/anx_wrapper.go +++ b/exchanges/anx/anx_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -60,6 +61,10 @@ func (a *ANX) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (e *ANX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + return orderbook.OrderbookBase{}, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the ANX exchange func (e *ANX) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/bitfinex/bitfinex_wrapper.go b/exchanges/bitfinex/bitfinex_wrapper.go index a48e54a8..156bc455 100644 --- a/exchanges/bitfinex/bitfinex_wrapper.go +++ b/exchanges/bitfinex/bitfinex_wrapper.go @@ -2,10 +2,12 @@ package bitfinex import ( "log" + "strconv" "time" "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -74,6 +76,36 @@ func (b *Bitfinex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (b *Bitfinex) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := b.GetOrderbook(currency, nil) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Asks { + price, _ := strconv.ParseFloat(orderbookNew.Asks[x].Price, 64) + amount, _ := strconv.ParseFloat(orderbookNew.Asks[x].Amount, 64) + orderBook.Asks = append(orderBook.Asks, orderbook.OrderbookItem{Price: price, Amount: amount}) + } + + for x, _ := range orderbookNew.Bids { + price, _ := strconv.ParseFloat(orderbookNew.Bids[x].Price, 64) + amount, _ := strconv.ParseFloat(orderbookNew.Bids[x].Amount, 64) + 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) + return orderBook, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Bitfinex exchange func (e *Bitfinex) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/bitstamp/bitstamp_wrapper.go b/exchanges/bitstamp/bitstamp_wrapper.go index 58a1b7cf..ce6116ab 100644 --- a/exchanges/bitstamp/bitstamp_wrapper.go +++ b/exchanges/bitstamp/bitstamp_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -66,6 +67,34 @@ func (b *Bitstamp) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (b *Bitstamp) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := b.GetOrderbook(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = currency[0:3] + orderBook.SecondCurrency = currency[3:] + orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Bitstamp exchange func (e *Bitstamp) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/btcc/btcc.go b/exchanges/btcc/btcc.go index 9ddce7b0..b6e70105 100644 --- a/exchanges/btcc/btcc.go +++ b/exchanges/btcc/btcc.go @@ -123,14 +123,15 @@ func (b *BTCC) GetTradeHistory(symbol string, limit, sinceTid int64, time time.T return true } -func (b *BTCC) GetOrderBook(symbol string, limit int) bool { +func (b *BTCC) GetOrderBook(symbol string, limit int) (BTCCOrderbook, error) { + result := BTCCOrderbook{} req := fmt.Sprintf("%sdata/orderbook?market=%s&limit=%d", BTCC_API_URL, symbol, limit) - err := common.SendHTTPGetRequest(req, true, nil) + err := common.SendHTTPGetRequest(req, true, &result) if err != nil { - log.Println(err) - return false + return BTCCOrderbook{}, err } - return true + + return result, nil } func (b *BTCC) GetAccountInfo(infoType string) { diff --git a/exchanges/btcc/btcc_types.go b/exchanges/btcc/btcc_types.go index 8f796eb3..7cd5bc48 100644 --- a/exchanges/btcc/btcc_types.go +++ b/exchanges/btcc/btcc_types.go @@ -29,6 +29,12 @@ type BTCCProfile struct { APIKeyPermission int64 `json:"api_key_permission"` } +type BTCCOrderbook struct { + Bids [][]float64 `json:"bids"` + Asks [][]float64 `json:"asks"` + Date int64 `json:"date"` +} + type BTCCCurrencyGeneric struct { Currency string Symbol string diff --git a/exchanges/btcc/btcc_wrapper.go b/exchanges/btcc/btcc_wrapper.go index 5dfde045..a1f2ee6c 100644 --- a/exchanges/btcc/btcc_wrapper.go +++ b/exchanges/btcc/btcc_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -62,6 +63,34 @@ func (b *BTCC) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (b *BTCC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := b.GetOrderBook(currency, 100) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(ob.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) + } + + for x, _ := range orderbookNew.Asks { + data := orderbookNew.Asks[x] + 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) + return orderBook, nil +} + //TODO: Retrieve BTCC info //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Kraken exchange func (e *BTCC) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { diff --git a/exchanges/btce/btce_wrapper.go b/exchanges/btce/btce_wrapper.go index 7eef7320..e612cb79 100644 --- a/exchanges/btce/btce_wrapper.go +++ b/exchanges/btce/btce_wrapper.go @@ -7,6 +7,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -55,8 +56,8 @@ func (b *BTCE) GetTickerPrice(currency string) (ticker.TickerPrice, error) { } tickerPrice.Ask = tick.Buy tickerPrice.Bid = tick.Sell - tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] + 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 @@ -65,6 +66,34 @@ func (b *BTCE) GetTickerPrice(currency string) (ticker.TickerPrice, error) { 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:])) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := b.GetDepth(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(ob.Bids, orderbook.OrderbookItem{Price: data[0], Amount: data[1]}) + } + + for x, _ := range orderbookNew.Asks { + data := orderbookNew.Asks[x] + 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) + return orderBook, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the BTCE exchange func (e *BTCE) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/btcmarkets/btcmarkets.go b/exchanges/btcmarkets/btcmarkets.go index e59b6557..b154e14f 100644 --- a/exchanges/btcmarkets/btcmarkets.go +++ b/exchanges/btcmarkets/btcmarkets.go @@ -45,7 +45,7 @@ func (b *BTCMarkets) Setup(exch config.ExchangeConfig) { if !exch.Enabled { b.SetEnabled(false) } else { - b.Enabled = false + b.Enabled = true b.AuthenticatedAPISupport = exch.AuthenticatedAPISupport b.SetAPIKeys(exch.APIKey, exch.APISecret, "", true) b.RESTPollingDelay = exch.RESTPollingDelay diff --git a/exchanges/btcmarkets/btcmarkets_wrapper.go b/exchanges/btcmarkets/btcmarkets_wrapper.go index 17dee1f1..924df389 100644 --- a/exchanges/btcmarkets/btcmarkets_wrapper.go +++ b/exchanges/btcmarkets/btcmarkets_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/currency" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -41,7 +42,7 @@ func (b *BTCMarkets) Run() { } func (b *BTCMarkets) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], currency[3:]) + tickerNew, err := ticker.GetTicker(b.GetName(), currency[0:3], "AUD") if err == nil { return tickerNew, nil } @@ -54,12 +55,40 @@ func (b *BTCMarkets) GetTickerPrice(currency string) (ticker.TickerPrice, error) tickerPrice.Ask = tick.BestAsk tickerPrice.Bid = tick.BestBID tickerPrice.FirstCurrency = currency[0:3] - tickerPrice.SecondCurrency = currency[3:] + tickerPrice.SecondCurrency = "AUD" tickerPrice.Last = tick.LastPrice ticker.ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) return tickerPrice, nil } +func (b *BTCMarkets) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(b.GetName(), currency[0:3], "AUD") + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := b.GetOrderbook(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = currency[0:3] + orderBook.SecondCurrency = "AUD" + orderbook.ProcessOrderbook(b.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the BTCMarkets exchange func (e *BTCMarkets) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/exchange.go b/exchanges/exchange.go index 162a7228..9fc44e74 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/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -51,6 +52,7 @@ type IBotExchange interface { GetName() string IsEnabled() bool GetTickerPrice(currency string) (ticker.TickerPrice, error) + GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) GetEnabledCurrencies() []string GetExchangeAccountInfo() (ExchangeAccountInfo, error) } diff --git a/exchanges/gdax/gdax.go b/exchanges/gdax/gdax.go index 9dc3aed7..7f5ab92c 100644 --- a/exchanges/gdax/gdax.go +++ b/exchanges/gdax/gdax.go @@ -111,11 +111,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) { continue } - order := make([]GDAXOrderL3, 1) - order[0].Price = price - order[0].Amount = amount - order[0].OrderID = x[2].(string) - ob.Asks = append(ob.Asks, order) + ob.Asks = append(ob.Asks, GDAXOrderL3{Price: price, Amount: amount, OrderID: x[2].(string)}) } for _, x := range orderbook.Bids { price, err := strconv.ParseFloat((x[0].(string)), 64) @@ -127,11 +123,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) { continue } - order := make([]GDAXOrderL3, 1) - order[0].Price = price - order[0].Amount = amount - order[0].OrderID = x[2].(string) - ob.Bids = append(ob.Bids, order) + ob.Bids = append(ob.Bids, GDAXOrderL3{Price: price, Amount: amount, OrderID: x[2].(string)}) } return ob, nil } else { @@ -147,11 +139,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) { continue } - order := make([]GDAXOrderL1L2, 1) - order[0].Price = price - order[0].Amount = amount - order[0].NumOrders = x[2].(float64) - ob.Asks = append(ob.Asks, order) + ob.Asks = append(ob.Asks, GDAXOrderL1L2{Price: price, Amount: amount, NumOrders: x[2].(float64)}) } for _, x := range orderbook.Bids { price, err := strconv.ParseFloat((x[0].(string)), 64) @@ -163,11 +151,7 @@ func (g *GDAX) GetOrderbook(symbol string, level int) (interface{}, error) { continue } - order := make([]GDAXOrderL1L2, 1) - order[0].Price = price - order[0].Amount = amount - order[0].NumOrders = x[2].(float64) - ob.Bids = append(ob.Bids, order) + ob.Bids = append(ob.Bids, GDAXOrderL1L2{Price: price, Amount: amount, NumOrders: x[2].(float64)}) } return ob, nil } diff --git a/exchanges/gdax/gdax_types.go b/exchanges/gdax/gdax_types.go index 857ab9f6..f5753172 100644 --- a/exchanges/gdax/gdax_types.go +++ b/exchanges/gdax/gdax_types.go @@ -30,15 +30,15 @@ type GDAXOrderL3 struct { } type GDAXOrderbookL1L2 struct { - Sequence int64 `json:"sequence"` - Bids [][]GDAXOrderL1L2 `json:"asks"` - Asks [][]GDAXOrderL1L2 `json:"asks"` + Sequence int64 `json:"sequence"` + Bids []GDAXOrderL1L2 `json:"asks"` + Asks []GDAXOrderL1L2 `json:"asks"` } type GDAXOrderbookL3 struct { - Sequence int64 `json:"sequence"` - Bids [][]GDAXOrderL3 `json:"asks"` - Asks [][]GDAXOrderL3 `json:"asks"` + Sequence int64 `json:"sequence"` + Bids []GDAXOrderL3 `json:"asks"` + Asks []GDAXOrderL3 `json:"asks"` } type GDAXOrderbookResponse struct { diff --git a/exchanges/gdax/gdax_wrapper.go b/exchanges/gdax/gdax_wrapper.go index 6b30b674..56631a3b 100644 --- a/exchanges/gdax/gdax_wrapper.go +++ b/exchanges/gdax/gdax_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -79,7 +80,7 @@ func (e *GDAX) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { } func (g *GDAX) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(g.GetName(), currency[0:3], currency[3:]) + tickerNew, err := ticker.GetTicker(g.GetName(), currency[0:3], currency[4:]) if err == nil { return tickerNew, nil } @@ -105,3 +106,30 @@ func (g *GDAX) GetTickerPrice(currency string) (ticker.TickerPrice, error) { ticker.ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) return tickerPrice, nil } + +func (g *GDAX) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(g.GetName(), currency[0:3], currency[4:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := g.GetOrderbook(currency, 2) + if err != nil { + return orderBook, err + } + + 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}) + } + + 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) + return orderBook, nil +} diff --git a/exchanges/gemini/gemini_types.go b/exchanges/gemini/gemini_types.go index 1e2ab074..68eaebbd 100644 --- a/exchanges/gemini/gemini_types.go +++ b/exchanges/gemini/gemini_types.go @@ -1,8 +1,8 @@ package gemini type GeminiOrderbookEntry struct { - Price float64 `json:"price,string"` - Quantity float64 `json:"quantity,string"` + Price float64 `json:"price,string"` + Amount float64 `json:"amount,string"` } type GeminiOrderbook struct { diff --git a/exchanges/gemini/gemini_wrapper.go b/exchanges/gemini/gemini_wrapper.go index ab05ef24..e80b89f9 100644 --- a/exchanges/gemini/gemini_wrapper.go +++ b/exchanges/gemini/gemini_wrapper.go @@ -2,9 +2,11 @@ package gemini import ( "log" + "net/url" "time" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -85,3 +87,29 @@ func (g *Gemini) GetTickerPrice(currency string) (ticker.TickerPrice, error) { ticker.ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice) return tickerPrice, nil } + +func (g *Gemini) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(g.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := g.GetOrderbook(currency, 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}) + } + + for x, _ := range orderbookNew.Asks { + 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) + return orderBook, nil +} diff --git a/exchanges/huobi/huobi.go b/exchanges/huobi/huobi.go index 4387f8d8..2e1c14f9 100644 --- a/exchanges/huobi/huobi.go +++ b/exchanges/huobi/huobi.go @@ -62,14 +62,14 @@ func (h *HUOBI) GetTicker(symbol string) (HuobiTicker, error) { return resp.Ticker, nil } -func (h *HUOBI) GetOrderBook(symbol string) bool { +func (h *HUOBI) GetOrderBook(symbol string) (HuobiOrderbook, error) { path := fmt.Sprintf("http://api.huobi.com/staticmarket/depth_%s_json.js", symbol) - err := common.SendHTTPGetRequest(path, true, nil) + resp := HuobiOrderbook{} + err := common.SendHTTPGetRequest(path, true, &resp) if err != nil { - log.Println(err) - return false + return resp, err } - return true + return resp, nil } func (h *HUOBI) GetAccountInfo() { diff --git a/exchanges/huobi/huobi_types.go b/exchanges/huobi/huobi_types.go index 1d3986aa..144d7d63 100644 --- a/exchanges/huobi/huobi_types.go +++ b/exchanges/huobi/huobi_types.go @@ -13,3 +13,11 @@ type HuobiTickerResponse struct { Time string Ticker HuobiTicker } + +type HuobiOrderbook struct { + ID float64 + TS float64 + Bids [][]float64 `json:"bids"` + Asks [][]float64 `json:"asks"` + Symbol string `json:"string"` +} diff --git a/exchanges/huobi/huobi_wrapper.go b/exchanges/huobi/huobi_wrapper.go index dfa1cf7a..65f08ae3 100644 --- a/exchanges/huobi/huobi_wrapper.go +++ b/exchanges/huobi/huobi_wrapper.go @@ -7,6 +7,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/currency" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -70,6 +71,33 @@ func (h *HUOBI) GetTickerPrice(currency string) (ticker.TickerPrice, error) { 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:])) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := h.GetOrderBook(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = common.StringToUpper(currency[0:3]) + orderBook.SecondCurrency = common.StringToUpper(currency[3:]) + orderbook.ProcessOrderbook(h.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} + //TODO: retrieve HUOBI balance info //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the HUOBI exchange func (e *HUOBI) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { diff --git a/exchanges/itbit/itbit_types.go b/exchanges/itbit/itbit_types.go index 7814efa2..fdfc96f8 100644 --- a/exchanges/itbit/itbit_types.go +++ b/exchanges/itbit/itbit_types.go @@ -20,15 +20,7 @@ type ItBitTicker struct { ServertimeUTC string } -type ItbitOrderbookEntry struct { - Quantitiy float64 `json:"quantity,string"` - Price float64 `json:"price,string"` -} - type ItBitOrderbookResponse struct { - ServerTimeUTC string `json:"serverTimeUTC"` - LastUpdatedTimeUTC string `json:"lastUpdatedTimeUTC"` - Ticker string `json:"ticker"` - Bids []ItbitOrderbookEntry `json:"bids"` - Asks []ItbitOrderbookEntry `json:"asks"` + Bids [][]string `json:"bids"` + Asks [][]string `json:"asks"` } diff --git a/exchanges/itbit/itbit_wrapper.go b/exchanges/itbit/itbit_wrapper.go index 11b73cc5..a51b0bae 100644 --- a/exchanges/itbit/itbit_wrapper.go +++ b/exchanges/itbit/itbit_wrapper.go @@ -2,9 +2,11 @@ package itbit import ( "log" + "strconv" "time" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -59,6 +61,49 @@ func (i *ItBit) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (i *ItBit) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(i.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := i.GetOrderbook(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + price, err := strconv.ParseFloat(data[0], 64) + if err != nil { + log.Println(err) + } + amount, err := strconv.ParseFloat(data[1], 64) + if err != nil { + log.Println(err) + } + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{Amount: amount, Price: price}) + } + + for x, _ := range orderbookNew.Asks { + data := orderbookNew.Asks[x] + price, err := strconv.ParseFloat(data[0], 64) + if err != nil { + log.Println(err) + } + amount, err := strconv.ParseFloat(data[1], 64) + if err != nil { + log.Println(err) + } + 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) + return orderBook, nil +} + //TODO Get current holdings from ItBit //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the ItBit exchange func (e *ItBit) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { diff --git a/exchanges/kraken/kraken_wrapper.go b/exchanges/kraken/kraken_wrapper.go index 43c31214..4b747148 100644 --- a/exchanges/kraken/kraken_wrapper.go +++ b/exchanges/kraken/kraken_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -64,6 +65,10 @@ func (k *Kraken) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (k *Kraken) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + return orderbook.OrderbookBase{}, nil +} + //TODO: Retrieve Kraken info //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Kraken exchange func (e *Kraken) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { diff --git a/exchanges/lakebtc/lakebtc_wrapper.go b/exchanges/lakebtc/lakebtc_wrapper.go index 555b2617..ca70857a 100644 --- a/exchanges/lakebtc/lakebtc_wrapper.go +++ b/exchanges/lakebtc/lakebtc_wrapper.go @@ -7,6 +7,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -63,6 +64,32 @@ func (l *LakeBTC) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, nil } +func (l *LakeBTC) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + ob, err := orderbook.GetOrderbook(l.GetName(), currency[0:3], currency[3:]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := l.GetOrderBook(currency) + 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}) + } + + for x, _ := range orderbookNew.Asks { + 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) + return orderBook, nil +} + func (l *LakeBTC) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo response.ExchangeName = l.GetName() diff --git a/exchanges/liqui/liqui_wrapper.go b/exchanges/liqui/liqui_wrapper.go index 2e6b5975..dd1f5aa0 100644 --- a/exchanges/liqui/liqui_wrapper.go +++ b/exchanges/liqui/liqui_wrapper.go @@ -7,6 +7,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -79,6 +80,34 @@ func (l *Liqui) GetTickerPrice(currency string) (ticker.TickerPrice, error) { 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]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := l.GetDepth(currency) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = currencies[0] + orderBook.SecondCurrency = currencies[1] + orderbook.ProcessOrderbook(l.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Liqui exchange func (e *Liqui) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/localbitcoins/localbitcoins_wrapper.go b/exchanges/localbitcoins/localbitcoins_wrapper.go index 36b3981d..c0ec30ad 100644 --- a/exchanges/localbitcoins/localbitcoins_wrapper.go +++ b/exchanges/localbitcoins/localbitcoins_wrapper.go @@ -5,6 +5,7 @@ import ( "time" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -58,6 +59,10 @@ func (l *LocalBitcoins) GetTickerPrice(currency string) (ticker.TickerPrice, err return tickerPrice, nil } +func (l *LocalBitcoins) GetOrderbookEx(currency string) (orderbook.OrderbookBase, error) { + return orderbook.OrderbookBase{}, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the LocalBitcoins exchange func (e *LocalBitcoins) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo diff --git a/exchanges/okcoin/okcoin_wrapper.go b/exchanges/okcoin/okcoin_wrapper.go index e680ea69..7f64f456 100644 --- a/exchanges/okcoin/okcoin_wrapper.go +++ b/exchanges/okcoin/okcoin_wrapper.go @@ -7,6 +7,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/currency" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -72,7 +73,7 @@ func (o *OKCoin) Run() { } func (o *OKCoin) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(o.GetName(), currency[0:3], currency[3:]) + tickerNew, err := ticker.GetTicker(o.GetName(), common.StringToUpper(currency[0:3]), common.StringToUpper(currency[4:])) if err == nil { return tickerNew, nil } @@ -94,6 +95,33 @@ func (o *OKCoin) GetTickerPrice(currency string) (ticker.TickerPrice, error) { 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:])) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := o.GetOrderBook(currency, 200, false) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = common.StringToUpper(currency[0:3]) + orderBook.SecondCurrency = common.StringToUpper(currency[4:]) + orderbook.ProcessOrderbook(o.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} + func (e *OKCoin) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo response.ExchangeName = e.GetName() diff --git a/exchanges/orderbook/orderbook.go b/exchanges/orderbook/orderbook.go new file mode 100644 index 00000000..29d3bf9e --- /dev/null +++ b/exchanges/orderbook/orderbook.go @@ -0,0 +1,146 @@ +package orderbook + +import ( + "errors" + "time" +) + +var ( + ErrOrderbookForExchangeNotFound = "Ticker for exchange does not exist." + ErrPrimaryCurrencyNotFound = "Error primary currency for orderbook not found." + ErrSecondaryCurrencyNotFound = "Error secondary currency for orderbook not found." + + Orderbooks []Orderbook +) + +type OrderbookItem struct { + Amount float64 + Price float64 +} + +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"` +} + +type Orderbook struct { + Orderbook map[string]map[string]OrderbookBase + ExchangeName string +} + +func (o *OrderbookBase) CalculateTotalBids() (float64, float64) { + amountCollated := float64(0) + total := float64(0) + for _, x := range o.Bids { + amountCollated += x.Amount + total += x.Amount * x.Price + } + return amountCollated, total +} + +func (o *OrderbookBase) CalculateTotalAsks() (float64, float64) { + amountCollated := float64(0) + total := float64(0) + for _, x := range o.Asks { + amountCollated += x.Amount + total += x.Amount * x.Price + } + return amountCollated, total +} + +func (o *OrderbookBase) Update(Bids, Asks []OrderbookItem) { + o.Bids = Bids + o.Asks = Asks + o.LastUpdated = time.Now() +} + +func GetOrderbook(exchange, firstCurrency, secondCurrency string) (OrderbookBase, error) { + orderbook, err := GetOrderbookByExchange(exchange) + if err != nil { + return OrderbookBase{}, err + } + + if !FirstCurrencyExists(exchange, firstCurrency) { + return OrderbookBase{}, errors.New(ErrPrimaryCurrencyNotFound) + } + + if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) { + return OrderbookBase{}, errors.New(ErrSecondaryCurrencyNotFound) + } + + return orderbook.Orderbook[firstCurrency][secondCurrency], nil +} + +func GetOrderbookByExchange(exchange string) (*Orderbook, error) { + for _, y := range Orderbooks { + if y.ExchangeName == exchange { + return &y, nil + } + } + return nil, errors.New(ErrOrderbookForExchangeNotFound) +} + +func FirstCurrencyExists(exchange, currency string) bool { + for _, y := range Orderbooks { + if y.ExchangeName == exchange { + if _, ok := y.Orderbook[currency]; ok { + return true + } + } + } + return false +} + +func SecondCurrencyExists(exchange, primary, secondary string) bool { + for _, y := range Orderbooks { + if y.ExchangeName == exchange { + if _, ok := y.Orderbook[primary]; ok { + if _, ok := y.Orderbook[primary][secondary]; ok { + return true + } + } + } + } + return false +} + +func CreateNewOrderbook(exchangeName string, firstCurrency, secondCurrency string, 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 + return orderbook +} + +func ProcessOrderbook(exchangeName string, firstCurrency, secondCurrency string, orderbookNew OrderbookBase) { + orderbookNew.CurrencyPair = orderbookNew.FirstCurrency + orderbookNew.SecondCurrency + + if len(Orderbooks) == 0 { + CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew) + return + } else { + orderbook, err := GetOrderbookByExchange(exchangeName) + if err != nil { + CreateNewOrderbook(exchangeName, firstCurrency, secondCurrency, orderbookNew) + return + } + + if FirstCurrencyExists(exchangeName, firstCurrency) { + if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) { + second := orderbook.Orderbook[firstCurrency] + second[secondCurrency] = orderbookNew + orderbook.Orderbook[firstCurrency] = second + return + } + } + sMap := make(map[string]OrderbookBase) + sMap[secondCurrency] = orderbookNew + orderbook.Orderbook[firstCurrency] = sMap + } +} diff --git a/exchanges/poloniex/poloniex.go b/exchanges/poloniex/poloniex.go index 9aa90e13..0cb695af 100644 --- a/exchanges/poloniex/poloniex.go +++ b/exchanges/poloniex/poloniex.go @@ -104,12 +104,7 @@ func (p *Poloniex) GetVolume() (interface{}, error) { return resp, nil } -//TO-DO: add support for individual pair depth fetching -func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (map[string]PoloniexOrderbook, error) { - type Response struct { - Data map[string]PoloniexOrderbook - } - +func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (PoloniexOrderbook, error) { vals := url.Values{} vals.Set("currencyPair", currencyPair) @@ -117,14 +112,29 @@ func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (map[string]Polo vals.Set("depth", strconv.Itoa(depth)) } - resp := Response{} + resp := PoloniexOrderbookResponse{} path := fmt.Sprintf("%s/public?command=returnOrderBook&%s", POLONIEX_API_URL, vals.Encode()) - err := common.SendHTTPGetRequest(path, true, &resp.Data) + err := common.SendHTTPGetRequest(path, true, &resp) if err != nil { - return resp.Data, err + return PoloniexOrderbook{}, err } - return resp.Data, nil + + ob := PoloniexOrderbook{} + for x, _ := range resp.Asks { + data := resp.Asks[x] + price, _ := strconv.ParseFloat(data[0].(string), 64) + amount := data[1].(float64) + ob.Asks = append(ob.Asks, PoloniexOrderbookItem{Price: price, Amount: amount}) + } + + for x, _ := range resp.Bids { + data := resp.Asks[x] + price, _ := strconv.ParseFloat(data[0].(string), 64) + amount := data[1].(float64) + ob.Bids = append(ob.Bids, PoloniexOrderbookItem{Price: price, Amount: amount}) + } + return ob, nil } func (p *Poloniex) GetTradeHistory(currencyPair, start, end string) ([]PoloniexTradeHistory, error) { diff --git a/exchanges/poloniex/poloniex_types.go b/exchanges/poloniex/poloniex_types.go index cb11d039..a293124e 100644 --- a/exchanges/poloniex/poloniex_types.go +++ b/exchanges/poloniex/poloniex_types.go @@ -16,12 +16,22 @@ type PoloniexTicker struct { Low24Hr float64 `json:"low24hr,string"` } -type PoloniexOrderbook struct { +type PoloniexOrderbookResponse struct { Asks [][]interface{} `json:"asks"` Bids [][]interface{} `json:"bids"` IsFrozen string `json:"isFrozen"` } +type PoloniexOrderbookItem struct { + Price float64 + Amount float64 +} + +type PoloniexOrderbook struct { + Asks []PoloniexOrderbookItem `json:"asks"` + Bids []PoloniexOrderbookItem `json:"bids"` +} + type PoloniexTradeHistory struct { GlobalTradeID int64 `json:"globalTradeID"` TradeID int64 `json:"tradeID"` diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 0f2f6574..4962173d 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -6,6 +6,7 @@ import ( "github.com/thrasher-/gocryptotrader/common" "github.com/thrasher-/gocryptotrader/exchanges" + "github.com/thrasher-/gocryptotrader/exchanges/orderbook" "github.com/thrasher-/gocryptotrader/exchanges/stats" "github.com/thrasher-/gocryptotrader/exchanges/ticker" ) @@ -44,7 +45,8 @@ func (p *Poloniex) Run() { } func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { - tickerNew, err := ticker.GetTicker(p.GetName(), currency[0:3], currency[3:]) + currencyPair := common.SplitStrings(currency, "_") + tickerNew, err := ticker.GetTicker(p.GetName(), currencyPair[0], currencyPair[1]) if err == nil { return tickerNew, nil } @@ -55,7 +57,6 @@ func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { return tickerPrice, err } - currencyPair := common.SplitStrings(currency, "_") tickerPrice.FirstCurrency = currencyPair[0] tickerPrice.SecondCurrency = currencyPair[1] tickerPrice.Ask = tick[currency].Last @@ -68,6 +69,34 @@ func (p *Poloniex) GetTickerPrice(currency string) (ticker.TickerPrice, error) { 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]) + if err == nil { + return ob, nil + } + + var orderBook orderbook.OrderbookBase + orderbookNew, err := p.GetOrderbook(currency, 1000) + if err != nil { + return orderBook, err + } + + for x, _ := range orderbookNew.Bids { + data := orderbookNew.Bids[x] + orderBook.Bids = append(orderBook.Bids, orderbook.OrderbookItem{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.FirstCurrency = currencyPair[0] + orderBook.SecondCurrency = currencyPair[1] + orderbook.ProcessOrderbook(p.GetName(), orderBook.FirstCurrency, orderBook.SecondCurrency, orderBook) + return orderBook, nil +} + //GetExchangeAccountInfo : Retrieves balances for all enabled currencies for the Poloniex exchange func (e *Poloniex) GetExchangeAccountInfo() (exchange.ExchangeAccountInfo, error) { var response exchange.ExchangeAccountInfo