-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
ws-private.ts
250 lines (212 loc) · 5.79 KB
/
ws-private.ts
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
// If you cloned the repo and are using typescript, you can import from src directly:
import { WebsocketClient, DefaultLogger } from '../src';
// or use the module installed via `npm install okx-api`:
// import { WebsocketClient, DefaultLogger } from 'okx-api';
// or if you're not using typescript (e.g. pure nodejs), change the "import" to "require":
// const { WebsocketClient, DefaultLogger } = require('okx-api');
// Optional: Inject a custom logger.
// This example overrides the default logger to also log "silly" (super verbose) messages, which are disabled by default
const logger = {
...DefaultLogger,
// silly: (...params) => console.log('silly', ...params),
};
// For private events, all 3 of the following are required (per account):
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
const API_PASSPHRASE = process.env.API_PASSPHRASE_COM;
if (!API_KEY) {
throw new Error('API_KEY is missing');
}
if (!API_SECRET) {
throw new Error('API_SECRET is missing');
}
if (!API_PASSPHRASE) {
throw new Error('API_PASSPHRASE is missing');
}
const wsClient = new WebsocketClient(
{
// The market defaults to "prod" for the live environment, but you can also ask to use the aws or demo environments:
// market: 'prod',
// market: 'aws',
// market: 'demo',
accounts: [
// For private topics, include one or more accounts in an array. Otherwise only public topics will work
{
apiKey: API_KEY,
apiSecret: API_SECRET,
apiPass: API_PASSPHRASE,
},
// {
// apiKey: 'yourApiKeyHere',
// apiSecret: 'yourApiSecretHere',
// apiPass: 'yourApiPassHere',
// },
// {
// apiKey: 'anotherAccountKey',
// apiSecret: 'anotherAccountSecret',
// apiPass: 'anotherAccountPass',
// },
],
},
logger
);
// Raw data will arrive on the 'update' event
wsClient.on('update', (data) => {
// console.log('ws update (raw data received)', JSON.stringify(data, null, 2));
console.log('ws update (raw data received)', JSON.stringify(data));
});
wsClient.on('open', (data) => {
console.log('connection opened open:', data.wsKey);
});
// Replies (e.g. authenticating or subscribing to channels) will arrive on the 'response' event
wsClient.on('response', (data) => {
// console.log('ws response: ', JSON.stringify(data, null, 2));
console.log('ws response: ', JSON.stringify(data));
});
wsClient.on('reconnect', ({ wsKey }) => {
console.log('ws automatically reconnecting.... ', wsKey);
});
wsClient.on('reconnected', (data) => {
console.log('ws has reconnected ', data?.wsKey);
});
wsClient.on('error', (data) => {
console.error('ws exception: ', data);
});
// Optional, connect before subscribing:
// wsClient.connectPrivate();
// This is optional though. The wsclient will automatically open and subscribe if the connection doesn't exist yet.
/**
* # Subscribing to channels
*
* Subscribe to channels using the inner "args" part of the subscription request described in the OKX API docs.
*
* For example, if the docs state your request should look as such:
{
op: "subscribe",
args: [
{
channel: "account"
}
]
}
*
* You should call the wsClient.subscribe function using only the "args".
*
* Either of these examples is correct (one channel vs one or more channels in an array):
wsClient.subscribe({
channel: 'account'
});
wsClient.subscribe([
{
channel: "account"
}
])
*/
// Subscribe one event at a time:
wsClient.subscribe({
channel: 'account',
});
// OR, combine multiple subscription events into one request using an array instead of an object:
wsClient.subscribe([
{
channel: 'account',
},
{
channel: 'positions',
instType: 'ANY',
},
]);
/**
* Examples for each private channel listed in the API docs:
* https://www.okx.com/docs-v5/en/#websocket-api-private-channel
*/
// Account events for all symbols
wsClient.subscribe({
channel: 'account',
});
// Account events for specific symbol only
wsClient.subscribe({
channel: 'account',
ccy: 'BTC',
});
// Withdrawal events for specific symbol only
wsClient.subscribe({
channel: 'withdrawal-info',
ccy: 'BTC',
});
// Position events on any instrument type
wsClient.subscribe({
channel: 'positions',
instType: 'ANY',
});
// Position events on specific instruments
wsClient.subscribe({
channel: 'positions',
instType: 'SWAP',
instFamily: 'ETH-USD',
instId: 'ETH-USD-SWAP',
});
// Balance & position channel
wsClient.subscribe({
channel: 'balance_and_position',
});
// Order channel
wsClient.subscribe({
channel: 'orders',
instType: 'ANY',
});
// Order channel with extra args
wsClient.subscribe({
channel: 'orders',
instType: 'FUTURES',
instFamily: 'BTC-USD',
});
// Algo orders channel
wsClient.subscribe({
channel: 'orders-algo',
instType: 'ANY',
});
// Advance algo orders channel
wsClient.subscribe({
channel: 'algo-advance',
instType: 'ANY',
});
// Position risk warning channel
wsClient.subscribe({
channel: 'liquidation-warning',
instType: 'ANY',
});
// Account greeks channel
wsClient.subscribe({
channel: 'account-greeks',
});
// Spot grid algo orders channel
wsClient.subscribe({
channel: 'grid-orders-spot',
instType: 'SPOT',
});
// Contract grid orders channel
wsClient.subscribe({
channel: 'grid-orders-contract',
instType: 'ANY',
});
// Moon grid orders channel
wsClient.subscribe({
channel: 'grid-orders-moon',
instType: 'ANY',
});
// Moon grid orders channel
wsClient.subscribe({
channel: 'grid-orders-moon',
instType: 'ANY',
});
// Grid positions channel
wsClient.subscribe({
channel: 'grid-positions',
algoId: '449327675342323712',
});
// Grid sub orders channel
wsClient.subscribe({
channel: 'grid-sub-orders',
algoId: '449327675342323712',
});