This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathoanda.v20.go
557 lines (534 loc) · 18 KB
/
oanda.v20.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/bitly/go-simplejson"
"github.com/miaolz123/conver"
"github.com/miaolz123/samaritan/constant"
"github.com/miaolz123/samaritan/model"
)
// OandaV20 the exchange struct of oanda.com v20
type OandaV20 struct {
stockTypeMap map[string][2]string
tradeTypeMap map[string]string
tradeTypeAntiMap map[int]string
tradeTypeLogMap map[string]string
contractTypeAntiMap map[string]string
leverageMap map[string]string
recordsPeriodMap map[string]string
minAmountMap map[string]float64
records map[string][]Record
host string
logger model.Logger
option Option
limit float64
lastSleep int64
lastTimes int64
}
// NewOandaV20 create an exchange struct of okcoin.cn
func NewOandaV20(opt Option) Exchange {
return &OandaV20{
stockTypeMap: map[string][2]string{
"BTC.WEEK/USD": {"btc_usd", "this_week"},
"BTC.WEEK2/USD": {"btc_usd", "next_week"},
"BTC.MONTH3/USD": {"btc_usd", "quarter"},
"LTC.WEEK/USD": {"ltc_usd", "this_week"},
"LTC.WEEK2/USD": {"ltc_usd", "next_week"},
"LTC.MONTH3/USD": {"ltc_usd", "quarter"},
},
tradeTypeMap: map[string]string{
constant.TradeTypeLong: "1",
constant.TradeTypeShort: "2",
constant.TradeTypeLongClose: "3",
constant.TradeTypeShortClose: "4",
},
tradeTypeAntiMap: map[int]string{
1: constant.TradeTypeLong,
2: constant.TradeTypeShort,
3: constant.TradeTypeLongClose,
4: constant.TradeTypeShortClose,
},
tradeTypeLogMap: map[string]string{
constant.TradeTypeLong: constant.LONG,
constant.TradeTypeShort: constant.SHORT,
constant.TradeTypeLongClose: constant.LONGCLOSE,
constant.TradeTypeShortClose: constant.SHORTCLOSE,
},
contractTypeAntiMap: map[string]string{
"this_week": "WEEK",
"next_week": "WEEK2",
"quarter": "MONTH3",
},
leverageMap: map[string]string{
"10": "10",
"20": "20",
},
recordsPeriodMap: map[string]string{
"M": "1min",
"M3": "3min",
"M5": "5min",
"M15": "15min",
"M30": "30min",
"H": "1hour",
"H2": "2hour",
"H4": "4hour",
"H6": "6hour",
"H12": "12hour",
"D": "1day",
"D3": "3day",
"W": "1week",
},
minAmountMap: map[string]float64{
"BTC/CNY": 0.01,
"LTC/CNY": 0.1,
},
records: make(map[string][]Record),
host: "https://api-fxtrade.oanda.com",
logger: model.Logger{TraderID: opt.TraderID, ExchangeType: opt.Type},
option: opt,
limit: 10.0,
lastSleep: time.Now().UnixNano(),
}
}
// Log print something to console
func (e *OandaV20) Log(msgs ...interface{}) {
e.logger.Log(constant.INFO, "", 0.0, 0.0, msgs...)
}
// GetType get the type of this exchange
func (e *OandaV20) GetType() string {
return e.option.Type
}
// GetName get the name of this exchange
func (e *OandaV20) GetName() string {
return e.option.Name
}
// SetLimit set the limit calls amount per second of this exchange
func (e *OandaV20) SetLimit(times interface{}) float64 {
e.limit = conver.Float64Must(times)
return e.limit
}
// AutoSleep auto sleep to achieve the limit calls amount per second of this exchange
func (e *OandaV20) AutoSleep() {
now := time.Now().UnixNano()
interval := 1e+9/e.limit*conver.Float64Must(e.lastTimes) - conver.Float64Must(now-e.lastSleep)
if interval > 0.0 {
time.Sleep(time.Duration(conver.Int64Must(interval)))
}
e.lastTimes = 0
e.lastSleep = now
}
// GetMinAmount get the min trade amonut of this exchange
func (e *OandaV20) GetMinAmount(stock string) float64 {
return e.minAmountMap[stock]
}
func (e *OandaV20) getAuthJSON(method, url string, body interface{}) (statusCode int, resp *simplejson.Json, err error) {
e.lastTimes++
bs := []byte{}
if body != nil {
if bs, err = json.Marshal(body); err != nil {
err = fmt.Errorf("[%s %s] HTTP Error Info: %v", method, url, err)
return
}
}
req, err := http.NewRequest(method, e.host+url, bytes.NewReader(bs))
if err != nil {
err = fmt.Errorf("[%s %s] HTTP Error Info: %v", method, url, err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+e.option.SecretKey)
response, err := client.Do(req)
rets := []byte{}
if response == nil {
err = fmt.Errorf("[%s %s] HTTP Error", method, url)
} else {
statusCode = response.StatusCode
rets, _ = ioutil.ReadAll(response.Body)
response.Body.Close()
}
resp, err = simplejson.NewJson(rets)
return
}
// GetAccount get the account detail of this exchange
func (e *OandaV20) GetAccount() interface{} {
statusCode, json, err := e.getAuthJSON("GET", "/v3/accounts/"+e.option.AccessKey+"/summary", nil)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetAccount() error, ", err)
return false
}
if statusCode > 200 {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetAccount() error, ", json.Get("errorMessage").Interface())
return false
}
currency := json.GetPath("account", "currency").MustString()
if currency == "" {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetAccount() error, can not get the currency")
return false
}
return map[string]float64{
currency: conver.Float64Must(json.GetPath("account", "marginAvailable").Interface()),
"Frozen" + currency: 0.0,
}
}
// GetPositions get the positions detail of this exchange
func (e *OandaV20) GetPositions(stockType string) interface{} {
stockTypeRaw := strings.ToUpper(stockType)
stockType = strings.Replace(stockTypeRaw, "/", "_", -1)
if !strings.Contains(stockType, "_") {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetPositions() error, unrecognized stockType: ", stockType)
return false
}
positions := []Position{}
statusCode, json, err := e.getAuthJSON("GET", "/v3/accounts/"+e.option.AccessKey+"/positions/"+stockType, nil)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetPositions() error, ", err)
return false
}
if statusCode > 200 {
if json.Get("errorCode").MustString() == "NO_SUCH_POSITION" {
return positions
}
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetAccount() error, ", json.Get("errorMessage").Interface())
return false
}
positionJSON := json.GetPath("position", "long")
amount := conver.Float64Must(positionJSON.Get("units").Interface())
if amount > 0.0 {
positions = append(positions, Position{
Price: conver.Float64Must(positionJSON.Get("averagePrice").Interface()),
Leverage: 1,
Amount: amount,
ConfirmAmount: amount,
FrozenAmount: 0.0,
Profit: conver.Float64Must(positionJSON.Get("resettablePL").Interface()),
ContractType: "",
TradeType: constant.TradeTypeLong,
StockType: stockTypeRaw,
})
}
positionJSON = json.GetPath("position", "short")
amount = conver.Float64Must(positionJSON.Get("units").Interface())
if amount > 0.0 {
positions = append(positions, Position{
Price: conver.Float64Must(positionJSON.Get("averagePrice").Interface()),
Leverage: 1,
Amount: amount,
ConfirmAmount: amount,
FrozenAmount: 0.0,
Profit: conver.Float64Must(positionJSON.Get("resettablePL").Interface()),
ContractType: "",
TradeType: constant.TradeTypeLong,
StockType: stockTypeRaw,
})
}
return positions
}
// Trade place an order
func (e *OandaV20) Trade(tradeType string, stockType string, _price, _amount interface{}, msgs ...interface{}) interface{} {
tradeType = strings.ToUpper(tradeType)
stockType = strings.ToUpper(stockType)
price := conver.Float64Must(_price)
amount := conver.Float64Must(_amount)
if _, ok := e.tradeTypeMap[tradeType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, unrecognized tradeType: ", tradeType)
return false
}
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, unrecognized stockType: ", stockType)
return false
}
if len(msgs) < 1 {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, unrecognized leverage")
return false
}
leverage := fmt.Sprint(msgs[0])
if _, ok := e.leverageMap[leverage]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, unrecognized leverage: ", leverage)
return false
}
matchPrice := "match_price=1"
if price > 0.0 {
matchPrice = "match_price=0"
} else {
price = 0.0
}
params := []string{
"symbol=" + e.stockTypeMap[stockType][0],
"contract_type=" + e.stockTypeMap[stockType][1],
fmt.Sprintf("price=%f", price),
fmt.Sprintf("amount=%f", amount),
"type=" + e.tradeTypeMap[tradeType],
matchPrice,
"lever_rate=" + leverage,
}
_, json, err := e.getAuthJSON("GET", e.host+"future_trade.do", params)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, ", err)
return false
}
if result := json.Get("result").MustBool(); !result {
err = fmt.Errorf("Trade() error, the error number is %v", json.Get("error_code").MustInt())
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, ", err)
return false
}
e.logger.Log(e.tradeTypeLogMap[tradeType], stockType, price, amount, msgs[2:]...)
return fmt.Sprint(json.Get("order_id").Interface())
}
// GetOrder get details of an order
func (e *OandaV20) GetOrder(stockType, id string) interface{} {
stockType = strings.ToUpper(stockType)
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrder() error, unrecognized stockType: ", stockType)
return false
}
params := []string{
"symbol=" + e.stockTypeMap[stockType][0],
"contract_type=" + e.stockTypeMap[stockType][1],
"order_id=" + id,
}
_, json, err := e.getAuthJSON("GET", e.host+"future_orders_info.do", params)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrder() error, ", err)
return false
}
if result := json.Get("result").MustBool(); !result {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrder() error, the error number is ", json.Get("error_code").MustInt())
return false
}
ordersJSON := json.Get("orders")
if len(ordersJSON.MustArray()) > 0 {
orderJSON := ordersJSON.GetIndex(0)
return Order{
ID: fmt.Sprint(orderJSON.Get("order_id").Interface()),
Price: orderJSON.Get("price").MustFloat64(),
Amount: orderJSON.Get("amount").MustFloat64(),
DealAmount: orderJSON.Get("deal_amount").MustFloat64(),
Fee: orderJSON.Get("fee").MustFloat64(),
TradeType: e.tradeTypeAntiMap[orderJSON.Get("type").MustInt()],
StockType: stockType,
}
}
return false
}
// GetOrders get all unfilled orders
func (e *OandaV20) GetOrders(stockType string) interface{} {
stockType = strings.ToUpper(stockType)
orders := []Order{}
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrders() error, unrecognized stockType: ", stockType)
return false
}
params := []string{
"symbol=" + e.stockTypeMap[stockType][0],
"contract_type=" + e.stockTypeMap[stockType][1],
"status=1",
"order_id=-1",
"current_page=1",
"page_length=50",
}
_, json, err := e.getAuthJSON("GET", e.host+"future_order_info.do", params)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrders() error, ", err)
return false
}
if result := json.Get("result").MustBool(); !result {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrders() error, the error number is ", json.Get("error_code").MustInt())
return false
}
ordersJSON := json.Get("orders")
count := len(ordersJSON.MustArray())
for i := 0; i < count; i++ {
orderJSON := ordersJSON.GetIndex(i)
orders = append(orders, Order{
ID: fmt.Sprint(orderJSON.Get("order_id").Interface()),
Price: orderJSON.Get("price").MustFloat64(),
Amount: orderJSON.Get("amount").MustFloat64(),
DealAmount: orderJSON.Get("deal_amount").MustFloat64(),
Fee: orderJSON.Get("fee").MustFloat64(),
TradeType: e.tradeTypeAntiMap[orderJSON.Get("type").MustInt()],
StockType: stockType,
})
}
return orders
}
// GetTrades get all filled orders recently
func (e *OandaV20) GetTrades(stockType string) interface{} {
stockType = strings.ToUpper(stockType)
orders := []Order{}
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetTrades() error, unrecognized stockType: ", stockType)
return false
}
params := []string{
"symbol=" + e.stockTypeMap[stockType][0],
"contract_type=" + e.stockTypeMap[stockType][1],
"status=2",
"order_id=-1",
"current_page=1",
"page_length=50",
}
_, json, err := e.getAuthJSON("GET", e.host+"future_order_info.do", params)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetTrades() error, ", err)
return false
}
if result := json.Get("result").MustBool(); !result {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetTrades() error, the error number is ", json.Get("error_code").MustInt())
return false
}
ordersJSON := json.Get("orders")
count := len(ordersJSON.MustArray())
for i := 0; i < count; i++ {
orderJSON := ordersJSON.GetIndex(i)
orders = append(orders, Order{
ID: fmt.Sprint(orderJSON.Get("order_id").Interface()),
Price: orderJSON.Get("price").MustFloat64(),
Amount: orderJSON.Get("amount").MustFloat64(),
DealAmount: orderJSON.Get("deal_amount").MustFloat64(),
Fee: orderJSON.Get("fee").MustFloat64(),
TradeType: e.tradeTypeAntiMap[orderJSON.Get("type").MustInt()],
StockType: stockType,
})
}
return orders
}
// CancelOrder cancel an order
func (e *OandaV20) CancelOrder(order Order) bool {
params := []string{
"symbol=" + e.stockTypeMap[order.StockType][0],
"order_id=" + order.ID,
"contract_type=" + e.stockTypeMap[order.StockType][1],
}
_, json, err := e.getAuthJSON("GET", e.host+"future_cancel.do", params)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "CancelOrder() error, ", err)
return false
}
if result := json.Get("result").MustBool(); !result {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "CancelOrder() error, the error number is ", json.Get("error_code").MustInt())
return false
}
e.logger.Log(constant.CANCEL, order.StockType, order.Price, order.Amount-order.DealAmount, order)
return true
}
// getTicker get market ticker & depth
func (e *OandaV20) getTicker(stockType string, sizes ...interface{}) (ticker Ticker, err error) {
stockType = strings.ToUpper(stockType)
if _, ok := e.stockTypeMap[stockType]; !ok {
err = fmt.Errorf("GetTicker() error, unrecognized stockType: %+v", stockType)
return
}
size := 20
if len(sizes) > 0 && conver.IntMust(sizes[0]) > 0 {
size = conver.IntMust(sizes[0])
}
resp, err := get(fmt.Sprintf("%vfuture_depth.do?symbol=%v&contract_type=%v&size=%v", e.host, e.stockTypeMap[stockType][0], e.stockTypeMap[stockType][1], size))
if err != nil {
err = fmt.Errorf("GetTicker() error, %+v", err)
return
}
json, err := simplejson.NewJson(resp)
if err != nil {
err = fmt.Errorf("GetTicker() error, %+v", err)
return
}
depthsJSON := json.Get("bids")
for i := 0; i < len(depthsJSON.MustArray()); i++ {
depthJSON := depthsJSON.GetIndex(i)
ticker.Bids = append(ticker.Bids, OrderBook{
Price: depthJSON.GetIndex(0).MustFloat64(),
Amount: depthJSON.GetIndex(1).MustFloat64(),
})
}
depthsJSON = json.Get("asks")
for i := len(depthsJSON.MustArray()); i > 0; i-- {
depthJSON := depthsJSON.GetIndex(i - 1)
ticker.Asks = append(ticker.Asks, OrderBook{
Price: depthJSON.GetIndex(0).MustFloat64(),
Amount: depthJSON.GetIndex(1).MustFloat64(),
})
}
if len(ticker.Bids) < 1 || len(ticker.Asks) < 1 {
err = fmt.Errorf("GetTicker() error, can not get enough Bids or Asks")
return
}
ticker.Buy = ticker.Bids[0].Price
ticker.Sell = ticker.Asks[0].Price
ticker.Mid = (ticker.Buy + ticker.Sell) / 2
return
}
// GetTicker get market ticker & depth
func (e *OandaV20) GetTicker(stockType string, sizes ...interface{}) interface{} {
ticker, err := e.getTicker(stockType, sizes...)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, err)
return false
}
return ticker
}
// GetRecords get candlestick data
func (e *OandaV20) GetRecords(stockType, period string, sizes ...interface{}) interface{} {
stockType = strings.ToUpper(stockType)
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetRecords() error, unrecognized stockType: ", stockType)
return false
}
if _, ok := e.recordsPeriodMap[period]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetRecords() error, unrecognized period: ", period)
return false
}
size := 200
if len(sizes) > 0 && conver.IntMust(sizes[0]) > 0 {
size = conver.IntMust(sizes[0])
}
resp, err := get(fmt.Sprintf("%vfuture_kline.do?symbol=%v&contract_type=%v&type=%v&size=%v", e.host, e.stockTypeMap[stockType][0], e.stockTypeMap[stockType][1], e.recordsPeriodMap[period], size))
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetRecords() error, ", err)
return false
}
json, err := simplejson.NewJson(resp)
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetRecords() error, ", err)
return false
}
timeLast := int64(0)
if len(e.records[period]) > 0 {
timeLast = e.records[period][len(e.records[period])-1].Time
}
recordsNew := []Record{}
for i := len(json.MustArray()); i > 0; i-- {
recordJSON := json.GetIndex(i - 1)
recordTime := recordJSON.GetIndex(0).MustInt64() / 1000
if recordTime > timeLast {
recordsNew = append([]Record{{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Low: recordJSON.GetIndex(3).MustFloat64(),
Close: recordJSON.GetIndex(4).MustFloat64(),
Volume: recordJSON.GetIndex(5).MustFloat64(),
}}, recordsNew...)
} else if timeLast > 0 && recordTime == timeLast {
e.records[period][len(e.records[period])-1] = Record{
Time: recordTime,
Open: recordJSON.GetIndex(1).MustFloat64(),
High: recordJSON.GetIndex(2).MustFloat64(),
Low: recordJSON.GetIndex(3).MustFloat64(),
Close: recordJSON.GetIndex(4).MustFloat64(),
Volume: recordJSON.GetIndex(5).MustFloat64(),
}
} else {
break
}
}
e.records[period] = append(e.records[period], recordsNew...)
if len(e.records[period]) > size {
e.records[period] = e.records[period][len(e.records[period])-size : len(e.records[period])]
}
return e.records[period]
}