-
Notifications
You must be signed in to change notification settings - Fork 156
/
api.go
329 lines (254 loc) · 7.69 KB
/
api.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
package crex
import (
"net/http"
"time"
)
type Parameters struct {
DebugMode bool
HttpClient *http.Client
ProxyURL string // example: socks5://127.0.0.1:1080 | http://127.0.0.1:1080
ApiURL string
WsURL string
Testnet bool
AccessKey string
SecretKey string
Passphrase string
WebSocket bool // Enable websocket option
}
type ApiOption func(p *Parameters)
func ApiDebugModeOption(debugMode bool) ApiOption {
return func(p *Parameters) {
p.DebugMode = debugMode
}
}
func ApiHttpClientOption(httpClient *http.Client) ApiOption {
return func(p *Parameters) {
p.HttpClient = httpClient
}
}
func ApiProxyURLOption(proxyURL string) ApiOption {
return func(p *Parameters) {
p.ProxyURL = proxyURL
}
}
func ApiApiURLOption(apiURL string) ApiOption {
return func(p *Parameters) {
p.ApiURL = apiURL
}
}
func ApiWsURLOption(wsURL string) ApiOption {
return func(p *Parameters) {
p.WsURL = wsURL
}
}
func ApiAccessKeyOption(accessKey string) ApiOption {
return func(p *Parameters) {
p.AccessKey = accessKey
}
}
func ApiSecretKeyOption(secretKey string) ApiOption {
return func(p *Parameters) {
p.SecretKey = secretKey
}
}
func ApiPassPhraseOption(passPhrase string) ApiOption {
return func(p *Parameters) {
p.Passphrase = passPhrase
}
}
func ApiTestnetOption(testnet bool) ApiOption {
return func(p *Parameters) {
p.Testnet = testnet
}
}
func ApiWebSocketOption(enabled bool) ApiOption {
return func(p *Parameters) {
p.WebSocket = enabled
}
}
type OrderParameter struct {
Stop bool // 是否是触发委托
}
// 订单选项
type OrderOption func(p *OrderParameter)
// 触发委托选项
func OrderStopOption(stop bool) OrderOption {
return func(p *OrderParameter) {
p.Stop = stop
}
}
func ParseOrderParameter(opts ...OrderOption) *OrderParameter {
p := &OrderParameter{}
for _, opt := range opts {
opt(p)
}
return p
}
type PlaceOrderParameter struct {
BasePrice float64
StopPx float64
PostOnly bool
ReduceOnly bool
PriceType string
ClientOId string
TimeInForce string
ActivationPrice float64
CallbackRate float64
ClosePosition bool
}
// 订单选项
type PlaceOrderOption func(p *PlaceOrderParameter)
// 基础价格选项(如: bybit 需要提供此参数)
func OrderBasePriceOption(basePrice float64) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.BasePrice = basePrice
}
}
// 触发价格选项
func OrderStopPxOption(stopPx float64) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.StopPx = stopPx
}
}
// 被动委托选项
func OrderPostOnlyOption(postOnly bool) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.PostOnly = postOnly
}
}
// 只减仓选项
func OrderReduceOnlyOption(reduceOnly bool) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.ReduceOnly = reduceOnly
}
}
// OrderPriceType 选项
func OrderPriceTypeOption(priceType string) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.PriceType = priceType
}
}
func OrderClientOIdOption(clientOId string) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.ClientOId = clientOId
}
}
func OrderTimeInForceOption(timeInForce string) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.TimeInForce = timeInForce
}
}
func OrderActivationPriceOption(activationPrice float64) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.ActivationPrice = activationPrice
}
}
func OrderCallbackRateOption(callbackRate float64) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.CallbackRate = callbackRate
}
}
func OrderClosePositionOption(closePosition bool) PlaceOrderOption {
return func(p *PlaceOrderParameter) {
p.ClosePosition = closePosition
}
}
func ParsePlaceOrderParameter(opts ...PlaceOrderOption) *PlaceOrderParameter {
p := &PlaceOrderParameter{}
for _, opt := range opts {
opt(p)
}
return p
}
// IBacktest 回测的接口
type IBacktest interface {
// 获取当前时间
GetTime() time.Time
}
// ExchangeSim 模拟交易所接口
type ExchangeSim interface {
//Exchange
// 设置回测组件
SetBacktest(backtest IBacktest)
// 设置交易撮合日志组件
SetExchangeLogger(l ExchangeLogger)
// 运行一次(回测系统调用)
RunEventLoopOnce() (err error) // Run sim match for backtest only
}
// ExchangeLogger 交易所撮合日志
type ExchangeLogger interface {
// Debug Using:log.Debug("test")
Debug(args ...interface{})
// Debugf Using:log.Debugf("test:%s", err)
Debugf(template string, args ...interface{})
// Debugw Using:log.Debugw("test", "field1", "value1", "field2", "value2")
Debugw(msg string, keysAndValues ...interface{})
Info(args ...interface{})
Infof(template string, args ...interface{})
Infow(msg string, keysAndValues ...interface{})
Warn(args ...interface{})
Warnf(template string, args ...interface{})
Warnw(msg string, keysAndValues ...interface{})
Error(args ...interface{})
Errorf(template string, args ...interface{})
Errorw(msg string, keysAndValues ...interface{})
// 同步,确保日志写入
Sync()
}
// Exchange 交易所接口
type Exchange interface {
// 获取 Exchange 名称
GetName() (name string)
// 获取交易所时间(ms)
GetTime() (tm int64, err error)
// 获取账号余额
GetBalance(currency string) (result *Balance, err error)
// 获取订单薄(OrderBook)
GetOrderBook(symbol string, depth int) (result *OrderBook, err error)
// 获取K线数据
// period: 数据周期. 分钟或者关键字1m(minute) 1h 1d 1w 1M(month) 1y 枚举值:1 3 5 15 30 60 120 240 360 720 "5m" "4h" "1d" ...
GetRecords(symbol string, period string, from int64, end int64, limit int) (records []*Record, err error)
// 设置合约类型
// currencyPair: 交易对,如: BTC-USD(OKEX) BTC(HBDM)
// contractType: W1,W2,Q1,Q2
SetContractType(currencyPair string, contractType string) (err error)
// 获取当前设置的合约ID
GetContractID() (symbol string, err error)
// 设置杠杆大小
SetLeverRate(value float64) (err error)
// 开多
OpenLong(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)
// 开空
OpenShort(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)
// 平多
CloseLong(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)
// 平空
CloseShort(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)
// 下单
PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, size float64,
opts ...PlaceOrderOption) (result *Order, err error)
// 获取活跃委托单列表
GetOpenOrders(symbol string, opts ...OrderOption) (result []*Order, err error)
// 获取委托信息
GetOrder(symbol string, id string, opts ...OrderOption) (result *Order, err error)
// 撤销全部委托单
CancelAllOrders(symbol string, opts ...OrderOption) (err error)
// 撤销单个委托单
CancelOrder(symbol string, id string, opts ...OrderOption) (result *Order, err error)
// 修改委托
AmendOrder(symbol string, id string, price float64, size float64, opts ...OrderOption) (result *Order, err error)
// 获取持仓
GetPositions(symbol string) (result []*Position, err error)
// 订阅成交记录
SubscribeTrades(market Market, callback func(trades []*Trade)) error
// 订阅L2 OrderBook
SubscribeLevel2Snapshots(market Market, callback func(ob *OrderBook)) error
// 订阅Balance
//SubscribeBalances(market Market, callback func(balance *Balance)) error
// 订阅委托
SubscribeOrders(market Market, callback func(orders []*Order)) error
// 订阅持仓
SubscribePositions(market Market, callback func(positions []*Position)) error
// 调用其他功能
IO(name string, params string) (string, error)
}