github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v1/order_book.go (about)

     1  package bitfinex
     2  
     3  import (
     4  	"net/url"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  )
     9  
    10  type OrderBookService struct {
    11  	client *Client
    12  }
    13  
    14  type OrderBookEntry struct {
    15  	Price     string
    16  	Rate      string
    17  	Amount    string
    18  	Period    int
    19  	Timestamp string
    20  	Frr       string
    21  }
    22  
    23  type OrderBook struct {
    24  	Bids []OrderBookEntry
    25  	Asks []OrderBookEntry
    26  }
    27  
    28  func (el *OrderBookEntry) ParseTime() (*time.Time, error) {
    29  	i, err := strconv.ParseFloat(el.Timestamp, 64)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	t := time.Unix(int64(i), 0)
    34  	return &t, nil
    35  }
    36  
    37  // GET /book
    38  func (s *OrderBookService) Get(pair string, limitBids, limitAsks int, noGroup bool) (OrderBook, error) {
    39  	pair = strings.ToUpper(pair)
    40  
    41  	params := url.Values{}
    42  	if limitBids != 0 {
    43  		params.Add("limit_bids", strconv.Itoa(limitBids))
    44  	}
    45  	if limitAsks != 0 {
    46  		params.Add("limit_asks", strconv.Itoa(limitAsks))
    47  	}
    48  	if noGroup {
    49  		params.Add("group", "0")
    50  	}
    51  
    52  	req, err := s.client.newRequest("GET", "book/"+pair, params)
    53  
    54  	if err != nil {
    55  		return OrderBook{}, err
    56  	}
    57  
    58  	var v OrderBook
    59  	_, err = s.client.do(req, &v)
    60  
    61  	if err != nil {
    62  		return OrderBook{}, err
    63  	}
    64  
    65  	return v, nil
    66  }