-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
websocket.go
159 lines (139 loc) · 5.59 KB
/
websocket.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
package kucoin
import (
"encoding/json"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
type WebSocketMessageType string
const (
WebSocketMessageTypePing WebSocketMessageType = "ping"
WebSocketMessageTypeSubscribe WebSocketMessageType = "subscribe"
WebSocketMessageTypeUnsubscribe WebSocketMessageType = "unsubscribe"
WebSocketMessageTypeAck WebSocketMessageType = "ack"
WebSocketMessageTypeError WebSocketMessageType = "error"
WebSocketMessageTypePong WebSocketMessageType = "pong"
WebSocketMessageTypeWelcome WebSocketMessageType = "welcome"
WebSocketMessageTypeMessage WebSocketMessageType = "message"
)
type WebSocketSubject string
const (
WebSocketSubjectTradeTicker WebSocketSubject = "trade.ticker"
WebSocketSubjectTradeSnapshot WebSocketSubject = "trade.snapshot" // ticker snapshot
WebSocketSubjectTradeL2Update WebSocketSubject = "trade.l2update" // order book L2
WebSocketSubjectLevel2 WebSocketSubject = "level2" // level2
WebSocketSubjectTradeCandlesUpdate WebSocketSubject = "trade.candles.update"
WebSocketSubjectTradeCandlesAdd WebSocketSubject = "trade.candles.add"
// private subjects
WebSocketSubjectOrderChange WebSocketSubject = "orderChange"
WebSocketSubjectAccountBalance WebSocketSubject = "account.balance"
WebSocketSubjectStopOrder WebSocketSubject = "stopOrder"
)
type WebSocketCommand struct {
Id int64 `json:"id"`
Type WebSocketMessageType `json:"type"`
Topic string `json:"topic"`
PrivateChannel bool `json:"privateChannel"`
Response bool `json:"response"`
}
func (c *WebSocketCommand) JSON() ([]byte, error) {
type tt WebSocketCommand
var a = (*tt)(c)
return json.Marshal(a)
}
type WebSocketEvent struct {
Type WebSocketMessageType `json:"type"`
Topic string `json:"topic"`
Subject WebSocketSubject `json:"subject"`
Data json.RawMessage `json:"data"`
Code int `json:"code"` // used in type error
// Object is used for storing the parsed Data
Object interface{} `json:"-"`
}
type WebSocketTickerEvent struct {
Sequence string `json:"sequence"`
Price fixedpoint.Value `json:"price"`
Size fixedpoint.Value `json:"size"`
BestAsk fixedpoint.Value `json:"bestAsk"`
BestAskSize fixedpoint.Value `json:"bestAskSize"`
BestBid fixedpoint.Value `json:"bestBid"`
BestBidSize fixedpoint.Value `json:"bestBidSize"`
}
type WebSocketOrderBookL2Event struct {
SequenceStart int64 `json:"sequenceStart"`
SequenceEnd int64 `json:"sequenceEnd"`
Symbol string `json:"symbol"`
Changes struct {
Asks types.PriceVolumeSlice `json:"asks"`
Bids types.PriceVolumeSlice `json:"bids"`
} `json:"changes"`
}
type WebSocketCandleEvent struct {
Symbol string `json:"symbol"`
Candles []string `json:"candles"`
Time types.MillisecondTimestamp `json:"time"`
// Interval is an injected field (not from the payload)
Interval types.Interval
// Is a new candle or not
Add bool
}
func (e *WebSocketCandleEvent) KLine() types.KLine {
startTime := types.MustParseUnixTimestamp(e.Candles[0])
openPrice := util.MustParseFloat(e.Candles[1])
closePrice := util.MustParseFloat(e.Candles[2])
highPrice := util.MustParseFloat(e.Candles[3])
lowPrice := util.MustParseFloat(e.Candles[4])
volume := util.MustParseFloat(e.Candles[5])
quoteVolume := util.MustParseFloat(e.Candles[6])
kline := types.KLine{
Exchange: types.ExchangeKucoin,
Symbol: toGlobalSymbol(e.Symbol),
StartTime: types.Time(startTime),
EndTime: types.Time(startTime.Add(e.Interval.Duration() - time.Millisecond)),
Interval: e.Interval,
Open: openPrice,
Close: closePrice,
High: highPrice,
Low: lowPrice,
Volume: volume,
QuoteVolume: quoteVolume,
Closed: false,
}
return kline
}
type WebSocketPrivateOrderEvent struct {
OrderId string `json:"orderId"`
TradeId string `json:"tradeId"`
Symbol string `json:"symbol"`
OrderType string `json:"orderType"`
Side string `json:"side"`
Type string `json:"type"`
OrderTime types.NanosecondTimestamp `json:"orderTime"`
Price fixedpoint.Value `json:"price"`
Size fixedpoint.Value `json:"size"`
FilledSize fixedpoint.Value `json:"filledSize"`
RemainSize fixedpoint.Value `json:"remainSize"`
Liquidity string `json:"liquidity"`
MatchPrice fixedpoint.Value `json:"matchPrice"`
MatchSize fixedpoint.Value `json:"matchSize"`
ClientOid string `json:"clientOid"`
Status string `json:"status"`
Ts types.MillisecondTimestamp `json:"ts"`
}
type WebSocketAccountBalanceEvent struct {
Total fixedpoint.Value `json:"total"`
Available fixedpoint.Value `json:"available"`
AvailableChange fixedpoint.Value `json:"availableChange"`
Currency string `json:"currency"`
Hold fixedpoint.Value `json:"hold"`
HoldChange fixedpoint.Value `json:"holdChange"`
RelationEvent string `json:"relationEvent"`
RelationEventId string `json:"relationEventId"`
RelationContext struct {
Symbol string `json:"symbol"`
TradeId string `json:"tradeId"`
OrderId string `json:"orderId"`
} `json:"relationContext"`
Time string `json:"time"`
}