github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/position/position.go (about) 1 package position 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/convert" 8 ) 9 10 type Position struct { 11 Id int64 12 Symbol string 13 Status string 14 Amount float64 15 BasePrice float64 16 MarginFunding float64 17 MarginFundingType int64 18 ProfitLoss float64 19 ProfitLossPercentage float64 20 LiquidationPrice float64 21 Leverage float64 22 Flag interface{} 23 MtsCreate int64 24 MtsUpdate int64 25 Type string 26 Collateral float64 27 CollateralMin float64 28 Meta map[string]interface{} 29 } 30 31 type New Position 32 type Update Position 33 type Cancel Position 34 35 type Snapshot struct { 36 Snapshot []*Position 37 } 38 39 func FromRaw(raw []interface{}) (p *Position, err error) { 40 if len(raw) < 20 { 41 return p, fmt.Errorf("data slice too short for position: %#v", raw) 42 } 43 44 p = &Position{ 45 Symbol: convert.SValOrEmpty(raw[0]), 46 Status: convert.SValOrEmpty(raw[1]), 47 Amount: convert.F64ValOrZero(raw[2]), 48 BasePrice: convert.F64ValOrZero(raw[3]), 49 MarginFunding: convert.F64ValOrZero(raw[4]), 50 MarginFundingType: convert.I64ValOrZero(raw[5]), 51 ProfitLoss: convert.F64ValOrZero(raw[6]), 52 ProfitLossPercentage: convert.F64ValOrZero(raw[7]), 53 LiquidationPrice: convert.F64ValOrZero(raw[8]), 54 Leverage: convert.F64ValOrZero(raw[9]), 55 Id: convert.I64ValOrZero(raw[11]), 56 MtsCreate: convert.I64ValOrZero(raw[12]), 57 MtsUpdate: convert.I64ValOrZero(raw[13]), 58 Type: convert.SValOrEmpty(raw[15]), 59 Collateral: convert.F64ValOrZero(raw[17]), 60 CollateralMin: convert.F64ValOrZero(raw[18]), 61 } 62 63 if meta, ok := raw[19].(map[string]interface{}); ok { 64 p.Meta = meta 65 } 66 67 return 68 } 69 70 func NewFromRaw(raw []interface{}) (New, error) { 71 p, err := FromRaw(raw) 72 if err != nil { 73 return New{}, err 74 } 75 p.Type = "pn" 76 return New(*p), nil 77 } 78 79 func UpdateFromRaw(raw []interface{}) (Update, error) { 80 p, err := FromRaw(raw) 81 if err != nil { 82 return Update{}, err 83 } 84 p.Type = "pu" 85 return Update(*p), nil 86 } 87 88 func CancelFromRaw(raw []interface{}) (Cancel, error) { 89 p, err := FromRaw(raw) 90 if err != nil { 91 return Cancel{}, err 92 } 93 p.Type = "pc" 94 return Cancel(*p), nil 95 } 96 97 func SnapshotFromRaw(raw []interface{}) (s *Snapshot, err error) { 98 if len(raw) == 0 { 99 return s, fmt.Errorf("data slice too short for position: %#v", raw) 100 } 101 102 ps := make([]*Position, 0) 103 switch raw[0].(type) { 104 case []interface{}: 105 for _, v := range raw { 106 if l, ok := v.([]interface{}); ok { 107 p, err := FromRaw(l) 108 if err != nil { 109 return s, err 110 } 111 ps = append(ps, p) 112 } 113 } 114 default: 115 return s, fmt.Errorf("not a position snapshot") 116 } 117 s = &Snapshot{Snapshot: ps} 118 119 return 120 } 121 122 type ClaimRequest struct { 123 Id int64 124 } 125 126 func (o *ClaimRequest) ToJSON() ([]byte, error) { 127 aux := struct { 128 Id int64 `json:"id"` 129 }{ 130 Id: o.Id, 131 } 132 return json.Marshal(aux) 133 }