Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Support of volume-based built-in indicators

Mathieu Colmon663a6c38c71a
diff --git a/README.md b/README.md
index 2ef8b8a..bf3545a 100644
--- a/README.md
+++ b/README.md
@@ -2,6 +2,7 @@
Get realtime market prices and indicator values from Tradingview !
## Features
+- [x] Premium features
- [x] Automatically backtest many strategies and try many settings in a very little time
- [x] Get drawings you made on your chart
- [x] Works with invite-only indicators
@@ -14,7 +15,7 @@ Get realtime market prices and indicator values from Tradingview !
- [ ] Get Screener top values
- [ ] Get Hotlists
- [ ] Get Calendar
-- IF YOU WANT A FEATURE, ASK ME !!
+- IF YOU WANT A FEATURE, ASK ME !
## Possibilities
- Trading bot
@@ -30,102 +31,9 @@ ___
npm i @mathieuc/tradingview
```
-## Examples (./tests/)
+## Examples
+You can find all the examples and snippets in `./examples` folder.
-```javascript
-/*
- ./tests/prices.js
- Search for Bitcoin and Ethereum and get real time prices
-*/
-
-const marketAPI = require('tradingview');
-
-(async () => {
- const market = marketAPI();
-
- market.on('logged', async () => {
- console.log('API LOGGED');
-
- const searchBTC = (await market.search('bitcoin euro', 'crypto'))[0];
- console.log('Found Bitcoin / Euro:', searchBTC);
- market.subscribe(searchBTC.id);
- });
-
- market.on('price', (data) => {
- console.log(data.symbol, '=>', data.price);
- });
-
- const searchETH = (await market.search('ethereum euro', 'crypto'))[0];
- console.log('Found Ethereum / Euro:', searchETH);
-
- setTimeout(() => {
- console.log('Subscribe to', searchETH.id);
- market.subscribe(searchETH.id);
- }, 10000);
-
- setTimeout(() => {
- console.log('Unsubscribe from', searchETH.id);
- market.unsubscribe(searchETH.id);
- }, 20000);
-})();
-```
-
-```javascript
-/*
- ./tests/analysis.js
- Search for Bitcoin and get the Technical Analysis in all timeframes
-*/
-
-const marketAPI = require('tradingview');
-
-(async () => {
- const market = marketAPI(false);
-
- const searchBTC = (await market.search('bitcoin euro', 'crypto'))[0];
- console.log('Found Bitcoin / Euro:', searchBTC);
-
- const TA = await searchBTC.getTA();
- console.log('Full technical analysis for Bitcoin:', TA);
-
- // You can also use this way: await market.getTA('crypto', 'BINANCE:BTCEUR');
-})();
-```
-
-```javascript
-/*
- ./tests/indicator.js
- Get indicator values
-*/
-
-const marketAPI = require('tradingview');
-
-const market = marketAPI(false); // 'false' for chart-only mode
-
-market.on('logged', () => {
- market.initChart({
- symbol: 'COINBASE:BTCEUR',
- period: '240',
- range: 50,
- indicators: [
- { name: 'ACCU_DISTRIB', id: 'STD;Accumulation_Distribution', version: '25' },
- { name: 'CIPHER_A', id: 'PUB;vrOJcNRPULteowIsuP6iHn3GIxBJdXwT', version: '1.0' },
- { name: 'CIPHER_B', id: 'PUB;uA35GeckoTA2EfgI63SD2WCSmca4njxp', version: '15.0' },
- // Color Changing moving average
- { name: 'CCMA', id: 'PUB;5nawr3gCESvSHQfOhrLPqQqT4zM23w3X', version: '6.0' },
- ],
- }, (periods) => {
- if (!periods[0].CIPHER_B) return;
- if (!periods[0].CCMA) return;
-
- console.log('Last period:', {
- price: periods[0].$prices.close,
- moneyFlow: (periods[0].CIPHER_B.RSIMFIArea >= 0) ? 'POSITIVE' : 'NEGATIVE',
- VWAP: periods[0].CIPHER_B.VWAP,
- MA: (periods[0].CCMA.Plot <= periods[0].$prices.close) ? 'ABOVE' : 'UNDER',
- });
- });
-});
-```
___
## Problems
If you have errors in console or unwanted behavior,
diff --git a/examples/BuiltInIndicator.js b/examples/BuiltInIndicator.js
index b862cd0..75819d6 100644
--- a/examples/BuiltInIndicator.js
+++ b/examples/BuiltInIndicator.js
@@ -14,12 +14,28 @@ const client = new TradingView.Client({
const chart = new client.Session.Chart();
chart.setMarket('BINANCE:BTCEUR', {
timeframe: '60',
+ range: 1,
});
const volumeProfile = new TradingView.BuiltInIndicator('VbPSessions@tv-volumebyprice-53');
+/* Required for other volume-based built-in indicators */
+// volumeProfile.setOption('first_bar_time', 1639080000000);
+// volumeProfile.setOption('last_bar_time', 1639328400000);
+// volumeProfile.setOption('first_visible_bar_time', 1639080000000);
+// volumeProfile.setOption('last_visible_bar_time', 1639328400000);
+
const VOL = new chart.Study(volumeProfile);
VOL.onUpdate(() => {
- console.log((VOL.graphic.horizlines));
+ VOL.graphic.hists
+ .filter((h) => h.lastBarTime === 0) // We only keep recent volume infos
+ .sort((a, b) => b.priceHigh - a.priceHigh)
+ .forEach((h) => {
+ console.log(
+ `~ ${Math.round((h.priceHigh + h.priceLow) / 2)} € :`,
+ `${'_'.repeat(h.rate[0] / 3)}${'_'.repeat(h.rate[1] / 3)}`,
+ );
+ });
+
client.end();
});
diff --git a/src/chart/graphicParser.js b/src/chart/graphicParser.js
index 58c10dd..7a09e31 100644
--- a/src/chart/graphicParser.js
+++ b/src/chart/graphicParser.js
@@ -18,6 +18,38 @@
* @prop {number} id Drawing ID
*/
+/**
+ * @typedef {Object} GraphicHorizline
+ * @prop {number} id Drawing ID
+ * @prop {number} level Y position of the line
+ * @prop {number} startIndex Start index of the line (`chart.periods[line.startIndex]`)
+ * @prop {number} endIndex End index of the line (`chart.periods[line.endIndex]`)
+ * @prop {boolean} extendRight Is the line extended to the right
+ * @prop {boolean} extendLeft Is the line extended to the left
+ */
+
+/**
+ * @typedef {Object} GraphicPoint
+ * @prop {number} index X position of the point
+ * @prop {number} level Y position of the point
+ */
+
+/**
+ * @typedef {Object} GraphicPolygon
+ * @prop {number} id Drawing ID
+ * @prop {GraphicPoint[]} points List of polygon points
+ */
+
+/**
+ * @typedef {Object} GraphicHist
+ * @prop {number} id Drawing ID
+ * @prop {number} priceLow Low Y position
+ * @prop {number} priceHigh High Y position
+ * @prop {number} firstBarTime First X position
+ * @prop {number} lastBarTime Last X position
+ * @prop {number[]} rate List of values
+ */
+
/**
* @typedef {Object} GraphicData List of drawings indexed by type
* @prop {GraphicLabel[]} labels List of labels drawings
@@ -35,7 +67,7 @@
* @returns {GraphicData}
*/
module.exports = function graphicParse(rawGraphic = {}, indexes = []) {
- // console.log(rawGraphic, indexes);
+ // console.log('indexes', indexes);
return {
labels: Object.values(rawGraphic.dwglabels ?? {}).map((l) => ({
...l,
@@ -55,14 +87,22 @@ module.exports = function graphicParse(rawGraphic = {}, indexes = []) {
horizlines: Object.values(rawGraphic.horizlines ?? {}).map((h) => ({
...h,
+ startIndex: indexes[h.startIndex],
+ endIndex: indexes[h.endIndex],
})),
polygons: Object.values(rawGraphic.polygons ?? {}).map((p) => ({
...p,
+ points: p.points.map((pt) => ({
+ ...pt,
+ index: indexes[pt.index],
+ })),
})),
hists: Object.values(rawGraphic.hhists ?? {}).map((h) => ({
...h,
+ firstBarTime: indexes[h.firstBarTime],
+ lastBarTime: indexes[h.lastBarTime],
})),
raw: rawGraphic,
diff --git a/src/chart/study.js b/src/chart/study.js
index 8894c06..f993c19 100644
--- a/src/chart/study.js
+++ b/src/chart/study.js
@@ -247,12 +247,12 @@ module.exports = (chartSession) => class ChartStudy {
Object.keys(this.#graphic).forEach((drawType) => {
this.#graphic[drawType] = {};
});
- } else this.#graphic[instruction.type] = {};
+ } else delete this.#graphic[instruction.type];
return;
}
if (instruction.action === 'one') {
- this.#graphic[instruction.type][instruction.id] = {};
+ delete this.#graphic[instruction.type][instruction.id];
}
// Can an 'instruction' contains other things ?
});
@@ -269,7 +269,7 @@ module.exports = (chartSession) => class ChartStudy {
});
}
- console.log('graphicsCmds', Object.keys(parsed.graphicsCmds));
+ // console.log('graphicsCmds', Object.keys(parsed.graphicsCmds));
// Can 'graphicsCmds' contains other things ?
changes.push('graphic');
diff --git a/src/miscRequests.js b/src/miscRequests.js
index 01d430e..5f18c2c 100644
--- a/src/miscRequests.js
+++ b/src/miscRequests.js
@@ -328,8 +328,8 @@ module.exports = {
});
return new PineIndicator({
- pineId: indicID,
- pineVersion: version,
+ pineId: data.result.metaInfo.scriptIdPart || indicID,
+ pineVersion: data.result.metaInfo.pine.version || version,
description: data.result.metaInfo.description,
shortDescription: data.result.metaInfo.shortDescription,
inputs,