Skip to content

📈 Get real-time stocks from TradingView

Better tests + Add "Authenticated" test

Mathieu Colmone33d709b2f8c
diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index 654231a..0b2bef5 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -26,3 +26,5 @@ jobs:
run: npm ci
- name: Run tests
run: npm test
+ env:
+ SESSION: ${{ secrets.TW_SESSION }}
diff --git a/test.js b/test.js
index 6347acc..72db92d 100644
--- a/test.js
+++ b/test.js
@@ -47,17 +47,21 @@ function success() {
}
console.info('§90§30§104 ==== ALL TESTS DONE ==== §0\n');
+
+ let errored = false;
+
Object.keys(TESTS).forEach((t) => {
let color = '2';
if (TESTS[t].warnings) color = '3';
- if (TESTS[t].errors) color = '1';
+ if (TESTS[t].errors) {
+ color = '1';
+ errored = true;
+ }
console.info(`§90§30§10${color} ${t} §0 §91E§0: ${TESTS[t].errors}§0 §93W§0: ${TESTS[t].warnings}`);
- // console.info(` - §91 Errors:§0 ${TESTS[t].errors}`);
- // console.info(` - §93 Warnings:§0 ${TESTS[t].warnings}`);
});
- process.exit(0);
+ process.exit(errored ? 1 : 0);
})();
setTimeout(() => {
diff --git a/tests/1. Search.js b/tests/01. Search.js
similarity index 92%
rename from tests/1. Search.js
rename to tests/01. Search.js
index 5843bd2..4e3aca9 100644
--- a/tests/1. Search.js
+++ b/tests/01. Search.js
@@ -11,7 +11,6 @@ module.exports = async (log, success, warn, err, cb) => {
if (markets.length < 10 || indicators.length < 10) {
err('Not enough results');
- throw new Error('Not enough results');
}
cb();
diff --git a/tests/2. QuoteSession.js b/tests/02. QuoteSession.js
similarity index 91%
rename from tests/2. QuoteSession.js
rename to tests/02. QuoteSession.js
index f749416..8acee93 100644
--- a/tests/2. QuoteSession.js
+++ b/tests/02. QuoteSession.js
@@ -5,7 +5,6 @@ module.exports = (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', ...error);
- throw new Error('Client error');
});
const quoteSession = new client.Session.Quote({
@@ -38,7 +37,6 @@ module.exports = (log, success, warn, err, cb) => {
keys.forEach((k) => {
if (!rsKeys.includes(k)) {
err(`Missing '${k}' key in`, rsKeys);
- throw new Error('Missing key');
}
});
@@ -49,6 +47,5 @@ module.exports = (log, success, warn, err, cb) => {
BTC.onError((...error) => {
err('BTCEUR ERROR:', error);
- throw new Error('Missing key');
});
};
diff --git a/tests/3. SimpleChart.js b/tests/03. SimpleChart.js
similarity index 95%
rename from tests/3. SimpleChart.js
rename to tests/03. SimpleChart.js
index 7630ac5..c2c6fd0 100644
--- a/tests/3. SimpleChart.js
+++ b/tests/03. SimpleChart.js
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', error);
- throw new Error('Client error');
});
const chart = new client.Session.Chart();
@@ -16,7 +15,6 @@ module.exports = async (log, success, warn, err, cb) => {
chart.onError((...error) => {
err('Chart error', error);
- throw new Error('Chart error');
});
chart.onSymbolLoaded(() => { // When the symbol is successfully loaded
diff --git a/tests/4. Indicators.js b/tests/04. Indicators.js
similarity index 94%
rename from tests/4. Indicators.js
rename to tests/04. Indicators.js
index 6a92231..82bbfc7 100644
--- a/tests/4. Indicators.js
+++ b/tests/04. Indicators.js
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', error);
- throw new Error('Client error');
});
const chart = new client.Session.Chart();
@@ -15,7 +14,6 @@ module.exports = async (log, success, warn, err, cb) => {
chart.onError((...error) => {
err('Chart error', error);
- throw new Error('Chart error');
});
TradingView.getIndicator('STD;Supertrend%Strategy').then((indicator) => {
@@ -31,7 +29,6 @@ module.exports = async (log, success, warn, err, cb) => {
SuperTrend.onError((...error) => {
err('SuperTrend error', error[0]);
- throw new Error('SuperTrend error');
});
let QTY = 10;
@@ -81,7 +78,6 @@ module.exports = async (log, success, warn, err, cb) => {
CipherB.onError((...error) => {
err('MarketCipher B error:', error[0]);
- throw new Error('MarketCipher B error');
});
CipherB.onUpdate(() => {
diff --git a/tests/5. BuiltInIndicator.js b/tests/05. BuiltInIndicator.js
similarity index 93%
rename from tests/5. BuiltInIndicator.js
rename to tests/05. BuiltInIndicator.js
index 4bcbc93..d35fa2d 100644
--- a/tests/5. BuiltInIndicator.js
+++ b/tests/05. BuiltInIndicator.js
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', error);
- throw new Error('Client error');
});
const chart = new client.Session.Chart();
@@ -15,7 +14,6 @@ module.exports = async (log, success, warn, err, cb) => {
chart.onError((...error) => {
err('Chart error', error);
- throw new Error('Chart error');
});
const volumeProfile = new TradingView.BuiltInIndicator('VbPFixed@tv-basicstudies-139!');
diff --git a/tests/6. FakeReplayMode.js b/tests/06. FakeReplayMode.js
similarity index 89%
rename from tests/6. FakeReplayMode.js
rename to tests/06. FakeReplayMode.js
index 27d7dd9..ae5e177 100644
--- a/tests/6. FakeReplayMode.js
+++ b/tests/06. FakeReplayMode.js
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', error);
- throw new Error('Client error');
});
const chart = new client.Session.Chart();
@@ -17,7 +16,6 @@ module.exports = async (log, success, warn, err, cb) => {
chart.onError((...error) => {
err('Chart error', error);
- throw new Error('Chart error');
});
let interval = NaN;
@@ -43,7 +41,7 @@ module.exports = async (log, success, warn, err, cb) => {
if (Number.isNaN(interval) && times.length >= 2) interval = intrval;
if (!Number.isNaN(interval) && interval !== intrval) {
- throw new Error(`Wrong interval: ${intrval} (should be ${interval})`);
+ err(`Wrong interval: ${intrval} (should be ${interval})`);
}
log('Next ->', times[0]);
diff --git a/tests/7. ReplayMode.js b/tests/07. ReplayMode.js
similarity index 93%
rename from tests/7. ReplayMode.js
rename to tests/07. ReplayMode.js
index 9aa32c2..7043f5c 100644
--- a/tests/7. ReplayMode.js
+++ b/tests/07. ReplayMode.js
@@ -5,7 +5,6 @@ module.exports = async (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', error);
- throw new Error('Client error');
});
const chart = new client.Session.Chart();
@@ -17,7 +16,6 @@ module.exports = async (log, success, warn, err, cb) => {
chart.onError((...error) => {
err('Chart error', error);
- throw new Error('Chart error');
});
let finished = false;
diff --git a/tests/8. CustomChartTypes.js b/tests/08. CustomChartTypes.js
similarity index 96%
rename from tests/8. CustomChartTypes.js
rename to tests/08. CustomChartTypes.js
index ec35ea3..5de689a 100644
--- a/tests/8. CustomChartTypes.js
+++ b/tests/08. CustomChartTypes.js
@@ -5,14 +5,12 @@ module.exports = async (log, success, warn, err, cb) => {
client.onError((...error) => {
err('Client error', error);
- throw new Error('Client error');
});
const chart = new client.Session.Chart();
chart.onError((...error) => {
err('Chart error', error);
- throw new Error('Chart error');
});
/* (0s) Heikin Ashi chart */
diff --git a/tests/9. AllErrors.js b/tests/09. AllErrors.js
similarity index 100%
rename from tests/9. AllErrors.js
rename to tests/09. AllErrors.js
diff --git a/tests/10. Authenticated.js b/tests/10. Authenticated.js
new file mode 100644
index 0000000..118bda4
--- /dev/null
+++ b/tests/10. Authenticated.js
@@ -0,0 +1,99 @@
+/* eslint-disable no-restricted-syntax */
+/* eslint-disable no-await-in-loop */
+const TradingView = require('../main');
+
+const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
+
+module.exports = async (log, success, warn, err, cb) => {
+ log('Getting user info');
+
+ const userInfos = await TradingView.getUser(process.env.SESSION);
+ if (userInfos && userInfos.id) {
+ success('User info:', {
+ id: userInfos.id,
+ username: userInfos.username,
+ firstName: userInfos.firstName,
+ lastName: userInfos.lastName,
+ following: userInfos.following,
+ followers: userInfos.followers,
+ notifications: userInfos.notifications,
+ joinDate: userInfos.joinDate,
+ });
+ } else err('Get user error. Result:', userInfos);
+
+ await wait(1000);
+
+ log('Getting user indicators');
+
+ const userIndicators = await TradingView.getPrivateIndicators(process.env.SESSION);
+ if (userIndicators) {
+ if (userIndicators.length === 0) warn('No private indicator found');
+ else success('User indicators:', userIndicators.map((i) => i.name));
+ } else err('Get indicators error. Result:', userIndicators);
+
+ await wait(1000);
+
+ log('Creating logged client');
+ const client = new TradingView.Client({
+ token: process.env.SESSION,
+ });
+ client.onError((...error) => {
+ err('Client error', error);
+ });
+
+ const chart = new client.Session.Chart();
+ chart.onError((...error) => {
+ err('Chart error', error);
+ });
+
+ log('Setting market to BINANCE:BTCEUR...');
+ chart.setMarket('BINANCE:BTCEUR', { timeframe: 'D' });
+
+ const checked = new Set();
+ async function check(item) {
+ await wait(1000);
+
+ checked.add(item);
+ if (checked.size < userIndicators.length + 1) {
+ log('Waiting for', userIndicators.length + 1 - checked.size, 'item(s)...');
+ return;
+ }
+
+ log('Closing client...');
+ chart.delete();
+ await client.end();
+ success('Client closed');
+ cb();
+ }
+
+ chart.onUpdate(async () => {
+ success('Market data:', {
+ name: chart.infos.pro_name,
+ description: chart.infos.short_description,
+ exchange: chart.infos.exchange,
+ price: chart.periods[0].close,
+ });
+
+ await check(Symbol('PRICE'));
+ });
+
+ await wait(1000);
+
+ log('Loading indicators...');
+
+ for (const indic of userIndicators) {
+ const privateIndic = await indic.get();
+ log(`[${indic.name}] Loading indicator...`);
+
+ const indicator = new chart.Study(privateIndic);
+
+ indicator.onReady(() => {
+ success(`[${indic.name}] Indicator loaded !`);
+ });
+
+ indicator.onUpdate(async () => {
+ success(`[${indic.name}] Last plot:`, indicator.periods[0]);
+ await check(indic.id);
+ });
+ }
+};