π Get real-time stocks from TradingView
TypeScript backtestingbacktesting-trading-strategiesbitcoinchartscryptocryptocurrencyethereumrealtime-stocksstocksstocks-apistocks-pricestradingtradingviewtradingview-apitradingview-indicatortradingview-replaytradingview-scraper
Add strategies
Mathieu Colmoneafbe34bb5a9
diff --git a/main.js b/main.jsindex 405390a..becae2e 100644--- a/main.js+++ b/main.js@@ -1,7 +1,9 @@const WebSocket = require('ws');+const JSZip = require('jszip');+const {search, getScreener, getTA, getIndicator,- getUser, getChartToken, getDrawings,+ getUser, getChartToken, getDrawings, searchIndicator,} = require('./miscRequests');let onPacket = () => null;@@ -197,13 +199,14 @@ module.exports = (autoInit = true) => {});},- getUser,- getChartToken,- getDrawings,search,getScreener,getTA,subscribed,+ searchIndicator,+ getUser,+ getChartToken,+ getDrawings,/*** Unsubscribe to a market@@ -228,8 +231,10 @@ module.exports = (autoInit = true) => {/*** @typedef {Object} IndicatorInfos Indicator infos* @property {string} id ID of the indicator (Like: XXX;XXXXXXXXXXXXXXXXXXXXX)+ * @property {string} [name] Name of the indicator* @property {string} version Wanted version of the indicator* @property {(string | number | boolean | null)[]} [settings] Indicator settings value+ * @property {'study' | 'strategy'} [type] Script type** @typedef {Object} ChartInfos* @property {string} [session] User 'sessionid' cookie@@ -255,19 +260,166 @@ module.exports = (autoInit = true) => {/*** Init a chart instance* @param {ChartInfos} chart- * @param {{(prices: Period[]): null}} onUpdate List of periods starting from the lastest+ * @param {{(prices: Period[], strategies: Object<string, StrategyReport>): null}} onUpdate*/async initChart(chart, onUpdate) {const chartSession = genSession();const periods = [];++ /**+ * @typedef {Object} RelAbsValue Relative and Absolute values+ * @property {number} v Absolute value+ * @property {number} p Relative value+ */++ /**+ * @typedef {Object} TradeReport Trade report++ * @property {Object} entry Trade entry+ * @property {string} entry.name Trade name+ * @property {'long' | 'short'} entry.type Entry type (long/short)+ * @property {number} entry.value Entry price value+ * @property {number} entry.time Entry timestamp++ * @property {Object} exit Trade exit+ * @property {'' | string} exit.name Trade name ('' if false exit)+ * @property {number} exit.value Exit price value+ * @property {number} exit.time Exit timestamp++ * @property {number} quantity Trade quantity+ * @property {RelAbsValue} profit Trade profit+ * @property {RelAbsValue} cumulative Trade cummulative profit+ * @property {RelAbsValue} runup Trade run-up+ * @property {RelAbsValue} drawdown Trade drawdown+ */++ /**+ * @typedef {Object} PerfReport+ * @property {number} avgBarsInTrade Average bars in trade+ * @property {number} avgBarsInWinTrade Average bars in winning trade+ * @property {number} avgBarsInLossTrade Average bars in losing trade+ * @property {number} avgTrade Average trade gain+ * @property {number} avgTradePercent Average trade performace+ * @property {number} avgLosTrade Average losing trade gain+ * @property {number} avgLosTradePercent Average losing trade performace+ * @property {number} avgWinTrade Average winning trade gain+ * @property {number} avgWinTradePercent Average winning trade performace+ * @property {number} commissionPaid Commission paid+ * @property {number} grossLoss Gross loss value+ * @property {number} grossLossPercent Gross loss percent+ * @property {number} grossProfit Gross profit+ * @property {number} grossProfitPercent Gross profit percent+ * @property {number} largestLosTrade Largest losing trade gain+ * @property {number} largestLosTradePercent Largent losing trade performance+ * @property {number} largestWinTrade Largest winning trade gain+ * @property {number} largestWinTradePercent Largest winning trade performance+ * @property {number} marginCalls Margin calls+ * @property {number} maxContractsHeld Max Contracts Held+ * @property {number} netProfit Net profit+ * @property {number} netProfitPercent Net performance+ * @property {number} numberOfLosingTrades Number of losing trades+ * @property {number} numberOfWiningTrades Number of winning trades+ * @property {number} percentProfitable Strategy winrate+ * @property {number} profitFactor Profit factor+ * @property {number} ratioAvgWinAvgLoss Ratio Average Win / Average Loss+ * @property {number} totalOpenTrades Total open trades+ * @property {number} totalTrades Total trades+ */++ /**+ * @typedef {Object} StrategyReport+ * @property {'EUR' | 'USD' | 'JPY' | '' | 'CHF'} currency Selected currency+ * @property {TradeReport[]} trades Trade list+ * @property {Object} history History Chart value+ * @property {number[]} history.buyHold Buy hold values+ * @property {number[]} history.buyHoldPercent Buy hold percent values+ * @property {number[]} history.drawDown Drawdown values+ * @property {number[]} history.drawDownPercent Drawdown percent values+ * @property {number[]} history.equity Equity values+ * @property {number[]} history.equityPercent Equity percent values+ * @property {Object} performance Strategy performance+ * @property {PerfReport} performance.all Strategy long/short performances+ * @property {PerfReport} performance.long Strategy long performances+ * @property {PerfReport} performance.short Strategy short performances+ * @property {number} performance.buyHoldReturn Strategy Buy & Hold Return+ * @property {number} performance.buyHoldReturnPercent Strategy Buy & Hold Return percent+ * @property {number} performance.maxDrawDown Strategy max drawdown+ * @property {number} performance.maxDrawDownPercent Strategy max drawdown percent+ * @property {number} performance.openPL Strategy Open P&L (Profit And Loss)+ * @property {number} performance.openPLPercent Strategy Open P&L (Profit And Loss) percent+ * @property {number} performance.sharpeRatio Strategy Sharpe Ratio+ * @property {number} performance.sortinoRatio Strategy Sortino Ratio+ */++ /** @type {Object<string, StrategyReport>} Strategies */+ const strategies = {};+const indicators = await Promise.all(- (chart.indicators || []).map((i) => getIndicator(i.id, i.version, i.settings)),+ (chart.indicators || []).map((i) => getIndicator(i.id, i.version, i.settings, i.type)),);- function updatePeriods(packet) {+ async function updatePeriods(packet) {const newData = packet.data;- Object.keys(newData).forEach((type) => {+ await Promise.all(Object.keys(newData).map(async (type) => {+ const std = chart.indicators[parseInt(type, 10)] || {};++ if (newData[type].ns && newData[type].ns.d) {+ const stratData = JSON.parse(newData[type].ns.d);++ if (stratData.dataCompressed) {+ const zip = new JSZip();+ const data = JSON.parse(+ await (+ await zip.loadAsync(stratData.dataCompressed, { base64: true })+ ).file('').async('text'),+ );++ strategies[std.name || type] = {+ currency: data.report.currency,++ trades: data.report.trades.map((t) => ({+ entry: {+ name: t.e.c,+ type: (t.e.tp[0] === 's' ? 'short' : 'long'),+ value: t.e.p,+ time: t.e.tm,+ },+ exit: {+ name: t.x.c,+ value: t.x.p,+ time: t.x.tm,+ },+ quantity: t.q,+ profit: t.tp,+ cumulative: t.cp,+ runup: t.rn,+ drawdown: t.dd,+ })),++ history: {+ buyHold: data.report.buyHold,+ buyHoldPercent: data.report.buyHoldPercent,+ drawDown: data.report.drawDown,+ drawDownPercent: data.report.drawDownPercent,+ equity: data.report.equity,+ equityPercent: data.report.equityPercent,+ },++ performance: data.report.performance,+ };+ return;+ }++ if (stratData.data && stratData.data.report && stratData.data.report.performance) {+ if (!strategies[std.name || type]) strategies[std.name || type] = { performance: {} };+ strategies[std.name || type].performance = stratData.data.report.performance;+ return;+ }++ return;+ }+(newData[type].s || newData[type].st || []).forEach((p) => {if (!periods[p.i]) periods[p.i] = {};@@ -295,20 +447,20 @@ module.exports = (autoInit = true) => {periods[p.i][chart.indicators[parseInt(type, 10)].name || `st${type}`] = period;}});- });+ }));}- chartCBs[chartSession] = (packet) => {+ chartCBs[chartSession] = async (packet) => {if (isEnded) return;if (['timescale_update', 'du'].includes(packet.type)) {- updatePeriods(packet);- onUpdate([...periods].reverse());+ await updatePeriods(packet);+ onUpdate([...periods].reverse(), strategies);return;}if (packet.type.endsWith('_error')) {- handleError(`Error on '${chart.symbol}' (${chartSession}) chart: "${packet.type}"`);+ handleError(`Error on '${chart.symbol}' (${chartSession}) chart: "${packet.type}":`, packet);}};@@ -342,7 +494,7 @@ module.exports = (autoInit = true) => {};});- send('create_study', [chartSession, `${i}`, 'st1', '$prices', 'Script@tv-scripting-101!', pineInfos]);+ send('create_study', [chartSession, `${i}`, 'st1', '$prices', indicator.typeID, pineInfos]);});},diff --git a/miscRequests.js b/miscRequests.jsindex d9b723e..fa7c908 100644--- a/miscRequests.js+++ b/miscRequests.js@@ -1,37 +1,62 @@const https = require('https');const indicators = ['Recommend.Other', 'Recommend.All', 'Recommend.MA'];+const indicList = [];-function fetchScanData(tickers = [], type = '', columns = []) {+/**+ * @param {https.RequestOptions} options HTTPS Request options+ * @param {boolean} [raw] Get raw or JSON data+ * @param {string} [content] Request body content+ * @returns {Promise<string | object | array>} Result+ */+function request(options = {}, raw = false, content = '') {return new Promise((cb, err) => {- const req = https.request({- method: 'POST',- hostname: 'scanner.tradingview.com',- path: `/${type}/scan`,- headers: {- 'Content-Type': 'application/json',- },- }, (res) => {+ const req = https.request(options, (res) => {let data = '';res.on('data', (c) => { data += c; });res.on('end', () => {- if (!data.startsWith('{')) {- err(new Error('Wrong screener or symbol'));+ if (raw) {+ cb(data);return;}+try {- cb(JSON.parse(data));+ data = JSON.parse(data);} catch (error) {err(new Error('Can\'t parse server response'));+ return;}++ cb(data);});});req.on('error', err);- req.end(JSON.stringify({ symbols: { tickers }, columns }));+ req.end(content);});}+async function fetchScanData(tickers = [], type = '', columns = []) {+ let data = await request({+ method: 'POST',+ hostname: 'scanner.tradingview.com',+ path: `/${type}/scan`,+ headers: {+ 'Content-Type': 'application/json',+ },+ }, true, JSON.stringify({ symbols: { tickers }, columns }));++ if (!data.startsWith('{')) throw new Error('Wrong screener or symbol');++ try {+ data = JSON.parse(data);+ } catch (e) {+ throw new Error('Can\'t parse server response');+ }++ return data;+}+/*** @typedef {number} advice*@@ -125,47 +150,102 @@ module.exports = {* @returns {Promise<SearchResult[]>} Search results*/async search(search, filter = '') {- return new Promise((cb, err) => {- https.get({- host: 'symbol-search.tradingview.com',- path: `/symbol_search/?text=${search.replace(/ /g, '%20')}&type=${filter}`,- origin: 'https://www.tradingview.com',- }, (res) => {- let rs = '';- res.on('data', (d) => { rs += d; });- res.on('end', () => {- try {- rs = JSON.parse(rs);- } catch (e) {- err(new Error('Can\'t parse server response'));- return;- }+ const data = await request({+ host: 'symbol-search.tradingview.com',+ path: `/symbol_search/?text=${search.replace(/ /g, '%20')}&type=${filter}`,+ origin: 'https://www.tradingview.com',+ });- cb(rs.map((s) => {- const exchange = s.exchange.split(' ')[0];- const id = `${exchange}:${s.symbol}`;-- const screener = (['forex', 'crypto'].includes(s.type)- ? s.type- : this.getScreener(exchange)- );-- return {- id,- exchange,- fullExchange: s.exchange,- screener,- symbol: s.symbol,- description: s.description,- type: s.type,- getTA: () => this.getTA(screener, id),- };- }));- });+ return data.map((s) => {+ const exchange = s.exchange.split(' ')[0];+ const id = `${exchange}:${s.symbol}`;++ const screener = (['forex', 'crypto'].includes(s.type)+ ? s.type+ : this.getScreener(exchange)+ );++ return {+ id,+ exchange,+ fullExchange: s.exchange,+ screener,+ symbol: s.symbol,+ description: s.description,+ type: s.type,+ getTA: () => this.getTA(screener, id),+ };+ });+ },- res.on('error', err);- });+ /**+ * @typedef {Object} IndicatorResult+ * @property {string} id Script ID+ * @property {string} version Script version+ * @property {string} name Script complete name+ * @property {{ id: number, username: string }} author Author user ID+ * @property {string} image Image ID https://tradingview.com/i/${image}+ * @property {string | ''} source Script source (if available)+ * @property {'study' | 'strategy'} type Script type (study / strategy)+ * @property {'open_source' | 'closed_source' | 'invite_only' | 'other'} access Script access type+ */++ /**+ * Find an indicator+ * @param {string} search Keywords+ * @returns {Promise<IndicatorResult[]>} Search results+ */+ async searchIndicator(search = '') {+ if (!indicList.length) {+ await Promise.all(['standard', 'candlestick', 'fundamental'].map(async (type) => {+ indicList.push(...await request({+ host: 'pine-facade.tradingview.com',+ path: `/pine-facade/list/?filter=${type}`,+ }));+ }));+ }++ const data = await request({+ host: 'www.tradingview.com',+ path: `/pubscripts-suggest-json/?search=${search.replace(/ /g, '%20')}`,});++ function norm(str = '') {+ return str.toUpperCase().replace(/[^A-Z]/g, '');+ }++ return [+ ...indicList.filter((i) => (+ norm(i.scriptName).includes(norm(search))+ || norm(i.extra.shortDescription).includes(norm(search))+ )).map((ind) => ({+ id: ind.scriptIdPart,+ version: ind.version,+ name: ind.scriptName,+ author: {+ id: ind.userId,+ username: '@TRADINGVIEW@',+ },+ image: '',+ access: 'closed_source',+ source: '',+ type: (ind.extra && ind.extra.kind) ? ind.extra.kind : 'study',+ })),++ ...data.results.map((ind) => ({+ id: ind.scriptIdPart,+ version: ind.version,+ name: ind.scriptName,+ author: {+ id: ind.author.id,+ username: ind.author.username,+ },+ image: ind.imageUrl,+ access: ['open_source', 'closed_source', 'invite_only'][ind.access - 1] || 'other',+ source: ind.scriptSource,+ type: (ind.extra && ind.extra.kind) ? ind.extra.kind : 'study',+ })),+ ];},/**@@ -186,6 +266,7 @@ module.exports = {* @property {string} pineVersion Indicator version* @property {string} description Indicator description* @property {string} shortDescription Indicator short description+ * @property {string} typeID Indicator script type ID* @property {Object<string, indicatorInput>} inputs Indicator inputs* @property {Object<string, string>} plots Indicator plots* @property {string} script Indicator script@@ -195,67 +276,68 @@ module.exports = {* Get an indicator* @param {string} id Indicator ID (Like: PUB;XXXXXXXXXXXXXXXXXXXXX)* @param {string} version Wanted version of the indicator+ * @param {'study' | 'strategy'} [type] Script type* @returns {Promise<Indicator>} Indicator*/- async getIndicator(id, version, settings = []) {- return new Promise((cb, err) => {- https.get(`https://pine-facade.tradingview.com/pine-facade/translate/${id}/${version}`, (res) => {- let rs = '';- res.on('data', (d) => { rs += d; });- res.on('end', () => {- try {- rs = JSON.parse(rs);- } catch (e) {- err(new Error('Inexistent or unsupported indicator'));- return;- }-- if (!rs.success || !rs.result.metaInfo || !rs.result.metaInfo.inputs) {- err(new Error('Inexistent or unsupported indicator'));- return;- }+ async getIndicator(id, version, settings = [], type = 'study') {+ const indicID = id.replace(/ |%/g, '%25');++ let data = await request({+ host: 'pine-facade.tradingview.com',+ path: `/pine-facade/translate/${indicID}/${version}`,+ }, true);++ try {+ data = JSON.parse(data);+ } catch (e) {+ throw new Error(`Inexistent or unsupported indicator: '${id}'`);+ }++ if (!data.success || !data.result.metaInfo || !data.result.metaInfo.inputs) {+ console.error(data);+ throw new Error('Inexistent or unsupported indicator');+ }++ const inputs = {};++ data.result.metaInfo.inputs.forEach((input) => {+ if (['text', 'pineId', 'pineVersion'].includes(input.id)) return;+ const inline = input.inline || input.name.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '');++ const i = parseInt(input.id.replace(/[^0-9]/g, ''), 10);++ inputs[input.id] = {+ name: input.name,+ type: input.type,+ value: settings[input.internalID] ?? settings[i] ?? input.defval,+ defVal: input.defval,+ hidden: !!input.isHidden,+ isFake: !!input.isFake,+ inline,+ };++ if (input.options) inputs[input.id].options = input.options;+ });- const inputs = {};-- rs.result.metaInfo.inputs.forEach((input) => {- if (['text', 'pineId', 'pineVersion'].includes(input.id)) return;- const inline = input.inline || input.name.replace(/ /g, '_').replace(/[^a-zA-Z0-9]/g, '');-- const i = parseInt(input.id.replace(/[^0-9]/g, ''), 10);-- inputs[input.id] = {- name: input.name,- type: input.type,- value: settings[i] ?? input.defval,- defVal: input.defval,- hidden: !!input.isHidden,- isFake: !!input.isFake,- inline,- };-- if (input.options) inputs[input.id].options = input.options;- });-- const plots = {};-- Object.keys(rs.result.metaInfo.styles).forEach((plotId) => {- plots[plotId] = rs.result.metaInfo.styles[plotId].title.replace(/ /g, '_').replace(/[^a-zA-Z0-9]/g, '');- });-- cb({- pineId: id,- pineVersion: version,- description: rs.result.metaInfo.description,- shortDescription: rs.result.metaInfo.shortDescription,- inputs,- plots,- script: rs.result.ilTemplate,- });- });+ const plots = {};- res.on('error', err);- });+ Object.keys(data.result.metaInfo.styles).forEach((plotId) => {+ plots[plotId] = data.result.metaInfo.styles[plotId].title.replace(/ /g, '_').replace(/[^a-zA-Z0-9_]/g, '');});++ return {+ pineId: indicID,+ pineVersion: version,+ description: data.result.metaInfo.description,+ shortDescription: data.result.metaInfo.shortDescription,+ typeID: {+ study: 'Script@tv-scripting-101!',+ strategy: 'StrategyScript@tv-scripting-101!',+ }[type] || type,+ inputs,+ plots,+ script: data.result.ilTemplate,+ };},/**@@ -336,30 +418,23 @@ module.exports = {* @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);+ const creds = credentials.id && credentials.session;+ const userID = creds ? credentials.id : -1;+ const session = creds ? credentials.session : null;++ const data = await request({+ host: 'www.tradingview.com',+ path: `/chart-token/?image_url=${layout}&user_id=${userID}`,+ headers: { cookie: session ? `sessionid=${session}` : '' },+ });- if (rs.token) cb(rs.token);- else err(new Error('Wrong layout or credentials'));- });+ if (!data.token) throw new Error('Wrong layout or credentials');- res.on('error', err);- }).end();- });+ return data.token;},/**- * @typedef {Object} DrawingPoint+ * @typedef {Object} DrawingPoint Drawing poitn* @property {number} time_t Point X time position* @property {number} price Point Y price position* @property {number} offset Point offset@@ -390,31 +465,20 @@ module.exports = {*/async getDrawings(layout, symbol = '', credentials = {}, chartID = 1) {const chartToken = await module.exports.getChartToken(layout, credentials);+ const creds = credentials.id && credentials.session;+ const session = creds ? credentials.session : null;++ const data = await request({+ host: 'charts-storage.tradingview.com',+ path: `/charts-storage/layout/${layout}/sources?chart_id=${chartID+ }&jwt=${chartToken}${symbol ? `&symbol=${symbol}` : ''}`,+ headers: { cookie: session ? `sessionid=${session}` : '' },+ });- 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.'));- });+ if (!data.payload) throw new Error('Wrong layout, user credentials, or chart id.');- res.on('error', err);- }).end();- });+ return Object.values(data.payload.sources || {}).map((drawing) => ({+ ...drawing, ...drawing.state,+ }));},};diff --git a/package.json b/package.jsonindex 5332141..89d60d8 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "@mathieuc/tradingview",- "version": "2.0.7",+ "version": "2.0.9","description": "Tradingview instant stocks API, indicator alerts, trading bot, and more !","main": "main.js","scripts": {@@ -24,6 +24,7 @@"author": "Mathieu Colmon","license": "ISC","dependencies": {+ "jszip": "^3.7.1","ws": "^7.4.3"},"devDependencies": {diff --git a/tests/strategy.js b/tests/strategy.jsnew file mode 100644index 0000000..0da86a5--- /dev/null+++ b/tests/strategy.js@@ -0,0 +1,56 @@+const marketAPI = require('../main');++const market = marketAPI(false);++market.on('logged', async () => {+ const indicators = (await market.searchIndicator('strategy'))+ .filter((st) => (st.type === 'strategy' && st.access !== 'invite_only'))+ .map((st) => ({+ name: st.name,+ id: st.id,+ version: st.version,+ settings: {+ commission_type: 'percent',+ commission_value: 0,+ initial_capital: 25000,+ default_qty_value: 20,+ default_qty_type: 'percent_of_equity',+ currency: 'EUR',+ pyramiding: 10,+ },+ }));++ let total = indicators.length;+ console.log('Found', indicators.length, 'strategies');++ market.initChart({+ symbol: 'COINBASE:BTCEUR',+ period: '1D',+ range: 0,+ indicators,+ }, (_, strategies) => {+ const loaded = Object.keys(strategies).length;+ console.log(`Loading... ${loaded}/${total}`);++ if (loaded < total) return;+ market.end();++ console.log(+ 'Best strategies:',+ Object.keys(strategies).map((stName) => ({+ n: stName,+ p: Math.round(strategies[stName].performance.all.netProfitPercent * 1000) / 1000,+ })).sort((a, b) => b.p - a.p),+ );+ });++ market.on('error', (...data) => {+ if (data[1].type === 'study_error') {+ console.error('Error with study:', indicators[data[1].data]);+ total -= 1;+ return;+ }++ console.error(...data);+ });+});