@mathieuc/tradingview 2.0.91 → 3.2.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.
Files changed (56) hide show
  1. package/.eslintrc.js +18 -16
  2. package/README.md +19 -98
  3. package/examples/AllPrivateIndicators.js +35 -0
  4. package/examples/BuiltInIndicator.js +41 -0
  5. package/examples/CurrentTimeframe.js +34 -0
  6. package/examples/CustomChartType.js +122 -0
  7. package/examples/CustomTimeframe.js +29 -0
  8. package/examples/Errors.js +147 -0
  9. package/examples/FakeReplayMode.js +41 -0
  10. package/examples/FromToData.js +29 -0
  11. package/examples/GraphicIndicator.js +36 -0
  12. package/examples/MultipleSyncFetch.js +38 -0
  13. package/examples/PinePermManage.js +57 -0
  14. package/examples/ReplayMode.js +97 -0
  15. package/examples/Search.js +15 -0
  16. package/examples/SimpleChart.js +63 -0
  17. package/examples/_RenkoIndicator.js +41 -0
  18. package/main.js +11 -504
  19. package/package.json +9 -8
  20. package/src/FormData.js +35 -0
  21. package/src/chart/graphicParser.js +308 -0
  22. package/src/chart/session.js +546 -0
  23. package/src/chart/study.js +403 -0
  24. package/src/classes/BuiltInIndicator.js +124 -0
  25. package/src/classes/PineIndicator.js +132 -0
  26. package/src/classes/PinePermManager.js +142 -0
  27. package/src/client.js +280 -0
  28. package/{miscRequests.js → src/miscRequests.js} +102 -89
  29. package/src/protocol.js +54 -0
  30. package/src/quote/market.js +123 -0
  31. package/src/quote/session.js +125 -0
  32. package/src/request.js +37 -0
  33. package/src/types.js +32 -0
  34. package/src/utils.js +8 -0
  35. package/test.js +64 -0
  36. package/tests/1. Search.js +18 -0
  37. package/tests/2. QuoteSession.js +54 -0
  38. package/tests/3. SimpleChart.js +61 -0
  39. package/tests/4. Indicators.js +99 -0
  40. package/tests/5. BuiltInIndicator.js +41 -0
  41. package/tests/6. FakeReplayMode.js +59 -0
  42. package/tests/7. ReplayMode.js +51 -0
  43. package/tests/8. CustomChartTypes.js +115 -0
  44. package/tests/9. AllErrors.js +146 -0
  45. package/tests/disabled/x. Template.js +24 -0
  46. package/.github/FUNDING.yml +0 -1
  47. package/tests/alerts/discordWH.js +0 -76
  48. package/tests/alerts/drawings.js +0 -41
  49. package/tests/alerts/envLoader.js +0 -11
  50. package/tests/alerts/manualAlert.js +0 -77
  51. package/tests/alerts/sentNotifs.js +0 -16
  52. package/tests/alerts/strategyAlert.js +0 -94
  53. package/tests/analysis.js +0 -13
  54. package/tests/indicator.js +0 -42
  55. package/tests/prices.js +0 -33
  56. package/tests/strategy.js +0 -56
@@ -0,0 +1,36 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example tests an indicator that sends
5
+ graphic data such as 'lines', 'labels',
6
+ 'boxes', 'tables', 'polygons', etc...
7
+ */
8
+
9
+ const client = new TradingView.Client();
10
+
11
+ const chart = new client.Session.Chart();
12
+ chart.setMarket('BINANCE:BTCEUR', {
13
+ timeframe: '5',
14
+ range: 10000,
15
+ });
16
+
17
+ // TradingView.getIndicator('USER;01efac32df544348810bc843a7515f36').then((indic) => {
18
+ // TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
19
+ TradingView.getIndicator('STD;Zig_Zag').then((indic) => {
20
+ const STD = new chart.Study(indic);
21
+
22
+ STD.onError((...err) => {
23
+ console.log('Study error:', ...err);
24
+ });
25
+
26
+ STD.onReady(() => {
27
+ console.log(`STD '${STD.instance.description}' Loaded !`);
28
+ });
29
+
30
+ STD.onUpdate(() => {
31
+ console.log('Graphic data:', STD.graphic);
32
+ // console.log('Tables:', changes, STD.graphic.tables);
33
+ // console.log('Cells', STD.graphic.tables[0].cells());
34
+ client.end();
35
+ });
36
+ });
@@ -0,0 +1,38 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This examples synchronously
5
+ fetches data from 3 indicators
6
+ */
7
+
8
+ const client = new TradingView.Client();
9
+ const chart = new client.Session.Chart();
10
+ chart.setMarket('BINANCE:DOTUSDT');
11
+
12
+ function getIndicData(indicator) {
13
+ return new Promise((res) => {
14
+ const STD = new chart.Study(indicator);
15
+
16
+ console.log(`Getting "${indicator.description}"...`);
17
+
18
+ STD.onUpdate(() => {
19
+ res(STD.periods);
20
+ console.log(`"${indicator.description}" done !`);
21
+ });
22
+ });
23
+ }
24
+
25
+ (async () => {
26
+ console.log('Getting all indicators...');
27
+
28
+ const indicData = await Promise.all([
29
+ await TradingView.getIndicator('PUB;3lEKXjKWycY5fFZRYYujEy8fxzRRUyF3'),
30
+ await TradingView.getIndicator('PUB;5nawr3gCESvSHQfOhrLPqQqT4zM23w3X'),
31
+ await TradingView.getIndicator('PUB;vrOJcNRPULteowIsuP6iHn3GIxBJdXwT'),
32
+ ].map(getIndicData));
33
+
34
+ console.log(indicData);
35
+ console.log('All done !');
36
+
37
+ client.end();
38
+ })();
@@ -0,0 +1,57 @@
1
+ const { PinePermManager } = require('../main');
2
+
3
+ /*
4
+ This example creates a pine
5
+ permission manager and tests
6
+ all the available functions
7
+ */
8
+
9
+ const sessionid = process.argv[2];
10
+ const pineid = process.argv[3];
11
+
12
+ const manager = new PinePermManager(sessionid, pineid);
13
+
14
+ (async () => {
15
+ console.log('Users:', await manager.getUsers());
16
+
17
+ console.log('Adding user \'TradingView\'...');
18
+
19
+ switch (await manager.addUser('TradingView')) {
20
+ case 'ok':
21
+ console.log('Done !');
22
+ break;
23
+ case 'exists':
24
+ console.log('This user is already authorized');
25
+ break;
26
+ default:
27
+ console.error('Unknown error...');
28
+ break;
29
+ }
30
+
31
+ console.log('Users:', await manager.getUsers());
32
+
33
+ console.log('Modifying expiration date...');
34
+
35
+ const newDate = new Date(Date.now() + 24 * 3600000); // Add one day
36
+ if (await manager.modifyExpiration('TradingView', newDate) === 'ok') {
37
+ console.log('Done !');
38
+ } else console.error('Unknown error...');
39
+
40
+ console.log('Users:', await manager.getUsers());
41
+
42
+ console.log('Removing expiration date...');
43
+
44
+ if (await manager.modifyExpiration('TradingView') === 'ok') {
45
+ console.log('Done !');
46
+ } else console.error('Unknown error...');
47
+
48
+ console.log('Users:', await manager.getUsers());
49
+
50
+ console.log('Removing user \'TradingView\'...');
51
+
52
+ if (await manager.removeUser('TradingView') === 'ok') {
53
+ console.log('Done !');
54
+ } else console.error('Unknown error...');
55
+
56
+ console.log('Users:', await manager.getUsers());
57
+ })();
@@ -0,0 +1,97 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example tests the real replay
5
+ mode by fetching indicator data and
6
+ stores it in a 'periods' variable
7
+ */
8
+
9
+ const config = {
10
+ symbol: 'BINANCE:BTCEUR',
11
+ timeframe: 'D',
12
+ startFrom: Math.round(Date.now() / 1000) - 86400 * 7, // Seven days before now
13
+ // startFrom: 1600000000,
14
+ };
15
+
16
+ console.log('----- Testing ReplayMode: -----');
17
+
18
+ const client = new TradingView.Client();
19
+ const chart = new client.Session.Chart();
20
+
21
+ chart.setMarket(config.symbol, {
22
+ timeframe: config.timeframe,
23
+ replay: config.startFrom,
24
+ range: 1,
25
+ });
26
+
27
+ // chart.onReplayLoaded(() => {
28
+ // console.log('STARTING REPLAY MODE');
29
+ // chart.replayStart(1000);
30
+
31
+ // // setTimeout(() => {
32
+ // // console.log('STOPPING REPLAY MODE');
33
+ // // chart.replayStop();
34
+ // // }, 7000);
35
+ // });
36
+
37
+ let loading = 0;
38
+ const indicators = [];
39
+ const periods = {};
40
+
41
+ let interval = NaN;
42
+
43
+ async function step() {
44
+ const period = { ...chart.periods[0] };
45
+
46
+ const times = Object.keys(periods);
47
+ const intrval = times[times.length - 1] - times[times.length - 2];
48
+ if (Number.isNaN(interval) && times.length >= 2) interval = intrval;
49
+
50
+ if (!Number.isNaN(interval) && interval !== intrval) {
51
+ throw new Error(`Wrong interval: ${intrval} (should be ${interval})`);
52
+ }
53
+
54
+ indicators.forEach(([n, i]) => {
55
+ const plots = { ...i.periods[0] };
56
+ delete plots.$time;
57
+ period[n] = { plots };
58
+
59
+ Object.keys(i.graphic).forEach((g) => {
60
+ if (!i.graphic[g].length) return;
61
+ if (!period[n].graphics) period[n].graphics = {};
62
+ period[n].graphics[g] = i.graphic[g];
63
+ });
64
+ });
65
+
66
+ periods[period.time] = period;
67
+
68
+ console.log('Next ->', period.time, times.length);
69
+
70
+ await chart.replayStep(1);
71
+ step();
72
+ }
73
+
74
+ chart.onReplayEnd(async () => {
75
+ await client.end();
76
+ console.log('Done !', Object.keys(periods).length);
77
+ });
78
+
79
+ async function addIndicator(name, pineId, options = {}) {
80
+ loading += 1;
81
+
82
+ const indic = pineId.includes('@')
83
+ ? new TradingView.BuiltInIndicator(pineId)
84
+ : await TradingView.getIndicator(pineId);
85
+ Object.keys(options).forEach((o) => { indic.setOption(o, options[o]); });
86
+
87
+ const std = new chart.Study(indic);
88
+
89
+ std.onReady(() => {
90
+ indicators.push([name, std]);
91
+ if (loading === indicators.length) step();
92
+ });
93
+ }
94
+
95
+ addIndicator('Volume', 'Volume@tv-basicstudies-144');
96
+ addIndicator('EMA_50', 'STD;EMA', { Length: 50 });
97
+ addIndicator('EMA_200', 'STD;EMA', { Length: 200 });
@@ -0,0 +1,15 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example tests the searching
5
+ functions such as 'searchMarket'
6
+ and 'searchIndicator'
7
+ */
8
+
9
+ TradingView.searchMarket('BINANCE:').then((rs) => {
10
+ console.log('Found Markets:', rs);
11
+ });
12
+
13
+ TradingView.searchIndicator('RSI').then((rs) => {
14
+ console.log('Found Indicators:', rs);
15
+ });
@@ -0,0 +1,63 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example creates a BTCEUR daily chart
5
+ */
6
+
7
+ const client = new TradingView.Client(); // Creates a websocket client
8
+
9
+ const chart = new client.Session.Chart(); // Init a Chart session
10
+
11
+ chart.setMarket('BINANCE:BTCEUR', { // Set the market
12
+ timeframe: 'D',
13
+ });
14
+
15
+ chart.onError((...err) => { // Listen for errors (can avoid crash)
16
+ console.error('Chart error:', ...err);
17
+ // Do something...
18
+ });
19
+
20
+ chart.onSymbolLoaded(() => { // When the symbol is successfully loaded
21
+ console.log(`Market "${chart.infos.description}" loaded !`);
22
+ });
23
+
24
+ chart.onUpdate(() => { // When price changes
25
+ if (!chart.periods[0]) return;
26
+ console.log(`[${chart.infos.description}]: ${chart.periods[0].close} ${chart.infos.currency_id}`);
27
+ // Do something...
28
+ });
29
+
30
+ // Wait 5 seconds and set the market to BINANCE:ETHEUR
31
+ setTimeout(() => {
32
+ console.log('\nSetting market to BINANCE:ETHEUR...');
33
+ chart.setMarket('BINANCE:ETHEUR', {
34
+ timeframe: 'D',
35
+ });
36
+ }, 5000);
37
+
38
+ // Wait 10 seconds and set the timeframe to 15 minutes
39
+ setTimeout(() => {
40
+ console.log('\nSetting timeframe to 15 minutes...');
41
+ chart.setSeries('15');
42
+ }, 10000);
43
+
44
+ // Wait 15 seconds and set the chart type to "Heikin Ashi"
45
+ setTimeout(() => {
46
+ console.log('\nSetting the chart type to "Heikin Ashi"s...');
47
+ chart.setMarket('BINANCE:ETHEUR', {
48
+ timeframe: 'D',
49
+ type: 'HeikinAshi',
50
+ });
51
+ }, 15000);
52
+
53
+ // Wait 20 seconds and close the chart
54
+ setTimeout(() => {
55
+ console.log('\nClosing the chart...');
56
+ chart.delete();
57
+ }, 20000);
58
+
59
+ // Wait 25 seconds and close the client
60
+ setTimeout(() => {
61
+ console.log('\nClosing the client...');
62
+ client.end();
63
+ }, 25000);
@@ -0,0 +1,41 @@
1
+ const TradingView = require('../main');
2
+
3
+ if (!process.argv[2]) throw Error('Please specify your \'sessionid\' cookie');
4
+
5
+ const client = new TradingView.Client({
6
+ token: process.argv[2],
7
+ });
8
+
9
+ const chart = new client.Session.Chart();
10
+ chart.setMarket('NASDAQ:AAPL', {
11
+ type: 'Renko',
12
+ timeframe: '1',
13
+ inputs: {
14
+ source: 'close',
15
+ sources: 'Close',
16
+ boxSize: 3,
17
+ style: 'Traditional',
18
+ atrLength: 14,
19
+ wicks: true,
20
+ },
21
+ });
22
+
23
+ TradingView.searchIndicator('CM_Ultimate_MA_MTF_V2').then(async (indicator) => {
24
+ console.log(
25
+ '*** Indicator information\n',
26
+ indicator[0].id,
27
+ );
28
+ const pineIndicator = await indicator[0].get();
29
+ const study = new chart.Study(pineIndicator);
30
+ study.onUpdate(() => {
31
+ console.log(
32
+ '\n*** Chart time and close:\n',
33
+ chart.periods[0].time,
34
+ chart.periods[0].close,
35
+ );
36
+ console.log(
37
+ '\n*** Indicator values:\n',
38
+ study.periods[0].MultiTimeframe_Moving_Avg,
39
+ );
40
+ });
41
+ });