Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Establishment of "graphicsCmds" parsing

Mathieu Colmon08efb508fcef
diff --git a/examples/SpecialIndicator.js b/examples/SpecialIndicator.js
new file mode 100644
index 0000000..334f7a0
--- /dev/null
+++ b/examples/SpecialIndicator.js
@@ -0,0 +1,43 @@
+const TradingView = require('../main');
+
+/*
+ This example tests an indicator that sends
+ its data in a particular way such as
+ 'lines', 'labels', 'boxes' and 'tables'
+*/
+
+const client = new TradingView.Client();
+
+const chart = new client.Session.Chart();
+chart.setMarket('BINANCE:BTCEUR', {
+ timeframe: 'W',
+});
+
+// TradingView.getIndicator('PUB;5xi4DbWeuIQrU0Fx6ZKiI2odDvIW9q2j').then((indic) => {
+TradingView.getIndicator('USER;8bbd8017fd3e4881bf91f4fea5e3d538').then((indic) => {
+ const STD = new chart.Study(indic);
+
+ STD.onError((...err) => {
+ console.log('Chart error:', ...err);
+ });
+
+ STD.onReady(() => {
+ console.log('STD Loaded !');
+ });
+
+ STD.onUpdate((changes) => {
+ STD.graphic;
+ console.log('Update:', changes);
+ });
+});
+
+setInterval(() => {
+ chart.fetchMore(100);
+}, 2000);
+
+setTimeout(() => {
+ console.log('Setting timeframe to: \'D\'');
+ chart.setSeries('D');
+}, 5000);
+
+chart.onUpdate(() => console.log(chart.periods.length));
diff --git a/src/chart/graphicParser.js b/src/chart/graphicParser.js
new file mode 100644
index 0000000..df0287d
--- /dev/null
+++ b/src/chart/graphicParser.js
@@ -0,0 +1,55 @@
+/**
+ * @typedef {Object} GraphicLabel
+ * @prop {number} id Drawing ID
+ */
+
+/**
+ * @typedef {Object} GraphicLine
+ * @prop {number} id Drawing ID
+ */
+
+/**
+ * @typedef {Object} GraphicBox
+ * @prop {number} id Drawing ID
+ */
+
+/**
+ * @typedef {Object} GraphicTable
+ * @prop {number} id Drawing ID
+ */
+
+/**
+ * @typedef {Object} GraphicData List of drawings indexed by type
+ * @prop {GraphicLabel[]} labels List of labels drawings
+ * @prop {GraphicLine[]} lines List of lines drawings
+ * @prop {GraphicBox[]} boxes List of boxes drawings
+ * @prop {GraphicTable[]} tables List of tables drawings
+ */
+
+/**
+ * @param {Object} rawGraphic Raw graphic data
+ * @param {Object} indexes Drawings xPos indexes
+ * @returns {GraphicData}
+ */
+module.exports = function graphicParse(rawGraphic = {}, indexes = []) {
+ // console.log(rawGraphic, indexes);
+ return {
+ labels: Object.values(rawGraphic.dwglabels ?? {}).map((l) => ({
+ ...l,
+ })),
+
+ lines: Object.values(rawGraphic.dwglines ?? {}).map((l) => ({
+ ...l,
+ })),
+
+ boxes: Object.values(rawGraphic.dwgboxes ?? {}).map((b) => ({
+ ...b,
+ })),
+
+ tables: Object.values(rawGraphic.dwgtables ?? {}).map((t) => ({
+ ...t,
+ })),
+
+ raw: rawGraphic,
+ };
+};
diff --git a/src/chart/session.js b/src/chart/session.js
index 6fc439a..60419b8 100644
--- a/src/chart/session.js
+++ b/src/chart/session.js
@@ -38,6 +38,7 @@ const ChartTypes = {
* @typedef {Object} ChartSessionBridge
* @prop {string} sessionID
* @prop {StudyListeners} studyListeners
+ * @prop {Object<number, number>} indexes
* @prop {import('../client').SendPacket} send
*/
@@ -192,6 +193,7 @@ module.exports = (client) => class ChartSession {
if (!periods || !periods.s) return;
periods.s.forEach((p) => {
+ [this.#chartSession.indexes[p.i]] = p.v;
this.#periods[p.v[0]] = {
time: p.v[0],
open: p.v[1],
@@ -351,6 +353,7 @@ module.exports = (client) => class ChartSession {
#chartSession = {
sessionID: this.#sessionID,
studyListeners: this.#studyListeners,
+ indexes: {},
send: (t, p) => this.#client.send(t, p),
};
diff --git a/src/chart/study.js b/src/chart/study.js
index 15763a3..0950a52 100644
--- a/src/chart/study.js
+++ b/src/chart/study.js
@@ -1,5 +1,6 @@
const { genSessionID } = require('../utils');
const { parseCompressed } = require('../protocol');
+const graphicParser = require('./graphicParser');
const PineIndicator = require('../classes/PineIndicator');
@@ -124,6 +125,33 @@ module.exports = (chartSession) => class ChartStudy {
return Object.values(this.#periods).sort((a, b) => b.$time - a.$time);
}
+ /**
+ * List of graphic xPos indexes
+ * @type {number[]}
+ */
+ #indexes = [];
+
+ /**
+ * Table of graphic drawings indexed by type and ID
+ * @type {Object<string, Object<number, {}>>}
+ */
+ #graphic = {};
+
+ /**
+ * Table of graphic drawings indexed by type
+ * @return {import('./graphicParser').GraphicData}
+ */
+ get graphic() {
+ const translator = {};
+
+ Object.keys(chartSession.indexes)
+ .sort((a, b) => chartSession.indexes[b] - chartSession.indexes[a])
+ .forEach((r, n) => { translator[r] = n; });
+
+ const indexes = this.#indexes.map((i) => translator[i]);
+ return graphicParser(this.#graphic, indexes);
+ }
+
/** @type {StrategyReport} */
#strategyReport = {
trades: [],
@@ -203,6 +231,43 @@ module.exports = (chartSession) => class ChartStudy {
if (data.ns && data.ns.d) {
const parsed = JSON.parse(data.ns.d);
+ if (parsed.graphicsCmds) {
+ if (parsed.graphicsCmds.erase) {
+ parsed.graphicsCmds.erase.forEach((instruction) => {
+ console.log('Erase', instruction);
+ if (instruction.action === 'all') {
+ if (!instruction.type) {
+ Object.keys(this.#graphic).forEach((drawType) => {
+ this.#graphic[drawType] = {};
+ });
+ } else this.#graphic[instruction.type] = {};
+ return;
+ }
+
+ if (instruction.action === 'one') {
+ this.#graphic[instruction.type][instruction.id] = {};
+ }
+ // Can an 'instruction' contains other things ?
+ });
+ }
+
+ if (parsed.graphicsCmds.create) {
+ Object.keys(parsed.graphicsCmds.create).forEach((drawType) => {
+ if (!this.#graphic[drawType]) this.#graphic[drawType] = {};
+ parsed.graphicsCmds.create[drawType].forEach((group) => {
+ group.data.forEach((item) => {
+ this.#graphic[drawType][item.id] = item;
+ });
+ });
+ });
+ }
+
+ console.log('graphicsCmds', Object.keys(parsed.graphicsCmds));
+ // Can 'graphicsCmds' contains other things ?
+
+ changes.push('graphic');
+ }
+
if (parsed.data && parsed.data.report && parsed.data.report.performance) {
this.#strategyReport.performance = parsed.data.report.performance;
changes.push('perfReport');
@@ -249,6 +314,10 @@ module.exports = (chartSession) => class ChartStudy {
}
}
+ if (data.ns.indexes && typeof data.ns.indexes === 'object') {
+ this.#indexes = data.ns.indexes;
+ }
+
this.#handleEvent('update', changes);
return;
}