-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
664 lines (597 loc) · 19.3 KB
/
index.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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
import { clearInterval } from "node:timers";
import ws, { WebSocket } from 'ws';
import { getUserAgent } from '../utils';
import { KiteTickerParams, TickerEvent, TickerEvents } from './types';
/**
* The WebSocket client for connecting to Kite connect streaming quotes service.
*
* @example
* Getting started:
* ----
* ```ts
* import { KiteTicker } from "kiteconnect";
* const ticker = new KiteTicker({
* api_key: "api_key",
* access_token: "access_token"
* });
*
* ticker.connect();
* ticker.on("ticks", onTicks);
* ticker.on("connect", subscribe);
*
* function onTicks(ticks) {
* console.log("Ticks", ticks);
* }
*
* function subscribe() {
* const items = [738561];
* ticker.subscribe(items);
* ticker.setMode(ticker.modeFull, items);
* }
* ```
*
* Auto reconnection
* -----------------
* Auto reconnection is enabled by default and it can be disabled by passing `reconnect` param while initialising `KiteTicker`.
*
* Auto reconnection mechanism is based on [Exponential backoff](https://en.wikipedia.org/wiki/Exponential_backoff) algorithm in which
* next retry interval will be increased exponentially. `max_delay` and `max_tries` params can be used to tweak
* the alogrithm where `max_delay` is the maximum delay after which subsequent reconnection interval will become constant and
* `max_tries` is maximum number of retries before it quits reconnection.
*
* For example if `max_delay` is 60 seconds and `max_tries` is 50 then the first reconnection interval starts from
* minimum interval which is 2 seconds and keep increasing up to 60 seconds after which it becomes constant and when reconnection attempt
* is reached upto 50 then it stops reconnecting.
*
* Callback `reconnect` will be called with current reconnect attempt and next reconnect interval and
* `on_noreconnect` is called when reconnection attempts reaches max retries.
*
* @example
* Here is an example demonstrating auto reconnection.
* ```ts
* import { KiteTicker } from "kiteconnect-ts";
* const ticker = new KiteTicker({
* api_key: "api_key",
* access_token: "access_token"
* });
*
* // set autoreconnect with 10 maximum reconnections and 5 second interval
* ticker.autoReconnect(true, 10, 5)
* ticker.connect();
* ticker.on("ticks", onTicks);
* ticker.on("connect", subscribe);
*
* ticker.on("noreconnect", () => {
* console.log("noreconnect");
* });
*
* ticker.on("reconnect", (reconnect_count, reconnect_interval) => {
* console.log("Reconnecting: attempt - ", reconnect_count, " interval - ", reconnect_interval);
* });
*
* ticker.on("message", (binary_msg) => {
* console.log("Binary message", binary_msg);
* });
*
* function onTicks(ticks) {
* console.log("Ticks", ticks);
* }
*
* function subscribe() {
* const items = [738561];
* ticker.subscribe(items);
* ticker.setMode(ticker.modeFull, items);
* }
* ```
*/
export class KiteTicker {
private root = 'wss://ws.kite.trade/';
private api_key: string | null = null;
private access_token: string | null = null;
private read_timeout = 5; // seconds
private reconnect_max_delay = 0;
private reconnect_max_tries = 0;
// message flags (outgoing)
private mSubscribe = 'subscribe';
private mUnSubscribe = 'unsubscribe';
private mSetMode = 'mode';
// incoming
private readonly mAlert = 10;
private readonly mMessage = 11;
private readonly mLogout = 12;
private readonly mReload = 13;
private readonly mClearCache = 14;
// public constants
/**
* Set mode full
*/
public readonly modeFull = 'full'; // Full quote including market depth. 164 bytes.
/**
* Set mode quote
*/
public readonly modeQuote = 'quote'; // Quote excluding market depth. 52 bytes.
/**
* Set mode LTP
*/
public readonly modeLTP = 'ltp';
private ws: WebSocket | null = null;
private triggers: Record<TickerEvent, Function[]> = {
connect: [],
ticks: [],
disconnect: [],
error: [],
close: [],
reconnect: [],
noreconnect: [],
message: [],
order_update: [],
};
private read_timer: NodeJS.Timeout | null = null;
private last_read: number = 0;
private reconnect_timer: NodeJS.Timeout | null = null;
private auto_reconnect = false;
private current_reconnection_count = 0;
private last_reconnect_interval: number | null = 0;
private current_ws_url: string | null = null;
private readonly defaultReconnectMaxDelay = 60;
private readonly defaultReconnectMaxRetries = 50;
private readonly maximumReconnectMaxRetries = 300;
private readonly minimumReconnectMaxDelay = 5;
// segment constants
private readonly NseCM = 1;
private readonly NseFO = 2;
private readonly NseCD = 3;
private readonly BseCM = 4;
private readonly BseFO = 5;
private readonly BseCD = 6;
private readonly McxFO = 7;
private readonly McxSX = 8;
private readonly Indices = 9;
/**
* KiteTicker constructor
*
* @param params KiteTicker parameters
* @returns An instance of The KiteTicker class
*/
constructor(params: KiteTickerParams) {
this.root = params.root || this.root;
this.api_key = params.api_key;
this.access_token = params.access_token;
// Enable auto reconnect by default
if (!params.reconnect) params.reconnect = true;
this.autoReconnect(params.reconnect, params.max_retry, params.max_delay);
}
/**
* Auto reconnect settings
*
* @param t Enable or disable auto disconnect, defaults to false
* @param max_retry is maximum number re-connection attempts. Defaults to 50 attempts and maximum up to 300 attempts.
* @param max_delay in seconds is the maximum delay after which subsequent re-connection interval will become constant. Defaults to 60s and minimum acceptable value is 5s.
*/
autoReconnect(t: boolean, max_retry?: number, max_delay?: number) {
this.auto_reconnect = t === true;
// Set default values
max_retry = max_retry || this.defaultReconnectMaxRetries;
max_delay = max_delay || this.defaultReconnectMaxDelay;
// Set reconnect constraints
this.reconnect_max_tries =
max_retry >= this.maximumReconnectMaxRetries
? this.maximumReconnectMaxRetries
: max_retry;
this.reconnect_max_delay =
max_delay <= this.minimumReconnectMaxDelay
? this.minimumReconnectMaxDelay
: max_delay;
}
/**
* Initiate a websocket connection
*/
connect() {
// Skip if its already connected
if (
this.ws &&
(this.ws.readyState == ws.CONNECTING || this.ws.readyState == ws.OPEN)
)
return;
const url =
this.root +
'?api_key=' +
this.api_key +
'&access_token=' +
this.access_token +
'&uid=' +
new Date().getTime().toString();
this.ws = new WebSocket(url, {
headers: {
'X-Kite-Version': '3',
'User-Agent': getUserAgent(),
},
});
this.ws.binaryType = 'arraybuffer';
this.ws.onopen = () => {
// Reset last reconnect interval
this.last_reconnect_interval = null;
// Reset current_reconnection_count attempt
this.current_reconnection_count = 0;
// Store current open connection url to check for auto re-connection.
if (!this.current_ws_url) this.current_ws_url = url;
// Trigger on connect event
this.trigger('connect');
// If there isn't an incoming message in n seconds, assume disconnection.
if (this.read_timer) clearInterval(this.read_timer);
this.last_read = Date.now();
this.read_timer = setInterval(() => {
if ((Date.now() - this.last_read) / 1000 >= this.read_timeout) {
// reset current_ws_url incase current connection times out
// This is determined when last heart beat received time interval
// exceeds read_timeout value
this.current_ws_url = null;
if (this.ws) this.ws.close();
if (this.read_timer) clearInterval(this.read_timer);
this.triggerDisconnect();
}
}, this.read_timeout * 1000);
};
this.ws.onmessage = (e) => {
// Binary tick data.
if (e.data instanceof ArrayBuffer) {
// Trigger on message event when binary message is received
this.trigger('message', [e.data]);
if (e.data.byteLength > 2) {
const d = this.parseBinary(e.data);
if (d) this.trigger('ticks', [d]);
}
} else {
this.parseTextMessage(e.data as string);
}
// Set last read time to check for connection timeout
this.last_read = Date.now();
};
this.ws.onerror = (e) => {
this.trigger('error', [e]);
// Force close to avoid ghost connections
if (this.ws && this.ws.readyState == ws.OPEN) this.ws.close();
};
this.ws.onclose = (e) => {
this.trigger('close', [e]);
// the ws id doesn't match the current global id,
// meaning it's a ghost close event. just ignore.
if (this.current_ws_url && url != this.current_ws_url) return;
this.triggerDisconnect(e);
};
}
/**
* Close the websocket connection
*/
disconnect() {
if (
this.ws &&
this.ws.readyState != ws.CLOSING &&
this.ws.readyState != ws.CLOSED
) {
this.ws.close();
}
}
/**
* Check if the ticker is connected
*
* @returns `true` if the ticker is connected or `false` otherwise.
*/
connected() {
if (this.ws && this.ws.readyState == ws.OPEN) {
return true;
} else {
return false;
}
}
/**
* Register websocket event callbacks. See {@link TickerEvent} for all available events.
*
* @param e Event to register callback on.
* @param callback Callback function
*
* @example
* ```ts
* ticker.on("ticks", callback);
* ticker.on("connect", callback);
* ticker.on("disconnect", callback);
* ```
*
* Tick structure (passed to the tick callback you assign):
* ```
* [
* {
* tradable: true,
* mode: 'full',
* instrument_token: 208947,
* last_price: 3939,
* last_quantity: 1,
* average_price: 3944.77,
* volume: 28940,
* buy_quantity: 4492,
* sell_quantity: 4704,
* ohlc: { open: 3927, high: 3955, low: 3927, close: 3906 },
* change: 0.8448540706605223,
* last_trade_time: 1515491369,
* timestamp: 1515491373,
* oi: 24355,
* oi_day_high: 0,
* oi_day_low: 0,
* depth: {
* buy: [
* { quantity: 59, price: 3223, orders: 5 },
* { quantity: 164, price: 3222, orders: 15 },
* { quantity: 123, price: 3221, orders: 7 },
* { quantity: 48, price: 3220, orders: 7 },
* { quantity: 33, price: 3219, orders: 5 },
* ],
* sell: [
* { quantity: 115, price: 3224, orders: 15 },
* { quantity: 50, price: 3225, orders: 5 },
* { quantity: 175, price: 3226, orders: 14 },
* { quantity: 49, price: 3227, orders: 10 },
* { quantity: 106, price: 3228, orders: 13 },
* ],
* },
* },
* ]
* ```
*/
on<K extends TickerEvent>(e: K, callback: TickerEvents[K]) {
if (this.triggers.hasOwnProperty(e)) {
this.triggers[e].push(callback);
}
}
/**
* Subscribe to array of tokens
*
* @param tokens Array of tokens to be subscribed
*
* @example
* ```ts
* ticker.subscribe([738561]);
* ```
*/
subscribe(tokens: number[]) {
if (tokens.length > 0) {
this.send({ a: this.mSubscribe, v: tokens });
}
return tokens;
}
/**
* Unsubscribe from array of tokens
*
* @param tokens Array of tokens to be unsubscribed
*
* @example
* ```ts
* ticker.unsubscribe([738561]);
* ```
*/
unsubscribe(tokens: number[]) {
if (tokens.length > 0) {
this.send({ a: this.mUnSubscribe, v: tokens });
}
return tokens;
}
/**
* Set mode for an array of tokens
*
* @param mode mode to set
* @param tokens Array of tokens to be subscribed
*
* @example
* ```
* ticker.setMode(ticker.modeFull, [738561]);
* ```
*/
setMode(mode: 'ltp' | 'quote' | 'full', tokens: number[]) {
if (tokens.length > 0) {
this.send({ a: this.mSetMode, v: [mode, tokens] });
}
return tokens;
}
private triggerDisconnect(e?: any) {
this.ws = null;
this.trigger('disconnect', [e]);
if (this.auto_reconnect) this.attemptReconnection();
}
// send a message via the socket
// automatically encodes json if possible
private send(message: any) {
if (!this.ws || this.ws.readyState != ws.OPEN) return;
try {
if (typeof message == 'object') {
message = JSON.stringify(message);
}
this.ws.send(message);
} catch (e) {
this.ws.close();
}
}
// trigger event callbacks
private trigger(e: TickerEvent, args?: any) {
if (!this.triggers[e]) return;
for (let n = 0; n < this.triggers[e].length; n++) {
this.triggers[e][n]?.apply(this.triggers[e][n], args ? args : []);
}
}
private parseTextMessage(dataString: string) {
let data: any;
try {
data = JSON.parse(dataString);
} catch (e) {
return;
}
if (data.type === 'order') {
this.trigger('order_update', [data.data]);
}
}
// parse received binary message. each message is a combination of multiple tick packets
// [2-bytes num packets][size1][tick1][size2][tick2] ...
private parseBinary(binpacks: ArrayBuffer) {
const packets = this.splitPackets(binpacks);
const ticks = [];
for (let n = 0; n < packets.length; n++) {
const bin = packets[n]!;
const instrument_token = this.buf2long(bin.slice(0, 4));
const segment = instrument_token & 0xff;
let tradable = true;
if (segment === this.Indices) tradable = false;
// Add price divisor based on segment
let divisor = 100.0;
if (segment === this.NseCD) {
divisor = 10000000.0;
} else if (segment == this.BseCD) {
divisor = 10000.0;
}
// Parse LTP
if (bin.byteLength === 8) {
ticks.push({
tradable: tradable,
mode: this.modeLTP,
instrument_token: instrument_token,
last_price: this.buf2long(bin.slice(4, 8)) / divisor,
});
// Parse indices quote and full mode
} else if (bin.byteLength === 28 || bin.byteLength === 32) {
let mode = this.modeQuote;
if (bin.byteLength === 32) mode = this.modeFull;
const tick: any = {
tradable: tradable,
mode: mode,
instrument_token: instrument_token,
last_price: this.buf2long(bin.slice(4, 8)) / divisor,
ohlc: {
high: this.buf2long(bin.slice(8, 12)) / divisor,
low: this.buf2long(bin.slice(12, 16)) / divisor,
open: this.buf2long(bin.slice(16, 20)) / divisor,
close: this.buf2long(bin.slice(20, 24)) / divisor,
},
change: this.buf2long(bin.slice(24, 28)),
};
// Compute the change price using close price and last price
if (tick.ohlc.close != 0) {
tick.change =
((tick.last_price - tick.ohlc.close) * 100) / tick.ohlc.close;
}
// Full mode with timestamp in seconds
if (bin.byteLength === 32) {
tick.exchange_timestamp = null;
const timestamp = this.buf2long(bin.slice(28, 32));
if (timestamp) tick.exchange_timestamp = new Date(timestamp * 1000);
}
ticks.push(tick);
} else if (bin.byteLength === 44 || bin.byteLength === 184) {
let mode = this.modeQuote;
if (bin.byteLength === 184) mode = this.modeFull;
const tick: any = {
tradable: tradable,
mode: mode,
instrument_token: instrument_token,
last_price: this.buf2long(bin.slice(4, 8)) / divisor,
last_traded_quantity: this.buf2long(bin.slice(8, 12)),
average_traded_price: this.buf2long(bin.slice(12, 16)) / divisor,
volume_traded: this.buf2long(bin.slice(16, 20)),
total_buy_quantity: this.buf2long(bin.slice(20, 24)),
total_sell_quantity: this.buf2long(bin.slice(24, 28)),
ohlc: {
open: this.buf2long(bin.slice(28, 32)) / divisor,
high: this.buf2long(bin.slice(32, 36)) / divisor,
low: this.buf2long(bin.slice(36, 40)) / divisor,
close: this.buf2long(bin.slice(40, 44)) / divisor,
},
};
// Compute the change price using close price and last price
if (tick.ohlc.close != 0) {
tick.change =
((tick.last_price - tick.ohlc.close) * 100) / tick.ohlc.close;
}
// Parse full mode
if (bin.byteLength === 184) {
// Parse last trade time
tick.last_trade_time = null;
const last_trade_time = this.buf2long(bin.slice(44, 48));
if (last_trade_time)
tick.last_trade_time = new Date(last_trade_time * 1000);
// Parse timestamp
tick.exchange_timestamp = null;
const timestamp = this.buf2long(bin.slice(60, 64));
if (timestamp) tick.exchange_timestamp = new Date(timestamp * 1000);
// Parse OI
tick.oi = this.buf2long(bin.slice(48, 52));
tick.oi_day_high = this.buf2long(bin.slice(52, 56));
tick.oi_day_low = this.buf2long(bin.slice(56, 60));
tick.depth = {
buy: [],
sell: [],
};
let s = 0;
const depth = bin.slice(64, 184);
for (let i = 0; i < 10; i++) {
s = i * 12;
tick.depth[i < 5 ? 'buy' : 'sell'].push({
quantity: this.buf2long(depth.slice(s, s + 4)),
price: this.buf2long(depth.slice(s + 4, s + 8)) / divisor,
orders: this.buf2long(depth.slice(s + 8, s + 10)),
});
}
}
ticks.push(tick);
}
}
return ticks;
}
// split one long binary message into individual tick packets
private splitPackets(bin: ArrayBuffer) {
// number of packets
const num = this.buf2long(bin.slice(0, 2));
let j = 2;
const packets = [];
for (let i = 0; i < num; i++) {
// first two bytes is the packet length
const size = this.buf2long(bin.slice(j, j + 2)),
packet = bin.slice(j + 2, j + 2 + size);
packets.push(packet);
j += 2 + size;
}
return packets;
}
private attemptReconnection() {
// Try reconnecting only so many times.
if (this.current_reconnection_count > this.reconnect_max_tries) {
this.trigger('noreconnect');
process.exit(1);
}
if (this.current_reconnection_count > 0) {
this.last_reconnect_interval = Math.pow(
2,
this.current_reconnection_count
);
} else if (!this.last_reconnect_interval) {
this.last_reconnect_interval = 1;
}
if (this.last_reconnect_interval > this.reconnect_max_delay) {
this.last_reconnect_interval = this.reconnect_max_delay;
}
this.current_reconnection_count++;
this.trigger('reconnect', [
this.current_reconnection_count,
this.last_reconnect_interval,
]);
this.reconnect_timer = setTimeout(() => {
this.connect();
}, this.last_reconnect_interval * 1000);
}
// Big endian byte array to long.
private buf2long(buf: ArrayBuffer) {
const b = new Uint8Array(buf);
let val = 0;
const len = b.length;
for (let i = 0, j = len - 1; i < len; i++, j--) {
val += b[j]! << (i * 8);
}
return val;
}
}
export * from './types';