-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
601 lines (531 loc) · 19.7 KB
/
types.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package banexg
import (
"compress/gzip"
"encoding/gob"
"github.com/banbox/banexg/errs"
"github.com/shopspring/decimal"
"net/http"
"net/url"
"os"
"sync"
)
type FuncSign = func(api *Entry, params map[string]interface{}) *HttpReq
type FuncFetchCurr = func(params map[string]interface{}) (CurrencyMap, *errs.Error)
type FuncFetchMarkets = func(marketTypes []string, params map[string]interface{}) (MarketMap, *errs.Error)
type FuncAuthWS = func(acc *Account, params map[string]interface{}) *errs.Error
type FuncCalcFee = func(market *Market, curr string, maker bool, amount, price decimal.Decimal, params map[string]interface{}) (*Fee, *errs.Error)
type FuncOnWsMsg = func(client *WsClient, msg *WsMsg)
type FuncOnWsMethod = func(client *WsClient, msg map[string]string, info *WsJobInfo)
type FuncOnWsErr = func(client *WsClient, err *errs.Error)
type FuncOnWsClose = func(client *WsClient, err *errs.Error)
type FuncOnWsReCon = func(client *WsClient, connID int) *errs.Error
type FuncGetWsJob = func(client *WsClient) (*WsJobInfo, *errs.Error)
type FuncCalcRateLimiterCost = func(api *Entry, params map[string]interface{}) float64
// key: acc@url#marketType@method
type FuncOnWsChan = func(key string, out interface{})
type Exchange struct {
*ExgInfo
Hosts *ExgHosts
Fees *ExgFee
Apis map[string]*Entry // 所有API的路径
Has map[string]map[string]int // 是否定义了某个API
Options map[string]interface{} // 用户传入的配置
Proxy *url.URL
onHost func(name string) string
CredKeys map[string]bool // cred keys required for exchange
Accounts map[string]*Account // name: account
DefAccName string // default account name
EnableRateLimit int // 是否启用请求速率控制:BoolNull/BoolTrue/BoolFalse
RateLimit int64 // 请求速率控制毫秒数,最小间隔单位
lastRequestMS int64 // 上次请求的13位时间戳
rateM sync.Mutex // 同步锁
CalcRateLimiterCost FuncCalcRateLimiterCost
MarketsWait chan interface{} // whether is loading markets
CareMarkets []string // markets to be fetch: spot/linear/inverse/option
Symbols []string
IDs []string
TimeFrames map[string]string // map timeframe from common to specific
CurrCodeMap map[string]string // common code maps
Retries map[string]int // retry nums for methods
TimeDelay int64 // 系统时钟延迟的毫秒数
HttpClient *http.Client
WSClients map[string]*WsClient // accName@url: websocket clients
WsIntvs map[string]int // milli secs interval for ws endpoints
WsOutChans map[string]interface{} // accName@url+msgHash: chan Type
WsChanRefs map[string]map[string]struct{} // accName@url+msgHash: symbols use this chan
WsCache []*WsLog // websocket cache logs waiting for replay/dump
WsNextMS int64 // timestamp of next replay log
WsReplayTo int64 // timestamp of latest replay log
WsFile *os.File // file to replay/dump
WsWriter *gzip.Writer
WsEncoder *gob.Encoder
WsReader *gzip.Reader
WsDecoder *gob.Decoder
WsBatchSize int
WsReplayFn map[string]func(item *WsLog) *errs.Error
wsCacheLock sync.Mutex
KeyTimeStamps map[string]int64 // key: int64 更新的时间戳
// for calling sub struct func in parent struct
Sign FuncSign
FetchCurrencies FuncFetchCurr
FetchMarkets FuncFetchMarkets
AuthWS FuncAuthWS
CalcFee FuncCalcFee
GetRetryWait func(e *errs.Error) int // 根据错误信息计算重试间隔秒数,<0表示无需重试
OnWsMsg FuncOnWsMsg
OnWsErr FuncOnWsErr
OnWsClose FuncOnWsClose
OnWsReCon FuncOnWsReCon
OnWsChan FuncOnWsChan
Flags map[string]string
}
type ExgInfo struct {
ID string // 交易所ID
Name string // 显示名称
Countries []string // 可用国家
NoHoliday bool // true表示365天全年开放
FullDay bool // true表示一天24小时可交易
Min1mHole int // 1分钟K线空洞的最小间隔,少于此认为正常无交易而非空洞
FixedLvg bool // 杠杆倍率是否固定不可修改
DebugWS bool // 是否输出WS调试信息
DebugAPI bool // 是否输出API请求测试信息
UserAgent string // UserAgent of http request
ReqHeaders map[string]string // http headers for request exchange
CurrenciesById CurrencyMap // CurrencyMap index by id
CurrenciesByCode CurrencyMap // CurrencyMap index by code
Markets MarketMap // cache for all markets
MarketsById MarketArrMap // markets index by id
OrderBooks map[string]*OrderBook // symbol: OrderBook update by wss
MarkPrices map[string]map[string]float64 // marketType: symbol: mark price
OdBookLock sync.Mutex
PrecPadZero bool // padding zero for precision
MarketType string // MarketSpot/MarketMargin/MarketLinear/MarketInverse/MarketOption
ContractType string // MarketSwap/MarketFuture
MarginMode string // MarginCross/MarginIsolated
TimeInForce string // GTC/IOC/FOK
}
type Account struct {
Name string
Creds *Credential
MarPositions map[string][]*Position // marketType: Position List
MarBalances map[string]*Balances // marketType: Balances
Leverages map[string]int // 币种当前的杠杆倍数
Data map[string]interface{}
LockPos *sync.Mutex
LockBalance *sync.Mutex
LockLeverage *sync.Mutex
LockData *sync.Mutex
}
type ExgHosts struct {
TestNet bool
Logo string
Test map[string]string
Prod map[string]string
Www string
Doc []string
Fees string
}
type ExgFee struct {
Main *TradeFee //默认
Linear *TradeFee //U本位合约
Inverse *TradeFee // 币本位合约
Option *TradeFee
}
type TradeFee struct {
FeeSide string
TierBased bool
Percentage bool
Taker float64
Maker float64
Tiers *FeeTiers
}
type FeeTiers struct {
Taker []*FeeTierItem
Maker []*FeeTierItem
}
type FeeTierItem struct {
Amount float64
Rate float64
}
type Entry struct {
Path string
Host string
RawHost string
Url string
Method string
Cost float64
More map[string]interface{}
CacheSecs int
}
type Credential struct {
ApiKey string
Secret string
UID string
Password string
}
type HttpReq struct {
AccName string
Url string
Method string
Headers http.Header
Body string
Private bool // 此请求需要认证信息
Error *errs.Error
}
type HttpRes struct {
AccName string `json:"acc_name"`
Url string `json:"url"`
Status int `json:"status"`
Headers http.Header `json:"headers"`
Content string `json:"content"`
Error *errs.Error
}
type ApiRes[T any] struct {
*HttpRes
Result T `json:"content"`
}
/*
************************** Currency **************************
*/
type CurrencyMap = map[string]*Currency
type Currency struct {
ID string
Name string
Code string
Type string
NumericID int
Precision float64
PrecMode int // 保留精度的模式:PrecModeDecimalPlace/PrecModeSignifDigits/PrecModeTickSize
Active bool
Deposit bool
Withdraw bool
Networks []*ChainNetwork
Fee float64
Fees map[string]float64
Limits *CodeLimits
Info interface{}
}
type ChainNetwork struct {
ID string
Network string
Name string
Active bool
Fee float64
Precision float64
Deposit bool
Withdraw bool
Limits *CodeLimits
Info interface{}
}
type CodeLimits struct {
Amount *LimitRange
Withdraw *LimitRange
Deposit *LimitRange
}
type LimitRange struct {
Min float64
Max float64
}
/*
************************** Market **************************
*/
type Market struct {
ID string `json:"id"`
LowercaseID string `json:"lowercaseId"`
Symbol string `json:"symbol"`
Base string `json:"base"`
Quote string `json:"quote"`
Settle string `json:"settle"`
BaseID string `json:"baseId"`
QuoteID string `json:"quoteId"`
SettleID string `json:"settleId"`
ExgReal string `json:"exgReal"`
Type string `json:"type"` // spot/linear/inverse/option 无法区分margin 和ccxt的值不同
Combined bool `json:"combined"` // 是否是二次组合的数据
Spot bool `json:"spot"` // 现货市场
Margin bool `json:"margin"` // 保证金杠杆市场
Swap bool `json:"swap"` // 期货永续合约市场
Future bool `json:"future"` // 期货市场
Option bool `json:"option"` // 期权市场
Active bool `json:"active"` // 是否可交易
Contract bool `json:"contract"` // 是否是合约
Linear bool `json:"linear"` // usd-based contract
Inverse bool `json:"inverse"` // coin-based contract
Taker float64 `json:"taker"` // 吃单方费率
Maker float64 `json:"maker"` // 挂单方费率
ContractSize float64 `json:"contractSize"`
Expiry int64 `json:"expiry"` // 过期的13毫秒数
ExpiryDatetime string `json:"expiryDatetime"`
Strike float64 `json:"strike"`
OptionType string `json:"optionType"`
DayTimes [][2]int64 `json:"dayTimes"` // 日盘交易时间
NightTimes [][2]int64 `json:"nightTimes"` // 夜盘交易时间
Precision *Precision `json:"precision"`
Limits *MarketLimits `json:"limits"`
Created int64 `json:"created"`
FeeSide string `json:"feeSide"` // get/give/base/quote/other
Info interface{} `json:"info"`
}
type Precision struct {
Amount float64 `json:"amount"`
Price float64 `json:"price"`
Base float64 `json:"base"`
Quote float64 `json:"quote"`
ModeAmount int `json:"modeAmount"` // PrecModeTickSize/PrecModeSignifDigits/PrecModeDecimalPlace
ModePrice int `json:"modePrice"`
ModeBase int `json:"modeBase"`
ModeQuote int `json:"modeQuote"`
}
type MarketLimits struct {
Leverage *LimitRange `json:"leverage"`
Amount *LimitRange `json:"amount"`
Price *LimitRange `json:"price"`
Cost *LimitRange `json:"cost"`
Market *LimitRange `json:"market"`
}
type MarketMap = map[string]*Market
type MarketArrMap = map[string][]*Market
type Ticker struct {
Symbol string `json:"symbol"`
TimeStamp int64 `json:"timestamp"`
Bid float64 `json:"bid"`
BidVolume float64 `json:"bidVolume"`
Ask float64 `json:"ask"`
AskVolume float64 `json:"askVolume"`
High float64 `json:"high"`
Low float64 `json:"low"`
Open float64 `json:"open"`
Close float64 `json:"close"`
Last float64 `json:"last"`
Change float64 `json:"change"`
Percentage float64 `json:"percentage"`
Average float64 `json:"average"`
Vwap float64 `json:"vwap"`
BaseVolume float64 `json:"baseVolume"`
QuoteVolume float64 `json:"quoteVolume"`
PreviousClose float64 `json:"previousClose"`
MarkPrice float64 `json:"markPrice"`
IndexPrice float64 `json:"indexPrice"`
Info interface{} `json:"info"`
}
/*
************************** Business Types **************************
*/
type OHLCVArr = [6]float64
type Kline struct {
Time int64
Open float64
High float64
Low float64
Close float64
Volume float64
Info float64
}
type PairTFKline struct {
Kline
Symbol string
TimeFrame string
}
type Balances struct {
TimeStamp int64
Free map[string]float64
Used map[string]float64
Total map[string]float64
Assets map[string]*Asset
IsolatedAssets map[string]map[string]*Asset // 逐仓账户资产,键是symbol
Info interface{}
}
type Asset struct {
Code string
Free float64
Used float64
Total float64
Debt float64
UPol float64
}
type Position struct {
ID string `json:"id"`
Symbol string `json:"symbol"`
TimeStamp int64 `json:"timestamp"`
Isolated bool `json:"isolated"` // 隔离
Hedged bool `json:"hedged"` // 对冲
Side string `json:"side"` // long or short
Contracts float64 `json:"contracts"` // 合约数量
ContractSize float64 `json:"contractSize"` // 单份合约价值
EntryPrice float64 `json:"entryPrice"` // 入场价格
MarkPrice float64 `json:"markPrice"` // 标记价格
Notional float64 `json:"notional"` // 名义价值
Leverage int `json:"leverage"` // 杠杆倍数
Collateral float64 `json:"collateral"` // 当前保证金:初始保证金+未实现盈亏
InitialMargin float64 `json:"initialMargin"` // 初始保证金额
MaintMargin float64 `json:"maintenanceMargin"` // 维持保证金额
InitialMarginPct float64 `json:"initialMarginPercentage"` // 初始保证金率
MaintMarginPct float64 `json:"maintenanceMarginPercentage"` // 维持保证金率
UnrealizedPnl float64 `json:"unrealizedPnl"` // 未实现盈亏
LiquidationPrice float64 `json:"liquidationPrice"` // 清算价格
MarginMode string `json:"marginMode"` // cross/isolated
MarginRatio float64 `json:"marginRatio"`
Percentage float64 `json:"percentage"` // 未实现盈亏百分比
Info interface{} `json:"info"`
}
type Order struct {
Info interface{} `json:"info"`
ID string `json:"id"`
ClientOrderID string `json:"clientOrderId"`
Datetime string `json:"datetime"`
Timestamp int64 `json:"timestamp"`
LastTradeTimestamp int64 `json:"lastTradeTimestamp"`
LastUpdateTimestamp int64 `json:"lastUpdateTimestamp"`
Status string `json:"status"`
Symbol string `json:"symbol"`
Type string `json:"type"`
TimeInForce string `json:"timeInForce"`
PositionSide string `json:"positionSide"`
Side string `json:"side"`
Price float64 `json:"price"`
Average float64 `json:"average"`
Amount float64 `json:"amount"`
Filled float64 `json:"filled"`
Remaining float64 `json:"remaining"`
TriggerPrice float64 `json:"triggerPrice"`
StopPrice float64 `json:"stopPrice"`
TakeProfitPrice float64 `json:"takeProfitPrice"`
StopLossPrice float64 `json:"stopLossPrice"`
Cost float64 `json:"cost"`
PostOnly bool `json:"postOnly"`
ReduceOnly bool `json:"reduceOnly"`
Trades []*Trade `json:"trades"`
Fee *Fee `json:"fee"`
}
type Trade struct {
ID string `json:"id"` // 交易ID
Symbol string `json:"symbol"` // 币种ID
Side string `json:"side"` // buy/sell
Type string `json:"type"` // market/limit
Amount float64 `json:"amount"` // 当前交易的数量
Price float64 `json:"price"` // 价格
Cost float64 `json:"cost"` // 当前交易花费
Order string `json:"order"` // 当前交易所属订单号
Timestamp int64 `json:"timestamp"` // 时间戳
Maker bool `json:"maker"` // 是否maker
Fee *Fee `json:"fee"` // 手续费
Info interface{} `json:"info"`
}
type MyTrade struct {
Trade
Filled float64 `json:"filled"` // 订单累计成交量(不止当前交易)
ClientID string `json:"clientID"` // 客户端订单ID
Average float64 `json:"average"` // 平均成交价格
State string `json:"state"` // 状态
PosSide string `json:"posSide"` // 持仓方向 long/short
ReduceOnly bool `json:"reduceOnly"` // 是否是只减仓单
Info interface{} `json:"info"`
}
type Fee struct {
IsMaker bool `json:"isMaker"` // for calculate fee
Currency string `json:"currency"`
Cost float64 `json:"cost"`
Rate float64 `json:"rate,omitempty"`
}
type OrderBook struct {
Symbol string `json:"symbol"`
TimeStamp int64 `json:"timestamp"`
Asks *OdBookSide `json:"asks"`
Bids *OdBookSide `json:"bids"`
Nonce int64 `json:"nonce"` // latest update id
Limit int `json:"limit"`
Cache []map[string]string
}
/*
OdBookSide
On one side of the order book. No need to add a lock, as only one goroutine can be modified
订单簿一侧。不需要加锁,因为只有一个goroutine可以修改
*/
type OdBookSide struct {
IsBuy bool
Price []float64 // bid: desc ask: asc
Size []float64
Depth int
Lock sync.Mutex
}
type Income struct {
Symbol string `json:"symbol"`
IncomeType string `json:"incomeType"`
Income float64 `json:"income"`
Asset string `json:"asset"`
Info string `json:"info"`
Time int64 `json:"time"`
TranID string `json:"tranId"`
TradeID string `json:"tradeId"`
}
type FundingRate struct {
Symbol string `json:"symbol"`
FundingRate float64 `json:"fundingRate"`
Timestamp int64 `json:"timestamp"`
Info interface{} `json:"info"`
}
type FundingRateCur struct {
Symbol string `json:"symbol"`
FundingRate float64 `json:"fundingRate"`
Timestamp int64 `json:"timestamp"`
MarkPrice float64 `json:"markPrice,omitempty"`
IndexPrice float64 `json:"indexPrice,omitempty"`
InterestRate float64 `json:"interestRate,omitempty"`
EstimatedSettlePrice float64 `json:"estimatedSettlePrice,omitempty"`
FundingTimestamp int64 `json:"fundingTimestamp,omitempty"`
NextFundingRate float64 `json:"nextFundingRate,omitempty"`
NextFundingTimestamp int64 `json:"nextFundingTimestamp,omitempty"`
PrevFundingRate float64 `json:"prevFundingRate,omitempty"`
PrevFundingTimestamp int64 `json:"prevFundingTimestamp,omitempty"`
Interval string `json:"interval,omitempty"`
Info interface{} `json:"info"`
}
type LastPrice struct {
Symbol string `json:"symbol"`
Timestamp int64 `json:"timestamp"`
Price float64 `json:"price"`
Info interface{} `json:"info"`
}
/*
************************** WebSockets **************************
*/
/*
WsJobInfo
store callback data for calling websocket API. Used for processing when returning results.
调用websocket api时暂存的任务信息。用于返回结果时处理。
*/
type WsJobInfo struct {
ID string
MsgHash string
Name string
Symbols []string
Method func(client *WsClient, msg map[string]string, info *WsJobInfo)
Params map[string]interface{}
}
/*
WsMsg
表示websocket收到的消息
*/
type WsMsg struct {
Event string
ID string
IsArray bool
Text string
Object map[string]string
List []map[string]string
}
type AccountConfig struct {
Symbol string
Leverage int
}
type WsLog struct {
Name string `json:"name,omitempty"`
TimeMS int64 `json:"timeMS,omitempty"`
Content string `json:"content,omitempty"`
}
type OdBookShotLog struct {
MarketType string `json:"marketType,omitempty"`
Symbol string `json:"symbol,omitempty"`
ChanKey string `json:"chanKey,omitempty"`
Book *OrderBook `json:"book,omitempty"`
}