market-data-tradingview-ws 0.0.7 → 0.0.8
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/lib/client.js +50 -57
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -5,54 +5,6 @@
|
|
|
5
5
|
const WebSocket = require('ws');
|
|
6
6
|
const tv = require('./tradingView.js');
|
|
7
7
|
|
|
8
|
-
const quotes = {
|
|
9
|
-
values: {},
|
|
10
|
-
set({ key, data }) {
|
|
11
|
-
if (data) {
|
|
12
|
-
this.values[key] = data;
|
|
13
|
-
}
|
|
14
|
-
},
|
|
15
|
-
add({ symbol }) {
|
|
16
|
-
for (let key in this.values) {
|
|
17
|
-
this.values[key].symbols.push(symbol);
|
|
18
|
-
return key;
|
|
19
|
-
}
|
|
20
|
-
return null;
|
|
21
|
-
},
|
|
22
|
-
get({ key }) {
|
|
23
|
-
return this.values[key];
|
|
24
|
-
},
|
|
25
|
-
getBySymbol({ symbol }) {
|
|
26
|
-
for (let key in this.values) {
|
|
27
|
-
if (this.values[key].symbols.includes(symbol)) return key;
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
},
|
|
31
|
-
delete({ key, symbol }) {
|
|
32
|
-
const arr = this.values[key].symbols;
|
|
33
|
-
const index = arr.indexOf(symbol);
|
|
34
|
-
if (index !== -1) arr.splice(index, 1);
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const charts = {
|
|
39
|
-
values: {},
|
|
40
|
-
set({ key, data }) {
|
|
41
|
-
if (data) {
|
|
42
|
-
this.values[key] = data;
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
get({ key }) {
|
|
46
|
-
return this.values[key];
|
|
47
|
-
},
|
|
48
|
-
getBySymbolPeriod({ symbol, period }) {
|
|
49
|
-
for (let key of Object.keys(this.values)) {
|
|
50
|
-
if (this.values[key].symbol === symbol && this.values[key].period === period) return key;
|
|
51
|
-
}
|
|
52
|
-
return null;
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
|
|
56
8
|
const periods = {
|
|
57
9
|
60: '1',
|
|
58
10
|
300: '5',
|
|
@@ -65,19 +17,60 @@ const periods = {
|
|
|
65
17
|
|
|
66
18
|
module.exports = class Client {
|
|
67
19
|
#ws;
|
|
68
|
-
#charts
|
|
69
|
-
|
|
20
|
+
#charts = {
|
|
21
|
+
values: {},
|
|
22
|
+
set({ key, data }) {
|
|
23
|
+
if (data) {
|
|
24
|
+
this.values[key] = data;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
get({ key }) {
|
|
28
|
+
return this.values[key];
|
|
29
|
+
},
|
|
30
|
+
getBySymbolPeriod({ symbol, period }) {
|
|
31
|
+
for (let key of Object.keys(this.values)) {
|
|
32
|
+
if (this.values[key].symbol === symbol && this.values[key].period === period) return key;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
#quotes = {
|
|
38
|
+
values: {},
|
|
39
|
+
set({ key, data }) {
|
|
40
|
+
if (data) {
|
|
41
|
+
this.values[key] = data;
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
add({ symbol }) {
|
|
45
|
+
for (let key in this.values) {
|
|
46
|
+
this.values[key].symbols.push(symbol);
|
|
47
|
+
return key;
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
},
|
|
51
|
+
get({ key }) {
|
|
52
|
+
return this.values[key];
|
|
53
|
+
},
|
|
54
|
+
getBySymbol({ symbol }) {
|
|
55
|
+
for (let key in this.values) {
|
|
56
|
+
if (this.values[key].symbols.includes(symbol)) return key;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
},
|
|
60
|
+
delete({ key, symbol }) {
|
|
61
|
+
const arr = this.values[key].symbols;
|
|
62
|
+
const index = arr.indexOf(symbol);
|
|
63
|
+
if (index !== -1) arr.splice(index, 1);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
70
66
|
#resolved;
|
|
71
67
|
#token = null;
|
|
72
68
|
maxSymbolKey = 0;
|
|
73
69
|
constructor() {
|
|
74
|
-
this.#charts = charts;
|
|
75
|
-
this.#quotes = quotes;
|
|
76
70
|
this.#resolved = new Map();
|
|
77
71
|
}
|
|
78
72
|
|
|
79
73
|
close() {
|
|
80
|
-
// console.log(this.#sessions.values, this.#charts.values, this.#quotes.values);
|
|
81
74
|
this.#ws.close();
|
|
82
75
|
}
|
|
83
76
|
|
|
@@ -180,7 +173,8 @@ module.exports = class Client {
|
|
|
180
173
|
addChartSymbol({ symbol, period, limit }) {
|
|
181
174
|
let key = this.#charts.getBySymbolPeriod({ symbol, period });
|
|
182
175
|
if (key) return 'exist';
|
|
183
|
-
|
|
176
|
+
|
|
177
|
+
// console.log('addChartSymbol1', symbol, period, limit, key);
|
|
184
178
|
key = this.createChartSession();
|
|
185
179
|
const data = this.#charts.get({ key });
|
|
186
180
|
|
|
@@ -202,7 +196,7 @@ module.exports = class Client {
|
|
|
202
196
|
limit,
|
|
203
197
|
}),
|
|
204
198
|
);
|
|
205
|
-
// console.log('
|
|
199
|
+
// console.log('addChartSymbol4', periods[period], data);
|
|
206
200
|
this.#charts.set({ key, data });
|
|
207
201
|
|
|
208
202
|
return 'add';
|
|
@@ -235,7 +229,6 @@ module.exports = class Client {
|
|
|
235
229
|
limit,
|
|
236
230
|
}),
|
|
237
231
|
);
|
|
238
|
-
// console.log('updateChartSymbol1', key, data);
|
|
239
232
|
this.#charts.set({ key, data });
|
|
240
233
|
return 'update';
|
|
241
234
|
}
|
|
@@ -290,7 +283,7 @@ module.exports = class Client {
|
|
|
290
283
|
} else if (['series', 'symbol', 'quote', 'session'].includes(packet.type)) {
|
|
291
284
|
// console.info(packet);
|
|
292
285
|
} else {
|
|
293
|
-
|
|
286
|
+
console.warn('error', raw.toString());
|
|
294
287
|
result('error', raw.toString())
|
|
295
288
|
}
|
|
296
289
|
}
|