π Get real-time stocks from TradingView
TypeScript backtestingbacktesting-trading-strategiesbitcoinchartscryptocryptocurrencyethereumrealtime-stocksstocksstocks-apistocks-pricestradingtradingviewtradingview-apitradingview-indicatortradingview-replaytradingview-scraper
[2.0.6] JSDoc + add functions
Mathieu Colmon0641142c9c35
diff --git a/README.md b/README.mdindex f6f819f..8742903 100644--- a/README.md+++ b/README.md@@ -56,9 +56,9 @@ const marketAPI = require('tradingview');const marketAPI = require('tradingview');-const market = marketAPI();+(async () => {+ const market = marketAPI(false);-market.on('logged', async () => {const searchBTC = (await market.search('bitcoin euro', 'crypto'))[0];console.log('Found Bitcoin / Euro:', searchBTC);@@ -66,7 +66,7 @@ market.on('logged', async () => {console.log('Full technical analysis for Bitcoin:', TA);// You can also use this way: await market.getTA('crypto', 'BINANCE:BTCEUR');-});+})();``````javascriptdiff --git a/main.js b/main.jsindex 5ac47b4..ac7f306 100644--- a/main.js+++ b/main.js@@ -1,6 +1,7 @@const WebSocket = require('ws');const {- search, getScreener, getTA, getIndicator, getUserToken,+ search, getScreener, getTA, getIndicator,+ getUser, getChartToken, getDrawings,} = require('./miscRequests');let onPacket = () => null;@@ -187,6 +188,9 @@ module.exports = (autoInit = true) => {*/end() { ws.close(); },+ getUser,+ getChartToken,+ getDrawings,search,getScreener,getTA,@@ -297,7 +301,7 @@ module.exports = (autoInit = true) => {}};- if (chart.session) send('set_auth_token', [await getUserToken(chart.session)]);+ if (chart.session) send('set_auth_token', [(await getUser(chart.session)).authToken]);send('chart_create_session', [chartSession, '']);if (chart.timezone) send('switch_timezone', [chartSession, chart.timezone]);send('resolve_symbol', [chartSession, 'sds_sym_1', `={"symbol":"${chart.symbol || 'BTCEUR'}","adjustment":"splits"}`]);diff --git a/miscRequests.js b/miscRequests.jsindex 037f48f..d9b723e 100644--- a/miscRequests.js+++ b/miscRequests.js@@ -80,7 +80,8 @@ module.exports = {return exchange.toLowerCase();},- /** Get technical analysis+ /**+ * Get technical analysis* @param {screener} screener* @param {string} id Full market id (Example: COINBASE:BTCEUR)* @returns {Promise<Periods>} results@@ -118,8 +119,9 @@ module.exports = {*//**- * @param {string} search Search a symbol- * @param {'stock' | 'futures' | 'forex' | 'cfd' | 'crypto' | 'index' | 'economic'} filter+ * Find a symbol+ * @param {string} search Keywords+ * @param {'stock' | 'futures' | 'forex' | 'cfd' | 'crypto' | 'index' | 'economic'} [filter]* @returns {Promise<SearchResult[]>} Search results*/async search(search, filter = '') {@@ -256,13 +258,32 @@ module.exports = {});},+ /**+ * @typedef {Object} User Instance of User+ * @property {number} id User ID+ * @property {string} username User username+ * @property {string} firstName User first name+ * @property {string} lastName User last name+ * @property {number} reputation User reputation+ * @property {number} following Number of following accounts+ * @property {number} followers Number of followers+ * @property {Object} notifications User's notifications+ * @property {number} notifications.user User notifications+ * @property {number} notifications.following Notification from following accounts+ * @property {string} session User session+ * @property {string} sessionHash User session hash+ * @property {string} privateChannel User private channel+ * @property {string} authToken User auth token+ * @property {Date} joinDate Account creation date+ */+/*** Get a token for an user from a 'sessionid' cookie* @param {string} session User 'sessionid' cookie* @param {string} [location] Auth page location (For france: https://fr.tradingview.com/)- * @returns {Promise<string>} Token+ * @returns {Promise<User>} Token*/- async getUserToken(session, location = 'https://www.tradingview.com/') {+ async getUser(session, location = 'https://www.tradingview.com/') {return new Promise((cb, err) => {https.get(location, {headers: { cookie: `sessionid=${session}` },@@ -271,11 +292,125 @@ module.exports = {res.on('data', (d) => { rs += d; });res.on('end', async () => {if (res.headers.location && location !== res.headers.location) {- cb(await module.exports.getUserToken(session, res.headers.location));+ cb(await module.exports.getUser(session, res.headers.location));return;}- if (rs.includes('auth_token')) cb(/"auth_token":"(.*?)"/g.exec(rs)[1]);- else err(new Error('Wrong or expired sessionid'));+ if (rs.includes('auth_token')) {+ cb({+ id: /"id":([0-9]{1,10}),/.exec(rs)[1],+ username: /"username":"(.*?)"/.exec(rs)[1],+ firstName: /"first_name":"(.*?)"/.exec(rs)[1],+ lastName: /"last_name":"(.*?)"/.exec(rs)[1],+ reputation: parseFloat(/"reputation":(.*?),/.exec(rs)[1] || 0),+ following: parseFloat(/,"following":([0-9]*?),/.exec(rs)[1] || 0),+ followers: parseFloat(/,"followers":([0-9]*?),/.exec(rs)[1] || 0),+ notifications: {+ following: parseFloat(/"notification_count":\{"following":([0-9]*),/.exec(rs)[1] || 0),+ user: parseFloat(/"notification_count":\{"following":[0-9]*,"user":([0-9]*)/.exec(rs)[1] || 0),+ },+ session,+ sessionHash: /"session_hash":"(.*?)"/.exec(rs)[1],+ privateChannel: /"private_channel":"(.*?)"/.exec(rs)[1],+ authToken: /"auth_token":"(.*?)"/.exec(rs)[1],+ joinDate: new Date(/"date_joined":"(.*?)"/.exec(rs)[1] || 0),+ });+ } else err(new Error('Wrong or expired sessionid'));+ });++ res.on('error', err);+ }).end();+ });+ },++ /**+ * User credentials+ * @typedef {Object} UserCredentials+ * @property {number} id User ID+ * @property {string} session User session ('sessionid' cookie)+ */++ /**+ * Get a chart token from a layout ID and the user credentials if the layout is not public+ * @param {string} layout The layout ID found in the layout URL (Like: 'XXXXXXXX')+ * @param {UserCredentials} [credentials] User credentials (id + session)+ * @returns {Promise<string>} Token+ */+ async getChartToken(layout, credentials = {}) {+ return new Promise((cb, err) => {+ const creds = credentials.id && credentials.session;+ const userID = creds ? credentials.id : -1;+ const session = creds ? credentials.session : null;++ https.get(`https://www.tradingview.com/chart-token/?image_url=${layout}&user_id=${userID}`, {+ headers: { cookie: session ? `sessionid=${session}` : '' },+ }, (res) => {+ let rs = '';+ res.on('data', (d) => { rs += d; });+ res.on('end', async () => {+ rs = JSON.parse(rs);++ if (rs.token) cb(rs.token);+ else err(new Error('Wrong layout or credentials'));+ });++ res.on('error', err);+ }).end();+ });+ },++ /**+ * @typedef {Object} DrawingPoint+ * @property {number} time_t Point X time position+ * @property {number} price Point Y price position+ * @property {number} offset Point offset+ */++ /**+ * @typedef {Object} Drawing+ * @property {string} id Drawing ID (Like: 'XXXXXX')+ * @property {string} symbol Layout market symbol (Like: 'BINANCE:BUCEUR')+ * @property {string} ownerSource Owner user ID (Like: 'XXXXXX')+ * @property {string} serverUpdateTime Drawing last update timestamp+ * @property {string} currencyId Currency ID (Like: 'EUR')+ * @property {any} unitId Unit ID+ * @property {string} type Drawing type+ * @property {DrawingPoint[]} points List of drawing points+ * @property {number} zorder Drawing Z order+ * @property {string} linkKey Drawing link key+ * @property {Object} state Drawing state+ */++ /**+ * Get a chart token from a layout ID and the user credentials if the layout is not public+ * @param {string} layout The layout ID found in the layout URL (Like: 'XXXXXXXX')+ * @param {string | ''} [symbol] Market filter (Like: 'BINANCE:BTCEUR')+ * @param {UserCredentials} [credentials] User credentials (id + session)+ * @param {number} [chartID = 1] Chart ID+ * @returns {Promise<Drawing[]>} Drawings+ */+ async getDrawings(layout, symbol = '', credentials = {}, chartID = 1) {+ const chartToken = await module.exports.getChartToken(layout, credentials);++ return new Promise((cb, err) => {+ const creds = credentials.id && credentials.session;+ const session = creds ? credentials.session : null;++ const url = `https://charts-storage.tradingview.com/charts-storage/layout/${+ layout}/sources?chart_id=${chartID}&jwt=${chartToken}${symbol ? `&symbol=${symbol}` : ''}`;++ https.get(url, {+ headers: { cookie: session ? `sessionid=${session}` : '' },+ }, (res) => {+ let rs = '';+ res.on('data', (d) => { rs += d; });+ res.on('end', async () => {+ rs = JSON.parse(rs);++ if (rs.payload) {+ cb(Object.values(rs.payload.sources || {}).map((drawing) => ({+ ...drawing, ...drawing.state,+ })));+ } else err(new Error('Wrong layout, user credentials, or chart id.'));});res.on('error', err);diff --git a/package.json b/package.jsonindex 55b0992..daf0853 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "@mathieuc/tradingview",- "version": "2.0.5",+ "version": "2.0.6","description": "Tradingview instant stocks API, indicator alerts, trading bot, and more !","main": "main.js","scripts": {diff --git a/tests/alerts/drawings.js b/tests/alerts/drawings.jsnew file mode 100644index 0000000..7912764--- /dev/null+++ b/tests/alerts/drawings.js@@ -0,0 +1,41 @@+require('./envLoader');+const marketAPI = require('../../main');++if (!process.env.LAYOUT_ID) console.error('You need to set a layout id');++const market = marketAPI(false);++console.log('Connecting...');++(async () => {+ /*+ * Important ! If you want to get a layout without login, you have to+ * set it in public mode. If you get an error, please wait few minutes.+ * Try getting to https://www.tradingview.com/chart/LAYOUT_ID/ in incognito mode.+ */+ const user = (process.env.SESSION_ID) ? await market.getUser(process.env.SESSION_ID) : {};+ console.log('Connected as', user.firstName || user.username || 'anonymous', '!');++ console.log(`Gettings drawings of '${process.env.LAYOUT_ID}' layout...`);++ const drawings = await market.getDrawings(process.env.LAYOUT_ID, '', {+ id: user.id,+ session: user.session,+ });++ const lines = drawings.filter((d) => (+ ['LineToolHorzLine', 'LineToolRay', 'LineToolTrendLine', 'LineToolHorzRay'].includes(d.type)+ && d.state.linestyle === 0+ && d.state.linewidth === 4+ ));++ console.log(+ `Found ${drawings.length} result(s) including ${lines.length} thick line(s):`,+ lines.map((l) => ({+ market: l.symbol,+ type: l.type,+ points: l.points,+ text: l.state.text,+ })),+ );+})();diff --git a/tests/analysis.js b/tests/analysis.jsindex 79a41fd..2acc4fc 100644--- a/tests/analysis.js+++ b/tests/analysis.js@@ -1,8 +1,8 @@const marketAPI = require('../main');-const market = marketAPI();+(async () => {+ const market = marketAPI(false);-market.on('logged', async () => {const searchBTC = (await market.search('bitcoin euro', 'crypto'))[0];console.log('Found Bitcoin / Euro:', searchBTC);@@ -10,4 +10,4 @@ market.on('logged', async () => {console.log('Full technical analysis for Bitcoin:', TA);// You can also use this way: await market.getTA('crypto', 'BINANCE:BTCEUR');-});+})();