Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Starting dev + Testing things

Mathieu Colmon1452de5fb24f
diff --git a/.eslintrc.js b/.eslintrc.js
index 79fa088..1544d05 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -7,8 +7,10 @@ module.exports = {
extends: [
'airbnb-base',
],
+ parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 12,
+ requireConfigFile: false,
},
rules: {
'no-console': 'off',
diff --git a/OLD.js b/OLD.js
new file mode 100644
index 0000000..7921bcf
--- /dev/null
+++ b/OLD.js
@@ -0,0 +1,505 @@
+const WebSocket = require('ws');
+const JSZip = require('jszip');
+
+const {
+ search, getScreener, getTA, getIndicator,
+ getUser, getChartToken, getDrawings, searchIndicator,
+} = require('./src/miscRequests');
+
+let onPacket = () => null;
+
+function parse(str) {
+ const packets = str.replace(/~h~/g, '').split(/~m~[0-9]{1,}~m~/g).map((p) => {
+ if (!p) return false;
+ try {
+ return JSON.parse(p);
+ } catch (error) {
+ console.warn('Cant parse', p);
+ return false;
+ }
+ }).filter((p) => p);
+
+ packets.forEach((packet) => {
+ if (packet.m === 'protocol_error') {
+ return onPacket({
+ type: 'error',
+ syntax: packet.p[0],
+ });
+ }
+
+ if (packet.m && packet.p) {
+ return onPacket({
+ type: packet.m,
+ session: packet.p[0],
+ data: packet.p[1],
+ });
+ }
+
+ if (typeof packet === 'number') return onPacket({ type: 'ping', ping: packet });
+
+ return onPacket({ type: 'info', ...packet });
+ });
+}
+
+function genSession() {
+ let r = '';
+ const c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+ for (let i = 0; i < 12; i += 1) r += c.charAt(Math.floor(Math.random() * c.length));
+ return `qs_${r}`;
+}
+
+module.exports = (autoInit = true) => {
+ const callbacks = {
+ connected: [],
+ disconnected: [],
+ logged: [],
+ subscribed: [],
+ ping: [],
+ price: [],
+ data: [],
+
+ error: [],
+ event: [],
+ };
+
+ const chartEventNames = [
+ 'du', 'timescale_update',
+ 'series_loading', 'series_completed', 'series_error',
+ 'symbol_resolved', 'symbol_error',
+ 'study_loading', 'study_error',
+ ];
+ const chartCBs = {};
+
+ function handleEvent(ev, ...data) {
+ callbacks[ev].forEach((e) => e(...data));
+ callbacks.event.forEach((e) => e(ev, ...data));
+ }
+
+ function handleError(...msgs) {
+ if (callbacks.error.length === 0) console.error(...msgs);
+ else handleEvent('error', ...msgs);
+ }
+
+ const ws = new WebSocket('wss://widgetdata.tradingview.com/socket.io/websocket', {
+ origin: 'https://s.tradingview.com',
+ });
+
+ let logged = false;
+ /** ID of the quote session */
+ let sessionId = '';
+
+ /** List of subscribed symbols */
+ let subscribed = [];
+
+ /** Websockets status */
+ let isEnded = false;
+
+ /**
+ * Send a custom packet
+ * @param {string} t Packet type
+ * @param {string[]} p Packet data
+ * @example
+ * // Subscribe manualy to BTCEUR
+ * send('quote_add_symbols', [sessionId, 'BTCEUR']);
+ */
+ function send(t, p = []) {
+ if (!sessionId) return;
+
+ const msg = JSON.stringify({ m: t, p });
+ ws.send(`~m~${msg.length}~m~${msg}`);
+ }
+
+ ws.on('open', () => {
+ sessionId = genSession();
+ handleEvent('connected');
+ });
+
+ ws.on('close', () => {
+ logged = false;
+ sessionId = '';
+ handleEvent('disconnected');
+ });
+
+ ws.on('message', parse);
+
+ onPacket = (packet) => {
+ if (packet.type === 'ping') {
+ const pingStr = `~h~${packet.ping}`;
+ ws.send(`~m~${pingStr.length}~m~${pingStr}`);
+ handleEvent('ping', packet.ping);
+ return;
+ }
+
+ if (packet.type === 'quote_completed' && packet.data) {
+ handleEvent('subscribed', packet.data);
+ return;
+ }
+
+ if (packet.type === 'qsd' && packet.data.n && packet.data.v.lp) {
+ handleEvent('price', {
+ symbol: packet.data.n,
+ price: packet.data.v.lp,
+ });
+
+ return;
+ }
+
+ if (chartEventNames.includes(packet.type) && chartCBs[packet.session]) {
+ chartCBs[packet.session](packet);
+ return;
+ }
+
+ if (!logged && packet.type === 'info') {
+ if (autoInit) {
+ send('set_auth_token', ['unauthorized_user_token']);
+ send('quote_create_session', [sessionId]);
+ send('quote_set_fields', [sessionId, 'lp']);
+
+ subscribed.forEach((symbol) => send('quote_add_symbols', [sessionId, symbol]));
+ }
+
+ handleEvent('logged', packet);
+ return;
+ }
+
+ if (packet.type === 'error') {
+ handleError(`Market API critical error: ${packet.syntax}`);
+ ws.close();
+ return;
+ }
+
+ handleEvent('data', packet);
+ };
+
+ return {
+ /** Event listener
+ * @param { 'connected' | 'disconnected' | 'logged'
+ * | 'subscribed' | 'price' | 'data' | 'error' | 'ping' } event Event
+ * @param {(...data: object) => null} cb Callback
+ */
+ on(event, cb) {
+ if (!callbacks[event]) {
+ console.log('Wrong event:', event);
+ console.log('Available events:', Object.keys(callbacks));
+ return;
+ }
+
+ callbacks[event].push(cb);
+ },
+
+ /**
+ * Close the websocket connection
+ * @return {Promise<void>} When websocket is closed
+ */
+ end() {
+ return new Promise((cb) => {
+ isEnded = true;
+ ws.close();
+ cb();
+ });
+ },
+
+ search,
+ getScreener,
+ getTA,
+ subscribed,
+ searchIndicator,
+ getUser,
+ getChartToken,
+ getDrawings,
+
+ /**
+ * Unsubscribe to a market
+ * @param {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
+ */
+ subscribe(symbol) {
+ if (subscribed.includes(symbol)) return;
+ send('quote_add_symbols', [sessionId, symbol]);
+ subscribed.push(symbol);
+ },
+
+ /**
+ * Unsubscribe from a market
+ * @param {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
+ */
+ unsubscribe(symbol) {
+ if (!subscribed.includes(symbol)) return;
+ send('quote_remove_symbols', [sessionId, symbol]);
+ subscribed = subscribed.filter((s) => s !== symbol);
+ },
+
+ /**
+ * @typedef {Object} IndicatorInfos Indicator infos
+ * @property {string} id ID of the indicator (Like: XXX;XXXXXXXXXXXXXXXXXXXXX)
+ * @property {string} [name] Name of the indicator
+ * @property {'last' | 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
+ * @property {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
+ * @property { '1' | '3' | '5' | '15' | '30' | '45'
+ * | '60' | '120' | '180' | '240'
+ * | '1D' | '1W' | '1M'
+ * } [period] Period
+ * @property {number} [range] Number of loaded periods
+ * @property {string} [timezone] Timezone in 'Europe/Paris' format
+ * @property {IndicatorInfos[]} [indicators] List of indicators
+ */
+
+ /**
+ * @typedef {Object} Period List of prices / indicator values
+ * @property {number} $time
+ * @property {{
+ * time: number, open: number, close: number,
+ * max: number, min: number, change: number,
+ * }} $prices
+ */
+
+ /**
+ * Init a chart instance
+ * @param {ChartInfos} chart
+ * @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, i.type)),
+ );
+
+ async function updatePeriods(packet) {
+ const newData = packet.data;
+
+ 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] = {};
+
+ if (newData[type].s) {
+ [periods[p.i].$time] = p.v;
+
+ periods[p.i][type] = {
+ open: p.v[1],
+ close: p.v[4],
+ max: p.v[2],
+ min: p.v[3],
+ change: Math.round(p.v[5] * 100) / 100,
+ };
+ }
+
+ if (newData[type].st) {
+ const period = {};
+ const indicator = indicators[parseInt(type, 10)];
+
+ p.v.forEach((val, i) => {
+ if (i === 0) return;
+ if (indicator.plots[`plot_${i - 1}`] && !period[indicator.plots[`plot_${i - 1}`]]) {
+ period[indicator.plots[`plot_${i - 1}`]] = val;
+ } else period[`_plot_${i - 1}`] = val;
+ });
+ periods[p.i][chart.indicators[parseInt(type, 10)].name || `st${type}`] = period;
+ }
+ });
+ }));
+ }
+
+ chartCBs[chartSession] = async (packet) => {
+ if (isEnded) return;
+
+ if (['timescale_update', 'du'].includes(packet.type)) {
+ await updatePeriods(packet);
+ if (!isEnded) onUpdate([...periods].reverse(), strategies);
+ return;
+ }
+
+ if (packet.type.endsWith('_error')) {
+ handleError(`Error on '${chart.symbol}' (${chartSession}) chart: "${packet.type}":`, packet);
+ }
+ };
+
+ 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"}`]);
+ send('create_series', [chartSession, '$prices', 's1', 'sds_sym_1', (chart.period || '240'), (chart.range || 100), '']);
+
+ indicators.forEach(async (indicator, i) => {
+ const pineInfos = {
+ pineId: indicator.pineId,
+ pineVersion: indicator.pineVersion,
+ text: indicator.script,
+ };
+
+ Object.keys(indicator.inputs).forEach((inputID, inp) => {
+ const input = indicator.inputs[inputID];
+ if (input.type === 'bool' && typeof input.value !== 'boolean') handleError(`Input '${input.name}' (${inp}) must be a boolean !`);
+ if (input.type === 'integer' && typeof input.value !== 'number') handleError(`Input '${input.name}' (${inp}) must be a number !`);
+ if (input.type === 'float' && typeof input.value !== 'number') handleError(`Input '${input.name}' (${inp}) must be a number !`);
+ if (input.type === 'text' && typeof input.value !== 'string') handleError(`Input '${input.name}' (${inp}) must be a string !`);
+ if (input.options && !input.options.includes(input.value)) {
+ handleError(`Input '${input.name}' (${inp}) must be one of these values:`, input.options);
+ }
+
+ pineInfos[inputID] = {
+ v: input.value,
+ f: input.isFake,
+ t: input.type,
+ };
+ });
+
+ send('create_study', [chartSession, `${i}`, 'st1', '$prices', indicator.typeID, pineInfos]);
+ });
+ },
+
+ send,
+ sessionId,
+ };
+};
diff --git a/main.js b/main.js
index 80de04c..5dc95a5 100644
--- a/main.js
+++ b/main.js
@@ -1,505 +1,7 @@
-const WebSocket = require('ws');
-const JSZip = require('jszip');
+const miscRequests = require('./src/miscRequests');
+const Client = require('./src/client');
-const {
- search, getScreener, getTA, getIndicator,
- getUser, getChartToken, getDrawings, searchIndicator,
-} = require('./miscRequests');
-
-let onPacket = () => null;
-
-function parse(str) {
- const packets = str.replace(/~h~/g, '').split(/~m~[0-9]{1,}~m~/g).map((p) => {
- if (!p) return false;
- try {
- return JSON.parse(p);
- } catch (error) {
- console.warn('Cant parse', p);
- return false;
- }
- }).filter((p) => p);
-
- packets.forEach((packet) => {
- if (packet.m === 'protocol_error') {
- return onPacket({
- type: 'error',
- syntax: packet.p[0],
- });
- }
-
- if (packet.m && packet.p) {
- return onPacket({
- type: packet.m,
- session: packet.p[0],
- data: packet.p[1],
- });
- }
-
- if (typeof packet === 'number') return onPacket({ type: 'ping', ping: packet });
-
- return onPacket({ type: 'info', ...packet });
- });
-}
-
-function genSession() {
- let r = '';
- const c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
- for (let i = 0; i < 12; i += 1) r += c.charAt(Math.floor(Math.random() * c.length));
- return `qs_${r}`;
-}
-
-module.exports = (autoInit = true) => {
- const callbacks = {
- connected: [],
- disconnected: [],
- logged: [],
- subscribed: [],
- ping: [],
- price: [],
- data: [],
-
- error: [],
- event: [],
- };
-
- const chartEventNames = [
- 'du', 'timescale_update',
- 'series_loading', 'series_completed', 'series_error',
- 'symbol_resolved', 'symbol_error',
- 'study_loading', 'study_error',
- ];
- const chartCBs = {};
-
- function handleEvent(ev, ...data) {
- callbacks[ev].forEach((e) => e(...data));
- callbacks.event.forEach((e) => e(ev, ...data));
- }
-
- function handleError(...msgs) {
- if (callbacks.error.length === 0) console.error(...msgs);
- else handleEvent('error', ...msgs);
- }
-
- const ws = new WebSocket('wss://widgetdata.tradingview.com/socket.io/websocket', {
- origin: 'https://s.tradingview.com',
- });
-
- let logged = false;
- /** ID of the quote session */
- let sessionId = '';
-
- /** List of subscribed symbols */
- let subscribed = [];
-
- /** Websockets status */
- let isEnded = false;
-
- /**
- * Send a custom packet
- * @param {string} t Packet type
- * @param {string[]} p Packet data
- * @example
- * // Subscribe manualy to BTCEUR
- * send('quote_add_symbols', [sessionId, 'BTCEUR']);
- */
- function send(t, p = []) {
- if (!sessionId) return;
-
- const msg = JSON.stringify({ m: t, p });
- ws.send(`~m~${msg.length}~m~${msg}`);
- }
-
- ws.on('open', () => {
- sessionId = genSession();
- handleEvent('connected');
- });
-
- ws.on('close', () => {
- logged = false;
- sessionId = '';
- handleEvent('disconnected');
- });
-
- ws.on('message', parse);
-
- onPacket = (packet) => {
- if (packet.type === 'ping') {
- const pingStr = `~h~${packet.ping}`;
- ws.send(`~m~${pingStr.length}~m~${pingStr}`);
- handleEvent('ping', packet.ping);
- return;
- }
-
- if (packet.type === 'quote_completed' && packet.data) {
- handleEvent('subscribed', packet.data);
- return;
- }
-
- if (packet.type === 'qsd' && packet.data.n && packet.data.v.lp) {
- handleEvent('price', {
- symbol: packet.data.n,
- price: packet.data.v.lp,
- });
-
- return;
- }
-
- if (chartEventNames.includes(packet.type) && chartCBs[packet.session]) {
- chartCBs[packet.session](packet);
- return;
- }
-
- if (!logged && packet.type === 'info') {
- if (autoInit) {
- send('set_auth_token', ['unauthorized_user_token']);
- send('quote_create_session', [sessionId]);
- send('quote_set_fields', [sessionId, 'lp']);
-
- subscribed.forEach((symbol) => send('quote_add_symbols', [sessionId, symbol]));
- }
-
- handleEvent('logged', packet);
- return;
- }
-
- if (packet.type === 'error') {
- handleError(`Market API critical error: ${packet.syntax}`);
- ws.close();
- return;
- }
-
- handleEvent('data', packet);
- };
-
- return {
- /** Event listener
- * @param { 'connected' | 'disconnected' | 'logged'
- * | 'subscribed' | 'price' | 'data' | 'error' | 'ping' } event Event
- * @param {(...data: object) => null} cb Callback
- */
- on(event, cb) {
- if (!callbacks[event]) {
- console.log('Wrong event:', event);
- console.log('Available events:', Object.keys(callbacks));
- return;
- }
-
- callbacks[event].push(cb);
- },
-
- /**
- * Close the websocket connection
- * @return {Promise<void>} When websocket is closed
- */
- end() {
- return new Promise((cb) => {
- isEnded = true;
- ws.close();
- cb();
- });
- },
-
- search,
- getScreener,
- getTA,
- subscribed,
- searchIndicator,
- getUser,
- getChartToken,
- getDrawings,
-
- /**
- * Unsubscribe to a market
- * @param {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
- */
- subscribe(symbol) {
- if (subscribed.includes(symbol)) return;
- send('quote_add_symbols', [sessionId, symbol]);
- subscribed.push(symbol);
- },
-
- /**
- * Unsubscribe from a market
- * @param {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
- */
- unsubscribe(symbol) {
- if (!subscribed.includes(symbol)) return;
- send('quote_remove_symbols', [sessionId, symbol]);
- subscribed = subscribed.filter((s) => s !== symbol);
- },
-
- /**
- * @typedef {Object} IndicatorInfos Indicator infos
- * @property {string} id ID of the indicator (Like: XXX;XXXXXXXXXXXXXXXXXXXXX)
- * @property {string} [name] Name of the indicator
- * @property {'last' | 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
- * @property {string} symbol Market symbol (Example: BTCEUR or COINBASE:BTCEUR)
- * @property { '1' | '3' | '5' | '15' | '30' | '45'
- * | '60' | '120' | '180' | '240'
- * | '1D' | '1W' | '1M'
- * } [period] Period
- * @property {number} [range] Number of loaded periods
- * @property {string} [timezone] Timezone in 'Europe/Paris' format
- * @property {IndicatorInfos[]} [indicators] List of indicators
- */
-
- /**
- * @typedef {Object} Period List of prices / indicator values
- * @property {number} $time
- * @property {{
- * time: number, open: number, close: number,
- * max: number, min: number, change: number,
- * }} $prices
- */
-
- /**
- * Init a chart instance
- * @param {ChartInfos} chart
- * @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, i.type)),
- );
-
- async function updatePeriods(packet) {
- const newData = packet.data;
-
- 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] = {};
-
- if (newData[type].s) {
- [periods[p.i].$time] = p.v;
-
- periods[p.i][type] = {
- open: p.v[1],
- close: p.v[4],
- max: p.v[2],
- min: p.v[3],
- change: Math.round(p.v[5] * 100) / 100,
- };
- }
-
- if (newData[type].st) {
- const period = {};
- const indicator = indicators[parseInt(type, 10)];
-
- p.v.forEach((val, i) => {
- if (i === 0) return;
- if (indicator.plots[`plot_${i - 1}`] && !period[indicator.plots[`plot_${i - 1}`]]) {
- period[indicator.plots[`plot_${i - 1}`]] = val;
- } else period[`_plot_${i - 1}`] = val;
- });
- periods[p.i][chart.indicators[parseInt(type, 10)].name || `st${type}`] = period;
- }
- });
- }));
- }
-
- chartCBs[chartSession] = async (packet) => {
- if (isEnded) return;
-
- if (['timescale_update', 'du'].includes(packet.type)) {
- await updatePeriods(packet);
- if (!isEnded) onUpdate([...periods].reverse(), strategies);
- return;
- }
-
- if (packet.type.endsWith('_error')) {
- handleError(`Error on '${chart.symbol}' (${chartSession}) chart: "${packet.type}":`, packet);
- }
- };
-
- 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"}`]);
- send('create_series', [chartSession, '$prices', 's1', 'sds_sym_1', (chart.period || '240'), (chart.range || 100), '']);
-
- indicators.forEach(async (indicator, i) => {
- const pineInfos = {
- pineId: indicator.pineId,
- pineVersion: indicator.pineVersion,
- text: indicator.script,
- };
-
- Object.keys(indicator.inputs).forEach((inputID, inp) => {
- const input = indicator.inputs[inputID];
- if (input.type === 'bool' && typeof input.value !== 'boolean') handleError(`Input '${input.name}' (${inp}) must be a boolean !`);
- if (input.type === 'integer' && typeof input.value !== 'number') handleError(`Input '${input.name}' (${inp}) must be a number !`);
- if (input.type === 'float' && typeof input.value !== 'number') handleError(`Input '${input.name}' (${inp}) must be a number !`);
- if (input.type === 'text' && typeof input.value !== 'string') handleError(`Input '${input.name}' (${inp}) must be a string !`);
- if (input.options && !input.options.includes(input.value)) {
- handleError(`Input '${input.name}' (${inp}) must be one of these values:`, input.options);
- }
-
- pineInfos[inputID] = {
- v: input.value,
- f: input.isFake,
- t: input.type,
- };
- });
-
- send('create_study', [chartSession, `${i}`, 'st1', '$prices', indicator.typeID, pineInfos]);
- });
- },
-
- send,
- sessionId,
- };
+module.exports = {
+ ...miscRequests,
+ Client,
};
diff --git a/package.json b/package.json
index dfd687c..19c386b 100644
--- a/package.json
+++ b/package.json
@@ -32,6 +32,7 @@
"ws": "^7.4.3"
},
"devDependencies": {
+ "@babel/eslint-parser": "^7.15.7",
"eslint": "^7.25.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1"
diff --git a/src/client.js b/src/client.js
new file mode 100644
index 0000000..d204888
--- /dev/null
+++ b/src/client.js
@@ -0,0 +1,274 @@
+const WebSocket = require('ws');
+
+const misc = require('./miscRequests');
+
+const quoteSessionGenerator = require('./quote/session');
+const chartSessionGenerator = require('./quote/session');
+
+/**
+ * @typedef {Object} Session
+ * @prop {'quote' | 'chart' | 'replay'} type Session type
+ * @prop {(data: {}) => null} onData When there is a data
+ */
+
+/** @typedef {Object<string, Session>} SessionList Session list */
+
+/**
+ * @typedef {(t: string, p: []) => null} SendPacket Send a custom packet
+ * @param {string} t Packet type
+ * @param {string[]} p Packet data
+*/
+
+/**
+ * @typedef {Object} ClientBridge
+ * @prop {SessionList} sessions
+ * @prop {SendPacket} send
+ */
+
+/**
+ * @typedef { 'connected' | 'disconnected'
+ * | 'logged' | 'ping' | 'data'
+ * | 'error' | 'event'
+ * } ClientEvent
+ */
+
+module.exports = class Client {
+ #ws;
+
+ #logged = false;
+
+ /** If the client is logged in */
+ get logged() {
+ return this.#logged;
+ }
+
+ /** If the cient was closed */
+ get isOpen() {
+ return this.#ws.readyState === this.#ws.OPEN;
+ }
+
+ /** @type {SessionList} */
+ #sessions = {};
+
+ #callbacks = {
+ connected: [],
+ disconnected: [],
+ logged: [],
+ ping: [],
+ data: [],
+
+ error: [],
+ event: [],
+ };
+
+ /**
+ * @param {ClientEvent} ev Client event
+ * @param {...{}} data Packet data
+ */
+ #handleEvent(ev, ...data) {
+ this.#callbacks[ev].forEach((e) => e(...data));
+ this.#callbacks.event.forEach((e) => e(ev, ...data));
+ }
+
+ #handleError(...msgs) {
+ if (this.#callbacks.error.length === 0) console.error(...msgs);
+ else this.#handleEvent('error', ...msgs);
+ }
+
+ /**
+ * When client is connected
+ * @param {() => void} cb Callback
+ * @event
+ */
+ onConnected(cb) {
+ this.#callbacks.connected.push(cb);
+ }
+
+ /**
+ * When client is disconnected
+ * @param {() => void} cb Callback
+ * @event
+ */
+ onDisconnected(cb) {
+ this.#callbacks.disconnected.push(cb);
+ }
+
+ /**
+ * @typedef {Object} SocketSession
+ * @prop {string} session_id Socket session ID
+ * @prop {number} timestamp Session start timestamp
+ * @prop {number} timestampMs Session start milliseconds timestamp
+ * @prop {string} release Release
+ * @prop {string} studies_metadata_hash Studies metadata hash
+ * @prop {'json' | string} protocol Used protocol
+ * @prop {string} javastudies Javastudies
+ * @prop {number} auth_scheme_vsn Auth scheme type
+ * @prop {string} via Socket IP
+
+ * When client is logged
+ * @param {(SocketSession: SocketSession) => void} cb Callback
+ * @event
+ */
+ onLogged(cb) {
+ this.#callbacks.logged.push(cb);
+ }
+
+ /**
+ * When server is pinging the client
+ * @param {(i: number) => void} cb Callback
+ * @event
+ */
+ onPing(cb) {
+ this.#callbacks.ping.push(cb);
+ }
+
+ /**
+ * When unparsed data is received
+ * @param {(...{}) => void} cb Callback
+ * @event
+ */
+ onData(cb) {
+ this.#callbacks.data.push(cb);
+ }
+
+ /**
+ * When a client error happens
+ * @param {(...{}) => void} cb Callback
+ * @event
+ */
+ onError(cb) {
+ this.#callbacks.error.push(cb);
+ }
+
+ /**
+ * When a client event happens
+ * @param {(...{}) => void} cb Callback
+ * @event
+ */
+ onEvent(cb) {
+ this.#callbacks.event.push(cb);
+ }
+
+ #parsePacket(str) {
+ if (!this.isOpen) return;
+
+ str.replace(/~h~/g, '').split(/~m~[0-9]{1,}~m~/g)
+ .map((p) => {
+ if (!p) return false;
+ try {
+ return JSON.parse(p);
+ } catch (error) {
+ console.warn('Cant parse', p);
+ return false;
+ }
+ })
+ .filter((p) => p)
+ .forEach((packet) => {
+ console.log('[CLIENT] PACKET', packet);
+ if (typeof packet === 'number') { // Ping
+ const pingStr = `~h~${packet}`;
+ this.#ws.send(`~m~${pingStr.length}~m~${pingStr}`);
+ this.#handleEvent('ping', packet);
+ return;
+ }
+
+ if (packet.m === 'protocol_error') { // Error
+ this.#handleError('Client critical error:', packet.p);
+ this.#ws.close();
+ return;
+ }
+
+ if (packet.m && packet.p) { // Normal packet
+ const parsed = {
+ type: packet.m,
+ session: packet.p[0],
+ data: packet.p[1],
+ };
+
+ if (parsed.session && this.#sessions[parsed.session]) {
+ this.#sessions[parsed.session].onData(parsed);
+ return;
+ }
+ }
+
+ if (!this.#logged) {
+ this.#handleEvent('logged', packet);
+ return;
+ }
+
+ this.#handleEvent('data', packet);
+ });
+ }
+
+ #sendQueue = [];
+
+ /** @type {SendPacket} Send a custom packet */
+ send(t, p = []) {
+ const msg = JSON.stringify({ m: t, p });
+ this.#sendQueue.push(`~m~${msg.length}~m~${msg}`);
+ this.sendQueue();
+ }
+
+ /** Send all waiting packets */
+ sendQueue() {
+ while (this.isOpen && this.#sendQueue.length > 0) {
+ const packet = this.#sendQueue.shift();
+ this.#ws.send(packet);
+ }
+ }
+
+ /**
+ * @typedef {Object} ClientOptions
+ * @prop {string} [token] User auth token (in 'sessionid' cookie)
+ */
+
+ /** Client object
+ * @param {ClientOptions} clientOptions TradingView client options
+ */
+ constructor(clientOptions = {}) {
+ this.#ws = new WebSocket('wss://widgetdata.tradingview.com/socket.io/websocket', {
+ origin: 'https://s.tradingview.com',
+ });
+
+ if (clientOptions.token) {
+ misc.getUser(clientOptions.token).then((user) => {
+ this.send('set_auth_token', [user.authToken]);
+ });
+ } else this.send('set_auth_token', ['unauthorized_user_token']);
+
+ this.#ws.on('open', () => {
+ this.#handleEvent('connected');
+ this.sendQueue();
+ });
+
+ this.#ws.on('close', () => {
+ this.#logged = false;
+ this.#handleEvent('disconnected');
+ });
+
+ this.#ws.on('message', (data) => this.#parsePacket(data));
+ }
+
+ /** @type {ClientBridge} */
+ #clientBridge = {
+ sessions: this.#sessions,
+ send: (t, p) => this.send(t, p),
+ };
+
+ /** @namespace Session */
+ Session = {
+ Quote: quoteSessionGenerator(this.#clientBridge),
+ Chart: chartSessionGenerator(this.#clientBridge),
+ };
+
+ /**
+ * Close the websocket connection
+ * @return {Promise<void>} When websocket is closed
+ */
+ end() {
+ return new Promise((cb) => {
+ this.#ws.close();
+ cb();
+ });
+ }
+};
diff --git a/miscRequests.js b/src/miscRequests.js
similarity index 100%
rename from miscRequests.js
rename to src/miscRequests.js
diff --git a/src/quote/market.js b/src/quote/market.js
new file mode 100644
index 0000000..ff781e9
--- /dev/null
+++ b/src/quote/market.js
@@ -0,0 +1,124 @@
+/**
+ * @typedef { 'loaded' | 'data' | 'error' } MarketEvent
+ */
+
+/**
+ * @param {import('./session').QuoteSessionBridge} quoteSession
+ */
+module.exports = (quoteSession) => class QuoteMarket {
+ #symbolList = quoteSession.symbols;
+
+ #symbol;
+
+ #symbolListenerID = 0;
+
+ #callbacks = {
+ loaded: [],
+ data: [],
+
+ event: [],
+ error: [],
+ };
+
+ #lastData = {};
+
+ /**
+ * @param {MarketEvent} ev Client event
+ * @param {...{}} data Packet data
+ */
+ #handleEvent(ev, ...data) {
+ this.#callbacks[ev].forEach((e) => e(...data));
+ this.#callbacks.event.forEach((e) => e(ev, ...data));
+ }
+
+ #handleError(...msgs) {
+ if (this.#callbacks.error.length === 0) console.error(...msgs);
+ else this.#handleEvent('error', ...msgs);
+ }
+
+ /**
+ * @param {string} symbol Market symbol (like: 'BTCEUR' or 'KRAKEN:BTCEUR')
+ */
+ constructor(symbol) {
+ this.#symbol = symbol;
+
+ if (!this.#symbolList[symbol]) {
+ this.#symbolList[symbol] = [];
+ quoteSession.send('quote_add_symbols', [
+ quoteSession.sessionID,
+ symbol,
+ ]);
+ }
+ this.#symbolListenerID = this.#symbolList[symbol].length;
+
+ this.#symbolList[symbol][this.#symbolListenerID] = (packet) => {
+ console.log('[MARKET] DATA', packet);
+
+ if (packet.type === 'qsd' && packet.data.s === 'ok') {
+ this.#lastData = {
+ ...this.#lastData,
+ ...packet.data.v,
+ };
+ this.#handleEvent('data', this.#lastData);
+ return;
+ }
+
+ if (packet.type === 'qsd' && packet.data.s === 'error') {
+ this.#handleError('Market error', packet.data);
+ return;
+ }
+
+ if (packet.type === 'quote_completed') {
+ this.#handleEvent('loaded');
+ return;
+ }
+ };
+ }
+
+ /**
+ * When quote market is loaded
+ * @param {() => void} cb Callback
+ * @event
+ */
+ onLoaded(cb) {
+ this.#callbacks.loaded.push(cb);
+ }
+
+ /**
+ * When quote data is received
+ * @param {(data: {}) => void} cb Callback
+ * @event
+ */
+ onData(cb) {
+ this.#callbacks.data.push(cb);
+ }
+
+ /**
+ * When quote event happens
+ * @param {(...any) => void} cb Callback
+ * @event
+ */
+ onEvent(cb) {
+ this.#callbacks.event.push(cb);
+ }
+
+ /**
+ * When quote error happens
+ * @param {(...any) => void} cb Callback
+ * @event
+ */
+ onError(cb) {
+ this.#callbacks.error.push(cb);
+ }
+
+ /** Close this listener */
+ close() {
+ if (this.#symbolList[this.#symbol].length <= 1) {
+ quoteSession.send('quote_remove_symbols', [
+ quoteSession.sessionID,
+ this.#symbol,
+ ]);
+ }
+ delete this.#symbolList[this.#symbol][this.#symbolListenerID];
+ }
+};
diff --git a/src/quote/session.js b/src/quote/session.js
new file mode 100644
index 0000000..b176ca6
--- /dev/null
+++ b/src/quote/session.js
@@ -0,0 +1,115 @@
+const { genSessionID } = require('../utils');
+
+const quoteMarketConstructor = require('./market');
+
+/** @typedef {Object<string, Function[]>} SymbolList */
+
+/**
+ * @typedef {Object} QuoteSessionBridge
+ * @prop {string} sessionID
+ * @prop {SymbolList} symbols
+ * @prop {import('../client').SendPacket} send
+*/
+
+/**
+ * @typedef {'base-currency-logoid'
+ * | 'ch' | 'chp' | 'currency-logoid' | 'provider_id'
+ * | 'currency_code' | 'current_session' | 'description'
+ * | 'exchange' | 'format' | 'fractional' | 'is_tradable'
+ * | 'language' | 'local_description' | 'logoid' | 'lp'
+ * | 'lp_time' | 'minmov' | 'minmove2' | 'original_name'
+ * | 'pricescale' | 'pro_name' | 'short_name' | 'type'
+ * | 'update_mode' | 'volume' | 'ask' | 'bid' | 'fundamentals'
+ * | 'high_price' | 'low_price' | 'open_price' | 'prev_close_price'
+ * | 'rch' | 'rchp' | 'rtc' | 'rtc_time' | 'status' | 'industry'
+ * | 'basic_eps_net_income' | 'beta_1_year' | 'market_cap_basic'
+ * | 'earnings_per_share_basic_ttm' | 'price_earnings_ttm'
+ * | 'sector' | 'dividends_yield' | 'timezone' | 'country_code'
+ * } quoteField Quote data field
+ */
+
+/** @param {'all' | 'price'} fieldsType */
+function getQuoteFields(fieldsType) {
+ if (fieldsType === 'price') {
+ return ['lp'];
+ }
+
+ return [
+ 'base-currency-logoid', 'ch', 'chp', 'currency-logoid',
+ 'currency_code', 'current_session', 'description',
+ 'exchange', 'format', 'fractional', 'is_tradable',
+ 'language', 'local_description', 'logoid', 'lp',
+ 'lp_time', 'minmov', 'minmove2', 'original_name',
+ 'pricescale', 'pro_name', 'short_name', 'type',
+ 'update_mode', 'volume', 'ask', 'bid', 'fundamentals',
+ 'high_price', 'low_price', 'open_price', 'prev_close_price',
+ 'rch', 'rchp', 'rtc', 'rtc_time', 'status', 'industry',
+ 'basic_eps_net_income', 'beta_1_year', 'market_cap_basic',
+ 'earnings_per_share_basic_ttm', 'price_earnings_ttm',
+ 'sector', 'dividends_yield', 'timezone', 'country_code',
+ 'provider_id',
+ ];
+}
+
+/**
+ * @typedef {Object} quoteSessionOptions Quote Session options
+ * @prop {'all' | 'price'} [fields] Asked quote fields
+ * @prop {quoteField[]} [customFields] List of asked quote fields
+ */
+
+/**
+ * @param {import('../client').ClientBridge} client
+ */
+module.exports = (client) => class QuoteSession {
+ #sessionID = genSessionID('qs');
+
+ #client = client;
+
+ /** @type {SymbolList} */
+ #symbols = {};
+
+ /**
+ * @param {quoteSessionOptions} options Quote settings options
+ */
+ constructor(options) {
+ this.#client.sessions[this.#sessionID] = {
+ type: 'quote',
+ onData: (packet) => {
+ console.log('[QUOTE SESSION] DATA', packet);
+
+ if (packet.type === 'quote_completed' && this.#symbols[packet.data]) {
+ this.#symbols[packet.data].forEach((h) => h(packet));
+ }
+
+ if (packet.type === 'qsd' && this.#symbols[packet.data.n]) {
+ this.#symbols[packet.data.n].forEach((h) => h(packet));
+ }
+ },
+ };
+
+ const fields = (options.customFields && options.customFields.length > 0
+ ? options.customFields
+ : getQuoteFields(options.fields)
+ );
+
+ this.#client.send('quote_create_session', [this.#sessionID]);
+ this.#client.send('quote_set_fields', [this.#sessionID, ...fields]);
+ }
+
+ /** @type {QuoteSessionBridge} */
+ #quoteSession = {
+ sessionID: this.#sessionID,
+ symbols: this.#symbols,
+ send: (t, p) => this.#client.send(t, p),
+ };
+
+ /** @constructor */
+ Market = quoteMarketConstructor(this.#quoteSession);
+
+ /** Delete the quote session */
+ delete() {
+ this.#quoteSession.send('quote_delete_session', [
+ this.#quoteSession.sessionID,
+ ]);
+ }
+};
diff --git a/src/types.js b/src/types.js
new file mode 100644
index 0000000..5ddba77
--- /dev/null
+++ b/src/types.js
@@ -0,0 +1,5 @@
+/**
+ * @typedef {string} MarketSymbol Market symbol (like: 'BTCEUR' or 'KRAKEN:BTCEUR')
+ */
+
+module.exports = {};
diff --git a/src/utils.js b/src/utils.js
new file mode 100644
index 0000000..1b79c48
--- /dev/null
+++ b/src/utils.js
@@ -0,0 +1,8 @@
+module.exports = {
+ genSessionID(type = 'xs') {
+ let r = '';
+ const c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+ for (let i = 0; i < 12; i += 1) r += c.charAt(Math.floor(Math.random() * c.length));
+ return `${type}_${r}`;
+ },
+};
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..e983806
--- /dev/null
+++ b/test.js
@@ -0,0 +1,34 @@
+const TradingView = require('./main');
+
+const client = new TradingView.Client({
+ token: null,
+});
+
+client.onEvent((event, data) => {
+ console.log('[TEST] EVENT:', event, data);
+});
+
+const quoteSession = new client.Session.Quote({
+ fields: 'price',
+});
+
+// const quoteSession2 = new client.Session.Quote({
+// fields: 'price',
+// });
+
+const BTC = new quoteSession.Market('BTCEUR');
+BTC.onData((data) => {
+ console.log('[TEST] BTCEUR DATA', data);
+});
+BTC.onError((...err) => {
+ console.log('[TEST] BTCEUR ERROR', err);
+});
+
+// const BTC = new quoteSession.Symbol('BTCEUR');
+// BTC.onData((symbol) => {
+// console.log(symbol);
+// });
+
+// setTimeout(() => {
+// BTC.close();
+// }, 5000);