forked from amir-the-h/okex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
market.go
207 lines (193 loc) · 6.03 KB
/
market.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
package rest
import (
"encoding/json"
"github.com/amir-the-h/okex"
requests "github.com/amir-the-h/okex/requests/rest/market"
responses "github.com/amir-the-h/okex/responses/market"
"net/http"
)
// Market
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data
type Market struct {
client *ClientRest
}
// NewMarket returns a pointer to a fresh Market
func NewMarket(c *ClientRest) *Market {
return &Market{c}
}
// GetTickers
// Retrieve the latest price snapshot, best bid/ask price, and trading volume in the last 24 hours.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-tickers
func (c *Market) GetTickers(req requests.GetTickers) (response responses.Ticker, err error) {
p := "/api/v5/market/tickers"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetTicker
// Retrieve the latest price snapshot, best bid/ask price, and trading volume in the last 24 hours.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-ticker
func (c *Market) GetTicker(req requests.GetTickers) (response responses.Ticker, err error) {
p := "/api/v5/market/ticker"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetIndexTickers
// Retrieve index tickers.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-index-tickers
func (c *Market) GetIndexTickers(req requests.GetIndexTickers) (response responses.Ticker, err error) {
p := "/api/v5/market/ticker"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetOrderBook
// Retrieve a instrument is order book.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-order-book
func (c *Market) GetOrderBook(req requests.GetOrderBook) (response responses.OrderBook, err error) {
p := "/api/v5/market/books"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetCandlesticks
// Retrieve the candlestick charts. This endpoint can retrieve the latest 1,440 data entries. Charts are returned in groups based on the requested bar.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-candlesticks
func (c *Market) GetCandlesticks(req requests.GetCandlesticks) (response responses.Candle, err error) {
p := "/api/v5/market/candles"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetCandlesticksHistory
// Retrieve history candlestick charts from recent years.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-candlesticks
func (c *Market) GetCandlesticksHistory(req requests.GetCandlesticks) (response responses.Candle, err error) {
p := "/api/v5/market/history-candles"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetIndexCandlesticks
// Retrieve the candlestick charts of the index. This endpoint can retrieve the latest 1,440 data entries. Charts are returned in groups based on the requested bar.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-index-candlesticks
func (c *Market) GetIndexCandlesticks(req requests.GetCandlesticks) (response responses.IndexCandle, err error) {
p := "/api/v5/market/index-candles"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetMarkPriceCandlesticks
// Retrieve the candlestick charts of mark price. This endpoint can retrieve the latest 1,440 data entries. Charts are returned in groups based on the requested bar.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-mark-price-candlesticks
func (c *Market) GetMarkPriceCandlesticks(req requests.GetCandlesticks) (response responses.CandleMarket, err error) {
p := "/api/v5/market/mark-price-candles"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetTrades
// Retrieve the recent transactions of an instrument.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-trades
func (c *Market) GetTrades(req requests.GetTrades) (response responses.Trade, err error) {
p := "/api/v5/market/trades"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// Get24HTotalVolume
// The 24-hour trading volume is calculated on a rolling basis, using USD as the pricing unit.
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-24h-total-volume
func (c *Market) Get24HTotalVolume() (response responses.TotalVolume24H, err error) {
p := "/api/v5/market/platform-24-volume"
res, err := c.client.Do(http.MethodGet, p, false)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}
// GetIndexComponents
// Get the index component information data on the market
//
// https://www.okex.com/docs-v5/en/#rest-api-market-data-get-index-components
func (c *Market) GetIndexComponents(req requests.GetIndexComponents) (response responses.IndexComponent, err error) {
p := "/api/v5/market/index-components"
m := okex.S2M(req)
res, err := c.client.Do(http.MethodGet, p, false, m)
if err != nil {
return
}
defer res.Body.Close()
d := json.NewDecoder(res.Body)
err = d.Decode(&response)
return
}