forked from andrewstuart/go-robinhood
/
positions.go
143 lines (126 loc) · 5.13 KB
/
positions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package robinhood
import (
"context"
"net/url"
)
type Position struct {
Meta
Account string `json:"account"`
AverageBuyPrice float64 `json:"average_buy_price,string"`
Instrument string `json:"instrument"`
IntradayAverageBuyPrice float64 `json:"intraday_average_buy_price,string"`
IntradayQuantity float64 `json:"intraday_quantity,string"`
Quantity float64 `json:"quantity,string"`
SharesHeldForBuys float64 `json:"shares_held_for_buys,string"`
SharesHeldForSells float64 `json:"shares_held_for_sells,string"`
}
// CryptoCurrency represents a sub object listed in CryptoPosition
type CryptoCurrency struct {
BrandColor string `json:"brand_color"`
Code string `json:"code"`
ID string `json:"id"`
Increment float64 `json:"increment,string"`
Name string `json:"name"`
Type string `json:"type"`
}
// CostBases represents the actual cost the robinhood user paid for asset
type CostBases struct {
CurrencyID string `json:"currency_id"`
DirectCostBasis float64 `json:"direct_cost_basis,string"`
DirectQuantity float64 `json:"direct_quantity,string"`
ID string `json:"id"`
}
// CryptoPosition returns all crypto position associated with an account
type CryptoPosition struct {
Meta
AccountID string `json:"account_id"`
ID string `json:"id"`
Currency CryptoCurrency `json:"currency"`
Cost []CostBases `json:"cost_bases"`
Quantity float64 `json:"quantity,string"`
QuantityAvailable float64 `json:"quantity_available,string"`
QuantityHeldForBuy float64 `json:"quantity_held_for_buy,string"`
QuantityHeldForSell float64 `json:"quantity_held_for_sell,string"`
}
type OptionPostion struct {
Chain string `json:"chain"`
AverageOpenPrice string `json:"average_open_price"`
Symbol string `json:"symbol"`
Quantity string `json:"quantity"`
Direction string `json:"direction"`
IntradayDirection string `json:"intraday_direction"`
TradeValueMultiplier string `json:"trade_value_multiplier"`
Account string `json:"account"`
Strategy string `json:"strategy"`
Legs []LegPosition `json:"legs"`
IntradayQuantity string `json:"intraday_quantity"`
UpdatedAt string `json:"updated_at"`
Id string `json:"id"`
IntradayAverageOpenPrice string `json:"intraday_average_open_price"`
CreatedAt string `json:"created_at"`
}
type LegPosition struct {
Id string `json:"id"`
Position string `json:"position"`
PositionType string `json:"position_type"`
Option string `json:"option"`
RatioQuantity string `json:"ratio_quantity"`
ExpirationDate string `json:"expiration_date"`
StrikePrice string `json:"strike_price"`
OptionType string `json:"option_type"`
}
type Unknown interface{}
// GetPositions returns all the positions associated with an account.
func (c *Client) GetOptionPositions(ctx context.Context) ([]OptionPostion, error) {
return c.GetOptionPositionsParams(ctx, PositionParams{NonZero: true})
}
// GetPositions returns all the positions associated with an account.
func (c *Client) GetPositions(ctx context.Context) ([]Position, error) {
return c.GetPositionsParams(ctx, PositionParams{NonZero: true})
}
// PositionParams encapsulates parameters known to the RobinHood positions API
// endpoint.
type PositionParams struct {
NonZero bool
}
// Encode returns the query string associated with the requested parameters
func (p PositionParams) encode() string {
v := url.Values{}
if p.NonZero {
v.Set("nonzero", "true")
}
return v.Encode()
}
// GetPositionsParams returns all the positions associated with a count, but
// passes the encoded PositionsParams object along to the RobinHood API as part
// of the query string.
func (c *Client) GetPositionsParams(ctx context.Context, p PositionParams) ([]Position, error) {
u, err := url.Parse(EPPositions)
if err != nil {
return nil, err
}
u.RawQuery = p.encode()
var r struct{ Results []Position }
return r.Results, c.GetAndDecode(ctx, u.String(), &r)
}
// GetPositionsParams returns all the positions associated with a count, but
// passes the encoded PositionsParams object along to the RobinHood API as part
// of the query string.
func (c *Client) GetOptionPositionsParams(ctx context.Context, p PositionParams) ([]OptionPostion, error) {
u, err := url.Parse(EPOptions + "aggregate_positions/")
if err != nil {
return nil, err
}
u.RawQuery = p.encode()
var r struct{ Results []OptionPostion }
return r.Results, c.GetAndDecode(ctx, u.String(), &r)
}
// GetCryptoPositions returns all positions associated with the account
func (c *Client) GetCryptoPositions(ctx context.Context) ([]CryptoPosition, error) {
var r struct{ Results []CryptoPosition }
err := c.GetAndDecode(ctx, EPCryptoHoldings, &r)
if err != nil {
return nil, err
}
return r.Results, err
}