Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SESSION=... # Your sessionid cookie
SIGNATURE=... # Your signature cookie
24 changes: 16 additions & 8 deletions examples/AllPrivateIndicators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,29 @@ 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();
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, '...');

Expand All @@ -32,5 +40,5 @@ TradingView.getPrivateIndicators(process.argv[2]).then((indicList) => {
console.log('Plot values', indicator.periods);
console.log('Strategy report', indicator.strategyReport);
});
});
});
}
})();
19 changes: 12 additions & 7 deletions examples/BuiltInIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
9 changes: 6 additions & 3 deletions examples/CustomChartType.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions examples/CustomTimeframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion examples/Errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" */
Expand Down
6 changes: 3 additions & 3 deletions examples/FakeReplayMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
});
11 changes: 9 additions & 2 deletions examples/FromToData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions examples/GetDrawings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 9 additions & 2 deletions examples/GraphicIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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();
});
});
19 changes: 13 additions & 6 deletions examples/MultipleSyncFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 !`);
Expand Down
18 changes: 14 additions & 4 deletions examples/PinePermManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
17 changes: 12 additions & 5 deletions examples/ReplayMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ 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',
startFrom: Math.round(Date.now() / 1000) - 86400 * 7, // Seven days before now
// 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,
Expand Down
4 changes: 2 additions & 2 deletions examples/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions src/chart/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ module.exports = (client) => class ChartSession {
if (options.currency) symbolInit['currency-id'] = options.currency;

if (options.replay) {
this.#replayMode = true;
this.#client.send('replay_create_session', [this.#replaySessionID]);
if (!this.#replayMode) {
this.#replayMode = true;
this.#client.send('replay_create_session', [this.#replaySessionID]);
}

this.#client.send('replay_add_series', [
this.#replaySessionID,
Expand Down
8 changes: 4 additions & 4 deletions src/classes/PinePermManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PinePermManager {
* | 'expiration,user__username'
* | '-expiration,user__username'
* } order Fetching order
* @returns {AuthorizationUser[]}
* @returns {Promise<AuthorizationUser[]>}
*/
async getUsers(limit = 10, order = '-created') {
try {
Expand All @@ -66,7 +66,7 @@ class PinePermManager {
* Adds an user to the authorized list
* @param {string} username User's username
* @param {Date} [expiration] Expiration date
* @returns {'ok' | 'exists' | null}
* @returns {Promise<'ok' | 'exists' | null>}
*/
async addUser(username, expiration = null) {
try {
Expand Down Expand Up @@ -100,7 +100,7 @@ class PinePermManager {
* Modify an authorization expiration date
* @param {string} username User's username
* @param {Date} [expiration] New expiration date
* @returns {'ok' | null}
* @returns {Promise<'ok' | null>}
*/
async modifyExpiration(username, expiration = null) {
try {
Expand Down Expand Up @@ -133,7 +133,7 @@ class PinePermManager {
/**
* Removes an user to the authorized list
* @param {string} username User's username
* @returns {'ok' | null}
* @returns {Promise<'ok' | null>}
*/
async removeUser(username) {
try {
Expand Down
Loading
Loading