@mathieuc/tradingview 3.5.2 → 4.0.0-beta.1

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 (120) hide show
  1. package/README.md +112 -51
  2. package/dist/chart/chart-session.d.ts +114 -0
  3. package/dist/chart/chart-session.js +339 -0
  4. package/dist/chart/graphics.d.ts +129 -0
  5. package/dist/chart/graphics.js +153 -0
  6. package/dist/chart/index.d.ts +11 -0
  7. package/dist/chart/index.js +6 -0
  8. package/dist/chart/strategy.d.ts +113 -0
  9. package/dist/chart/strategy.js +78 -0
  10. package/dist/chart/study.d.ts +60 -0
  11. package/dist/chart/study.js +179 -0
  12. package/dist/chart/timeframes.d.ts +7 -0
  13. package/dist/chart/timeframes.js +22 -0
  14. package/dist/chart/types.d.ts +105 -0
  15. package/dist/chart/types.js +9 -0
  16. package/dist/client/client.d.ts +89 -0
  17. package/dist/client/client.js +254 -0
  18. package/dist/client/index.d.ts +4 -0
  19. package/dist/client/index.js +2 -0
  20. package/dist/client/transport.d.ts +27 -0
  21. package/dist/client/transport.js +38 -0
  22. package/dist/data/candles.d.ts +40 -0
  23. package/dist/data/candles.js +101 -0
  24. package/dist/data/history.d.ts +61 -0
  25. package/dist/data/history.js +101 -0
  26. package/dist/data/index.d.ts +30 -0
  27. package/dist/data/index.js +16 -0
  28. package/dist/data/indicators.d.ts +54 -0
  29. package/dist/data/indicators.js +139 -0
  30. package/dist/data/operation.d.ts +71 -0
  31. package/dist/data/operation.js +205 -0
  32. package/dist/data/provider.d.ts +23 -0
  33. package/dist/data/provider.js +11 -0
  34. package/dist/data/quotes.d.ts +33 -0
  35. package/dist/data/quotes.js +117 -0
  36. package/dist/data/symbols.d.ts +9 -0
  37. package/dist/data/symbols.js +19 -0
  38. package/dist/errors.d.ts +53 -0
  39. package/dist/errors.js +24 -0
  40. package/dist/events.d.ts +36 -0
  41. package/dist/events.js +70 -0
  42. package/dist/http/account.d.ts +47 -0
  43. package/dist/http/account.js +127 -0
  44. package/dist/http/index.d.ts +11 -0
  45. package/dist/http/index.js +5 -0
  46. package/dist/http/indicators.d.ts +37 -0
  47. package/dist/http/indicators.js +144 -0
  48. package/dist/http/layouts.d.ts +36 -0
  49. package/dist/http/layouts.js +24 -0
  50. package/dist/http/market.d.ts +45 -0
  51. package/dist/http/market.js +59 -0
  52. package/dist/http/pine-permissions.d.ts +29 -0
  53. package/dist/http/pine-permissions.js +56 -0
  54. package/dist/http/request.d.ts +44 -0
  55. package/dist/http/request.js +83 -0
  56. package/dist/index.d.ts +21 -0
  57. package/dist/index.js +19 -0
  58. package/dist/indicators/builtin-indicator.d.ts +17 -0
  59. package/dist/indicators/builtin-indicator.js +75 -0
  60. package/dist/indicators/index.d.ts +8 -0
  61. package/dist/indicators/index.js +2 -0
  62. package/dist/indicators/pine-indicator.d.ts +63 -0
  63. package/dist/indicators/pine-indicator.js +101 -0
  64. package/dist/protocol/compression.d.ts +12 -0
  65. package/dist/protocol/compression.js +83 -0
  66. package/dist/protocol/framing.d.ts +59 -0
  67. package/dist/protocol/framing.js +68 -0
  68. package/dist/protocol/ids.d.ts +2 -0
  69. package/dist/protocol/ids.js +10 -0
  70. package/dist/protocol/index.d.ts +4 -0
  71. package/dist/protocol/index.js +3 -0
  72. package/dist/quote/fields.d.ts +35 -0
  73. package/dist/quote/fields.js +21 -0
  74. package/dist/quote/index.d.ts +4 -0
  75. package/dist/quote/index.js +2 -0
  76. package/dist/quote/quote-session.d.ts +73 -0
  77. package/dist/quote/quote-session.js +205 -0
  78. package/docs/README.es.md +58 -0
  79. package/docs/README.fr.md +58 -0
  80. package/docs/README.pt.md +58 -0
  81. package/docs/data-api.md +285 -0
  82. package/docs/low-level-api.md +213 -0
  83. package/docs/migration-v4.md +128 -0
  84. package/docs/release-4.0.0-beta.1.md +39 -0
  85. package/docs/v4-coverage.md +213 -0
  86. package/package.json +53 -20
  87. package/.env.sample +0 -2
  88. package/.eslintrc.js +0 -28
  89. package/.gitattributes +0 -2
  90. package/docs/DOCS.md +0 -3
  91. package/examples/AllPrivateIndicators.js +0 -44
  92. package/examples/BuiltInIndicator.js +0 -51
  93. package/examples/CustomChartType.js +0 -123
  94. package/examples/CustomTimeframe.js +0 -31
  95. package/examples/Errors.js +0 -155
  96. package/examples/FakeReplayMode.js +0 -40
  97. package/examples/FromToData.js +0 -35
  98. package/examples/GetDrawings.js +0 -27
  99. package/examples/GraphicIndicator.js +0 -42
  100. package/examples/MultipleSyncFetch.js +0 -44
  101. package/examples/PinePermManage.js +0 -67
  102. package/examples/ReplayMode.js +0 -103
  103. package/examples/Search.js +0 -14
  104. package/examples/SimpleChart.js +0 -63
  105. package/examples/UserLogin.js +0 -16
  106. package/main.js +0 -11
  107. package/src/chart/graphicParser.js +0 -308
  108. package/src/chart/session.js +0 -553
  109. package/src/chart/study.js +0 -435
  110. package/src/classes/BuiltInIndicator.js +0 -137
  111. package/src/classes/PineIndicator.js +0 -132
  112. package/src/classes/PinePermManager.js +0 -159
  113. package/src/client.js +0 -298
  114. package/src/miscRequests.js +0 -609
  115. package/src/protocol.js +0 -60
  116. package/src/quote/market.js +0 -132
  117. package/src/quote/session.js +0 -125
  118. package/src/types.js +0 -36
  119. package/src/utils.js +0 -20
  120. package/vite.config.js +0 -7
@@ -1,159 +0,0 @@
1
- const axios = require('axios');
2
- const { genAuthCookies } = require('../utils');
3
-
4
- /**
5
- * @typedef {Object} AuthorizationUser
6
- * @prop {id} id User id
7
- * @prop {string} username User's username
8
- * @prop {string} userpic User's profile picture URL
9
- * @prop {string} expiration Authorization expiration date
10
- * @prop {string} created Authorization creation date
11
- */
12
-
13
- /** @class */
14
- class PinePermManager {
15
- sessionId;
16
-
17
- pineId;
18
-
19
- /**
20
- * Creates a PinePermManager instance
21
- * @param {string} sessionId Token from `sessionid` cookie
22
- * @param {string} signature Signature cookie
23
- * @param {string} pineId Indicator ID (Like: PUB;XXXXXXXXXXXXXXXXXXXXX)
24
- */
25
- constructor(sessionId, signature, pineId) {
26
- if (!sessionId) throw new Error('Please provide a SessionID');
27
- if (!signature) throw new Error('Please provide a Signature');
28
- if (!pineId) throw new Error('Please provide a PineID');
29
- this.sessionId = sessionId;
30
- this.signature = signature;
31
- this.pineId = pineId;
32
- }
33
-
34
- /**
35
- * Get list of authorized users
36
- * @param {number} limit Fetching limit
37
- * @param {'user__username'
38
- * | '-user__username'
39
- * | 'created' | 'created'
40
- * | 'expiration,user__username'
41
- * | '-expiration,user__username'
42
- * } order Fetching order
43
- * @returns {Promise<AuthorizationUser[]>}
44
- */
45
- async getUsers(limit = 10, order = '-created') {
46
- try {
47
- const { data } = await axios.post(
48
- `https://www.tradingview.com/pine_perm/list_users/?limit=${limit}&order_by=${order}`,
49
- `pine_id=${this.pineId.replace(/;/g, '%3B')}`,
50
- {
51
- headers: {
52
- origin: 'https://www.tradingview.com',
53
- 'Content-Type': 'application/x-www-form-urlencoded',
54
- cookie: genAuthCookies(this.sessionId, this.signature),
55
- },
56
- },
57
- );
58
-
59
- return data.results;
60
- } catch (e) {
61
- throw new Error(e.response.data.detail || 'Wrong credentials or pineId');
62
- }
63
- }
64
-
65
- /**
66
- * Adds an user to the authorized list
67
- * @param {string} username User's username
68
- * @param {Date} [expiration] Expiration date
69
- * @returns {Promise<'ok' | 'exists' | null>}
70
- */
71
- async addUser(username, expiration = null) {
72
- try {
73
- const { data } = await axios.post(
74
- 'https://www.tradingview.com/pine_perm/add/',
75
- `pine_id=${
76
- this.pineId.replace(/;/g, '%3B')
77
- }&username_recip=${
78
- username
79
- }${
80
- expiration && expiration instanceof Date
81
- ? `&expiration=${expiration.toISOString()}`
82
- : ''
83
- }`,
84
- {
85
- headers: {
86
- origin: 'https://www.tradingview.com',
87
- 'Content-Type': 'application/x-www-form-urlencoded',
88
- cookie: genAuthCookies(this.sessionId, this.signature),
89
- },
90
- },
91
- );
92
-
93
- return data.status;
94
- } catch (e) {
95
- throw new Error(e.response.data.detail || 'Wrong credentials or pineId');
96
- }
97
- }
98
-
99
- /**
100
- * Modify an authorization expiration date
101
- * @param {string} username User's username
102
- * @param {Date} [expiration] New expiration date
103
- * @returns {Promise<'ok' | null>}
104
- */
105
- async modifyExpiration(username, expiration = null) {
106
- try {
107
- const { data } = await axios.post(
108
- 'https://www.tradingview.com/pine_perm/modify_user_expiration/',
109
- `pine_id=${
110
- this.pineId.replace(/;/g, '%3B')
111
- }&username_recip=${
112
- username
113
- }${
114
- expiration && expiration instanceof Date
115
- ? `&expiration=${expiration.toISOString()}`
116
- : ''
117
- }`,
118
- {
119
- headers: {
120
- origin: 'https://www.tradingview.com',
121
- 'Content-Type': 'application/x-www-form-urlencoded',
122
- cookie: genAuthCookies(this.sessionId, this.signature),
123
- },
124
- },
125
- );
126
-
127
- return data.status;
128
- } catch (e) {
129
- throw new Error(e.response.data.detail || 'Wrong credentials or pineId');
130
- }
131
- }
132
-
133
- /**
134
- * Removes an user to the authorized list
135
- * @param {string} username User's username
136
- * @returns {Promise<'ok' | null>}
137
- */
138
- async removeUser(username) {
139
- try {
140
- const { data } = await axios.post(
141
- 'https://www.tradingview.com/pine_perm/remove/',
142
- `pine_id=${this.pineId.replace(/;/g, '%3B')}&username_recip=${username}`,
143
- {
144
- headers: {
145
- origin: 'https://www.tradingview.com',
146
- 'Content-Type': 'application/x-www-form-urlencoded',
147
- cookie: genAuthCookies(this.sessionId, this.signature),
148
- },
149
- },
150
- );
151
-
152
- return data.status;
153
- } catch (e) {
154
- throw new Error(e.response.data.detail || 'Wrong credentials or pineId');
155
- }
156
- }
157
- }
158
-
159
- module.exports = PinePermManager;
package/src/client.js DELETED
@@ -1,298 +0,0 @@
1
- const WebSocket = require('ws');
2
-
3
- const misc = require('./miscRequests');
4
- const protocol = require('./protocol');
5
-
6
- const quoteSessionGenerator = require('./quote/session');
7
- const chartSessionGenerator = require('./chart/session');
8
-
9
- /**
10
- * @typedef {Object} Session
11
- * @prop {'quote' | 'chart' | 'replay'} type Session type
12
- * @prop {(data: {}) => null} onData When there is a data
13
- */
14
-
15
- /** @typedef {Object<string, Session>} SessionList Session list */
16
-
17
- /**
18
- * @callback SendPacket Send a custom packet
19
- * @param {string} t Packet type
20
- * @param {string[]} p Packet data
21
- * @returns {void}
22
- */
23
-
24
- /**
25
- * @typedef {Object} ClientBridge
26
- * @prop {SessionList} sessions
27
- * @prop {SendPacket} send
28
- */
29
-
30
- /**
31
- * @typedef { 'connected' | 'disconnected'
32
- * | 'logged' | 'ping' | 'data'
33
- * | 'error' | 'event'
34
- * } ClientEvent
35
- */
36
-
37
- /** @class */
38
- module.exports = class Client {
39
- #ws;
40
-
41
- #logged = false;
42
-
43
- /** If the client is logged in */
44
- get isLogged() {
45
- return this.#logged;
46
- }
47
-
48
- /** If the cient was closed */
49
- get isOpen() {
50
- return this.#ws.readyState === this.#ws.OPEN;
51
- }
52
-
53
- /** @type {SessionList} */
54
- #sessions = {};
55
-
56
- #callbacks = {
57
- connected: [],
58
- disconnected: [],
59
- logged: [],
60
- ping: [],
61
- data: [],
62
-
63
- error: [],
64
- event: [],
65
- };
66
-
67
- /**
68
- * @param {ClientEvent} ev Client event
69
- * @param {...{}} data Packet data
70
- */
71
- #handleEvent(ev, ...data) {
72
- this.#callbacks[ev].forEach((e) => e(...data));
73
- this.#callbacks.event.forEach((e) => e(ev, ...data));
74
- }
75
-
76
- #handleError(...msgs) {
77
- if (this.#callbacks.error.length === 0) console.error(...msgs);
78
- else this.#handleEvent('error', ...msgs);
79
- }
80
-
81
- /**
82
- * When client is connected
83
- * @param {() => void} cb Callback
84
- * @event onConnected
85
- */
86
- onConnected(cb) {
87
- this.#callbacks.connected.push(cb);
88
- }
89
-
90
- /**
91
- * When client is disconnected
92
- * @param {() => void} cb Callback
93
- * @event onDisconnected
94
- */
95
- onDisconnected(cb) {
96
- this.#callbacks.disconnected.push(cb);
97
- }
98
-
99
- /**
100
- * @typedef {Object} SocketSession
101
- * @prop {string} session_id Socket session ID
102
- * @prop {number} timestamp Session start timestamp
103
- * @prop {number} timestampMs Session start milliseconds timestamp
104
- * @prop {string} release Release
105
- * @prop {string} studies_metadata_hash Studies metadata hash
106
- * @prop {'json' | string} protocol Used protocol
107
- * @prop {string} javastudies Javastudies
108
- * @prop {number} auth_scheme_vsn Auth scheme type
109
- * @prop {string} via Socket IP
110
- */
111
-
112
- /**
113
- * When client is logged
114
- * @param {(SocketSession: SocketSession) => void} cb Callback
115
- * @event onLogged
116
- */
117
- onLogged(cb) {
118
- this.#callbacks.logged.push(cb);
119
- }
120
-
121
- /**
122
- * When server is pinging the client
123
- * @param {(i: number) => void} cb Callback
124
- * @event onPing
125
- */
126
- onPing(cb) {
127
- this.#callbacks.ping.push(cb);
128
- }
129
-
130
- /**
131
- * When unparsed data is received
132
- * @param {(...{}) => void} cb Callback
133
- * @event onData
134
- */
135
- onData(cb) {
136
- this.#callbacks.data.push(cb);
137
- }
138
-
139
- /**
140
- * When a client error happens
141
- * @param {(...{}) => void} cb Callback
142
- * @event onError
143
- */
144
- onError(cb) {
145
- this.#callbacks.error.push(cb);
146
- }
147
-
148
- /**
149
- * When a client event happens
150
- * @param {(...{}) => void} cb Callback
151
- * @event onEvent
152
- */
153
- onEvent(cb) {
154
- this.#callbacks.event.push(cb);
155
- }
156
-
157
- #parsePacket(str) {
158
- if (!this.isOpen) return;
159
-
160
- protocol.parseWSPacket(str).forEach((packet) => {
161
- if (global.TW_DEBUG) console.log('§90§30§107 CLIENT §0 PACKET', packet);
162
- if (typeof packet === 'number') { // Ping
163
- this.#ws.send(protocol.formatWSPacket(`~h~${packet}`));
164
- this.#handleEvent('ping', packet);
165
- return;
166
- }
167
-
168
- if (packet.m === 'protocol_error') { // Error
169
- this.#handleError('Client critical error:', packet.p);
170
- this.#ws.close();
171
- return;
172
- }
173
-
174
- if (packet.m && packet.p) { // Normal packet
175
- const parsed = {
176
- type: packet.m,
177
- data: packet.p,
178
- };
179
-
180
- const session = packet.p[0];
181
-
182
- if (session && this.#sessions[session]) {
183
- this.#sessions[session].onData(parsed);
184
- return;
185
- }
186
- }
187
-
188
- if (!this.#logged) {
189
- this.#handleEvent('logged', packet);
190
- return;
191
- }
192
-
193
- this.#handleEvent('data', packet);
194
- });
195
- }
196
-
197
- #sendQueue = [];
198
-
199
- /** @type {SendPacket} Send a custom packet */
200
- send(t, p = []) {
201
- this.#sendQueue.push(protocol.formatWSPacket({ m: t, p }));
202
- this.sendQueue();
203
- }
204
-
205
- /** Send all waiting packets */
206
- sendQueue() {
207
- while (this.isOpen && this.#logged && this.#sendQueue.length > 0) {
208
- const packet = this.#sendQueue.shift();
209
- this.#ws.send(packet);
210
- if (global.TW_DEBUG) console.log('§90§30§107 > §0', packet);
211
- }
212
- }
213
-
214
- /**
215
- * @typedef {Object} ClientOptions
216
- * @prop {string} [token] User auth token (in 'sessionid' cookie)
217
- * @prop {string} [signature] User auth token signature (in 'sessionid_sign' cookie)
218
- * @prop {boolean} [DEBUG] Enable debug mode
219
- * @prop {'data' | 'prodata' | 'widgetdata'} [server] Server type
220
- * @prop {string} [location] Auth page location (For france: https://fr.tradingview.com/)
221
- */
222
-
223
- /**
224
- * Client object
225
- * @param {ClientOptions} clientOptions TradingView client options
226
- */
227
- constructor(clientOptions = {}) {
228
- if (clientOptions.DEBUG) global.TW_DEBUG = clientOptions.DEBUG;
229
-
230
- const server = clientOptions.server || 'data';
231
- this.#ws = new WebSocket(`wss://${server}.tradingview.com/socket.io/websocket?type=chart`, {
232
- origin: 'https://www.tradingview.com',
233
- });
234
-
235
- if (clientOptions.token) {
236
- misc.getUser(
237
- clientOptions.token,
238
- clientOptions.signature ? clientOptions.signature : '',
239
- clientOptions.location ? clientOptions.location : 'https://tradingview.com',
240
- ).then((user) => {
241
- this.#sendQueue.unshift(protocol.formatWSPacket({
242
- m: 'set_auth_token',
243
- p: [user.authToken],
244
- }));
245
- this.#logged = true;
246
- this.sendQueue();
247
- }).catch((err) => {
248
- this.#handleError('Credentials error:', err.message);
249
- });
250
- } else {
251
- this.#sendQueue.unshift(protocol.formatWSPacket({
252
- m: 'set_auth_token',
253
- p: ['unauthorized_user_token'],
254
- }));
255
- this.#logged = true;
256
- this.sendQueue();
257
- }
258
-
259
- this.#ws.on('open', () => {
260
- this.#handleEvent('connected');
261
- this.sendQueue();
262
- });
263
-
264
- this.#ws.on('close', () => {
265
- this.#logged = false;
266
- this.#handleEvent('disconnected');
267
- });
268
-
269
- this.#ws.on('error', (err) => {
270
- this.#handleError('WebSocket error:', err.message);
271
- });
272
-
273
- this.#ws.on('message', (data) => this.#parsePacket(data));
274
- }
275
-
276
- /** @type {ClientBridge} */
277
- #clientBridge = {
278
- sessions: this.#sessions,
279
- send: (t, p) => this.send(t, p),
280
- };
281
-
282
- /** @namespace Session */
283
- Session = {
284
- Quote: quoteSessionGenerator(this.#clientBridge),
285
- Chart: chartSessionGenerator(this.#clientBridge),
286
- };
287
-
288
- /**
289
- * Close the websocket connection
290
- * @return {Promise<void>} When websocket is closed
291
- */
292
- end() {
293
- return new Promise((cb) => {
294
- if (this.#ws.readyState) this.#ws.close();
295
- cb();
296
- });
297
- }
298
- };