forked from jyap808/go-poloniex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poloniex.go
280 lines (245 loc) · 7.48 KB
/
poloniex.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Package Poloniex is an implementation of the Poloniex API in Golang.
package poloniex
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
)
const (
API_BASE = "https://poloniex.com" // Poloniex API endpoint
)
// New returns an instantiated poloniex struct
func New(apiKey, apiSecret string) *Poloniex {
client := NewClient(apiKey, apiSecret)
return &Poloniex{client}
}
// New returns an instantiated poloniex struct with custom timeout
func NewWithCustomTimeout(apiKey, apiSecret string, timeout time.Duration) *Poloniex {
client := NewClientWithCustomTimeout(apiKey, apiSecret, timeout)
return &Poloniex{client}
}
// poloniex represent a poloniex client
type Poloniex struct {
client *client
}
// set enable/disable http request/response dump
func (c *Poloniex) SetDebug(enable bool) {
c.client.debug = enable
}
// GetTickers is used to get the ticker for all markets
func (b *Poloniex) GetTickers() (tickers map[string]Ticker, err error) {
r, err := b.client.do("GET", "public?command=returnTicker", nil, false)
if err != nil {
return
}
if err = json.Unmarshal(r, &tickers); err != nil {
return
}
return
}
// GetVolumes is used to get the volume for all markets
func (b *Poloniex) GetVolumes() (vc VolumeCollection, err error) {
r, err := b.client.do("GET", "public?command=return24hVolume", nil, false)
if err != nil {
return
}
if err = json.Unmarshal(r, &vc); err != nil {
return
}
return
}
func (b *Poloniex) GetCurrencies() (currencies Currencies, err error) {
r, err := b.client.do("GET", "public?command=returnCurrencies", nil, false)
if err != nil {
return
}
if err = json.Unmarshal(r, ¤cies.Pair); err != nil {
return
}
return
}
// GetOrderBook is used to get retrieve the orderbook for a given market
// market: a string literal for the market (ex: BTC_NXT). 'all' not implemented.
// cat: bid, ask or both to identify the type of orderbook to return.
// depth: how deep of an order book to retrieve
func (b *Poloniex) GetOrderBook(market, cat string, depth int) (orderBook OrderBook, err error) {
// not implemented
if cat != "bid" && cat != "ask" && cat != "both" {
cat = "both"
}
if depth > 100 {
depth = 100
}
if depth < 1 {
depth = 1
}
r, err := b.client.do("GET", fmt.Sprintf("public?command=returnOrderBook¤cyPair=%s&depth=%d", strings.ToUpper(market), depth), nil, false)
if err != nil {
return
}
if err = json.Unmarshal(r, &orderBook); err != nil {
return
}
if orderBook.Error != "" {
err = errors.New(orderBook.Error)
return
}
return
}
// Returns candlestick chart data. Required GET parameters are "currencyPair",
// "period" (candlestick period in seconds; valid values are 300, 900, 1800,
// 7200, 14400, and 86400), "start", and "end". "Start" and "end" are given in
// UNIX timestamp format and used to specify the date range for the data
// returned.
func (b *Poloniex) ChartData(currencyPair string, period int, start, end time.Time) (candles []*CandleStick, err error) {
r, err := b.client.do("GET", fmt.Sprintf(
"public?command=returnChartData¤cyPair=%s&period=%d&start=%d&end=%d",
strings.ToUpper(currencyPair),
period,
start.Unix(),
end.Unix(),
), nil, false)
if err != nil {
return
}
if err = json.Unmarshal(r, &candles); err != nil {
return
}
return
}
func (b *Poloniex) GetBalances() (balances map[string]Balance, err error) {
balances = make(map[string]Balance)
r, err := b.client.doCommand("returnCompleteBalances", nil)
if err != nil {
return
}
if err = json.Unmarshal(r, &balances); err != nil {
return
}
return
}
func (b *Poloniex) GetTradeHistory(pair string, start uint32) (trades map[string][]Trade, err error) {
trades = make(map[string][]Trade)
r, err := b.client.doCommand("returnTradeHistory", map[string]string{"currencyPair": pair, "start": strconv.FormatUint(uint64(start), 10)})
if err != nil {
return
}
if pair == "all" {
if err = json.Unmarshal(r, &trades); err != nil {
return
}
} else {
var pairTrades []Trade
if err = json.Unmarshal(r, &pairTrades); err != nil {
return
}
trades[pair] = pairTrades
}
return
}
type responseDepositsWithdrawals struct {
Deposits []Deposit `json:"deposits"`
Withdrawals []Withdrawal `json:"withdrawals"`
}
func (b *Poloniex) GetDepositsWithdrawals(start uint32, end uint32) (deposits []Deposit, withdrawals []Withdrawal, err error) {
deposits = make([]Deposit, 0)
withdrawals = make([]Withdrawal, 0)
r, err := b.client.doCommand("returnDepositsWithdrawals", map[string]string{"start": strconv.FormatUint(uint64(start), 10), "end": strconv.FormatUint(uint64(end), 10)})
if err != nil {
return
}
var response responseDepositsWithdrawals
if err = json.Unmarshal(r, &response); err != nil {
return
}
return response.Deposits, response.Withdrawals, nil
}
func (b *Poloniex) Buy(pair string, rate float64, amount float64, tradeType string) (TradeOrder, error) {
reqParams := map[string]string{
"currencyPair": pair, "rate": strconv.FormatFloat(rate, 'f', -1, 64),
"amount": strconv.FormatFloat(amount, 'f', -1, 64)}
if tradeType != "" {
reqParams[tradeType] = "1"
}
r, err := b.client.doCommand("buy", reqParams)
if err != nil {
return TradeOrder{}, err
}
var orderResponse TradeOrder
if err = json.Unmarshal(r, &orderResponse); err != nil {
return TradeOrder{}, err
}
if orderResponse.ErrorMessage != "" {
return TradeOrder{}, errors.New(orderResponse.ErrorMessage)
}
return orderResponse, nil
}
func (b *Poloniex) Sell(pair string, rate float64, amount float64, tradeType string) (TradeOrder, error) {
reqParams := map[string]string{
"currencyPair": pair, "rate": strconv.FormatFloat(rate, 'f', -1, 64),
"amount": strconv.FormatFloat(amount, 'f', -1, 64)}
if tradeType != "" {
reqParams[tradeType] = "1"
}
r, err := b.client.doCommand("sell", reqParams)
if err != nil {
return TradeOrder{}, err
}
var orderResponse TradeOrder
if err = json.Unmarshal(r, &orderResponse); err != nil {
return TradeOrder{}, err
}
if orderResponse.ErrorMessage != "" {
return TradeOrder{}, errors.New(orderResponse.ErrorMessage)
}
return orderResponse, nil
}
func (b *Poloniex) GetOpenOrders(pair string) (openOrders map[string][]OpenOrder, err error) {
openOrders = make(map[string][]OpenOrder)
r, err := b.client.doCommand("returnOpenOrders", map[string]string{"currencyPair": pair})
if err != nil {
return
}
if pair == "all" {
if err = json.Unmarshal(r, &openOrders); err != nil {
return
}
} else {
var onePairOrders []OpenOrder
if err = json.Unmarshal(r, &onePairOrders); err != nil {
return
}
openOrders[pair] = onePairOrders
}
return
}
func (b *Poloniex) CancelOrder(orderNumber string) error {
_, err := b.client.doCommand("cancelOrder", map[string]string{"orderNumber": orderNumber})
if err != nil {
return err
}
return nil
}
// Returns whole lending history chart data. Required GET parameters are "start",
// "end" (UNIX timestamp format and used to specify the date range for the data returned)
// and optionally limit (<0 for no limit, poloniex automatically limits to 500 records)
func (b *Poloniex) LendingHistory(start, end time.Time, limit int) (lendings []Lending, err error) {
lendings = make([]Lending, 0)
reqParams := map[string]string{
"start": strconv.FormatUint(uint64(start.Unix()), 10),
"end": strconv.FormatUint(uint64(end.Unix()), 10)}
if limit >= 0 {
reqParams["limit"] = string(limit)
}
r, err := b.client.doCommand("returnLendingHistory", reqParams)
if err != nil {
return
}
if err = json.Unmarshal(r, &lendings); err != nil {
return
}
return
}