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

     1  package rest
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/common"
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/stats"
     9  )
    10  
    11  // StatsService manages the Stats endpoint.
    12  type StatsService struct {
    13  	requestFactory
    14  	Synchronous
    15  }
    16  
    17  func (ss *StatsService) get(symbol string, key common.StatKey, extra string, section string) ([]interface{}, error) {
    18  	var params string
    19  	if extra != "" {
    20  		params = fmt.Sprintf("%s:1m:%s:%s", string(key), symbol, extra)
    21  	} else {
    22  		params = fmt.Sprintf("%s:1m:%s", string(key), symbol)
    23  	}
    24  	req := NewRequestWithMethod(path.Join("stats1", params, section), "GET")
    25  	raw, err := ss.Request(req)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	return raw, nil
    30  }
    31  
    32  func (ss *StatsService) getHistory(symbol string, key common.StatKey, extra string) ([]*stats.Stat, error) {
    33  	raw, err := ss.get(symbol, key, extra, "hist")
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	return stats.SnapshotFromRaw(raw)
    39  }
    40  
    41  func (ss *StatsService) getLast(symbol string, key common.StatKey, extra string) (*stats.Stat, error) {
    42  	raw, err := ss.get(symbol, key, extra, "last")
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	return stats.FromRaw(raw)
    48  }
    49  
    50  // Retrieves platform statistics for funding history
    51  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    52  func (ss *StatsService) FundingHistory(symbol string) ([]*stats.Stat, error) {
    53  	return ss.getHistory(symbol, common.FundingSizeKey, "")
    54  }
    55  
    56  // Retrieves platform statistics for funding last
    57  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    58  func (ss *StatsService) FundingLast(symbol string) (*stats.Stat, error) {
    59  	return ss.getLast(symbol, common.FundingSizeKey, "")
    60  }
    61  
    62  // Retrieves platform statistics for credit size history
    63  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    64  func (ss *StatsService) CreditSizeHistory(symbol string, side common.OrderSide) ([]*stats.Stat, error) {
    65  	return ss.getHistory(symbol, common.CreditSizeKey, "")
    66  }
    67  
    68  // Retrieves platform statistics for credit size last
    69  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    70  func (ss *StatsService) CreditSizeLast(symbol string, side common.OrderSide) (*stats.Stat, error) {
    71  	return ss.getLast(symbol, common.CreditSizeKey, "")
    72  }
    73  
    74  // Retrieves platform statistics for credit size history
    75  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    76  func (ss *StatsService) SymbolCreditSizeHistory(fundingSymbol string, tradingSymbol string) ([]*stats.Stat, error) {
    77  	return ss.getHistory(fundingSymbol, common.CreditSizeSymKey, tradingSymbol)
    78  }
    79  
    80  // Retrieves platform statistics for credit size last
    81  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    82  func (ss *StatsService) SymbolCreditSizeLast(fundingSymbol string, tradingSymbol string) (*stats.Stat, error) {
    83  	return ss.getLast(fundingSymbol, common.CreditSizeSymKey, tradingSymbol)
    84  }
    85  
    86  // Retrieves platform statistics for position history
    87  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
    88  func (ss *StatsService) PositionHistory(symbol string, side common.OrderSide) ([]*stats.Stat, error) {
    89  	var strSide string
    90  	if side == common.Long {
    91  		strSide = "long"
    92  	} else if side == common.Short {
    93  		strSide = "short"
    94  	} else {
    95  		return nil, fmt.Errorf("Unrecognized side %v in PositionHistory", side)
    96  	}
    97  	return ss.getHistory(symbol, common.PositionSizeKey, strSide)
    98  }
    99  
   100  // Retrieves platform statistics for position last
   101  // see https://docs.bitfinex.com/reference#rest-public-stats for more info
   102  func (ss *StatsService) PositionLast(symbol string, side common.OrderSide) (*stats.Stat, error) {
   103  	var strSide string
   104  	if side == common.Long {
   105  		strSide = "long"
   106  	} else if side == common.Short {
   107  		strSide = "short"
   108  	} else {
   109  		return nil, fmt.Errorf("Unrecognized side %v in PositionHistory", side)
   110  	}
   111  	return ss.getLast(symbol, common.PositionSizeKey, strSide)
   112  }