Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Use .env file for examples

Mathieu Colmona6361d40c212
diff --git a/.env.sample b/.env.sample
new file mode 100644
index 0000000..cb26b5c
--- /dev/null
+++ b/.env.sample
@@ -0,0 +1,2 @@
+SESSION=... # Your sessionid cookie
+SIGNATURE=... # Your signature cookie
diff --git a/examples/AllPrivateIndicators.js b/examples/AllPrivateIndicators.js
index c085bec..8a63b44 100644
--- a/examples/AllPrivateIndicators.js
+++ b/examples/AllPrivateIndicators.js
@@ -4,12 +4,13 @@ const TradingView = require('../main');
* This example creates a chart with all user's private indicators
*/
-if (!process.argv[2]) throw Error('Please specify your \'sessionid\' cookie');
-if (!process.argv[3]) throw Error('Please specify your \'signature\' cookie');
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
const client = new TradingView.Client({
- token: process.argv[2],
- signature: process.argv[3],
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
});
const chart = new client.Session.Chart();
@@ -17,8 +18,15 @@ chart.setMarket('BINANCE:BTCEUR', {
timeframe: 'D',
});
-TradingView.getPrivateIndicators(process.argv[2]).then((indicList) => {
- indicList.forEach(async (indic) => {
+(async () => {
+ const indicList = await TradingView.getPrivateIndicators(process.argv[2]);
+
+ if (!indicList.length) {
+ console.error('Your account has no private indicators');
+ process.exit(0);
+ }
+
+ for (const indic of indicList) {
const privateIndic = await indic.get();
console.log('Loading indicator', indic.name, '...');
@@ -32,5 +40,5 @@ TradingView.getPrivateIndicators(process.argv[2]).then((indicList) => {
console.log('Plot values', indicator.periods);
console.log('Strategy report', indicator.strategyReport);
});
- });
-});
+ }
+})();
diff --git a/examples/BuiltInIndicator.js b/examples/BuiltInIndicator.js
index 5dab76e..1dd6804 100644
--- a/examples/BuiltInIndicator.js
+++ b/examples/BuiltInIndicator.js
@@ -6,19 +6,24 @@ const TradingView = require('../main');
const volumeProfile = new TradingView.BuiltInIndicator('VbPFixed@tv-basicstudies-241!');
-const AUTHENTICATED_INDICATORS = [
+const needAuth = ![
'VbPFixed@tv-basicstudies-241',
'VbPFixed@tv-basicstudies-241!',
'Volume@tv-basicstudies-241',
-];
+].includes(volumeProfile.type);
-if (!process.argv[2] && !AUTHENTICATED_INDICATORS.includes(volumeProfile.type)) {
- throw Error('Please specify your \'sessionid\' cookie');
+if (needAuth && (!process.env.SESSION || !process.env.SIGNATURE)) {
+ throw Error('Please set your sessionid and signature cookies');
}
-const client = new TradingView.Client({
- token: process.argv[2],
-});
+const client = new TradingView.Client(
+ needAuth
+ ? {
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+ }
+ : {},
+);
const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
diff --git a/examples/CustomChartType.js b/examples/CustomChartType.js
index 2fb9047..3119bfe 100644
--- a/examples/CustomChartType.js
+++ b/examples/CustomChartType.js
@@ -6,9 +6,12 @@ const TradingView = require('../main');
*/
const client = new TradingView.Client({
- /* Token is only required if you want to use intraday
- timeframes (if you have a paid TradingView account) */
- token: process.argv[2],
+ /*
+ Token and signature are only required if you want to use
+ intraday timeframes (if you have a paid TradingView account)
+ */
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
});
const chart = new client.Session.Chart();
diff --git a/examples/CustomTimeframe.js b/examples/CustomTimeframe.js
index 73c4f75..395da8b 100644
--- a/examples/CustomTimeframe.js
+++ b/examples/CustomTimeframe.js
@@ -4,10 +4,13 @@ const TradingView = require('../main');
* This example tests custom timeframes like 1 second
*/
-if (!process.argv[2]) throw Error('Please specify your \'sessionid\' cookie');
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
const client = new TradingView.Client({
- token: process.argv[2],
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
});
const chart = new client.Session.Chart();
diff --git a/examples/Errors.js b/examples/Errors.js
index 8ec7263..4ecd104 100644
--- a/examples/Errors.js
+++ b/examples/Errors.js
@@ -4,7 +4,15 @@ const TradingView = require('../main');
* This example tests many types of errors
*/
-const client = new TradingView.Client(); // Creates a websocket client
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
+
+// Creates a websocket client
+const client = new TradingView.Client({
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+});
const tests = [
(next) => { /* Testing "Credentials error" */
diff --git a/examples/FakeReplayMode.js b/examples/FakeReplayMode.js
index 441b6d8..3961ffb 100644
--- a/examples/FakeReplayMode.js
+++ b/examples/FakeReplayMode.js
@@ -11,7 +11,7 @@ const client = new Client();
const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
- timeframe: 'D',
+ timeframe: '240',
range: -1, // Range is negative, so 'to' means 'from'
to: Math.round(Date.now() / 1000) - 86400 * 7, // Seven days before now
// to: 1600000000,
@@ -31,10 +31,10 @@ chart.onUpdate(async () => {
console.log('Next ->', times[0]);
- if ((times[0] + 86400) * 1000 > Date.now()) {
+ if (times[0] > ((Date.now() / 1000) - 86400 * 1)) {
await client.end();
console.log('Done !', times.length);
}
- chart.fetchMore(-1);
+ chart.fetchMore(-2);
});
diff --git a/examples/FromToData.js b/examples/FromToData.js
index cacab04..b818aa9 100644
--- a/examples/FromToData.js
+++ b/examples/FromToData.js
@@ -5,13 +5,20 @@ const TradingView = require('../main');
* of candles before or after a timestamp
*/
-const client = new TradingView.Client();
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
+
+const client = new TradingView.Client({
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+});
const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
timeframe: '240',
range: 2, // Can be positive to get before or negative to get after
- to: 1600000000,
+ to: 1700000000,
});
// This works with indicators
diff --git a/examples/GetDrawings.js b/examples/GetDrawings.js
index e848b70..c12c9e1 100644
--- a/examples/GetDrawings.js
+++ b/examples/GetDrawings.js
@@ -5,14 +5,16 @@ const TradingView = require('../main');
*/
// First parameter must be the layoutID
-// (if the layout is private) Second parameter must be the sessionid cookie
-// (if the layout is private) Third parameter must be the userid (you can use getUser function)
+// If the layout is private:
+// - Second parameter must be the userid (you can use getUser function)
+// - You should provide your sessionid and signature cookies in .env file
if (!process.argv[2]) throw Error('Please specify a layoutID');
TradingView.getDrawings(process.argv[2], null, {
- session: process.argv[3],
- id: process.argv[4],
+ session: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+ id: process.argv[3],
}).then((drawings) => {
console.log(`Found ${drawings.length} drawings:`, drawings.map((d) => ({
id: d.id,
diff --git a/examples/GraphicIndicator.js b/examples/GraphicIndicator.js
index d4e926f..95ef865 100644
--- a/examples/GraphicIndicator.js
+++ b/examples/GraphicIndicator.js
@@ -5,7 +5,14 @@ const TradingView = require('../main');
* as 'lines', 'labels', 'boxes', 'tables', 'polygons', etc...
*/
-const client = new TradingView.Client();
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
+
+const client = new TradingView.Client({
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+});
const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
@@ -29,7 +36,7 @@ TradingView.getIndicator('STD;Zig_Zag').then((indic) => {
STD.onUpdate(() => {
console.log('Graphic data:', STD.graphic);
// console.log('Tables:', changes, STD.graphic.tables);
- // console.log('Cells', STD.graphic.tables[0].cells());
+ // console.log('Cells:', STD.graphic.tables[0].cells());
client.end();
});
});
diff --git a/examples/MultipleSyncFetch.js b/examples/MultipleSyncFetch.js
index fde4da7..a1dbf89 100644
--- a/examples/MultipleSyncFetch.js
+++ b/examples/MultipleSyncFetch.js
@@ -4,16 +4,23 @@ const TradingView = require('../main');
* This examples synchronously fetches data from 3 indicators
*/
-const client = new TradingView.Client();
-const chart = new client.Session.Chart();
-chart.setMarket('BINANCE:DOTUSDT');
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
+
+const client = new TradingView.Client({
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+});
function getIndicData(indicator) {
- return new Promise((res) => {
- const STD = new chart.Study(indicator);
+ const chart = new client.Session.Chart();
+ chart.setMarket('BINANCE:DOTUSDT');
+ const STD = new chart.Study(indicator);
- console.log(`Getting "${indicator.description}"...`);
+ console.log(`Getting "${indicator.description}"...`);
+ return new Promise((res) => {
STD.onUpdate(() => {
res(STD.periods);
console.log(`"${indicator.description}" done !`);
diff --git a/examples/PinePermManage.js b/examples/PinePermManage.js
index 2bf40c5..5ec5640 100644
--- a/examples/PinePermManage.js
+++ b/examples/PinePermManage.js
@@ -5,11 +5,21 @@ const { PinePermManager } = require('../main');
* and tests all the available functions
*/
-const sessionid = process.argv[2];
-const signature = process.argv[3];
-const pineid = process.argv[4];
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
-const manager = new PinePermManager(sessionid, signature, pineid);
+const pineid = process.argv[2];
+
+if (!pineid) throw Error('Please specify a pine id as first argument');
+
+console.log('Pine ID:', pineid);
+
+const manager = new PinePermManager(
+ process.env.SESSION,
+ process.env.SIGNATURE,
+ pineid,
+);
(async () => {
console.log('Users:', await manager.getUsers());
diff --git a/examples/ReplayMode.js b/examples/ReplayMode.js
index a3f58ed..236f542 100644
--- a/examples/ReplayMode.js
+++ b/examples/ReplayMode.js
@@ -5,6 +5,18 @@ const TradingView = require('../main');
* indicator data and stores it in a 'periods' variable
*/
+if (!process.env.SESSION || !process.env.SIGNATURE) {
+ throw Error('Please set your sessionid and signature cookies');
+}
+
+console.log('----- Testing ReplayMode: -----');
+
+const client = new TradingView.Client({
+ token: process.env.SESSION,
+ signature: process.env.SIGNATURE,
+});
+const chart = new client.Session.Chart();
+
const config = {
symbol: 'BINANCE:BTCEUR',
timeframe: 'D',
@@ -12,11 +24,6 @@ const config = {
// startFrom: 1600000000,
};
-console.log('----- Testing ReplayMode: -----');
-
-const client = new TradingView.Client();
-const chart = new client.Session.Chart();
-
chart.setMarket(config.symbol, {
timeframe: config.timeframe,
replay: config.startFrom,
diff --git a/examples/Search.js b/examples/Search.js
index 1635177..e40c9bf 100644
--- a/examples/Search.js
+++ b/examples/Search.js
@@ -2,10 +2,10 @@ const TradingView = require('../main');
/**
* This example tests the searching functions such
- * as 'searchMarket' and 'searchIndicator'
+ * as 'searchMarketV3' and 'searchIndicator'
*/
-TradingView.searchMarket('BINANCE:').then((rs) => {
+TradingView.searchMarketV3('BINANCE:').then((rs) => {
console.log('Found Markets:', rs);
});
diff --git a/package.json b/package.json
index 3796672..290a259 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,9 @@
"description": "Tradingview instant stocks API, indicator alerts, trading bot, and more !",
"main": "main.js",
"scripts": {
- "test": "vitest"
+ "test": "vitest",
+ "example": "node --env-file=.env",
+ "example:dev": "nodemon --env-file=.env"
},
"repository": {
"type": "git",