mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Fetch Poloniex orderbook in batches
This commit is contained in:
committed by
Adrian Gallagher
parent
747640cd90
commit
5081a4b0c5
@@ -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
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user