Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Add FromTo fetching + Better naming

Mathieu Colmone17b61de523c
diff --git a/examples/FromToData.js b/examples/FromToData.js
new file mode 100644
index 0000000..af47728
--- /dev/null
+++ b/examples/FromToData.js
@@ -0,0 +1,29 @@
+const TradingView = require('../main');
+
+/*
+ This example tests fetching chart
+ data of a number of candles before
+ or after a timestamp
+*/
+
+const client = new TradingView.Client();
+
+const chart = new client.Session.Chart();
+chart.setMarket('BINANCE:BTCEUR', {
+ timeframe: '240',
+ range: 2, // Can be positive to get before or negative to get after
+ to: 1600000000,
+});
+
+// This works with indicators
+
+TradingView.getIndicator('STD;Supertrend').then(async (indic) => {
+ console.log(`Loading '${indic.description}' study...`);
+ const SUPERTREND = new chart.Study(indic);
+
+ SUPERTREND.onUpdate(() => {
+ console.log('Prices periods:', chart.periods);
+ console.log('Study periods:', SUPERTREND.periods);
+ client.end();
+ });
+});
diff --git a/examples/GraphicIndicator.js b/examples/GraphicIndicator.js
index cefae54..303d64d 100644
--- a/examples/GraphicIndicator.js
+++ b/examples/GraphicIndicator.js
@@ -10,23 +10,25 @@ const client = new TradingView.Client();
const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
- timeframe: 'W',
+ timeframe: '5',
+ range: 10000,
});
// TradingView.getIndicator('USER;01efac32df544348810bc843a7515f36').then((indic) => {
-TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
+// TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
+TradingView.getIndicator('STD;Zig_Zag').then((indic) => {
const STD = new chart.Study(indic);
STD.onError((...err) => {
- console.log('Chart error:', ...err);
+ console.log('Study error:', ...err);
});
STD.onReady(() => {
- console.log('STD Loaded !');
+ console.log(`STD '${STD.instance.description}' Loaded !`);
});
STD.onUpdate(() => {
- console.log(STD.graphic);
+ console.log('Graphic data:', STD.graphic);
// console.log('Tables:', changes, STD.graphic.tables);
// console.log('Cells', STD.graphic.tables[0].cells());
client.end();
diff --git a/src/chart/session.js b/src/chart/session.js
index 60419b8..d042ded 100644
--- a/src/chart/session.js
+++ b/src/chart/session.js
@@ -53,7 +53,7 @@ const ChartTypes = {
* @prop {number} close Period close value
* @prop {number} max Period max value
* @prop {number} min Period min value
- * @prop {number} change Period change absolute value
+ * @prop {number} volume Period volume value
*/
/**
@@ -91,7 +91,7 @@ const ChartTypes = {
* @prop {number} minmove2 Minimum move value 2
* @prop {string} timezone Used timezone
* @prop {boolean} is_replayable If the replay mode is available
- * @prop {boolean} has_adjustment If the adjustment mode is enabled ????
+ * @prop {boolean} has_adjustment If the adjustment mode is enabled
* @prop {boolean} has_extended_hours Has extended hours
* @prop {string} bar_source Bar source
* @prop {string} bar_transform Bar transform
@@ -200,7 +200,7 @@ module.exports = (client) => class ChartSession {
close: p.v[4],
max: p.v[2],
min: p.v[3],
- change: Math.round(p.v[5] * 100) / 100,
+ volume: Math.round(p.v[5] * 100) / 100,
};
});
@@ -241,13 +241,16 @@ module.exports = (client) => class ChartSession {
/**
* @param {import('../types').TimeFrame} timeframe Chart period timeframe
* @param {number} [range] Number of loaded periods/candles (Default: 100)
+ * @param {number} [reference] Reference candle timestamp (Default is now)
*/
- setSeries(timeframe = '240', range = 100) {
+ setSeries(timeframe = '240', range = 100, reference = null) {
if (!this.#currentSeries) {
this.#handleError('Please set the market before setting series');
return;
}
+ const calcRange = !reference ? range : ['bar_count', reference, range];
+
this.#periods = {};
this.#client.send(`${this.#seriesCreated ? 'modify' : 'create'}_series`, [
@@ -256,7 +259,7 @@ module.exports = (client) => class ChartSession {
's1',
`ser_${this.#currentSeries}`,
timeframe,
- this.#seriesCreated ? '' : range,
+ this.#seriesCreated ? '' : calcRange,
]);
this.#seriesCreated = true;
@@ -268,6 +271,7 @@ module.exports = (client) => class ChartSession {
* @param {Object} [options] Chart options
* @param {import('../types').TimeFrame} [options.timeframe] Chart period timeframe
* @param {number} [options.range] Number of loaded periods/candles (Default: 100)
+ * @param {number} [options.to] Last candle timestamp (Default is now)
* @param {'splits' | 'dividends'} [options.adjustment] Market adjustment
* @param {'regular' | 'extended'} [options.session] Chart session
* @param {'EUR' | 'USD' | string} [options.currency] Chart currency
@@ -302,7 +306,7 @@ module.exports = (client) => class ChartSession {
`=${JSON.stringify(chartInit)}`,
]);
- this.setSeries(options.timeframe, options.range);
+ this.setSeries(options.timeframe, options.range, options.to);
}
/**