@mathieuc/tradingview 2.0.7 → 3.0.0

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/miscRequests.js DELETED
@@ -1,420 +0,0 @@
1
- const https = require('https');
2
-
3
- const indicators = ['Recommend.Other', 'Recommend.All', 'Recommend.MA'];
4
-
5
- function fetchScanData(tickers = [], type = '', columns = []) {
6
- return new Promise((cb, err) => {
7
- const req = https.request({
8
- method: 'POST',
9
- hostname: 'scanner.tradingview.com',
10
- path: `/${type}/scan`,
11
- headers: {
12
- 'Content-Type': 'application/json',
13
- },
14
- }, (res) => {
15
- let data = '';
16
- res.on('data', (c) => { data += c; });
17
- res.on('end', () => {
18
- if (!data.startsWith('{')) {
19
- err(new Error('Wrong screener or symbol'));
20
- return;
21
- }
22
- try {
23
- cb(JSON.parse(data));
24
- } catch (error) {
25
- err(new Error('Can\'t parse server response'));
26
- }
27
- });
28
- });
29
-
30
- req.on('error', err);
31
- req.end(JSON.stringify({ symbols: { tickers }, columns }));
32
- });
33
- }
34
-
35
- /**
36
- * @typedef {number} advice
37
- *
38
- * @typedef {{ Other: advice, All: advice, MA: advice }} Period
39
- *
40
- * @typedef {{
41
- * '1': Period,
42
- * '5': Period,
43
- * '15': Period,
44
- * '60': Period,
45
- * '240': Period,
46
- * '1D': Period,
47
- * '1W': Period,
48
- * '1M': Period,
49
- * }} Periods
50
- *
51
- * @typedef {string | 'forex' | 'crypto'
52
- * | 'america' | 'australia' | 'canada' | 'egypt'
53
- * | 'germany' | 'india' | 'israel' | 'italy'
54
- * | 'luxembourg' | 'poland' | 'sweden' | 'turkey'
55
- * | 'uk' | 'vietnam'} screener
56
- * You can use `getScreener(exchange)` function for non-forex and non-crypto markets.
57
- */
58
-
59
- module.exports = {
60
- /**
61
- * @param {string} exchange Example: BINANCE, EURONEXT, NASDAQ
62
- * @returns {screener}
63
- */
64
- getScreener(exchange) {
65
- const e = exchange.toUpperCase();
66
- if (['NASDAQ', 'NYSE', 'NYSE ARCA', 'OTC'].includes(e)) return 'america';
67
- if (['ASX'].includes(e)) return 'australia';
68
- if (['TSX', 'TSXV', 'CSE', 'NEO'].includes(e)) return 'canada';
69
- if (['EGX'].includes(e)) return 'egypt';
70
- if (['FWB', 'SWB', 'XETR'].includes(e)) return 'germany';
71
- if (['BSE', 'NSE'].includes(e)) return 'india';
72
- if (['TASE'].includes(e)) return 'israel';
73
- if (['MIL', 'MILSEDEX'].includes(e)) return 'italy';
74
- if (['LUXSE'].includes(e)) return 'luxembourg';
75
- if (['NEWCONNECT'].includes(e)) return 'poland';
76
- if (['NGM'].includes(e)) return 'sweden';
77
- if (['BIST'].includes(e)) return 'turkey';
78
- if (['LSE', 'LSIN'].includes(e)) return 'uk';
79
- if (['HNX'].includes(e)) return 'vietnam';
80
- return exchange.toLowerCase();
81
- },
82
-
83
- /**
84
- * Get technical analysis
85
- * @param {screener} screener
86
- * @param {string} id Full market id (Example: COINBASE:BTCEUR)
87
- * @returns {Promise<Periods>} results
88
- */
89
- async getTA(screener, id) {
90
- const advice = {};
91
-
92
- const cols = ['1', '5', '15', '60', '240', '1D', '1W', '1M']
93
- .map((t) => indicators.map((i) => (t !== '1D' ? `${i}|${t}` : i)))
94
- .flat();
95
-
96
- const rs = await fetchScanData([id], screener, cols);
97
- if (!rs.data || !rs.data[0]) return false;
98
-
99
- rs.data[0].d.forEach((val, i) => {
100
- const [name, period] = cols[i].split('|');
101
- const pName = period || '1D';
102
- if (!advice[pName]) advice[pName] = {};
103
- advice[pName][name.split('.').pop()] = Math.round(val * 1000) / 500;
104
- });
105
-
106
- return advice;
107
- },
108
-
109
- /**
110
- * @typedef {Object} SearchResult
111
- * @property {string} id
112
- * @property {string} exchange
113
- * @property {string} fullExchange
114
- * @property {string} screener
115
- * @property {string} symbol
116
- * @property {string} description
117
- * @property {string} type
118
- * @property {() => Promise<Periods>} getTA
119
- */
120
-
121
- /**
122
- * Find a symbol
123
- * @param {string} search Keywords
124
- * @param {'stock' | 'futures' | 'forex' | 'cfd' | 'crypto' | 'index' | 'economic'} [filter]
125
- * @returns {Promise<SearchResult[]>} Search results
126
- */
127
- async search(search, filter = '') {
128
- return new Promise((cb, err) => {
129
- https.get({
130
- host: 'symbol-search.tradingview.com',
131
- path: `/symbol_search/?text=${search.replace(/ /g, '%20')}&type=${filter}`,
132
- origin: 'https://www.tradingview.com',
133
- }, (res) => {
134
- let rs = '';
135
- res.on('data', (d) => { rs += d; });
136
- res.on('end', () => {
137
- try {
138
- rs = JSON.parse(rs);
139
- } catch (e) {
140
- err(new Error('Can\'t parse server response'));
141
- return;
142
- }
143
-
144
- cb(rs.map((s) => {
145
- const exchange = s.exchange.split(' ')[0];
146
- const id = `${exchange}:${s.symbol}`;
147
-
148
- const screener = (['forex', 'crypto'].includes(s.type)
149
- ? s.type
150
- : this.getScreener(exchange)
151
- );
152
-
153
- return {
154
- id,
155
- exchange,
156
- fullExchange: s.exchange,
157
- screener,
158
- symbol: s.symbol,
159
- description: s.description,
160
- type: s.type,
161
- getTA: () => this.getTA(screener, id),
162
- };
163
- }));
164
- });
165
-
166
- res.on('error', err);
167
- });
168
- });
169
- },
170
-
171
- /**
172
- * @typedef {Object} indicatorInput
173
- * @property {string} name
174
- * @property {string} inline Inline name
175
- * @property { 'text' | 'source' | 'integer' | 'float' | 'resolution' | 'bool' } type
176
- * @property {string | number | boolean} value
177
- * @property {string | number | boolean} defVal
178
- * @property {boolean} hidden
179
- * @property {boolean} isFake
180
- * @property {string[]} [options]
181
- */
182
-
183
- /**
184
- * @typedef {Object} Indicator
185
- * @property {string} pineId Indicator ID
186
- * @property {string} pineVersion Indicator version
187
- * @property {string} description Indicator description
188
- * @property {string} shortDescription Indicator short description
189
- * @property {Object<string, indicatorInput>} inputs Indicator inputs
190
- * @property {Object<string, string>} plots Indicator plots
191
- * @property {string} script Indicator script
192
- */
193
-
194
- /**
195
- * Get an indicator
196
- * @param {string} id Indicator ID (Like: PUB;XXXXXXXXXXXXXXXXXXXXX)
197
- * @param {string} version Wanted version of the indicator
198
- * @returns {Promise<Indicator>} Indicator
199
- */
200
- async getIndicator(id, version, settings = []) {
201
- return new Promise((cb, err) => {
202
- https.get(`https://pine-facade.tradingview.com/pine-facade/translate/${id}/${version}`, (res) => {
203
- let rs = '';
204
- res.on('data', (d) => { rs += d; });
205
- res.on('end', () => {
206
- try {
207
- rs = JSON.parse(rs);
208
- } catch (e) {
209
- err(new Error('Inexistent or unsupported indicator'));
210
- return;
211
- }
212
-
213
- if (!rs.success || !rs.result.metaInfo || !rs.result.metaInfo.inputs) {
214
- err(new Error('Inexistent or unsupported indicator'));
215
- return;
216
- }
217
-
218
- const inputs = {};
219
-
220
- rs.result.metaInfo.inputs.forEach((input) => {
221
- if (['text', 'pineId', 'pineVersion'].includes(input.id)) return;
222
- const inline = input.inline || input.name.replace(/ /g, '_').replace(/[^a-zA-Z0-9]/g, '');
223
-
224
- const i = parseInt(input.id.replace(/[^0-9]/g, ''), 10);
225
-
226
- inputs[input.id] = {
227
- name: input.name,
228
- type: input.type,
229
- value: settings[i] ?? input.defval,
230
- defVal: input.defval,
231
- hidden: !!input.isHidden,
232
- isFake: !!input.isFake,
233
- inline,
234
- };
235
-
236
- if (input.options) inputs[input.id].options = input.options;
237
- });
238
-
239
- const plots = {};
240
-
241
- Object.keys(rs.result.metaInfo.styles).forEach((plotId) => {
242
- plots[plotId] = rs.result.metaInfo.styles[plotId].title.replace(/ /g, '_').replace(/[^a-zA-Z0-9]/g, '');
243
- });
244
-
245
- cb({
246
- pineId: id,
247
- pineVersion: version,
248
- description: rs.result.metaInfo.description,
249
- shortDescription: rs.result.metaInfo.shortDescription,
250
- inputs,
251
- plots,
252
- script: rs.result.ilTemplate,
253
- });
254
- });
255
-
256
- res.on('error', err);
257
- });
258
- });
259
- },
260
-
261
- /**
262
- * @typedef {Object} User Instance of User
263
- * @property {number} id User ID
264
- * @property {string} username User username
265
- * @property {string} firstName User first name
266
- * @property {string} lastName User last name
267
- * @property {number} reputation User reputation
268
- * @property {number} following Number of following accounts
269
- * @property {number} followers Number of followers
270
- * @property {Object} notifications User's notifications
271
- * @property {number} notifications.user User notifications
272
- * @property {number} notifications.following Notification from following accounts
273
- * @property {string} session User session
274
- * @property {string} sessionHash User session hash
275
- * @property {string} privateChannel User private channel
276
- * @property {string} authToken User auth token
277
- * @property {Date} joinDate Account creation date
278
- */
279
-
280
- /**
281
- * Get a token for an user from a 'sessionid' cookie
282
- * @param {string} session User 'sessionid' cookie
283
- * @param {string} [location] Auth page location (For france: https://fr.tradingview.com/)
284
- * @returns {Promise<User>} Token
285
- */
286
- async getUser(session, location = 'https://www.tradingview.com/') {
287
- return new Promise((cb, err) => {
288
- https.get(location, {
289
- headers: { cookie: `sessionid=${session}` },
290
- }, (res) => {
291
- let rs = '';
292
- res.on('data', (d) => { rs += d; });
293
- res.on('end', async () => {
294
- if (res.headers.location && location !== res.headers.location) {
295
- cb(await module.exports.getUser(session, res.headers.location));
296
- return;
297
- }
298
- if (rs.includes('auth_token')) {
299
- cb({
300
- id: /"id":([0-9]{1,10}),/.exec(rs)[1],
301
- username: /"username":"(.*?)"/.exec(rs)[1],
302
- firstName: /"first_name":"(.*?)"/.exec(rs)[1],
303
- lastName: /"last_name":"(.*?)"/.exec(rs)[1],
304
- reputation: parseFloat(/"reputation":(.*?),/.exec(rs)[1] || 0),
305
- following: parseFloat(/,"following":([0-9]*?),/.exec(rs)[1] || 0),
306
- followers: parseFloat(/,"followers":([0-9]*?),/.exec(rs)[1] || 0),
307
- notifications: {
308
- following: parseFloat(/"notification_count":\{"following":([0-9]*),/.exec(rs)[1] || 0),
309
- user: parseFloat(/"notification_count":\{"following":[0-9]*,"user":([0-9]*)/.exec(rs)[1] || 0),
310
- },
311
- session,
312
- sessionHash: /"session_hash":"(.*?)"/.exec(rs)[1],
313
- privateChannel: /"private_channel":"(.*?)"/.exec(rs)[1],
314
- authToken: /"auth_token":"(.*?)"/.exec(rs)[1],
315
- joinDate: new Date(/"date_joined":"(.*?)"/.exec(rs)[1] || 0),
316
- });
317
- } else err(new Error('Wrong or expired sessionid'));
318
- });
319
-
320
- res.on('error', err);
321
- }).end();
322
- });
323
- },
324
-
325
- /**
326
- * User credentials
327
- * @typedef {Object} UserCredentials
328
- * @property {number} id User ID
329
- * @property {string} session User session ('sessionid' cookie)
330
- */
331
-
332
- /**
333
- * Get a chart token from a layout ID and the user credentials if the layout is not public
334
- * @param {string} layout The layout ID found in the layout URL (Like: 'XXXXXXXX')
335
- * @param {UserCredentials} [credentials] User credentials (id + session)
336
- * @returns {Promise<string>} Token
337
- */
338
- async getChartToken(layout, credentials = {}) {
339
- return new Promise((cb, err) => {
340
- const creds = credentials.id && credentials.session;
341
- const userID = creds ? credentials.id : -1;
342
- const session = creds ? credentials.session : null;
343
-
344
- https.get(`https://www.tradingview.com/chart-token/?image_url=${layout}&user_id=${userID}`, {
345
- headers: { cookie: session ? `sessionid=${session}` : '' },
346
- }, (res) => {
347
- let rs = '';
348
- res.on('data', (d) => { rs += d; });
349
- res.on('end', async () => {
350
- rs = JSON.parse(rs);
351
-
352
- if (rs.token) cb(rs.token);
353
- else err(new Error('Wrong layout or credentials'));
354
- });
355
-
356
- res.on('error', err);
357
- }).end();
358
- });
359
- },
360
-
361
- /**
362
- * @typedef {Object} DrawingPoint
363
- * @property {number} time_t Point X time position
364
- * @property {number} price Point Y price position
365
- * @property {number} offset Point offset
366
- */
367
-
368
- /**
369
- * @typedef {Object} Drawing
370
- * @property {string} id Drawing ID (Like: 'XXXXXX')
371
- * @property {string} symbol Layout market symbol (Like: 'BINANCE:BUCEUR')
372
- * @property {string} ownerSource Owner user ID (Like: 'XXXXXX')
373
- * @property {string} serverUpdateTime Drawing last update timestamp
374
- * @property {string} currencyId Currency ID (Like: 'EUR')
375
- * @property {any} unitId Unit ID
376
- * @property {string} type Drawing type
377
- * @property {DrawingPoint[]} points List of drawing points
378
- * @property {number} zorder Drawing Z order
379
- * @property {string} linkKey Drawing link key
380
- * @property {Object} state Drawing state
381
- */
382
-
383
- /**
384
- * Get a chart token from a layout ID and the user credentials if the layout is not public
385
- * @param {string} layout The layout ID found in the layout URL (Like: 'XXXXXXXX')
386
- * @param {string | ''} [symbol] Market filter (Like: 'BINANCE:BTCEUR')
387
- * @param {UserCredentials} [credentials] User credentials (id + session)
388
- * @param {number} [chartID = 1] Chart ID
389
- * @returns {Promise<Drawing[]>} Drawings
390
- */
391
- async getDrawings(layout, symbol = '', credentials = {}, chartID = 1) {
392
- const chartToken = await module.exports.getChartToken(layout, credentials);
393
-
394
- return new Promise((cb, err) => {
395
- const creds = credentials.id && credentials.session;
396
- const session = creds ? credentials.session : null;
397
-
398
- const url = `https://charts-storage.tradingview.com/charts-storage/layout/${
399
- layout}/sources?chart_id=${chartID}&jwt=${chartToken}${symbol ? `&symbol=${symbol}` : ''}`;
400
-
401
- https.get(url, {
402
- headers: { cookie: session ? `sessionid=${session}` : '' },
403
- }, (res) => {
404
- let rs = '';
405
- res.on('data', (d) => { rs += d; });
406
- res.on('end', async () => {
407
- rs = JSON.parse(rs);
408
-
409
- if (rs.payload) {
410
- cb(Object.values(rs.payload.sources || {}).map((drawing) => ({
411
- ...drawing, ...drawing.state,
412
- })));
413
- } else err(new Error('Wrong layout, user credentials, or chart id.'));
414
- });
415
-
416
- res.on('error', err);
417
- }).end();
418
- });
419
- },
420
- };