@mathieuc/tradingview 2.0.5 → 3.0.0-pre

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/.eslintrc.js CHANGED
@@ -1,16 +1,18 @@
1
- module.exports = {
2
- env: {
3
- commonjs: true,
4
- es2021: true,
5
- node: true,
6
- },
7
- extends: [
8
- 'airbnb-base',
9
- ],
10
- parserOptions: {
11
- ecmaVersion: 12,
12
- },
13
- rules: {
14
- 'no-console': 'off',
15
- },
16
- };
1
+ module.exports = {
2
+ env: {
3
+ commonjs: true,
4
+ es2021: true,
5
+ node: true,
6
+ },
7
+ extends: [
8
+ 'airbnb-base',
9
+ ],
10
+ parser: '@babel/eslint-parser',
11
+ parserOptions: {
12
+ ecmaVersion: 12,
13
+ requireConfigFile: false,
14
+ },
15
+ rules: {
16
+ 'no-console': 'off',
17
+ },
18
+ };
@@ -0,0 +1 @@
1
+ github: Mathieu2301
package/README.md CHANGED
@@ -1,6 +1,29 @@
1
- # Tradingview API
1
+ # TradingView API
2
2
  Get realtime market prices and indicator values from Tradingview !
3
3
 
4
+ ## Features
5
+ - [x] Premium features
6
+ - [x] Automatically backtest many strategies and try many settings in a very little time
7
+ - [x] Get drawings you made on your chart
8
+ - [x] Works with invite-only indicators
9
+ - [x] Unlimited simultaneous indicators
10
+ - [x] Realtime
11
+ - [x] Get TradingView's technical analysis
12
+ - [ ] Get values from a specific date range
13
+ - [ ] TradingView socket server emulation
14
+ - [ ] Interract with public chats
15
+ - [ ] Get Screener top values
16
+ - [ ] Get Hotlists
17
+ - [ ] Get Calendar
18
+ - IF YOU WANT A FEATURE, ASK ME !
19
+
20
+ ## Possibilities
21
+ - Trading bot
22
+ - Discord alerts
23
+ - Hard backtest
24
+ - Machine Learning based indicator
25
+ - Free replay mode for all timeframes
26
+
4
27
  ___
5
28
  ## Installation
6
29
 
@@ -8,102 +31,9 @@ ___
8
31
  npm i @mathieuc/tradingview
9
32
  ```
10
33
 
11
- ## Examples (./tests/)
12
-
13
- ```javascript
14
- /*
15
- ./tests/prices.js
16
- Search for Bitcoin and Ethereum and get real time prices
17
- */
18
-
19
- const marketAPI = require('tradingview');
20
-
21
- (async () => {
22
- const market = marketAPI();
23
-
24
- market.on('logged', async () => {
25
- console.log('API LOGGED');
26
-
27
- const searchBTC = (await market.search('bitcoin euro', 'crypto'))[0];
28
- console.log('Found Bitcoin / Euro:', searchBTC);
29
- market.subscribe(searchBTC.id);
30
- });
31
-
32
- market.on('price', (data) => {
33
- console.log(data.symbol, '=>', data.price);
34
- });
35
-
36
- const searchETH = (await market.search('ethereum euro', 'crypto'))[0];
37
- console.log('Found Ethereum / Euro:', searchETH);
38
-
39
- setTimeout(() => {
40
- console.log('Subscribe to', searchETH.id);
41
- market.subscribe(searchETH.id);
42
- }, 10000);
43
-
44
- setTimeout(() => {
45
- console.log('Unsubscribe from', searchETH.id);
46
- market.unsubscribe(searchETH.id);
47
- }, 20000);
48
- })();
49
- ```
50
-
51
- ```javascript
52
- /*
53
- ./tests/analysis.js
54
- Search for Bitcoin and get the Technical Analysis in all timeframes
55
- */
34
+ ## Examples
35
+ You can find all the examples and snippets in `./examples` folder.
56
36
 
57
- const marketAPI = require('tradingview');
58
-
59
- const market = marketAPI();
60
-
61
- market.on('logged', async () => {
62
- const searchBTC = (await market.search('bitcoin euro', 'crypto'))[0];
63
- console.log('Found Bitcoin / Euro:', searchBTC);
64
-
65
- const TA = await searchBTC.getTA();
66
- console.log('Full technical analysis for Bitcoin:', TA);
67
-
68
- // You can also use this way: await market.getTA('crypto', 'BINANCE:BTCEUR');
69
- });
70
- ```
71
-
72
- ```javascript
73
- /*
74
- ./tests/indicator.js
75
- Get indicator values
76
- */
77
-
78
- const marketAPI = require('tradingview');
79
-
80
- const market = marketAPI(false); // 'false' for chart-only mode
81
-
82
- market.on('logged', () => {
83
- market.initChart({
84
- symbol: 'COINBASE:BTCEUR',
85
- period: '240',
86
- range: 50,
87
- indicators: [
88
- { name: 'ACCU_DISTRIB', id: 'STD;Accumulation_Distribution', version: '25' },
89
- { name: 'CIPHER_A', id: 'PUB;vrOJcNRPULteowIsuP6iHn3GIxBJdXwT', version: '1.0' },
90
- { name: 'CIPHER_B', id: 'PUB;uA35GeckoTA2EfgI63SD2WCSmca4njxp', version: '15.0' },
91
- // Color Changing moving average
92
- { name: 'CCMA', id: 'PUB;5nawr3gCESvSHQfOhrLPqQqT4zM23w3X', version: '6.0' },
93
- ],
94
- }, (periods) => {
95
- if (!periods[0].CIPHER_B) return;
96
- if (!periods[0].CCMA) return;
97
-
98
- console.log('Last period:', {
99
- price: periods[0].$prices.close,
100
- moneyFlow: (periods[0].CIPHER_B.RSIMFIArea >= 0) ? 'POSITIVE' : 'NEGATIVE',
101
- VWAP: periods[0].CIPHER_B.VWAP,
102
- MA: (periods[0].CCMA.Plot <= periods[0].$prices.close) ? 'ABOVE' : 'UNDER',
103
- });
104
- });
105
- });
106
- ```
107
37
  ___
108
38
  ## Problems
109
39
  If you have errors in console or unwanted behavior,
@@ -0,0 +1,35 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example creates a chart with
5
+ all user's private indicators
6
+ */
7
+
8
+ const client = new TradingView.Client({
9
+ /* Token is only needed if at least one indicator is
10
+ PRIVATE (if you have a paid TradingView account) */
11
+ token: 'YOUR_SESSION_ID_COOKIE',
12
+ });
13
+
14
+ const chart = new client.Session.Chart();
15
+ chart.setMarket('BINANCE:BTCEUR', {
16
+ timeframe: 'D',
17
+ });
18
+
19
+ TradingView.getPrivateIndicators('YOUR_SESSION_ID_COOKIE').then((indicList) => {
20
+ indicList.forEach(async (indic) => {
21
+ const privateIndic = await indic.get();
22
+ console.log('Loading indicator', indic.name, '...');
23
+
24
+ const indicator = new chart.Study(privateIndic);
25
+
26
+ indicator.onReady(() => {
27
+ console.log('Indicator', indic.name, 'loaded !');
28
+ });
29
+
30
+ indicator.onUpdate(() => {
31
+ console.log('Plot values', indicator.periods);
32
+ console.log('Strategy report', indicator.strategyReport);
33
+ });
34
+ });
35
+ });
@@ -0,0 +1,41 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example tests built-in indicators
5
+ like volume-based indicators
6
+ */
7
+
8
+ const volumeProfile = new TradingView.BuiltInIndicator('VbPFixed@tv-basicstudies-139!');
9
+
10
+ if (!process.argv[2] && volumeProfile.type !== 'VbPFixed@tv-basicstudies-139!') {
11
+ throw Error('Please specify your \'sessionid\' cookie');
12
+ }
13
+
14
+ const client = new TradingView.Client({
15
+ token: process.argv[2],
16
+ });
17
+
18
+ const chart = new client.Session.Chart();
19
+ chart.setMarket('BINANCE:BTCEUR', {
20
+ timeframe: '60',
21
+ range: 1,
22
+ });
23
+
24
+ /* Required or not, depending on the indicator */
25
+ volumeProfile.setOption('first_bar_time', Date.now() - 10 ** 8);
26
+ // volumeProfile.setOption('first_visible_bar_time', Date.now() - 10 ** 8);
27
+
28
+ const VOL = new chart.Study(volumeProfile);
29
+ VOL.onUpdate(() => {
30
+ VOL.graphic.horizHists
31
+ .filter((h) => h.lastBarTime === 0) // We only keep recent volume infos
32
+ .sort((a, b) => b.priceHigh - a.priceHigh)
33
+ .forEach((h) => {
34
+ console.log(
35
+ `~ ${Math.round((h.priceHigh + h.priceLow) / 2)} € :`,
36
+ `${'_'.repeat(h.rate[0] / 3)}${'_'.repeat(h.rate[1] / 3)}`,
37
+ );
38
+ });
39
+
40
+ client.end();
41
+ });
@@ -0,0 +1,121 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example creates charts of custom
5
+ types such as 'HeikinAshi', 'Renko',
6
+ 'LineBreak', 'Kagi', 'PointAndFigure',
7
+ and 'Range' with default settings
8
+ */
9
+
10
+ const client = new TradingView.Client({
11
+ /* Token is only required if you want to use intraday
12
+ timeframes (if you have a paid TradingView account) */
13
+ token: process.argv[2],
14
+ });
15
+
16
+ const chart = new client.Session.Chart();
17
+
18
+ chart.onError((...err) => {
19
+ console.log('Chart error:', ...err);
20
+ });
21
+
22
+ chart.onUpdate(() => {
23
+ if (!chart.periods[0]) return;
24
+ console.log('Last period', chart.periods[0]);
25
+ });
26
+
27
+ /* (0s) Heikin Ashi chart */
28
+ setTimeout(() => {
29
+ console.log('\nSetting chart type to: HeikinAshi');
30
+
31
+ chart.setMarket('BINANCE:BTCEUR', {
32
+ type: 'HeikinAshi',
33
+ timeframe: 'D',
34
+ });
35
+ }, 0);
36
+
37
+ /* (5s) Renko chart */
38
+ setTimeout(() => {
39
+ console.log('\nSetting chart type to: Renko');
40
+
41
+ chart.setMarket('BINANCE:BTCEUR', {
42
+ type: 'Renko',
43
+ timeframe: 'D',
44
+ inputs: {
45
+ source: 'close',
46
+ sources: 'Close',
47
+ boxSize: 3,
48
+ style: 'ATR',
49
+ atrLength: 14,
50
+ wicks: true,
51
+ },
52
+ });
53
+ }, 5000);
54
+
55
+ /* (10s) Line Break chart */
56
+ setTimeout(() => {
57
+ console.log('\nSetting chart type to: LineBreak');
58
+
59
+ chart.setMarket('BINANCE:BTCEUR', {
60
+ type: 'LineBreak',
61
+ timeframe: 'D',
62
+ inputs: {
63
+ source: 'close',
64
+ lb: 3,
65
+ },
66
+ });
67
+ }, 10000);
68
+
69
+ /* (15s) Kagi chart */
70
+ setTimeout(() => {
71
+ console.log('\nSetting chart type to: Kagi');
72
+
73
+ chart.setMarket('BINANCE:BTCEUR', {
74
+ type: 'Kagi',
75
+ timeframe: 'D',
76
+ inputs: {
77
+ source: 'close',
78
+ style: 'ATR',
79
+ atrLength: 14,
80
+ reversalAmount: 1,
81
+ },
82
+ });
83
+ }, 15000);
84
+
85
+ /* (20s) Point & Figure chart */
86
+ setTimeout(() => {
87
+ console.log('\nSetting chart type to: PointAndFigure');
88
+
89
+ chart.setMarket('BINANCE:BTCEUR', {
90
+ type: 'PointAndFigure',
91
+ timeframe: 'D',
92
+ inputs: {
93
+ sources: 'Close',
94
+ reversalAmount: 3,
95
+ boxSize: 1,
96
+ style: 'ATR',
97
+ atrLength: 14,
98
+ oneStepBackBuilding: false,
99
+ },
100
+ });
101
+ }, 20000);
102
+
103
+ /* (25s) Range chart */
104
+ setTimeout(() => {
105
+ console.log('\nSetting chart type to: Range');
106
+
107
+ chart.setMarket('BINANCE:BTCEUR', {
108
+ type: 'Range',
109
+ timeframe: 'D',
110
+ inputs: {
111
+ range: 1,
112
+ phantomBars: false,
113
+ },
114
+ });
115
+ }, 25000);
116
+
117
+ /* (30s) Delete chart, close client */
118
+ setTimeout(() => {
119
+ console.log('\nClosing client...');
120
+ client.end();
121
+ }, 30000);
@@ -0,0 +1,29 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example tests custom
5
+ timeframes like 1 second
6
+ */
7
+
8
+ if (!process.argv[2]) throw Error('Please specify your \'sessionid\' cookie');
9
+
10
+ const client = new TradingView.Client({
11
+ token: process.argv[2],
12
+ });
13
+
14
+ const chart = new client.Session.Chart();
15
+ chart.setTimezone('Europe/Paris');
16
+
17
+ chart.setMarket('CAPITALCOM:US100', {
18
+ timeframe: '1S',
19
+ range: 10,
20
+ });
21
+
22
+ chart.onSymbolLoaded(() => {
23
+ console.log(chart.infos.name, 'loaded !');
24
+ });
25
+
26
+ chart.onUpdate(() => {
27
+ console.log('OK', chart.periods);
28
+ client.end();
29
+ });
@@ -0,0 +1,153 @@
1
+ const TradingView = require('../main');
2
+
3
+ /*
4
+ This example tests many types of errors
5
+ */
6
+
7
+ const client = new TradingView.Client(); // Creates a websocket client
8
+
9
+ const tests = [
10
+ (next) => { /* Testing "Credentials error" */
11
+ console.info('\nTesting "Credentials error" error:');
12
+
13
+ const client2 = new TradingView.Client({
14
+ token: 'FAKE_CREDENTIALS', // Set wrong credentials
15
+ });
16
+
17
+ client2.onError((...err) => {
18
+ console.error(' => Client error:', err);
19
+ client2.end();
20
+ next();
21
+ });
22
+ },
23
+
24
+ (next) => { /* Testing "Invalid symbol" */
25
+ console.info('\nTesting "Invalid symbol" error:');
26
+
27
+ const chart = new client.Session.Chart();
28
+ chart.onError((...err) => { // Listen for errors
29
+ console.error(' => Chart error:', err);
30
+ chart.delete();
31
+ next();
32
+ });
33
+
34
+ chart.setMarket('XXXXX'); // Set a wrong market
35
+ },
36
+
37
+ (next) => { /* Testing "Invalid timezone" */
38
+ console.info('\nTesting "Invalid timezone" error:');
39
+
40
+ const chart = new client.Session.Chart();
41
+ chart.onError((...err) => { // Listen for errors
42
+ console.error(' => Chart error:', err);
43
+ next();
44
+ });
45
+
46
+ chart.setMarket('BINANCE:BTCEUR'); // Set a market
47
+ chart.setTimezone('Nowhere/Nowhere'); // Set a fake timezone
48
+ },
49
+
50
+ (next) => { /* Testing "Custom timeframe" */
51
+ console.info('\nTesting "Custom timeframe" error:');
52
+
53
+ const chart = new client.Session.Chart();
54
+ chart.onError((...err) => { // Listen for errors
55
+ console.error(' => Chart error:', err);
56
+ chart.delete();
57
+ next();
58
+ });
59
+
60
+ chart.setMarket('BINANCE:BTCEUR', { // Set a market
61
+ timeframe: '20', // Set a custom timeframe
62
+ /*
63
+ Timeframe '20' isn't available because we are
64
+ not logged in as a premium TradingView user
65
+ */
66
+ });
67
+ },
68
+
69
+ (next) => { /* Testing "Invalid timeframe" */
70
+ console.info('\nTesting "Invalid timeframe" error:');
71
+
72
+ const chart = new client.Session.Chart();
73
+ chart.onError((...err) => { // Listen for errors
74
+ console.error(' => Chart error:', err);
75
+ next();
76
+ });
77
+
78
+ chart.setMarket('BINANCE:BTCEUR', { // Set a market
79
+ timeframe: 'XX', // Set a wrong timeframe
80
+ });
81
+ },
82
+
83
+ (next) => { /* Testing "Study not auth" */
84
+ console.info('\nTesting "Study not auth" error:');
85
+
86
+ const chart = new client.Session.Chart();
87
+ chart.onError((...err) => { // Listen for errors
88
+ console.error(' => Chart error:', err);
89
+ next();
90
+ });
91
+
92
+ chart.setMarket('BINANCE:BTCEUR', { // Set a market
93
+ timeframe: '15',
94
+ type: 'Renko',
95
+ });
96
+
97
+ chart.onUpdate(() => {
98
+ console.log('DATA', chart.periods[0]);
99
+ });
100
+ },
101
+
102
+ (next) => { /* Testing "Set the market before" */
103
+ console.info('\nTesting "Set the market before..." error:');
104
+
105
+ const chart = new client.Session.Chart();
106
+ chart.onError((...err) => { // Listen for errors
107
+ console.error(' => Chart error:', err);
108
+ chart.delete();
109
+ next();
110
+ });
111
+
112
+ chart.setSeries('15'); // Set series before setting the market
113
+ },
114
+
115
+ (next) => { /* Testing "Inexistent indicator" */
116
+ console.info('\nTesting "Inexistent indicator" error:');
117
+
118
+ TradingView.getIndicator('STD;XXXXXXX')
119
+ .catch((err) => {
120
+ console.error(' => API error:', [err.message]);
121
+ next();
122
+ });
123
+ },
124
+
125
+ async (next) => { /* Testing "Invalid value" */
126
+ console.info('\nTesting "Invalid value" error:');
127
+
128
+ const chart = new client.Session.Chart();
129
+ chart.setMarket('BINANCE:BTCEUR'); // Set a market
130
+
131
+ const ST = await TradingView.getIndicator('STD;Supertrend');
132
+ ST.setOption('Factor', -1); // This will cause an error
133
+
134
+ const Supertrend = new chart.Study(ST);
135
+ Supertrend.onError((...err) => {
136
+ console.error(' => Study error:', err);
137
+ chart.delete();
138
+ next();
139
+ });
140
+ },
141
+ ];
142
+
143
+ let i = 0;
144
+ const execTest = () => {
145
+ if (tests[i + 1]) {
146
+ tests[(i += 1) - 1](execTest);
147
+ } else {
148
+ tests[(i += 1) - 1](() => {
149
+ console.log(`\nTests ${i}/${i} done !`);
150
+ });
151
+ }
152
+ };
153
+ execTest();
@@ -0,0 +1,44 @@
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: 'W',
14
+ });
15
+
16
+ // TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
17
+ TradingView.getIndicator('USER;8bbd8017fd3e4881bf91f4fea5e3d538').then((indic) => {
18
+ const STD = new chart.Study(indic);
19
+
20
+ STD.onError((...err) => {
21
+ console.log('Chart error:', ...err);
22
+ });
23
+
24
+ STD.onReady(() => {
25
+ console.log('STD Loaded !');
26
+ });
27
+
28
+ STD.onUpdate((changes) => {
29
+ // STD.graphic;
30
+ console.log('Update:', changes, STD.graphic);
31
+ // client.end();
32
+ });
33
+ });
34
+
35
+ // setInterval(() => {
36
+ // chart.fetchMore(100);
37
+ // }, 2000);
38
+
39
+ // setTimeout(() => {
40
+ // console.log('Setting timeframe to: \'D\'');
41
+ // chart.setSeries('D');
42
+ // }, 5000);
43
+
44
+ chart.onUpdate(() => console.log(chart.periods.length));
@@ -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,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
+ });