Skip to content

📈 Get real-time stocks from TradingView

Add replay mode (beta)

Mathieu Colmon8e1f1376ae60
diff --git a/README.md b/README.md
index ac42162..5120d15 100644
--- a/README.md
+++ b/README.md
@@ -9,6 +9,7 @@ Get realtime market prices and indicator values from Tradingview !
- [x] Unlimited simultaneous indicators
- [x] Realtime
- [x] Get TradingView's technical analysis
+- [x] Replay mode + Fake Replay mode (for free plan)
- [x] Get values from a specific date range
- [ ] TradingView socket server emulation
- [ ] Interract with public chats
diff --git a/examples/FakeReplayMode.js b/examples/FakeReplayMode.js
new file mode 100644
index 0000000..36799a6
--- /dev/null
+++ b/examples/FakeReplayMode.js
@@ -0,0 +1,41 @@
+const { Client } = require('../main');
+
+/*
+ This example tests the fake
+ replay mode which works in
+ intraday even with free plan
+*/
+
+console.log('----- Testing FakeReplayMode: -----');
+
+const client = new Client();
+const chart = new client.Session.Chart();
+
+chart.setMarket('BINANCE:BTCEUR', {
+ timeframe: 'D',
+ range: -1, // Range is negative, so 'to' means 'from'
+ to: Math.round(Date.now() / 1000) - 86400 * 7, // Seven days before now
+ // to: 1600000000,
+});
+
+let interval = NaN;
+
+chart.onUpdate(async () => {
+ const times = chart.periods.map((p) => p.time);
+
+ const intrval = times[0] - times[1];
+ if (Number.isNaN(interval) && times.length >= 2) interval = intrval;
+
+ if (!Number.isNaN(interval) && interval !== intrval) {
+ throw new Error(`Wrong interval: ${intrval} (should be ${interval})`);
+ }
+
+ console.log('Next ->', times[0]);
+
+ if ((times[0] + 86400) * 1000 > Date.now()) {
+ await client.end();
+ console.log('Done !', times.length);
+ }
+
+ chart.fetchMore(-1);
+});
diff --git a/examples/ReplayMode.js b/examples/ReplayMode.js
new file mode 100644
index 0000000..37a7199
--- /dev/null
+++ b/examples/ReplayMode.js
@@ -0,0 +1,97 @@
+const TradingView = require('../main');
+
+/*
+ This example tests the real replay
+ mode by fetching indicator data and
+ stores it in a 'periods' variable
+*/
+
+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,
+ range: 1,
+});
+
+// chart.onReplayLoaded(() => {
+// console.log('STARTING REPLAY MODE');
+// chart.replayStart(1000);
+
+// // setTimeout(() => {
+// // console.log('STOPPING REPLAY MODE');
+// // chart.replayStop();
+// // }, 7000);
+// });
+
+let loading = 0;
+const indicators = [];
+const periods = {};
+
+let interval = NaN;
+
+async function step() {
+ const period = { ...chart.periods[0] };
+
+ const times = Object.keys(periods);
+ const intrval = times[times.length - 1] - times[times.length - 2];
+ if (Number.isNaN(interval) && times.length >= 2) interval = intrval;
+
+ if (!Number.isNaN(interval) && interval !== intrval) {
+ throw new Error(`Wrong interval: ${intrval} (should be ${interval})`);
+ }
+
+ indicators.forEach(([n, i]) => {
+ const plots = { ...i.periods[0] };
+ delete plots.$time;
+ period[n] = { plots };
+
+ Object.keys(i.graphic).forEach((g) => {
+ if (!i.graphic[g].length) return;
+ if (!period[n].graphics) period[n].graphics = {};
+ period[n].graphics[g] = i.graphic[g];
+ });
+ });
+
+ periods[period.time] = period;
+
+ console.log('Next ->', period.time, times.length);
+
+ await chart.replayStep(1);
+ step();
+}
+
+chart.onReplayEnd(async () => {
+ await client.end();
+ console.log('Done !', Object.keys(periods).length);
+});
+
+async function addIndicator(name, pineId, options = {}) {
+ loading += 1;
+
+ const indic = pineId.includes('@')
+ ? new TradingView.BuiltInIndicator(pineId)
+ : await TradingView.getIndicator(pineId);
+ Object.keys(options).forEach((o) => { indic.setOption(o, options[o]); });
+
+ const std = new chart.Study(indic);
+
+ std.onReady(() => {
+ indicators.push([name, std]);
+ if (loading === indicators.length) step();
+ });
+}
+
+addIndicator('Volume', 'Volume@tv-basicstudies-144');
+addIndicator('EMA_50', 'STD;EMA', { Length: 50 });
+addIndicator('EMA_200', 'STD;EMA', { Length: 200 });
diff --git a/src/chart/session.js b/src/chart/session.js
index eaea953..155c1c1 100644
--- a/src/chart/session.js
+++ b/src/chart/session.js
@@ -113,7 +113,14 @@ const ChartTypes = {
* @param {import('../client').ClientBridge} client
*/
module.exports = (client) => class ChartSession {
- #sessionID = genSessionID('cs');
+ #chartSessionID = genSessionID('cs');
+
+ #replaySessionID = genSessionID('rs');
+
+ #replayMode = false;
+
+ /** @type {Object<string, (): any>} */
+ #replayOKCB = {}
/** Parent client */
#client = client;
@@ -148,6 +155,11 @@ module.exports = (client) => class ChartSession {
symbolLoaded: [],
update: [],
+ replayLoaded: [],
+ replayPoint: [],
+ replayResolution: [],
+ replayEnd: [],
+
event: [],
error: [],
};
@@ -167,7 +179,7 @@ module.exports = (client) => class ChartSession {
}
constructor() {
- this.#client.sessions[this.#sessionID] = {
+ this.#client.sessions[this.#chartSessionID] = {
type: 'chart',
onData: (packet) => {
if (global.TW_DEBUG) console.log('§90§30§106 CHART SESSION §0 DATA', packet);
@@ -188,7 +200,10 @@ module.exports = (client) => class ChartSession {
}
if (['timescale_update', 'du'].includes(packet.type)) {
+ const changes = [];
+
Object.keys(packet.data[1]).forEach((k) => {
+ changes.push(k);
if (k === '$prices') {
const periods = packet.data[1].$prices;
if (!periods || !periods.s) return;
@@ -211,7 +226,7 @@ module.exports = (client) => class ChartSession {
if (this.#studyListeners[k]) this.#studyListeners[k](packet);
});
- this.#handleEvent('update');
+ this.#handleEvent('update', changes);
return;
}
@@ -232,7 +247,47 @@ module.exports = (client) => class ChartSession {
},
};
- this.#client.send('chart_create_session', [this.#sessionID]);
+ this.#client.sessions[this.#replaySessionID] = {
+ type: 'replay',
+ onData: (packet) => {
+ if (global.TW_DEBUG) console.log('§90§30§106 REPLAY SESSION §0 DATA', packet);
+
+ if (packet.type === 'replay_ok') {
+ if (this.#replayOKCB[packet.data[1]]) {
+ this.#replayOKCB[packet.data[1]]();
+ delete this.#replayOKCB[packet.data[1]];
+ }
+ return;
+ }
+
+ if (packet.type === 'replay_instance_id') {
+ this.#handleEvent('replayLoaded', packet.data[1]);
+ return;
+ }
+
+ if (packet.type === 'replay_point') {
+ this.#handleEvent('replayPoint', packet.data[1]);
+ return;
+ }
+
+ if (packet.type === 'replay_resolutions') {
+ this.#handleEvent('replayResolution', packet.data[1], packet.data[2]);
+ return;
+ }
+
+ if (packet.type === 'replay_data_end') {
+ this.#handleEvent('replayEnd');
+ return;
+ }
+
+ if (packet.type === 'critical_error') {
+ const [, name, description] = packet.data;
+ this.#handleError('Critical error:', name, description);
+ }
+ },
+ };
+
+ this.#client.send('chart_create_session', [this.#chartSessionID]);
}
#seriesCreated = false;
@@ -255,7 +310,7 @@ module.exports = (client) => class ChartSession {
this.#periods = {};
this.#client.send(`${this.#seriesCreated ? 'modify' : 'create'}_series`, [
- this.#sessionID,
+ this.#chartSessionID,
'$prices',
's1',
`ser_${this.#currentSeries}`,
@@ -278,31 +333,56 @@ module.exports = (client) => class ChartSession {
* @param {'EUR' | 'USD' | string} [options.currency] Chart currency
* @param {ChartType} [options.type] Chart custom type
* @param {ChartInputs} [options.inputs] Chart custom inputs
+ * @param {number} [options.replay] Replay mode starting point (Timestamp)
*/
setMarket(symbol, options = {}) {
this.#periods = {};
+ if (this.#replayMode) {
+ this.#replayMode = false;
+ this.#client.send('replay_delete_session', [this.#replaySessionID]);
+ }
+
const symbolInit = {
symbol: symbol || 'BTCEUR',
adjustment: options.adjustment || 'splits',
};
if (options.session) symbolInit.session = options.session;
-
if (options.currency) symbolInit['currency-id'] = options.currency;
- const chartInit = (options.type && ChartTypes[options.type]) ? {} : symbolInit;
+ if (options.replay) {
+ this.#replayMode = true;
+ this.#client.send('replay_create_session', [this.#replaySessionID]);
+
+ this.#client.send('replay_add_series', [
+ this.#replaySessionID,
+ 'req_replay_addseries',
+ `=${JSON.stringify(symbolInit)}`,
+ options.timeframe,
+ ]);
+
+ this.#client.send('replay_reset', [
+ this.#replaySessionID,
+ 'req_replay_reset',
+ options.replay,
+ ]);
+ }
+
+ const complex = (options.type && ChartTypes[options.type]) || options.replay;
+ const chartInit = complex ? {} : symbolInit;
- if (options.type && ChartTypes[options.type]) {
+ if (complex) {
+ if (options.replay) chartInit.replay = this.#replaySessionID;
chartInit.symbol = symbolInit;
- chartInit.type = ChartTypes[options.type];
- chartInit.inputs = { ...options.inputs };
+ if (options.type) chartInit.type = ChartTypes[options.type];
+ if (options.inputs) chartInit.inputs = { ...options.inputs };
}
this.#currentSeries += 1;
this.#client.send('resolve_symbol', [
- this.#sessionID,
+ this.#chartSessionID,
`ser_${this.#currentSeries}`,
`=${JSON.stringify(chartInit)}`,
]);
@@ -316,7 +396,7 @@ module.exports = (client) => class ChartSession {
*/
setTimezone(timezone) {
this.#periods = {};
- this.#client.send('switch_timezone', [this.#sessionID, timezone]);
+ this.#client.send('switch_timezone', [this.#chartSessionID, timezone]);
}
/**
@@ -324,7 +404,60 @@ module.exports = (client) => class ChartSession {
* @param {number} number Number of additional periods/candles you want to fetch
*/
fetchMore(number = 1) {
- this.#client.send('request_more_data', [this.#sessionID, '$prices', number]);
+ this.#client.send('request_more_data', [this.#chartSessionID, '$prices', number]);
+ }
+
+ /**
+ * Fetch x additional previous periods/candles values
+ * @param {number} number Number of additional periods/candles you want to fetch
+ * @returns {Promise} Raise when the data has been fetched
+ */
+ replayStep(number = 1) {
+ return new Promise((cb) => {
+ if (!this.#replayMode) {
+ this.#handleError('No replay session');
+ return;
+ }
+
+ const reqID = genSessionID('rsq_step');
+ this.#client.send('replay_step', [this.#replaySessionID, reqID, number]);
+ this.#replayOKCB[reqID] = () => { cb(); };
+ });
+ }
+
+ /**
+ * Start fetching a new period/candle every x ms
+ * @param {number} interval Number of additional periods/candles you want to fetch
+ * @returns {Promise} Raise when the replay mode starts
+ */
+ replayStart(interval = 1000) {
+ return new Promise((cb) => {
+ if (!this.#replayMode) {
+ this.#handleError('No replay session');
+ return;
+ }
+
+ const reqID = genSessionID('rsq_start');
+ this.#client.send('replay_start', [this.#replaySessionID, reqID, interval]);
+ this.#replayOKCB[reqID] = () => { cb(); };
+ });
+ }
+
+ /**
+ * Stop fetching a new period/candle every x ms
+ * @returns {Promise} Raise when the replay mode stops
+ */
+ replayStop() {
+ return new Promise((cb) => {
+ if (!this.#replayMode) {
+ this.#handleError('No replay session');
+ return;
+ }
+
+ const reqID = genSessionID('rsq_stop');
+ this.#client.send('replay_stop', [this.#replaySessionID, reqID]);
+ this.#replayOKCB[reqID] = () => { cb(); };
+ });
}
/**
@@ -338,13 +471,52 @@ module.exports = (client) => class ChartSession {
/**
* When a chart update happens
- * @param {() => void} cb
+ * @param {(changes: ('$prices' | string)[]) => void} cb
* @event
*/
onUpdate(cb) {
this.#callbacks.update.push(cb);
}
+ /**
+ * When the replay session is ready
+ * @param {() => void} cb
+ * @event
+ */
+ onReplayLoaded(cb) {
+ this.#callbacks.replayLoaded.push(cb);
+ }
+
+ /**
+ * When the replay session has new resolution
+ * @param {(
+ * timeframe: import('../types').TimeFrame,
+ * index: number,
+ * ) => void} cb
+ * @event
+ */
+ onReplayResolution(cb) {
+ this.#callbacks.replayResolution.push(cb);
+ }
+
+ /**
+ * When the replay session ends
+ * @param {() => void} cb
+ * @event
+ */
+ onReplayEnd(cb) {
+ this.#callbacks.replayEnd.push(cb);
+ }
+
+ /**
+ * When the replay session cursor has moved
+ * @param {(index: number) => void} cb
+ * @event
+ */
+ onReplayPoint(cb) {
+ this.#callbacks.replayPoint.push(cb);
+ }
+
/**
* When chart error happens
* @param {(...any) => void} cb Callback
@@ -356,7 +528,7 @@ module.exports = (client) => class ChartSession {
/** @type {ChartSessionBridge} */
#chartSession = {
- sessionID: this.#sessionID,
+ sessionID: this.#chartSessionID,
studyListeners: this.#studyListeners,
indexes: {},
send: (t, p) => this.#client.send(t, p),
@@ -366,7 +538,9 @@ module.exports = (client) => class ChartSession {
/** Delete the chart session */
delete() {
- this.#client.send('quote_delete_session', [this.#sessionID]);
- delete this.#client.sessions[this.#sessionID];
+ if (this.#replayMode) this.#client.send('replay_delete_session', [this.#replaySessionID]);
+ this.#client.send('chart_delete_session', [this.#chartSessionID]);
+ delete this.#client.sessions[this.#chartSessionID];
+ this.#replayMode = false;
}
};