github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/rest/candles.go (about)

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"path"
     7  	"strconv"
     8  	"strings"
     9  
    10  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
    11  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/candle"
    12  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
    13  )
    14  
    15  // CandleService manages the Candles endpoint.
    16  type CandleService struct {
    17  	Synchronous
    18  }
    19  
    20  func getPathSegments(symbol string, resolution common.CandleResolution) (s string, err error) {
    21  	if len(symbol) == 0 {
    22  		err = fmt.Errorf("symbol cannot be empty")
    23  		return
    24  	}
    25  
    26  	segments := []string{"trade", string(resolution), symbol}
    27  	s = strings.Join(segments, ":")
    28  	return
    29  }
    30  
    31  // Last - retrieve the last candle for the given symbol with the given resolution
    32  // See https://docs.bitfinex.com/reference#rest-public-candles for more info
    33  func (c *CandleService) Last(symbol string, resolution common.CandleResolution) (*candle.Candle, error) {
    34  	segments, err := getPathSegments(symbol, resolution)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	req := NewRequestWithMethod(path.Join("candles", segments, "LAST"), "GET")
    40  	raw, err := c.Request(req)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	cs, err := candle.FromRaw(symbol, resolution, raw)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	return cs, nil
    51  }
    52  
    53  // History - retrieves all candles (Max=1000) with the given symbol and the given candle resolution
    54  // See https://docs.bitfinex.com/reference#rest-public-candles for more info
    55  func (c *CandleService) History(symbol string, resolution common.CandleResolution) (*candle.Snapshot, error) {
    56  	segments, err := getPathSegments(symbol, resolution)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	req := NewRequestWithMethod(path.Join("candles", segments, "HIST"), "GET")
    62  	raw, err := c.Request(req)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	cs, err := candle.SnapshotFromRaw(symbol, resolution, convert.ToInterfaceArray(raw))
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	return cs, nil
    73  }
    74  
    75  // HistoryWithQuery - retrieves all candles (Max=1000) that fit the given query criteria
    76  // See https://docs.bitfinex.com/reference#rest-public-candles for more info
    77  func (c *CandleService) HistoryWithQuery(
    78  	symbol string,
    79  	resolution common.CandleResolution,
    80  	start common.Mts,
    81  	end common.Mts,
    82  	limit common.QueryLimit,
    83  	sort common.SortOrder,
    84  ) (*candle.Snapshot, error) {
    85  	segments, err := getPathSegments(symbol, resolution)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  
    90  	req := NewRequestWithMethod(path.Join("candles", segments, "HIST"), "GET")
    91  	req.Params = make(url.Values)
    92  	req.Params.Add("end", strconv.FormatInt(int64(end), 10))
    93  	req.Params.Add("start", strconv.FormatInt(int64(start), 10))
    94  	req.Params.Add("limit", strconv.FormatInt(int64(limit), 10))
    95  	req.Params.Add("sort", strconv.FormatInt(int64(sort), 10))
    96  
    97  	raw, err := c.Request(req)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	cs, err := candle.SnapshotFromRaw(symbol, resolution, convert.ToInterfaceArray(raw))
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	return cs, nil
   108  }