market-data-tradingview-ws 0.0.6 → 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.
Files changed (2) hide show
  1. package/lib/client.js +54 -60
  2. 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
- #quotes;
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
 
@@ -115,7 +108,7 @@ module.exports = class Client {
115
108
  this.#ws.send(this.auth(this.#token));
116
109
  this.#ws.send(tv.setLocaleRu());
117
110
 
118
- const chartKey = this.createChartSession();
111
+ // const chartKey = this.createChartSession();
119
112
  const quoteKey = this.createQuoteSmallSession();
120
113
  // this.addQuoteSymbols({ symbols: ['NASDAQ:PYPL'] });
121
114
 
@@ -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
- // console.log('addChartSymbol', symbol, period, limit, key);
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('addChartSymbol1', periods[period], data);
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
  }
@@ -254,9 +247,9 @@ module.exports = class Client {
254
247
  if (packet.type === 'ping') {
255
248
  // console.info('pong', packet);
256
249
  this.#ws.send(tv.createPong({ value: packet.data }));
257
- } else if (packet.type === 'protocol_error') {
258
- console.warn('protocol_error', packet);
259
- this.close();
250
+ // } else if (packet.type === 'protocol_error') {
251
+ // console.warn('protocol_error', packet);
252
+ // this.close();
260
253
  } else if (packet.type === 'qsd') {
261
254
  const send = packet.data[1].v;
262
255
  const name = send.bid === undefined && send.ask === undefined ? 'data' : 'levelI';
@@ -291,6 +284,7 @@ module.exports = class Client {
291
284
  // console.info(packet);
292
285
  } else {
293
286
  console.warn('error', raw.toString());
287
+ result('error', raw.toString())
294
288
  }
295
289
  }
296
290
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "market-data-tradingview-ws",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "marketData from Tradingview ws",
5
5
  "main": "index.js",
6
6
  "scripts": {