fyers-api-v3 1.3.4 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HSM/datasocket.min.js +1 -1
- package/HSM/mapper.js +220 -220
- package/HSM_Package/hslib.js +4 -4
- package/README.md +171 -171
- package/apiService/apiService.js +1026 -995
- package/config/config.js +30 -29
- package/errorHandler/errorHandler.js +36 -36
- package/index.js +11 -9
- package/logger/log.js +61 -61
- package/obfuscate.js +24 -24
- package/ordersocket/fyersSocket.js +325 -325
- package/ordersocket/mapper.js +78 -78
- package/package.json +25 -24
- package/sample/api.js +45 -45
- package/sample/datasocket.js +24 -24
- package/sample/ordersocket.js +29 -29
- package/sample/tbtsocket.js +25 -0
- package/tbtsocket/models.js +172 -0
- package/tbtsocket/msg.js +5163 -0
- package/tbtsocket/tbtSocket.js +339 -0
package/ordersocket/mapper.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
const ordersMapper = {
|
|
2
|
-
'client_id': 'clientId',
|
|
3
|
-
'id': 'id',
|
|
4
|
-
'id_parent': 'parentId',
|
|
5
|
-
'id_exchange': 'exchOrdId',
|
|
6
|
-
'qty': 'qty',
|
|
7
|
-
'qty_remaining': 'remainingQuantity',
|
|
8
|
-
'qty_filled': 'filledQty',
|
|
9
|
-
'price_limit': 'limitPrice',
|
|
10
|
-
'price_stop': 'stopPrice',
|
|
11
|
-
'price_traded': 'tradedPrice',
|
|
12
|
-
'ord_type': 'type',
|
|
13
|
-
'fy_token': 'fyToken',
|
|
14
|
-
'exchange': 'exchange',
|
|
15
|
-
'segment': 'segment',
|
|
16
|
-
'symbol': 'symbol',
|
|
17
|
-
'instrument': 'instrument',
|
|
18
|
-
'oms_msg': 'message',
|
|
19
|
-
'offline_flag': 'offlineOrder',
|
|
20
|
-
'time_oms': 'orderDateTime',
|
|
21
|
-
'validity': 'orderValidity',
|
|
22
|
-
'product_type': 'productType',
|
|
23
|
-
'tran_side': 'side',
|
|
24
|
-
'ord_status': 'status',
|
|
25
|
-
'ord_source': 'source',
|
|
26
|
-
'symbol_exch': 'ex_sym',
|
|
27
|
-
'symbol_desc': 'description',
|
|
28
|
-
'ordertag': 'orderTag'
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const positionbookMapper = {
|
|
32
|
-
'symbol': 'symbol',
|
|
33
|
-
'id': 'id',
|
|
34
|
-
'buy_avg': 'buyAvg',
|
|
35
|
-
'buy_qty': 'buyQty',
|
|
36
|
-
'buy_val': 'buyVal',
|
|
37
|
-
'sell_avg': 'sellAvg',
|
|
38
|
-
'sell_qty': 'sellQty',
|
|
39
|
-
'sell_val': 'sellVal',
|
|
40
|
-
'net_avg': 'netAvg',
|
|
41
|
-
'net_qty': 'netQty',
|
|
42
|
-
'tran_side': 'side',
|
|
43
|
-
'qty': 'qty',
|
|
44
|
-
'product_type': 'productType',
|
|
45
|
-
'pl_realized': 'realized_profit',
|
|
46
|
-
'rbirefrate': 'rbiRefRate',
|
|
47
|
-
'fy_token': 'fyToken',
|
|
48
|
-
'exchange': 'exchange',
|
|
49
|
-
'segment': 'segment',
|
|
50
|
-
'day_buy_qty': 'dayBuyQty',
|
|
51
|
-
'day_sell_qty': 'daySellQty',
|
|
52
|
-
'cf_buy_qty': 'cfBuyQty',
|
|
53
|
-
'cf_sell_qty': 'cfSellQty',
|
|
54
|
-
'qty_multiplier': 'qtyMulti_com',
|
|
55
|
-
'pl_total': 'pl',
|
|
56
|
-
'cross_curr_flag': 'crossCurrency',
|
|
57
|
-
'pl_unrealized': 'unrealized_profit',
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const tradebookMapper = {
|
|
61
|
-
'id_fill': 'tradeNumber',
|
|
62
|
-
'id': 'orderNumber',
|
|
63
|
-
'qty_traded': 'tradedQty',
|
|
64
|
-
'price_traded': 'tradePrice',
|
|
65
|
-
'traded_val': 'tradeValue',
|
|
66
|
-
'product_type': 'productType',
|
|
67
|
-
'client_id': 'clientId',
|
|
68
|
-
'id_exchange': 'exchangeOrderNo',
|
|
69
|
-
'ord_type': 'orderType',
|
|
70
|
-
'tran_side': 'side',
|
|
71
|
-
'symbol': 'symbol',
|
|
72
|
-
'fill_time': 'orderDateTime',
|
|
73
|
-
'fy_token': 'fyToken',
|
|
74
|
-
'exchange': 'exchange',
|
|
75
|
-
'segment': 'segment',
|
|
76
|
-
'ordertag': 'orderTag'
|
|
77
|
-
}
|
|
78
|
-
|
|
1
|
+
const ordersMapper = {
|
|
2
|
+
'client_id': 'clientId',
|
|
3
|
+
'id': 'id',
|
|
4
|
+
'id_parent': 'parentId',
|
|
5
|
+
'id_exchange': 'exchOrdId',
|
|
6
|
+
'qty': 'qty',
|
|
7
|
+
'qty_remaining': 'remainingQuantity',
|
|
8
|
+
'qty_filled': 'filledQty',
|
|
9
|
+
'price_limit': 'limitPrice',
|
|
10
|
+
'price_stop': 'stopPrice',
|
|
11
|
+
'price_traded': 'tradedPrice',
|
|
12
|
+
'ord_type': 'type',
|
|
13
|
+
'fy_token': 'fyToken',
|
|
14
|
+
'exchange': 'exchange',
|
|
15
|
+
'segment': 'segment',
|
|
16
|
+
'symbol': 'symbol',
|
|
17
|
+
'instrument': 'instrument',
|
|
18
|
+
'oms_msg': 'message',
|
|
19
|
+
'offline_flag': 'offlineOrder',
|
|
20
|
+
'time_oms': 'orderDateTime',
|
|
21
|
+
'validity': 'orderValidity',
|
|
22
|
+
'product_type': 'productType',
|
|
23
|
+
'tran_side': 'side',
|
|
24
|
+
'ord_status': 'status',
|
|
25
|
+
'ord_source': 'source',
|
|
26
|
+
'symbol_exch': 'ex_sym',
|
|
27
|
+
'symbol_desc': 'description',
|
|
28
|
+
'ordertag': 'orderTag'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const positionbookMapper = {
|
|
32
|
+
'symbol': 'symbol',
|
|
33
|
+
'id': 'id',
|
|
34
|
+
'buy_avg': 'buyAvg',
|
|
35
|
+
'buy_qty': 'buyQty',
|
|
36
|
+
'buy_val': 'buyVal',
|
|
37
|
+
'sell_avg': 'sellAvg',
|
|
38
|
+
'sell_qty': 'sellQty',
|
|
39
|
+
'sell_val': 'sellVal',
|
|
40
|
+
'net_avg': 'netAvg',
|
|
41
|
+
'net_qty': 'netQty',
|
|
42
|
+
'tran_side': 'side',
|
|
43
|
+
'qty': 'qty',
|
|
44
|
+
'product_type': 'productType',
|
|
45
|
+
'pl_realized': 'realized_profit',
|
|
46
|
+
'rbirefrate': 'rbiRefRate',
|
|
47
|
+
'fy_token': 'fyToken',
|
|
48
|
+
'exchange': 'exchange',
|
|
49
|
+
'segment': 'segment',
|
|
50
|
+
'day_buy_qty': 'dayBuyQty',
|
|
51
|
+
'day_sell_qty': 'daySellQty',
|
|
52
|
+
'cf_buy_qty': 'cfBuyQty',
|
|
53
|
+
'cf_sell_qty': 'cfSellQty',
|
|
54
|
+
'qty_multiplier': 'qtyMulti_com',
|
|
55
|
+
'pl_total': 'pl',
|
|
56
|
+
'cross_curr_flag': 'crossCurrency',
|
|
57
|
+
'pl_unrealized': 'unrealized_profit',
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const tradebookMapper = {
|
|
61
|
+
'id_fill': 'tradeNumber',
|
|
62
|
+
'id': 'orderNumber',
|
|
63
|
+
'qty_traded': 'tradedQty',
|
|
64
|
+
'price_traded': 'tradePrice',
|
|
65
|
+
'traded_val': 'tradeValue',
|
|
66
|
+
'product_type': 'productType',
|
|
67
|
+
'client_id': 'clientId',
|
|
68
|
+
'id_exchange': 'exchangeOrderNo',
|
|
69
|
+
'ord_type': 'orderType',
|
|
70
|
+
'tran_side': 'side',
|
|
71
|
+
'symbol': 'symbol',
|
|
72
|
+
'fill_time': 'orderDateTime',
|
|
73
|
+
'fy_token': 'fyToken',
|
|
74
|
+
'exchange': 'exchange',
|
|
75
|
+
'segment': 'segment',
|
|
76
|
+
'ordertag': 'orderTag'
|
|
77
|
+
}
|
|
78
|
+
|
|
79
79
|
module.exports = { "orders": ordersMapper, "position": positionbookMapper, "tradebook": tradebookMapper }
|
package/package.json
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "fyers-api-v3",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "connect to fyers API",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"author": "",
|
|
10
|
-
"license": "ISC",
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"axios": "^1.3.5",
|
|
13
|
-
"jsonwebtoken": "^9.0.1",
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "fyers-api-v3",
|
|
3
|
+
"version": "1.4.2",
|
|
4
|
+
"description": "connect to fyers API",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"axios": "^1.3.5",
|
|
13
|
+
"jsonwebtoken": "^9.0.1",
|
|
14
|
+
"protobufjs": "^7.4.0",
|
|
15
|
+
"ws": "^8.13.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"javascript-obfuscator": "^4.0.2"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/FyersDev/fyers-api-sample-code/tree/sample_v3/v3/node"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://myapi.fyers.in/docsv3"
|
|
25
|
+
}
|
package/sample/api.js
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
var fyersModel= require("../index.js").fyersModel
|
|
2
|
-
|
|
3
|
-
var fyers= new fyersModel()
|
|
4
|
-
|
|
5
|
-
fyers.setAppId("QC7T0H8425-100")
|
|
6
|
-
|
|
7
|
-
fyers.setRedirectUrl("https://www.google.com")
|
|
8
|
-
// generate authcode
|
|
9
|
-
////////////////////////////
|
|
10
|
-
|
|
11
|
-
// var URL=fyers.generateAuthCode()
|
|
12
|
-
|
|
13
|
-
// console.log(URL)
|
|
14
|
-
|
|
15
|
-
// after getting authcode
|
|
16
|
-
|
|
17
|
-
////////////////////////////
|
|
18
|
-
|
|
19
|
-
// generate accesstoken
|
|
20
|
-
////////////////////////////
|
|
21
|
-
// var authcode=""
|
|
22
|
-
|
|
23
|
-
// fyers.generate_access_token({"client_id":fyers.AppID,"secret_key":"4362L6SS2C","auth_code":authcode}).then((response)=>{
|
|
24
|
-
// console.log(response)
|
|
25
|
-
// })
|
|
26
|
-
////////////////////////////
|
|
27
|
-
|
|
28
|
-
fyers.setAccessToken("")
|
|
29
|
-
|
|
30
|
-
fyers.get_profile().then((response)=>{
|
|
31
|
-
console.log(response)
|
|
32
|
-
}).catch((err)=>{
|
|
33
|
-
console.log(err)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
fyers.getQuotes(["NSE:SBIN-EQ","NSE:TCS-EQ"]).then((response)=>{
|
|
37
|
-
console.log(response)
|
|
38
|
-
}).catch((err)=>{
|
|
39
|
-
console.log(err)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
fyers.getMarketDepth({"symbol":["NSE:SBIN-EQ","NSE:TCS-EQ"],"ohlcv_flag":1}).then((response)=>{
|
|
43
|
-
console.log(response)
|
|
44
|
-
}).catch((err)=>{
|
|
45
|
-
console.log(err)
|
|
1
|
+
var fyersModel= require("../index.js").fyersModel
|
|
2
|
+
|
|
3
|
+
var fyers= new fyersModel()
|
|
4
|
+
|
|
5
|
+
fyers.setAppId("QC7T0H8425-100")
|
|
6
|
+
|
|
7
|
+
fyers.setRedirectUrl("https://www.google.com")
|
|
8
|
+
// generate authcode
|
|
9
|
+
////////////////////////////
|
|
10
|
+
|
|
11
|
+
// var URL=fyers.generateAuthCode()
|
|
12
|
+
|
|
13
|
+
// console.log(URL)
|
|
14
|
+
|
|
15
|
+
// after getting authcode
|
|
16
|
+
|
|
17
|
+
////////////////////////////
|
|
18
|
+
|
|
19
|
+
// generate accesstoken
|
|
20
|
+
////////////////////////////
|
|
21
|
+
// var authcode=""
|
|
22
|
+
|
|
23
|
+
// fyers.generate_access_token({"client_id":fyers.AppID,"secret_key":"4362L6SS2C","auth_code":authcode}).then((response)=>{
|
|
24
|
+
// console.log(response)
|
|
25
|
+
// })
|
|
26
|
+
////////////////////////////
|
|
27
|
+
|
|
28
|
+
fyers.setAccessToken("")
|
|
29
|
+
|
|
30
|
+
fyers.get_profile().then((response)=>{
|
|
31
|
+
console.log(response)
|
|
32
|
+
}).catch((err)=>{
|
|
33
|
+
console.log(err)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
fyers.getQuotes(["NSE:SBIN-EQ","NSE:TCS-EQ"]).then((response)=>{
|
|
37
|
+
console.log(response)
|
|
38
|
+
}).catch((err)=>{
|
|
39
|
+
console.log(err)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
fyers.getMarketDepth({"symbol":["NSE:SBIN-EQ","NSE:TCS-EQ"],"ohlcv_flag":1}).then((response)=>{
|
|
43
|
+
console.log(response)
|
|
44
|
+
}).catch((err)=>{
|
|
45
|
+
console.log(err)
|
|
46
46
|
})
|
package/sample/datasocket.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
let DataSocket = require("../index.js").fyersDataSocket;
|
|
2
|
-
|
|
3
|
-
var skt= DataSocket.getInstance("")
|
|
4
|
-
|
|
5
|
-
skt.on("connect",function(){skt.subscribe(['NSE:IDEA-EQ',"NSE:SBIN-EQ"],false,1)
|
|
6
|
-
skt.mode(skt.FullMode,1)
|
|
7
|
-
console.log(skt.isConnected())
|
|
8
|
-
// skt.mode(skt.LiteMode,[11,12,13])
|
|
9
|
-
// skt.channelresume(2)
|
|
10
|
-
// skt.unsubscribe(['NSE:IDEA-EQ'],false,1)
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
skt.on("message",function(message){
|
|
14
|
-
console.log({"TEST":message})
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
skt.on("error",function(message){
|
|
18
|
-
console.log("erroris",message)
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
skt.on("close",function(){
|
|
22
|
-
console.log("socket closed")
|
|
23
|
-
})
|
|
24
|
-
skt.connect()
|
|
1
|
+
let DataSocket = require("../index.js").fyersDataSocket;
|
|
2
|
+
|
|
3
|
+
var skt= DataSocket.getInstance("")
|
|
4
|
+
|
|
5
|
+
skt.on("connect",function(){skt.subscribe(['NSE:IDEA-EQ',"NSE:SBIN-EQ"],false,1)
|
|
6
|
+
skt.mode(skt.FullMode,1)
|
|
7
|
+
console.log(skt.isConnected())
|
|
8
|
+
// skt.mode(skt.LiteMode,[11,12,13])
|
|
9
|
+
// skt.channelresume(2)
|
|
10
|
+
// skt.unsubscribe(['NSE:IDEA-EQ'],false,1)
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
skt.on("message",function(message){
|
|
14
|
+
console.log({"TEST":message})
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
skt.on("error",function(message){
|
|
18
|
+
console.log("erroris",message)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
skt.on("close",function(){
|
|
22
|
+
console.log("socket closed")
|
|
23
|
+
})
|
|
24
|
+
skt.connect()
|
|
25
25
|
skt.autoreconnect()
|
package/sample/ordersocket.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
var fyersOrderSocket= require("../index.js").fyersOrderSocket
|
|
2
|
-
|
|
3
|
-
var skt=new fyersOrderSocket("")
|
|
4
|
-
|
|
5
|
-
skt.on("error",function (errmsg) {
|
|
6
|
-
console.log(errmsg)
|
|
7
|
-
})
|
|
8
|
-
skt.on('general',function (msg) {
|
|
9
|
-
console.log(msg)
|
|
10
|
-
})
|
|
11
|
-
skt.on('connect',function () {
|
|
12
|
-
skt.subscribe([skt.orderUpdates,skt.tradeUpdates,skt.positionUpdates,skt.edis,skt.pricealerts])
|
|
13
|
-
console.log(skt.isConnected())
|
|
14
|
-
})
|
|
15
|
-
skt.on('close',function () {
|
|
16
|
-
console.log('closed')
|
|
17
|
-
})
|
|
18
|
-
skt.on('orders',function (msg) {
|
|
19
|
-
console.log("orders",msg)
|
|
20
|
-
})
|
|
21
|
-
skt.on('trades',function (msg) {
|
|
22
|
-
console.log('trades',msg)
|
|
23
|
-
})
|
|
24
|
-
skt.on('positions',function (msg) {
|
|
25
|
-
console.log('positions',msg)
|
|
26
|
-
})
|
|
27
|
-
skt.autoreconnect()
|
|
28
|
-
skt.connect()
|
|
29
|
-
|
|
1
|
+
var fyersOrderSocket= require("../index.js").fyersOrderSocket
|
|
2
|
+
|
|
3
|
+
var skt=new fyersOrderSocket("")
|
|
4
|
+
|
|
5
|
+
skt.on("error",function (errmsg) {
|
|
6
|
+
console.log(errmsg)
|
|
7
|
+
})
|
|
8
|
+
skt.on('general',function (msg) {
|
|
9
|
+
console.log(msg)
|
|
10
|
+
})
|
|
11
|
+
skt.on('connect',function () {
|
|
12
|
+
skt.subscribe([skt.orderUpdates,skt.tradeUpdates,skt.positionUpdates,skt.edis,skt.pricealerts])
|
|
13
|
+
console.log(skt.isConnected())
|
|
14
|
+
})
|
|
15
|
+
skt.on('close',function () {
|
|
16
|
+
console.log('closed')
|
|
17
|
+
})
|
|
18
|
+
skt.on('orders',function (msg) {
|
|
19
|
+
console.log("orders",msg)
|
|
20
|
+
})
|
|
21
|
+
skt.on('trades',function (msg) {
|
|
22
|
+
console.log('trades',msg)
|
|
23
|
+
})
|
|
24
|
+
skt.on('positions',function (msg) {
|
|
25
|
+
console.log('positions',msg)
|
|
26
|
+
})
|
|
27
|
+
skt.autoreconnect()
|
|
28
|
+
skt.connect()
|
|
29
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const { SubscriptionModes } = require("../tbtsocket/models.js")
|
|
2
|
+
|
|
3
|
+
var fyersOrderSocket= require("../index.js").fyersTbtSocket
|
|
4
|
+
|
|
5
|
+
var skt=new fyersOrderSocket("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuZnllcnMuaW4iLCJpYXQiOjE3NDA2MzQ4MzcsImV4cCI6MTc0MDcwMjYzNywibmJmIjoxNzQwNjM0ODM3LCJhdWQiOlsieDowIl0sInN1YiI6ImFjY2Vzc190b2tlbiIsImF0X2hhc2giOiJnQUFBQUFCbnZfclY3RUhfMEM2RGwyOWhGdE9kT0tlM0c0QnctbnZ1LUNUMUdwd09HaDdBbkMxRlQ2ZVJRanFWUWl0WFI4MG1rTWZRYVJJdi11eXJJWUF6cUxsYVV1cldkMm4yRnhkVUZHMVpTN3VyWF8zOWJScz0iLCJkaXNwbGF5X25hbWUiOiJQQVRJTCBQSVlVU0ggU1VSRVNIIiwib21zIjoiSzEiLCJoc21fa2V5IjoiYTM1MmYxN2I5MmRiZTU4NTgyMzg2MmRkYjMxZWE1NTY1OGE0MTc2OTM4MmM3NDNlYTQyMjg1OGYiLCJpc0RkcGlFbmFibGVkIjoiTiIsImlzTXRmRW5hYmxlZCI6Ik4iLCJmeV9pZCI6IlhQMDE0NzAiLCJhcHBUeXBlIjoxMDEsInBvYV9mbGFnIjoiTiJ9.PY8BHrSRhIP9DD8iLmVfjQz13Nk_A0f_bLuyiUQsZjs")
|
|
6
|
+
|
|
7
|
+
skt.on("error",function (errmsg) {
|
|
8
|
+
console.log(errmsg)
|
|
9
|
+
})
|
|
10
|
+
skt.on('open', function() {
|
|
11
|
+
skt.subscribe(['NSE:NIFTY25FEBFUT', 'NSE:IOC25FEBFUT', 'NSE:BANKNIFTY25FEBFUT', 'NSE:FINNIFTY25FEBFUT', 'NSE:TCS25FEBFUT'], '1', SubscriptionModes.DEPTH)
|
|
12
|
+
skt.switchChannel([], ['1'])
|
|
13
|
+
})
|
|
14
|
+
skt.on('depth',function (ticker, data) {
|
|
15
|
+
console.log('depth',ticker, data)
|
|
16
|
+
})
|
|
17
|
+
skt.on('close',function () {
|
|
18
|
+
console.log('closed')
|
|
19
|
+
})
|
|
20
|
+
skt.on('servererror',function (msg) {
|
|
21
|
+
console.log('serverr', msg)
|
|
22
|
+
})
|
|
23
|
+
skt.autoreconnect()
|
|
24
|
+
skt.connect()
|
|
25
|
+
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// Import protobufjs (make sure you've installed it: npm install protobufjs)
|
|
2
|
+
const protobuf = require('protobufjs');
|
|
3
|
+
|
|
4
|
+
const SubscriptionModes = Object.freeze({
|
|
5
|
+
DEPTH: 'depth',
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
class Depth {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.tbq = 0;
|
|
11
|
+
this.tsq = 0;
|
|
12
|
+
this.bidprice = new Array(50).fill(0.0);
|
|
13
|
+
this.askprice = new Array(50).fill(0.0);
|
|
14
|
+
this.bidqty = new Array(50).fill(0);
|
|
15
|
+
this.askqty = new Array(50).fill(0);
|
|
16
|
+
this.bidordn = new Array(50).fill(0);
|
|
17
|
+
this.askordn = new Array(50).fill(0);
|
|
18
|
+
this.snapshot = false;
|
|
19
|
+
this.timestamp = 0;
|
|
20
|
+
this.sendtime = 0;
|
|
21
|
+
this.seqNo = 0;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
toString() {
|
|
25
|
+
return `Depth{ts: ${this.timestamp}, send_ts: ${this.sendtime}, tbq: ${this.tbq}, tsq: ${this.tsq}, ` +
|
|
26
|
+
`bidprice: ${this.bidprice}, askprice: ${this.askprice}, ` +
|
|
27
|
+
`bidqty: ${this.bidqty}, askqty: ${this.askqty}, ` +
|
|
28
|
+
`bidordn: ${this.bidordn}, askordn: ${this.askordn}, ` +
|
|
29
|
+
`snapshot: ${this.snapshot}, sNo: ${this.seqNo} }`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Adds depth data from a protobuf message.
|
|
34
|
+
* @param {Object} currdata - The protobuf message.
|
|
35
|
+
* @param {boolean} isSnapshot - Whether this is a snapshot.
|
|
36
|
+
*/
|
|
37
|
+
_addDepth(currdata, isSnapshot) {
|
|
38
|
+
// Check if the 'depth' field exists.
|
|
39
|
+
if (currdata.depth != null) {
|
|
40
|
+
this.snapshot = isSnapshot;
|
|
41
|
+
|
|
42
|
+
// Using wrapper types, so the values are in the `value` property.
|
|
43
|
+
if (currdata.depth.tbq != null) {
|
|
44
|
+
this.tbq = currdata.depth.tbq.value / 1;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (currdata.depth.tsq != null) {
|
|
48
|
+
this.tsq = currdata.depth.tsq.value / 1;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Process asks if present.
|
|
52
|
+
if (Array.isArray(currdata.depth.asks)) {
|
|
53
|
+
currdata.depth.asks.forEach((ask, i) => {
|
|
54
|
+
if (ask.price != null) {
|
|
55
|
+
this.askprice[i] = ask.price.value / 100;
|
|
56
|
+
}
|
|
57
|
+
if (ask.qty != null) {
|
|
58
|
+
this.askqty[i] = ask.qty.value;
|
|
59
|
+
}
|
|
60
|
+
if (ask.nord != null) {
|
|
61
|
+
this.askordn[i] = ask.nord.value;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Process bids if present.
|
|
67
|
+
if (Array.isArray(currdata.depth.bids)) {
|
|
68
|
+
currdata.depth.bids.forEach((bid, i) => {
|
|
69
|
+
if (bid.price != null) {
|
|
70
|
+
this.bidprice[i] = bid.price.value / 100;
|
|
71
|
+
}
|
|
72
|
+
if (bid.qty != null) {
|
|
73
|
+
this.bidqty[i] = bid.qty.value;
|
|
74
|
+
}
|
|
75
|
+
if (bid.nord != null) {
|
|
76
|
+
this.bidordn[i] = bid.nord.value;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Process other fields (assuming wrapper types)
|
|
82
|
+
if (currdata.feedTime != null ) {
|
|
83
|
+
this.timestamp = currdata.feedTime.value / 1;
|
|
84
|
+
}
|
|
85
|
+
if (currdata.sendTime != null ) {
|
|
86
|
+
this.sendtime = currdata.sendTime.value / 1;
|
|
87
|
+
}
|
|
88
|
+
this.seqNo = currdata.sequenceNo / 1;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class SubscriptionInfo {
|
|
94
|
+
constructor() {
|
|
95
|
+
this._symbols = {}; // Stores channel-wise symbol sets
|
|
96
|
+
this._modeInfo = {}; // Stores channel-wise subscription modes
|
|
97
|
+
this._activeChannels = new Set(); // Stores active channels
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
subscribe(symbols, channelNo, mode) {
|
|
101
|
+
if (this._symbols[channelNo]) {
|
|
102
|
+
this._symbols[channelNo] = new Set([...this._symbols[channelNo], ...symbols]);
|
|
103
|
+
} else {
|
|
104
|
+
this._symbols[channelNo] = new Set(symbols);
|
|
105
|
+
}
|
|
106
|
+
this._modeInfo[channelNo] = mode;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
unsubscribe(symbols, channelNo) {
|
|
110
|
+
if (this._symbols[channelNo]) {
|
|
111
|
+
symbols.forEach(symbol => this._symbols[channelNo].delete(symbol));
|
|
112
|
+
if (this._symbols[channelNo].size === 0) {
|
|
113
|
+
delete this._symbols[channelNo];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
updateChannels(pauseChannels, resumeChannels) {
|
|
119
|
+
pauseChannels.forEach(channel => this._activeChannels.delete(channel));
|
|
120
|
+
resumeChannels.forEach(channel => this._activeChannels.add(channel));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
updateMode(modeConfig) {
|
|
124
|
+
Object.entries(modeConfig).forEach(([channelNo, mode]) => {
|
|
125
|
+
this._modeInfo[channelNo] = mode;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
getSymbolsInfo(channelNo) {
|
|
130
|
+
return this._symbols[channelNo] || new Set();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
getModeInfo(channelNo) {
|
|
134
|
+
return this._modeInfo[channelNo] || null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getChannelInfo() {
|
|
138
|
+
return this._activeChannels;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
class DataStore {
|
|
143
|
+
constructor() {
|
|
144
|
+
this.depth = {}; // Stores depth data by symbol
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
updateDepth(packet, cb, diffOnly) {
|
|
148
|
+
if (packet.feeds) {
|
|
149
|
+
Object.entries(packet.feeds).forEach(([_, value]) => {
|
|
150
|
+
const symbol = value.ticker;
|
|
151
|
+
|
|
152
|
+
if (!this.depth[symbol]) {
|
|
153
|
+
this.depth[symbol] = new Depth();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (!diffOnly) {
|
|
157
|
+
this.depth[symbol]._addDepth(value, packet.snapshot);
|
|
158
|
+
if (cb) cb(symbol, this.depth[symbol]);
|
|
159
|
+
} else {
|
|
160
|
+
const depth = new Depth();
|
|
161
|
+
depth._addDepth(value, packet.snapshot);
|
|
162
|
+
if (cb) cb(symbol, depth);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
module.exports.DataStore = DataStore;
|
|
170
|
+
module.exports.SubscriptionInfo = SubscriptionInfo;
|
|
171
|
+
module.exports.Depth = Depth;
|
|
172
|
+
module.exports.SubscriptionModes = SubscriptionModes;
|