github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/ticker/ticker.go (about)

     1  package ticker
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     9  )
    10  
    11  type Ticker struct {
    12  	Symbol          string
    13  	Frr             float64
    14  	Bid             float64
    15  	BidPeriod       int64
    16  	BidSize         float64
    17  	Ask             float64
    18  	AskPeriod       int64
    19  	AskSize         float64
    20  	DailyChange     float64
    21  	DailyChangePerc float64
    22  	LastPrice       float64
    23  	Volume          float64
    24  	High            float64
    25  	Low             float64
    26  	// PLACEHOLDER,
    27  	// PLACEHOLDER,
    28  	FrrAmountAvailable float64
    29  }
    30  
    31  type Update Ticker
    32  type Snapshot struct {
    33  	Snapshot []*Ticker
    34  }
    35  
    36  func SnapshotFromRaw(symbol string, raw [][]interface{}) (*Snapshot, error) {
    37  	if len(raw) == 0 {
    38  		return nil, fmt.Errorf("data slice too short for ticker snapshot: %#v", raw)
    39  	}
    40  
    41  	snap := make([]*Ticker, 0)
    42  	for _, f := range raw {
    43  		c, err := FromRaw(symbol, f)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		snap = append(snap, c)
    48  	}
    49  
    50  	return &Snapshot{Snapshot: snap}, nil
    51  }
    52  
    53  func FromRaw(symbol string, raw []interface{}) (t *Ticker, err error) {
    54  	// trading pair update / snapshot
    55  	if strings.HasPrefix(symbol, "t") && len(raw) >= 10 {
    56  		t = &Ticker{
    57  			Symbol:          symbol,
    58  			Bid:             convert.F64ValOrZero(raw[0]),
    59  			BidSize:         convert.F64ValOrZero(raw[1]),
    60  			Ask:             convert.F64ValOrZero(raw[2]),
    61  			AskSize:         convert.F64ValOrZero(raw[3]),
    62  			DailyChange:     convert.F64ValOrZero(raw[4]),
    63  			DailyChangePerc: convert.F64ValOrZero(raw[5]),
    64  			LastPrice:       convert.F64ValOrZero(raw[6]),
    65  			Volume:          convert.F64ValOrZero(raw[7]),
    66  			High:            convert.F64ValOrZero(raw[8]),
    67  			Low:             convert.F64ValOrZero(raw[9]),
    68  		}
    69  		return
    70  	}
    71  
    72  	// funding pair update
    73  	if strings.HasPrefix(symbol, "f") {
    74  		if len(raw) >= 13 {
    75  			t = &Ticker{
    76  				Symbol:          symbol,
    77  				Frr:             convert.F64ValOrZero(raw[0]),
    78  				Bid:             convert.F64ValOrZero(raw[1]),
    79  				BidPeriod:       convert.I64ValOrZero(raw[2]),
    80  				BidSize:         convert.F64ValOrZero(raw[3]),
    81  				Ask:             convert.F64ValOrZero(raw[4]),
    82  				AskPeriod:       convert.I64ValOrZero(raw[5]),
    83  				AskSize:         convert.F64ValOrZero(raw[6]),
    84  				DailyChange:     convert.F64ValOrZero(raw[7]),
    85  				DailyChangePerc: convert.F64ValOrZero(raw[8]),
    86  				LastPrice:       convert.F64ValOrZero(raw[9]),
    87  				Volume:          convert.F64ValOrZero(raw[10]),
    88  				High:            convert.F64ValOrZero(raw[11]),
    89  				Low:             convert.F64ValOrZero(raw[12]),
    90  			}
    91  		}
    92  
    93  		// funding pair snapshot
    94  		if len(raw) >= 16 {
    95  			t.FrrAmountAvailable = convert.F64ValOrZero(raw[15])
    96  		}
    97  		return
    98  	}
    99  
   100  	err = fmt.Errorf("unrecognized data slice format for pair:%s, data:%#v", symbol, raw)
   101  	return
   102  }
   103  
   104  func FromRestRaw(raw []interface{}) (t *Ticker, err error) {
   105  	if len(raw) == 0 {
   106  		return t, fmt.Errorf("data slice too short for ticker")
   107  	}
   108  
   109  	return FromRaw(raw[0].(string), raw[1:])
   110  }
   111  
   112  // FromWSRaw - based on condition will return snapshot of tickers or single tick
   113  func FromWSRaw(symbol string, data []interface{}) (interface{}, error) {
   114  	if len(data) == 0 {
   115  		return nil, errors.New("empty data slice")
   116  	}
   117  
   118  	_, isSnapshot := data[0].([]interface{})
   119  	if isSnapshot {
   120  		return SnapshotFromRaw(symbol, convert.ToInterfaceArray(data))
   121  	}
   122  
   123  	return FromRaw(symbol, data)
   124  }