-
Notifications
You must be signed in to change notification settings - Fork 2
/
product.go
260 lines (221 loc) · 9.13 KB
/
product.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package coinbasev3
import (
"fmt"
"strings"
)
// GetProduct get information on a single product by product ID.
func (c *ApiClient) GetProduct(productId string) (Product, error) {
u := c.makeV3Url(fmt.Sprintf("/brokerage/products/%s", productId))
var data Product
resp, err := c.httpClient.Get(u)
if err != nil {
return data, err
}
if !resp.IsSuccessState() {
return data, ErrFailedToUnmarshal
}
err = resp.Unmarshal(&data)
if err != nil {
return data, err
}
return data, nil
}
type Product struct {
ProductId string `json:"product_id"`
Price string `json:"price"`
PricePercentageChange24H string `json:"price_percentage_change_24h"`
Volume24H string `json:"volume_24h"`
VolumePercentageChange24H string `json:"volume_percentage_change_24h"`
BaseIncrement string `json:"base_increment"`
QuoteIncrement string `json:"quote_increment"`
QuoteMinSize string `json:"quote_min_size"`
QuoteMaxSize string `json:"quote_max_size"`
BaseMinSize string `json:"base_min_size"`
BaseMaxSize string `json:"base_max_size"`
BaseName string `json:"base_name"`
QuoteName string `json:"quote_name"`
Watched bool `json:"watched"`
IsDisabled bool `json:"is_disabled"`
New bool `json:"new"`
Status string `json:"status"`
CancelOnly bool `json:"cancel_only"`
LimitOnly bool `json:"limit_only"`
PostOnly bool `json:"post_only"`
TradingDisabled bool `json:"trading_disabled"`
AuctionMode bool `json:"auction_mode"`
ProductType string `json:"product_type"`
QuoteCurrencyId string `json:"quote_currency_id"`
BaseCurrencyId string `json:"base_currency_id"`
FcmTradingSessionDetails FcmTradingSessionDetails `json:"fcm_trading_session_details"`
MidMarketPrice string `json:"mid_market_price"`
Alias string `json:"alias"`
AliasTo []string `json:"alias_to"`
BaseDisplaySymbol string `json:"base_display_symbol"`
QuoteDisplaySymbol string `json:"quote_display_symbol"`
ViewOnly bool `json:"view_only"`
PriceIncrement string `json:"price_increment"`
FutureProductDetails FutureProductDetails `json:"future_product_details"`
}
type FcmTradingSessionDetails struct {
IsSessionOpen string `json:"is_session_open"`
OpenTime string `json:"open_time"`
CloseTime string `json:"close_time"`
}
type FutureProductDetails struct {
Venue string `json:"venue"`
ContractCode string `json:"contract_code"`
ContractExpiry string `json:"contract_expiry"`
ContractSize string `json:"contract_size"`
ContractRootUnit string `json:"contract_root_unit"`
GroupDescription string `json:"group_description"`
ContractExpiryTimezone string `json:"contract_expiry_timezone"`
GroupShortDescription string `json:"group_short_description"`
RiskManagedBy string `json:"risk_managed_by"`
ContractExpiryType string `json:"contract_expiry_type"`
PerpetualDetails PerpetualDetails `json:"perpetual_details"`
ContractDisplayName string `json:"contract_display_name"`
}
type PerpetualDetails struct {
OpenInterest string `json:"open_interest"`
FundingRate string `json:"funding_rate"`
FundingTime string `json:"funding_time"`
}
// GetProducts gets a list of available currency pairs for trading.
func (c *ApiClient) GetProducts() ([]Products, error) {
u := c.makeExchangeUrl("/products")
var data []Products
resp, err := c.httpClient.Get(u)
if err != nil {
return nil, err
}
if !resp.IsSuccessState() {
return nil, ErrFailedToUnmarshal
}
err = resp.Unmarshal(&data)
if err != nil {
return nil, err
}
return data, nil
}
type Products struct {
Id string `json:"id"`
BaseCurrency string `json:"base_currency"`
QuoteCurrency string `json:"quote_currency"`
QuoteIncrement string `json:"quote_increment"`
BaseIncrement string `json:"base_increment"`
DisplayName string `json:"display_name"`
MinMarketFunds string `json:"min_market_funds"`
MarginEnabled bool `json:"margin_enabled"`
PostOnly bool `json:"post_only"`
LimitOnly bool `json:"limit_only"`
CancelOnly bool `json:"cancel_only"`
Status string `json:"status"`
StatusMessage string `json:"status_message"`
TradingDisabled bool `json:"trading_disabled"`
FxStablecoin bool `json:"fx_stablecoin"`
MaxSlippagePercentage string `json:"max_slippage_percentage"`
AuctionMode bool `json:"auction_mode"`
HighBidLimitPercentage string `json:"high_bid_limit_percentage"`
}
type Granularity string
const (
GranularityUnknown Granularity = "UNKNOWN_GRANULARITY"
GranularityOneMin Granularity = "ONE_MINUTE"
GranularityFiveMin Granularity = "FIVE_MINUTE"
GranularityFifteenMin Granularity = "FIFTEEN_MINUTE"
GranularityThirtyMin Granularity = "THIRTY_MINUTE"
GranularityOneHour Granularity = "ONE_HOUR"
GranularityTwoHour Granularity = "TWO_HOUR"
GranularitySixHour Granularity = "SIX_HOUR"
GranularityOneDay Granularity = "ONE_DAY"
)
// GetProductCandles get rates for a single product by product ID, grouped in buckets.
func (c *ApiClient) GetProductCandles(productId, start, end string, granularity Granularity) ([]ProductCandles, error) {
u := c.makeV3Url(fmt.Sprintf("/brokerage/products/%s/candles?start=%s&end=%s&granularity=%s", productId, start, end, granularity))
var data ProductCandlesData
resp, err := c.httpClient.Get(u)
if err != nil {
return data.Candles, err
}
if !resp.IsSuccessState() {
return data.Candles, ErrFailedToUnmarshal
}
err = resp.Unmarshal(&data)
if err != nil {
return data.Candles, err
}
return data.Candles, nil
}
type ProductCandlesData struct {
Candles []ProductCandles `json:"candles"`
}
type ProductCandles struct {
Start string `json:"start"`
Low string `json:"low"`
High string `json:"high"`
Open string `json:"open"`
Close string `json:"close"`
Volume string `json:"volume"`
}
// GetMarketTrades get snapshot information, by product ID, about the last trades (ticks), best bid/ask, and 24h volume.
func (c *ApiClient) GetMarketTrades(productId string, limit int32) (MarketTradesData, error) {
u := c.makeV3Url(fmt.Sprintf("/brokerage/products/%s/ticker?limit=%d", productId, limit))
var data MarketTradesData
resp, err := c.httpClient.Get(u)
if err != nil {
return data, err
}
if !resp.IsSuccessState() {
return data, ErrFailedToUnmarshal
}
err = resp.Unmarshal(&data)
if err != nil {
return MarketTradesData{}, err
}
return data, nil
}
type MarketTradesData struct {
Trades []MarketTrade `json:"trades"`
BestBid string `json:"best_bid"`
BestAsk string `json:"best_ask"`
}
// GetProductBook get a list of bids/asks for a single product. The amount of detail shown can be customized with the limit parameter.
func (c *ApiClient) GetProductBook(productId string, limit int32) (ProductBookData, error) {
u := c.makeV3Url(fmt.Sprintf("/brokerage/product_book?product_id=%s&limit=%d", productId, limit))
var data ProductBookData
if res, err := c.get(u, &data); err != nil {
return data, newResponseError(res)
}
return data, nil
}
type ProductBookData struct {
PriceBook PriceBook `json:"pricebook"`
}
type PriceBook struct {
ProductId string `json:"product_id"`
Bids []PriceBookOrder `json:"bids"`
Asks []PriceBookOrder `json:"asks"`
Time string `json:"time"`
}
type PriceBookOrder struct {
Price string `json:"price"`
Size string `json:"size"`
}
// GetBestBidAsk get the best bid/ask for all products. A subset of all products can be returned instead by using the product_ids input.
func (c *ApiClient) GetBestBidAsk(productIds []string) (BestBidAskData, error) {
query := strings.Join(productIds, "&product_ids=")
if query != "" {
query = "product_ids=" + query
} else {
return BestBidAskData{}, fmt.Errorf("no product ids provided")
}
u := c.makeV3Url(fmt.Sprintf("/brokerage/best_bid_ask?%s", query))
var data BestBidAskData
if res, err := c.get(u, &data); err != nil {
return data, newResponseError(res)
}
return data, nil
}
type BestBidAskData struct {
PriceBooks []PriceBook `json:"pricebooks"`
}