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

     1  package tradeexecutionupdate
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     7  )
     8  
     9  // TradeExecutionUpdate represents a full update to a trade on the private data feed.  Following a TradeExecution,
    10  // TradeExecutionUpdates include additional details, e.g. the trade's execution ID (TradeID).
    11  type TradeExecutionUpdate struct {
    12  	ID          int64
    13  	Pair        string
    14  	MTS         int64
    15  	OrderID     int64
    16  	ExecAmount  float64
    17  	ExecPrice   float64
    18  	OrderType   string
    19  	OrderPrice  float64
    20  	Maker       int
    21  	Fee         float64
    22  	FeeCurrency string
    23  }
    24  
    25  type Snapshot struct {
    26  	Snapshot []*TradeExecutionUpdate
    27  }
    28  
    29  type HistoricalTradeSnapshot Snapshot
    30  
    31  // public trade update just looks like a trade
    32  func FromRaw(raw []interface{}) (tu *TradeExecutionUpdate, err error) {
    33  	if len(raw) == 4 {
    34  		tu = &TradeExecutionUpdate{
    35  			ID:         convert.I64ValOrZero(raw[0]),
    36  			MTS:        convert.I64ValOrZero(raw[1]),
    37  			ExecAmount: convert.F64ValOrZero(raw[2]),
    38  			ExecPrice:  convert.F64ValOrZero(raw[3]),
    39  		}
    40  		return
    41  	}
    42  	if len(raw) > 10 {
    43  		tu = &TradeExecutionUpdate{
    44  			ID:          convert.I64ValOrZero(raw[0]),
    45  			Pair:        convert.SValOrEmpty(raw[1]),
    46  			MTS:         convert.I64ValOrZero(raw[2]),
    47  			OrderID:     convert.I64ValOrZero(raw[3]),
    48  			ExecAmount:  convert.F64ValOrZero(raw[4]),
    49  			ExecPrice:   convert.F64ValOrZero(raw[5]),
    50  			OrderType:   convert.SValOrEmpty(raw[6]),
    51  			OrderPrice:  convert.F64ValOrZero(raw[7]),
    52  			Maker:       convert.ToInt(raw[8]),
    53  			Fee:         convert.F64ValOrZero(raw[9]),
    54  			FeeCurrency: convert.SValOrEmpty(raw[10]),
    55  		}
    56  		return
    57  	}
    58  	return tu, fmt.Errorf("data slice too short for trade update: %#v", raw)
    59  }
    60  
    61  func SnapshotFromRaw(raw []interface{}) (s *Snapshot, err error) {
    62  	if len(raw) == 0 {
    63  		return nil, fmt.Errorf("data slice is too short for trade execution update snapshot: %#v", raw)
    64  	}
    65  
    66  	ts := make([]*TradeExecutionUpdate, 0)
    67  	for _, v := range raw {
    68  		if l, ok := v.([]interface{}); ok {
    69  			t, err := FromRaw(l)
    70  			if err != nil {
    71  				return s, err
    72  			}
    73  			ts = append(ts, t)
    74  		}
    75  	}
    76  
    77  	s = &Snapshot{Snapshot: ts}
    78  	return
    79  }