@mathieuc/tradingview 3.3.0 → 3.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mathieuc/tradingview",
3
- "version": "3.3.0",
3
+ "version": "3.3.3",
4
4
  "description": "Tradingview instant stocks API, indicator alerts, trading bot, and more !",
5
5
  "main": "main.js",
6
6
  "scripts": {
@@ -32,6 +32,27 @@ function getInputs(options) {
32
32
  return options.options;
33
33
  }
34
34
 
35
+ const parseTrades = (trades) => trades.reverse().map((t) => ({
36
+ entry: {
37
+ name: t.e.c,
38
+ type: (t.e.tp[0] === 's' ? 'short' : 'long'),
39
+ value: t.e.p,
40
+ time: t.e.tm,
41
+ },
42
+ exit: {
43
+ name: t.x.c,
44
+ value: t.x.p,
45
+ time: t.x.tm,
46
+ },
47
+ quantity: t.q,
48
+ profit: t.tp,
49
+ cumulative: t.cp,
50
+ runup: t.rn,
51
+ drawdown: t.dd,
52
+ }));
53
+
54
+ // const historyParser = (history) => history.reverse().map((h) => ({
55
+
35
56
  /**
36
57
  * @typedef {Object} TradeReport Trade report
37
58
 
@@ -86,9 +107,19 @@ function getInputs(options) {
86
107
  * @prop {number} totalTrades Total trades
87
108
  */
88
109
 
110
+ /**
111
+ * @typedef {Object} FromTo
112
+ * @prop {number} from From timestamp
113
+ * @prop {number} to To timestamp
114
+ */
115
+
89
116
  /**
90
117
  * @typedef {Object} StrategyReport
91
118
  * @prop {'EUR' | 'USD' | 'JPY' | '' | 'CHF'} [currency] Selected currency
119
+ * @prop {Object} [settings] Backtester settings
120
+ * @prop {Object} [settings.dateRange] Backtester date range
121
+ * @prop {FromTo} [settings.dateRange.backtest] Date range for backtest
122
+ * @prop {FromTo} [settings.dateRange.trade] Date range for trade
92
123
  * @prop {TradeReport[]} trades Trade list starting by the last
93
124
  * @prop {Object} history History Chart value
94
125
  * @prop {number[]} [history.buyHold] Buy hold values
@@ -275,50 +306,45 @@ module.exports = (chartSession) => class ChartStudy {
275
306
  changes.push('graphic');
276
307
  }
277
308
 
278
- if (parsed.data && parsed.data.report && parsed.data.report.performance) {
279
- this.#strategyReport.performance = parsed.data.report.performance;
280
- changes.push('perfReport');
281
- }
309
+ const updateStrategyReport = (report) => {
310
+ if (report.currency) {
311
+ this.#strategyReport.currency = report.currency;
312
+ changes.push('report.currency');
313
+ }
314
+
315
+ if (report.settings) {
316
+ this.#strategyReport.settings = report.settings;
317
+ changes.push('report.settings');
318
+ }
319
+
320
+ if (report.performance) {
321
+ this.#strategyReport.performance = report.performance;
322
+ changes.push('report.perf');
323
+ }
324
+
325
+ if (report.trades) {
326
+ this.#strategyReport.trades = parseTrades(report.trades);
327
+ changes.push('report.trades');
328
+ }
329
+
330
+ if (report.equity) {
331
+ this.#strategyReport.history = {
332
+ buyHold: report.buyHold,
333
+ buyHoldPercent: report.buyHoldPercent,
334
+ drawDown: report.drawDown,
335
+ drawDownPercent: report.drawDownPercent,
336
+ equity: report.equity,
337
+ equityPercent: report.equityPercent,
338
+ };
339
+ changes.push('report.history');
340
+ }
341
+ };
282
342
 
283
343
  if (parsed.dataCompressed) {
284
- const parsedC = await parseCompressed(parsed.dataCompressed);
285
-
286
- this.#strategyReport = {
287
- currency: parsedC.report.currency,
288
-
289
- trades: parsedC.report.trades.reverse().map((t) => ({
290
- entry: {
291
- name: t.e.c,
292
- type: (t.e.tp[0] === 's' ? 'short' : 'long'),
293
- value: t.e.p,
294
- time: t.e.tm,
295
- },
296
- exit: {
297
- name: t.x.c,
298
- value: t.x.p,
299
- time: t.x.tm,
300
- },
301
- quantity: t.q,
302
- profit: t.tp,
303
- cumulative: t.cp,
304
- runup: t.rn,
305
- drawdown: t.dd,
306
- })),
307
-
308
- history: {
309
- buyHold: parsedC.report.buyHold,
310
- buyHoldPercent: parsedC.report.buyHoldPercent,
311
- drawDown: parsedC.report.drawDown,
312
- drawDownPercent: parsedC.report.drawDownPercent,
313
- equity: parsedC.report.equity,
314
- equityPercent: parsedC.report.equityPercent,
315
- },
316
-
317
- performance: parsedC.report.performance,
318
- };
319
-
320
- changes.push('fullReport');
344
+ updateStrategyReport((await parseCompressed(parsed.dataCompressed)).report);
321
345
  }
346
+
347
+ if (parsed.data && parsed.data.report) updateStrategyReport(parsed.data.report);
322
348
  }
323
349
 
324
350
  if (data.ns.indexes && typeof data.ns.indexes === 'object') {
@@ -372,7 +398,12 @@ module.exports = (chartSession) => class ChartStudy {
372
398
  this.#callbacks.studyCompleted.push(cb);
373
399
  }
374
400
 
375
- /** @typedef {'plots' | 'perfReport' | 'fullReport'} UpdateChangeType */
401
+ /**
402
+ * @typedef {'plots' | 'report.currency'
403
+ * | 'report.settings' | 'report.perf'
404
+ * | 'report.trades' | 'report.history'
405
+ * } UpdateChangeType
406
+ */
376
407
 
377
408
  /**
378
409
  * When an indicator update happens
package/src/client.js CHANGED
@@ -215,6 +215,7 @@ module.exports = class Client {
215
215
  * @typedef {Object} ClientOptions
216
216
  * @prop {string} [token] User auth token (in 'sessionid' cookie)
217
217
  * @prop {boolean} [DEBUG] Enable debug mode
218
+ * @prop {'data' | 'prodata' | 'widgetdata'} [server] Server type
218
219
  */
219
220
 
220
221
  /** Client object
@@ -223,7 +224,8 @@ module.exports = class Client {
223
224
  constructor(clientOptions = {}) {
224
225
  if (clientOptions.DEBUG) global.TW_DEBUG = clientOptions.DEBUG;
225
226
 
226
- this.#ws = new WebSocket('wss://data.tradingview.com/socket.io/websocket', {
227
+ const server = clientOptions.server || 'data';
228
+ this.#ws = new WebSocket(`wss://${server}.tradingview.com/socket.io/websocket`, {
227
229
  origin: 'https://s.tradingview.com',
228
230
  });
229
231
 
@@ -275,10 +275,12 @@ module.exports = {
275
275
  data.result.metaInfo.inputs.forEach((input) => {
276
276
  if (['text', 'pineId', 'pineVersion'].includes(input.id)) return;
277
277
 
278
+ const inlineName = input.name.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '');
279
+
278
280
  inputs[input.id] = {
279
281
  name: input.name,
280
- inline: input.inline || input.name.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, ''),
281
- internalID: input.internalID,
282
+ inline: input.inline || inlineName,
283
+ internalID: input.internalID || inlineName,
282
284
  tooltip: input.tooltip,
283
285
 
284
286
  type: input.type,