diff --git a/exchanges/poloniex/poloniex.go b/exchanges/poloniex/poloniex.go index a44925f4..3b1b1466 100644 --- a/exchanges/poloniex/poloniex.go +++ b/exchanges/poloniex/poloniex.go @@ -119,48 +119,80 @@ func (p *Poloniex) GetVolume() (interface{}, error) { } // GetOrderbook returns the full orderbook from poloniex -func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (PoloniexOrderbook, error) { +func (p *Poloniex) GetOrderbook(currencyPair string, depth int) (PoloniexOrderbookAll, error) { vals := url.Values{} - vals.Set("currencyPair", currencyPair) if depth != 0 { vals.Set("depth", strconv.Itoa(depth)) } - resp := PoloniexOrderbookResponse{} - path := fmt.Sprintf("%s/public?command=returnOrderBook&%s", poloniexAPIURL, vals.Encode()) - - err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp) - if err != nil { - return PoloniexOrderbook{}, err - } - - if len(resp.Error) != 0 { - log.Println(resp.Error) - return PoloniexOrderbook{}, fmt.Errorf("Poloniex GetOrderbook() error: %s", resp.Error) - } - - ob := PoloniexOrderbook{} - for x := range resp.Asks { - data := resp.Asks[x] - price, err := strconv.ParseFloat(data[0].(string), 64) + oba := PoloniexOrderbookAll{Data: make(map[string]PoloniexOrderbook)} + if currencyPair != "" { + vals.Set("currencyPair", currencyPair) + resp := PoloniexOrderbookResponse{} + path := fmt.Sprintf("%s/public?command=returnOrderBook&%s", poloniexAPIURL, vals.Encode()) + err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp) if err != nil { - return ob, err + return oba, err + } + if len(resp.Error) != 0 { + log.Println(resp.Error) + return oba, fmt.Errorf("Poloniex GetOrderbook() error: %s", resp.Error) + } + ob := PoloniexOrderbook{} + for x := range resp.Asks { + data := resp.Asks[x] + price, err := strconv.ParseFloat(data[0].(string), 64) + if err != nil { + return oba, err + } + amount := data[1].(float64) + ob.Asks = append(ob.Asks, PoloniexOrderbookItem{Price: price, Amount: amount}) } - amount := data[1].(float64) - ob.Asks = append(ob.Asks, PoloniexOrderbookItem{Price: price, Amount: amount}) - } - for x := range resp.Bids { - data := resp.Bids[x] - price, err := strconv.ParseFloat(data[0].(string), 64) - if err != nil { - return ob, err + for x := range resp.Bids { + data := resp.Bids[x] + price, err := strconv.ParseFloat(data[0].(string), 64) + if err != nil { + return oba, err + } + amount := data[1].(float64) + ob.Bids = append(ob.Bids, PoloniexOrderbookItem{Price: price, Amount: amount}) + } + oba.Data[currencyPair] = PoloniexOrderbook{Bids: ob.Bids, Asks: ob.Asks} + } else { + vals.Set("currencyPair", "all") + resp := PoloniexOrderbookResponseAll{} + path := fmt.Sprintf("%s/public?command=returnOrderBook&%s", poloniexAPIURL, vals.Encode()) + err := common.SendHTTPGetRequest(path, true, p.Verbose, &resp.Data) + if err != nil { + return oba, err + } + for currency, orderbook := range resp.Data { + ob := PoloniexOrderbook{} + for x := range orderbook.Asks { + data := orderbook.Asks[x] + price, err := strconv.ParseFloat(data[0].(string), 64) + if err != nil { + return oba, err + } + amount := data[1].(float64) + ob.Asks = append(ob.Asks, PoloniexOrderbookItem{Price: price, Amount: amount}) + } + + for x := range orderbook.Bids { + data := orderbook.Bids[x] + price, err := strconv.ParseFloat(data[0].(string), 64) + if err != nil { + return oba, err + } + amount := data[1].(float64) + ob.Bids = append(ob.Bids, PoloniexOrderbookItem{Price: price, Amount: amount}) + } + oba.Data[currency] = PoloniexOrderbook{Bids: ob.Bids, Asks: ob.Asks} } - amount := data[1].(float64) - ob.Bids = append(ob.Bids, PoloniexOrderbookItem{Price: price, Amount: amount}) } - return ob, nil + return oba, nil } // GetTradeHistory returns trades history from poloniex diff --git a/exchanges/poloniex/poloniex_types.go b/exchanges/poloniex/poloniex_types.go index cb6b597c..6b747d54 100644 --- a/exchanges/poloniex/poloniex_types.go +++ b/exchanges/poloniex/poloniex_types.go @@ -12,6 +12,10 @@ type PoloniexTicker struct { Low24Hr float64 `json:"low24hr,string"` } +type PoloniexOrderbookResponseAll struct { + Data map[string]PoloniexOrderbookResponse +} + type PoloniexOrderbookResponse struct { Asks [][]interface{} `json:"asks"` Bids [][]interface{} `json:"bids"` @@ -24,6 +28,10 @@ type PoloniexOrderbookItem struct { Amount float64 } +type PoloniexOrderbookAll struct { + Data map[string]PoloniexOrderbook +} + type PoloniexOrderbook struct { Asks []PoloniexOrderbookItem `json:"asks"` Bids []PoloniexOrderbookItem `json:"bids"` diff --git a/exchanges/poloniex/poloniex_wrapper.go b/exchanges/poloniex/poloniex_wrapper.go index 3a0fae85..e269562d 100644 --- a/exchanges/poloniex/poloniex_wrapper.go +++ b/exchanges/poloniex/poloniex_wrapper.go @@ -88,22 +88,34 @@ func (p *Poloniex) GetOrderbookEx(currencyPair pair.CurrencyPair, assetType stri // UpdateOrderbook updates and returns the orderbook for a currency pair func (p *Poloniex) UpdateOrderbook(currencyPair pair.CurrencyPair, assetType string) (orderbook.Base, error) { var orderBook orderbook.Base - orderbookNew, err := p.GetOrderbook(exchange.FormatExchangeCurrency(p.GetName(), currencyPair).String(), 1000) + orderbookNew, err := p.GetOrderbook("", 1000) if err != nil { return orderBook, err } - for x := range orderbookNew.Bids { - data := orderbookNew.Bids[x] - orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: data.Amount, Price: data.Price}) - } + for _, x := range p.GetEnabledCurrencies() { + currency := exchange.FormatExchangeCurrency(p.Name, x).String() + data, ok := orderbookNew.Data[currency] + if !ok { + continue + } + orderBook.Pair = x - for x := range orderbookNew.Asks { - data := orderbookNew.Asks[x] - orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: data.Amount, Price: data.Price}) - } + var obItems []orderbook.Item + for y := range data.Bids { + obData := data.Bids[y] + obItems = append(obItems, orderbook.Item{Amount: obData.Amount, Price: obData.Price}) + } - orderbook.ProcessOrderbook(p.GetName(), currencyPair, orderBook, assetType) + orderBook.Bids = obItems + obItems = []orderbook.Item{} + for y := range data.Asks { + obData := data.Asks[y] + obItems = append(obItems, orderbook.Item{Amount: obData.Amount, Price: obData.Price}) + } + orderBook.Asks = obItems + orderbook.ProcessOrderbook(p.Name, x, orderBook, assetType) + } return orderbook.GetOrderbook(p.Name, currencyPair, assetType) }