π Get real-time stocks from TradingView
TypeScript backtestingbacktesting-trading-strategiesbitcoinchartscryptocryptocurrencyethereumrealtime-stocksstocksstocks-apistocks-pricestradingtradingviewtradingview-apitradingview-indicatortradingview-replaytradingview-scraper
Fix searchMarket
Mathieu Colmonff56269582b9
diff --git a/src/miscRequests.js b/src/miscRequests.jsindex d358616..cc5984e 100644--- a/src/miscRequests.js+++ b/src/miscRequests.js@@ -8,11 +8,15 @@ const validateStatus = (status) => status < 500;const indicators = ['Recommend.Other', 'Recommend.All', 'Recommend.MA'];const builtInIndicList = [];-async function fetchScanData(tickers = [], type = '', columns = []) {- const { data } = await axios.post(`https://scanner.tradingview.com/${type}/scan`, {- symbols: { tickers },- columns,- }, { validateStatus });+async function fetchScanData(tickers = [], columns = []) {+ const { data } = await axios.post(+ 'https://scanner.tradingview.com/global/scan',+ {+ symbols: { tickers },+ columns,+ },+ { validateStatus },+ );return data;}@@ -40,56 +44,21 @@ async function fetchScanData(tickers = [], type = '', columns = []) {* }} Periods*/-// /**-// * @typedef {string | 'forex' | 'crypto'-// * | 'america' | 'australia' | 'canada' | 'egypt'-// * | 'germany' | 'india' | 'israel' | 'italy'-// * | 'luxembourg' | 'poland' | 'sweden' | 'turkey'-// * | 'uk' | 'vietnam'} Screener-// * You can use `getScreener(exchange)` function for non-forex and non-crypto markets.-// */-module.exports = {- // /**- // * Get a screener from an exchange- // * @function getScreener- // * @param {string} exchange Example: BINANCE, EURONEXT, NASDAQ- // * @returns {Screener}- // */- // getScreener(exchange) {- // const e = exchange.toUpperCase();- // if (['NASDAQ', 'NYSE', 'NYSE ARCA', 'OTC'].includes(e)) return 'america';- // if (['ASX'].includes(e)) return 'australia';- // if (['TSX', 'TSXV', 'CSE', 'NEO'].includes(e)) return 'canada';- // if (['EGX'].includes(e)) return 'egypt';- // if (['FWB', 'SWB', 'XETR'].includes(e)) return 'germany';- // if (['BSE', 'NSE'].includes(e)) return 'india';- // if (['TASE'].includes(e)) return 'israel';- // if (['MIL', 'MILSEDEX'].includes(e)) return 'italy';- // if (['LUXSE'].includes(e)) return 'luxembourg';- // if (['NEWCONNECT'].includes(e)) return 'poland';- // if (['NGM'].includes(e)) return 'sweden';- // if (['BIST'].includes(e)) return 'turkey';- // if (['LSE', 'LSIN'].includes(e)) return 'uk';- // if (['HNX'].includes(e)) return 'vietnam';- // return 'global';- // },-/*** Get technical analysis* @function getTA- * @param {Screener} screener Market screener* @param {string} id Full market id (Example: COINBASE:BTCEUR)* @returns {Promise<Periods>} results*/- async getTA(screener, id) {+ async getTA(id) {const advice = {};const cols = ['1', '5', '15', '60', '240', '1D', '1W', '1M'].map((t) => indicators.map((i) => (t !== '1D' ? `${i}|${t}` : i))).flat();- const rs = await fetchScanData([id], screener, cols);+ const rs = await fetchScanData([id], cols);if (!rs.data || !rs.data[0]) return false;rs.data[0].d.forEach((val, i) => {@@ -107,7 +76,6 @@ module.exports = {* @prop {string} id Market full symbol* @prop {string} exchange Market exchange name* @prop {string} fullExchange Market exchange full name- * @prop {Screener | 'forex' | 'crypto'} screener Market screener* @prop {string} symbol Market symbol* @prop {string} description Market name* @prop {string} type Market type@@ -115,7 +83,7 @@ module.exports = {*//**- * Find a symbol+ * Find a symbol (deprecated)* @function searchMarket* @param {string} search Keywords* @param {'stock'@@ -123,15 +91,20 @@ module.exports = {* | 'crypto' | 'index' | 'economic'* } [filter] Caterogy filter* @returns {Promise<SearchMarketResult[]>} Search results+ * @deprecated Use searchMarketV3 instead*/async searchMarket(search, filter = '') {const { data } = await axios.get(- `https://symbol-search.tradingview.com/symbol_search/?text=${search.replace(/ /g, '%20')}&type=${filter}`,+ 'https://symbol-search.tradingview.com/symbol_search',{- validateStatus,+ params: {+ text: search.replace(/ /g, '%20'),+ type: filter,+ },headers: {origin: 'https://www.tradingview.com',},+ validateStatus,},);@@ -139,21 +112,63 @@ module.exports = {const exchange = s.exchange.split(' ')[0];const id = `${exchange}:${s.symbol}`;- // const screener = (['forex', 'crypto'].includes(s.type)- // ? s.type- // : this.getScreener(exchange)- // );- const screener = 'global';+ return {+ id,+ exchange,+ fullExchange: s.exchange,+ symbol: s.symbol,+ description: s.description,+ type: s.type,+ getTA: () => this.getTA(id),+ };+ });+ },++ /**+ * Find a symbol+ * @function searchMarketV3+ * @param {string} search Keywords+ * @param {'stock'+ * | 'futures' | 'forex' | 'cfd'+ * | 'crypto' | 'index' | 'economic'+ * } [filter] Caterogy filter+ * @returns {Promise<SearchMarketResult[]>} Search results+ */+ async searchMarketV3(search, filter = '') {+ const splittedSearch = search.toUpperCase().replace(/ /g, '+').split(':');++ const request = await axios.get(+ 'https://symbol-search.tradingview.com/symbol_search/v3',+ {+ params: {+ exchange: (splittedSearch.length === 2+ ? splittedSearch[0]+ : undefined+ ),+ text: splittedSearch.pop(),+ search_type: filter,+ },+ headers: {+ origin: 'https://www.tradingview.com',+ },+ validateStatus,+ },+ );++ const { data } = request;++ return data.symbols.map((s) => {+ const exchange = s.exchange.split(' ')[0];+ const id = `${exchange.toUpperCase()}:${s.symbol}`;return {id,exchange,fullExchange: s.exchange,- screener,symbol: s.symbol,description: s.description,type: s.type,- getTA: () => this.getTA(screener, id),+ getTA: () => this.getTA(id),};});},@@ -182,16 +197,26 @@ module.exports = {if (!builtInIndicList.length) {await Promise.all(['standard', 'candlestick', 'fundamental'].map(async (type) => {const { data } = await axios.get(- `https://pine-facade.tradingview.com/pine-facade/list/?filter=${type}`,- { validateStatus },+ 'https://pine-facade.tradingview.com/pine-facade/list',+ {+ params: {+ filter: type,+ },+ validateStatus,+ },);builtInIndicList.push(...data);}));}const { data } = await axios.get(- `https://www.tradingview.com/pubscripts-suggest-json/?search=${search.replace(/ /g, '%20')}`,- { validateStatus },+ 'https://www.tradingview.com/pubscripts-suggest-json',+ {+ params: {+ search: search.replace(/ /g, '%20'),+ },+ validateStatus,+ },);function norm(str = '') {@@ -253,10 +278,10 @@ module.exports = {const { data } = await axios.get(`https://pine-facade.tradingview.com/pine-facade/translate/${indicID}/${version}`,{- validateStatus,headers: {cookie: `${session ? `sessionid=${session};` : ''}${signature ? `sessionid_sign=${signature};` : ''}`,},+ validateStatus,},);@@ -356,12 +381,12 @@ module.exports = {'https://www.tradingview.com/accounts/signin/',`username=${username}&password=${password}${remember ? '&remember=on' : ''}`,{- validateStatus,headers: {referer: 'https://www.tradingview.com','Content-Type': 'application/x-www-form-urlencoded','User-agent': `${UA} (${os.version()}; ${os.platform()}; ${os.arch()})`,},+ validateStatus,},);@@ -403,10 +428,10 @@ module.exports = {*/async getUser(session, signature = '', location = 'https://www.tradingview.com/') {const { data } = await axios.get(location, {- validateStatus,headers: {cookie: `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`,},+ validateStatus,});if (data.includes('auth_token')) {@@ -442,12 +467,18 @@ module.exports = {* @returns {Promise<SearchIndicatorResult[]>} Search results*/async getPrivateIndicators(session, signature = '') {- const { data } = await axios.get('https://pine-facade.tradingview.com/pine-facade/list?filter=saved', {- validateStatus,- headers: {- cookie: `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`,+ const { data } = await axios.get(+ 'https://pine-facade.tradingview.com/pine-facade/list',+ {+ headers: {+ cookie: `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`,+ },+ params: {+ filter: 'saved',+ },+ validateStatus,},- });+ );return data.map((ind) => ({id: ind.scriptIdPart,@@ -495,14 +526,18 @@ module.exports = {);const { data } = await axios.get(- `https://www.tradingview.com/chart-token/?image_url=${layout}&user_id=${id}`,+ 'https://www.tradingview.com/chart-token',{- validateStatus,headers: {cookie: session? `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`: '',},+ params: {+ image_url: layout,+ user_id: id,+ },+ validateStatus,},);@@ -548,14 +583,15 @@ module.exports = {const { data } = await axios.get(`https://charts-storage.tradingview.com/charts-storage/get/layout/${layout- }/sources?chart_id=${- chartID- }&jwt=${- chartToken- }${- (symbol ? `&symbol=${symbol}` : '')- }`,- { validateStatus },+ }/sources`,+ {+ params: {+ chart_id: chartID,+ jwt: chartToken,+ symbol,+ },+ validateStatus,+ },);if (!data.payload) throw new Error('Wrong layout, user credentials, or chart id.');diff --git a/tests/search.test.ts b/tests/search.test.tsindex ffbfb28..8810291 100644--- a/tests/search.test.ts+++ b/tests/search.test.ts@@ -1,8 +1,8 @@import { describe, it, expect } from 'vitest';-import { searchMarket, searchIndicator } from '../main';+import { searchMarket, searchIndicator, searchMarketV3 } from '../main';describe('Search functions', () => {- it('market search: "BINANCE:" has results', async () => {+ it('market search (old): "BINANCE:" has results', async () => {console.log('Searching market: "BINANCE:"');const markets = await searchMarket('BINANCE:');console.log('Found', markets.length, 'markets');@@ -10,6 +10,14 @@ describe('Search functions', () => {expect(markets.length).toBeGreaterThan(10);});+ it('market search: "BINANCE:" has results', async () => {+ console.log('Searching market: "BINANCE:"');+ const markets = await searchMarketV3('BINANCE:');+ console.log('Found', markets.length, 'markets');++ expect(markets.length).toBeGreaterThan(10);+ });+it('indicator search: "RSI" has results', async () => {console.log('Searching indicator: "RSI"');const indicators = await searchIndicator('RSI');@@ -20,21 +28,31 @@ describe('Search functions', () => {});describe('Technical Analysis', () => {- function checkTA(ta = {}) {- expect(ta).toBeDefined();- for (const period of ['1', '5', '15', '60', '240', '1D', '1W', '1M']) {- expect(ta[period]).toBeDefined();- expect(ta[period].Other).toBeDefined();- expect(ta[period].All).toBeDefined();- expect(ta[period].MA).toBeDefined();- }- }+ const SEARCHES = {+ // search text: expected first result+ 'binance:BTCUSD': 'BINANCE:BTCUSD',+ 'nasdaq apple': 'NASDAQ:AAPL',+ };- it('gets TA for BINANCE:BTCUSD', async () => {- checkTA(await (await searchMarket('BINANCE:BTCUSD'))[0].getTA());- });+ for (const marketName in SEARCHES) {+ it(`gets TA for '${marketName}'`, async () => {+ const foundMarkets = await searchMarketV3(marketName);+ const firstResult = foundMarkets[0];- it('gets TA for "NASDAQ APPLE"', async () => {- checkTA(await (await searchMarket('NASDAQ APPLE'))[0].getTA());- });+ console.log(`Market search first result for '${marketName}':`, firstResult);++ expect(firstResult).toBeDefined();+ expect(firstResult.id).toBe(SEARCHES[marketName]);++ const ta = await firstResult.getTA();+ expect(ta).toBeDefined();++ for (const period of ['1', '5', '15', '60', '240', '1D', '1W', '1M']) {+ expect(ta[period]).toBeDefined();+ expect(ta[period].Other).toBeDefined();+ expect(ta[period].All).toBeDefined();+ expect(ta[period].MA).toBeDefined();+ }+ });+ }});