-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtrading.go
390 lines (302 loc) · 8.89 KB
/
trading.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package poloniex
import (
"encoding/json"
"errors"
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
)
func (p *Poloniex) GetBalances() (balances map[string]string, err error) {
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("returnBalances", nil, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &balances)
return
}
type Balance struct {
Available decimal.Decimal `json:"available, string"`
OnOrders decimal.Decimal `json:"onOrders, string"`
BtcValue decimal.Decimal `json:"btcValue, string"`
}
func (p *Poloniex) GetCompleteBalances() (completebalances map[string]Balance, err error) {
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("returnCompleteBalances", nil, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &completebalances)
return
}
type Accounts struct {
Margin map[string]decimal.Decimal `json:"margin"`
Lending map[string]decimal.Decimal `json:"lending"`
Exchange map[string]decimal.Decimal `json:"exchange"`
}
func (p *Poloniex) GetAccountBalances() (accounts Accounts, err error) {
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("returnAvailableAccountBalances", nil, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &accounts)
return
}
func (p *Poloniex) GetDepositAddresses() (depositaddresses map[string]string, err error) {
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("returnDepositAddresses", nil, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &depositaddresses)
return
}
type NewAddress struct {
Success int `json:"success"`
Response string `json:"response"`
}
func (p *Poloniex) GenerateNewAddress(currency string) (newaddress NewAddress, err error) {
respch := make(chan []byte)
errch := make(chan error)
parameters := map[string]string{"currency": strings.ToUpper(currency)}
go p.tradingRequest("generateNewAddress", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &newaddress)
return
}
type OpenOrder struct {
OrderNumber string `json:"orderNumber"`
Type string `json:"type"`
Price decimal.Decimal `json:"rate, string"`
StartingAmount decimal.Decimal `json:"startingAmount, string"`
Amount decimal.Decimal `json:"amount, string"`
Total decimal.Decimal `json:"total, string"`
Date string `json:"date"`
Margin int `json:"margin"`
}
// Send market to get open orders.
func (p *Poloniex) GetOpenOrders(market string) (openorders []OpenOrder, err error) {
respch := make(chan []byte)
errch := make(chan error)
parameters := map[string]string{"currencyPair": strings.ToUpper(market)}
go p.tradingRequest("returnOpenOrders", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &openorders)
return
}
// This method returns all open orders.
func (p *Poloniex) GetAllOpenOrders() (openorders map[string][]OpenOrder, err error) {
respch := make(chan []byte)
errch := make(chan error)
parameters := map[string]string{"currencyPair": "all"}
go p.tradingRequest("returnOpenOrders", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &openorders)
if err != nil {
return
}
for k, v := range openorders {
if len(v) == 0 {
delete(openorders, k)
}
}
return
}
type CancelOrder struct {
Success int `json:"success"`
}
func (p *Poloniex) CancelOrder(orderNumber string) (cancelorder CancelOrder, err error) {
respch := make(chan []byte)
errch := make(chan error)
parameters := map[string]string{"orderNumber": orderNumber}
go p.tradingRequest("cancelOrder", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &cancelorder)
return
}
type TradeHistory struct {
GlobalTradeID int `json:"globalTradeId"`
TradeID string `json:"tradeId"`
Date string `json:"date"`
Price decimal.Decimal `json:"rate, string"`
Amount decimal.Decimal `json:"amount, string"`
Total decimal.Decimal `json:"total, string"`
Fee decimal.Decimal `json:"fee,string"`
OrderNumber decimal.Decimal `json:"orderNumber,string"`
Type string `json:"type"`
Category string `json:"category"`
}
func (p *Poloniex) GetTradeHistory(market string, start, end time.Time, limit int) (tradehistory []TradeHistory, err error) {
parameters := map[string]string{
"currencyPair": strings.ToUpper(market),
"start": strconv.FormatInt(start.Unix(), 10),
"end": strconv.FormatInt(end.Unix(), 10),
"limit": strconv.Itoa(limit),
}
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("returnTradeHistory", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &tradehistory)
return
}
type OrderTrade struct {
GlobalTradeID decimal.Decimal `json:"globalTradeId"`
TradeID decimal.Decimal `json:"tradeId"`
Market string `json:"currencyPair"`
Type string `json:"type"`
Price decimal.Decimal `json:"rate"`
Amount decimal.Decimal `json:"amount"`
Total decimal.Decimal `json:"total"`
Fee decimal.Decimal `json:"fee"`
Date string `json:"date"`
}
func (p *Poloniex) GetTradesByOrderID(orderNumber string) (ordertrades []OrderTrade, err error) {
respch := make(chan []byte)
errch := make(chan error)
parameters := map[string]string{"orderNumber": orderNumber}
go p.tradingRequest("returnOrderTrades", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &ordertrades)
return
}
type OrderStat struct {
Status string `json:"status"`
Rate decimal.Decimal `json:"rate"`
Amount decimal.Decimal `json:"amount"`
CurrencyPair string `json:"currencyPair"`
Date string `json:"date"`
Total decimal.Decimal `json:"total"`
Type string `json:"type"`
StartingAmount decimal.Decimal `json:"startingAmount"`
}
// error result
type OrderStat1 struct {
Success int `json:"success"`
Result struct {
Error string `json:"error"`
} `json:"result"`
}
// success result
type OrderStat2 struct {
Success int `json:"success"`
Result map[string]OrderStat `json:"result"`
}
func (p *Poloniex) GetOrderStat(orderNumber string) (orderstat OrderStat, err error) {
var check1 OrderStat1
var check2 OrderStat2
respch := make(chan []byte)
errch := make(chan error)
parameters := map[string]string{"orderNumber": orderNumber}
go p.tradingRequest("returnOrderStatus", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
// check error
err = json.Unmarshal(resp, &check1)
if err != nil {
return
}
if check1.Success == 0 && len(check1.Result.Error) > 0 {
err = errors.New(check1.Result.Error)
return
}
// check success
err = json.Unmarshal(resp, &check2)
if err != nil {
return
}
if check2.Success == 1 {
orderstat = check2.Result[orderNumber]
return
}
err = errors.New("Unexpected Result!")
return
}
type ResultTrades struct {
Amount decimal.Decimal `json:"amount"`
Date string `json:"date"`
Rate decimal.Decimal `json:"rate"`
Total decimal.Decimal `json:"total"`
TradeID decimal.Decimal `json:"tradeId"`
Type string `json:"type"`
}
type Buy struct {
OrderNumber string `json:"orderNumber"`
ResultingTrades []ResultTrades
}
func (p *Poloniex) Buy(market string, price, amount float64) (buy Buy, err error) {
parameters := map[string]string{
"currencyPair": strings.ToUpper(market),
"rate": strconv.FormatFloat(float64(price), 'f', 8, 64),
"amount": strconv.FormatFloat(float64(amount), 'f', 8, 64),
}
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("buy", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &buy)
return
}
type Sell Buy
func (p *Poloniex) Sell(market string, price, amount float64) (sell Sell, err error) {
parameters := map[string]string{
"currencyPair": strings.ToUpper(market),
"rate": strconv.FormatFloat(float64(price), 'f', 8, 64),
"amount": strconv.FormatFloat(float64(amount), 'f', 8, 64),
}
respch := make(chan []byte)
errch := make(chan error)
go p.tradingRequest("sell", parameters, respch, errch)
resp := <-respch
err = <-errch
if err != nil {
return
}
err = json.Unmarshal(resp, &sell)
return
}