forked from phonegapX/QuantBot
/
bigone.go
294 lines (270 loc) · 8.48 KB
/
bigone.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
package api
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/miaolz123/conver"
"github.com/phonegapX/QuantBot/api/BigoneAPI"
"github.com/phonegapX/QuantBot/constant"
"github.com/phonegapX/QuantBot/model"
)
// BigOne the exchange struct of big.one
type BigOne struct {
stockTypeMap map[string]string
tradeTypeMap map[string]string
recordsPeriodMap map[string]string
minAmountMap map[string]float64
records map[string][]Record
logger model.Logger
option Option
limit float64
lastSleep int64
lastTimes int64
}
var bo *BigoneAPI.Bigone
// NewBigOne create an exchange struct of big.one
func NewBigOne(opt Option) Exchange {
bo = BigoneAPI.New(http.DefaultClient, opt.AccessKey, opt.SecretKey)
//...
return &BigOne{
stockTypeMap: map[string]string{
"BTC/USDT": "BTC-USDT",
"ONE/USDT": "ONE-USDT",
"EOS/USDT": "EOS-USDT",
"ETH/USDT": "ETH-USDT",
"BCH/USDT": "BCH-USDT",
"EOS/ETH": "EOS-ETH",
},
tradeTypeMap: map[string]string{
"BID": constant.TradeTypeBuy,
"ASK": constant.TradeTypeSell,
},
recordsPeriodMap: map[string]string{
"M": "001",
"M5": "005",
"M15": "015",
"M30": "030",
"H": "060",
"D": "100",
"W": "200",
},
minAmountMap: map[string]float64{
"BTC/USDT": 0.001,
"ONE/USDT": 0.001,
"EOS/USDT": 0.001,
"ETH/USDT": 0.001,
"BCH/USDT": 0.001,
"EOS/ETH": 0.001,
},
records: make(map[string][]Record),
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 *BigOne) Log(msgs ...interface{}) {
e.logger.Log(constant.INFO, "", 0.0, 0.0, msgs...)
}
// GetType get the type of this exchange
func (e *BigOne) GetType() string {
return e.option.Type
}
// GetName get the name of this exchange
func (e *BigOne) GetName() string {
return e.option.Name
}
// SetLimit set the limit calls amount per second of this exchange
func (e *BigOne) 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 *BigOne) 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 *BigOne) GetMinAmount(stock string) float64 {
return e.minAmountMap[stock]
}
// GetAccount get the account detail of this exchange
func (e *BigOne) GetAccount() interface{} {
result, err := bo.GetAccount()
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetAccount() error, ", err)
return false
}
if len(result.Errors) > 0 {
//log.Printf("response error : %v", result.Errors)
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetAccount() error, ", result.Errors[0].Message)
return false
}
accInfo := make(map[string]float64)
for _, v := range result.Data {
available := conver.Float64Must(v.Balance)
freez := conver.Float64Must(v.LockedBalance)
if available != 0 {
accInfo[strings.ToUpper(v.AssetID)] = available
}
if freez != 0 {
accInfo["Frozen"+strings.ToUpper(v.AssetID)] = freez
}
}
return accInfo
}
// Trade place an order
func (e *BigOne) Trade(tradeType string, stockType string, _price, _amount interface{}, msgs ...interface{}) interface{} {
stockType = strings.ToUpper(stockType)
tradeType = strings.ToUpper(tradeType)
price := conver.Float64Must(_price)
amount := conver.Float64Must(_amount)
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, unrecognized stockType: ", stockType)
return false
}
switch tradeType {
case constant.TradeTypeBuy:
return e.buy(stockType, price, amount, msgs...)
case constant.TradeTypeSell:
return e.sell(stockType, price, amount, msgs...)
default:
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Trade() error, unrecognized tradeType: ", tradeType)
return false
}
}
func (e *BigOne) buy(stockType string, price, amount float64, msgs ...interface{}) interface{} {
result, err := bo.LimitBuy(conver.StringMust(amount), conver.StringMust(price), e.stockTypeMap[stockType])
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Buy() error, ", err)
return false
}
if len(result.Errors) > 0 {
//log.Printf("response error : %v", result.Errors)
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Buy() error, ", result.Errors[0].Message)
return false
}
e.logger.Log(constant.BUY, stockType, price, amount, msgs...)
return result.Data.ID
}
func (e *BigOne) sell(stockType string, price, amount float64, msgs ...interface{}) interface{} {
result, err := bo.LimitSell(conver.StringMust(amount), conver.StringMust(price), e.stockTypeMap[stockType])
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Sell() error, ", err)
return false
}
if len(result.Errors) > 0 {
//log.Printf("response error : %v", result.Errors)
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "Sell() error, ", result.Errors[0].Message)
return false
}
e.logger.Log(constant.SELL, stockType, price, amount, msgs...)
return result.Data.ID
}
// GetOrder get details of an order
func (e *BigOne) GetOrder(stockType, id string) interface{} {
return nil
}
// GetOrders get all unfilled orders
func (e *BigOne) GetOrders(stockType string) interface{} {
stockType = strings.ToUpper(stockType)
if _, ok := e.stockTypeMap[stockType]; !ok {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrders() error, unrecognized stockType: ", stockType)
return false
}
result, err := bo.GetUnfinishOrders(e.stockTypeMap[stockType])
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrders() error, ", err)
return false
}
if len(result.Errors) > 0 {
//log.Printf("response error : %v", result.Errors)
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "GetOrders() error, ", result.Errors[0].Message)
return false
}
orders := []Order{}
for _, v := range result.Data.Edges {
n := v.Node
orders = append(orders, Order{
ID: n.ID,
Price: conver.Float64Must(n.Price),
Amount: conver.Float64Must(n.Amount),
DealAmount: conver.Float64Must(n.FilledAmount),
TradeType: e.tradeTypeMap[n.Side],
StockType: stockType,
})
}
return orders
}
// GetTrades get all filled orders recently
func (e *BigOne) GetTrades(stockType string) interface{} {
return nil
}
// CancelOrder cancel an order
func (e *BigOne) CancelOrder(order Order) bool {
result, err := bo.CancelOrder(order.ID, e.stockTypeMap[order.StockType])
if err != nil {
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "CancelOrder() error, ", err)
return false
}
if len(result.Errors) > 0 {
//log.Printf("response error : %v", result.Errors)
e.logger.Log(constant.ERROR, "", 0.0, 0.0, "CancelOrder() error, ", result.Errors[0].Message)
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 *BigOne) 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
}
result, err := bo.GetDepth(e.stockTypeMap[stockType])
if err != nil {
err = fmt.Errorf("GetTicker() error, %+v", err)
return
}
for _, bid := range result.Data.Bids {
ticker.Bids = append(ticker.Bids, OrderBook{
Price: conver.Float64Must(bid.Price),
Amount: conver.Float64Must(bid.Amount),
})
}
for _, ask := range result.Data.Asks {
ticker.Asks = append(ticker.Asks, OrderBook{
Price: conver.Float64Must(ask.Price),
Amount: conver.Float64Must(ask.Amount),
})
}
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 *BigOne) 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 *BigOne) GetRecords(stockType, period string, sizes ...interface{}) interface{} {
return nil
}