Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Better strategy parsing

Mathieu Colmon9451ffff5b03
diff --git a/package.json b/package.json
index 4a6e462..3479372 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@mathieuc/tradingview",
- "version": "3.3.2",
+ "version": "3.3.3",
"description": "Tradingview instant stocks API, indicator alerts, trading bot, and more !",
"main": "main.js",
"scripts": {
diff --git a/src/chart/study.js b/src/chart/study.js
index b127a43..b931792 100644
--- a/src/chart/study.js
+++ b/src/chart/study.js
@@ -32,6 +32,27 @@ function getInputs(options) {
return options.options;
}
+const parseTrades = (trades) => trades.reverse().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,
+}));
+
+// const historyParser = (history) => history.reverse().map((h) => ({
+
/**
* @typedef {Object} TradeReport Trade report
@@ -86,9 +107,19 @@ function getInputs(options) {
* @prop {number} totalTrades Total trades
*/
+/**
+ * @typedef {Object} FromTo
+ * @prop {number} from From timestamp
+ * @prop {number} to To timestamp
+ */
+
/**
* @typedef {Object} StrategyReport
* @prop {'EUR' | 'USD' | 'JPY' | '' | 'CHF'} [currency] Selected currency
+ * @prop {Object} [settings] Backtester settings
+ * @prop {Object} [settings.dateRange] Backtester date range
+ * @prop {FromTo} [settings.dateRange.backtest] Date range for backtest
+ * @prop {FromTo} [settings.dateRange.trade] Date range for trade
* @prop {TradeReport[]} trades Trade list starting by the last
* @prop {Object} history History Chart value
* @prop {number[]} [history.buyHold] Buy hold values
@@ -275,60 +306,45 @@ module.exports = (chartSession) => class ChartStudy {
changes.push('graphic');
}
- if (parsed.data && parsed.data.report && parsed.data.report.performance) {
- this.#strategyReport.performance = parsed.data.report.performance;
- changes.push('perfReport');
- }
+ const updateStrategyReport = (report) => {
+ if (report.currency) {
+ this.#strategyReport.currency = report.currency;
+ changes.push('report.currency');
+ }
- if (parsed.data && parsed.data.report && parsed.data.report.trades) {
- this.#strategyReport.trades = parsed.data.report.trades;
- changes.push('tradesReport');
- }
+ if (report.settings) {
+ this.#strategyReport.settings = report.settings;
+ changes.push('report.settings');
+ }
- if (parsed.data && parsed.data.report && parsed.data.report.history) {
- this.#strategyReport.history = parsed.data.report.history;
- changes.push('historyReport');
- }
+ if (report.performance) {
+ this.#strategyReport.performance = report.performance;
+ changes.push('report.perf');
+ }
+
+ if (report.trades) {
+ this.#strategyReport.trades = parseTrades(report.trades);
+ changes.push('report.trades');
+ }
+
+ if (report.equity) {
+ this.#strategyReport.history = {
+ buyHold: report.buyHold,
+ buyHoldPercent: report.buyHoldPercent,
+ drawDown: report.drawDown,
+ drawDownPercent: report.drawDownPercent,
+ equity: report.equity,
+ equityPercent: report.equityPercent,
+ };
+ changes.push('report.history');
+ }
+ };
if (parsed.dataCompressed) {
- const parsedC = await parseCompressed(parsed.dataCompressed);
-
- this.#strategyReport = {
- currency: parsedC.report.currency,
-
- trades: parsedC.report.trades.reverse().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: parsedC.report.buyHold,
- buyHoldPercent: parsedC.report.buyHoldPercent,
- drawDown: parsedC.report.drawDown,
- drawDownPercent: parsedC.report.drawDownPercent,
- equity: parsedC.report.equity,
- equityPercent: parsedC.report.equityPercent,
- },
-
- performance: parsedC.report.performance,
- };
-
- changes.push('fullReport');
+ updateStrategyReport((await parseCompressed(parsed.dataCompressed)).report);
}
+
+ if (parsed.data && parsed.data.report) updateStrategyReport(parsed.data.report);
}
if (data.ns.indexes && typeof data.ns.indexes === 'object') {
@@ -383,8 +399,9 @@ module.exports = (chartSession) => class ChartStudy {
}
/**
- * @typedef {
- * 'plots' | 'perfReport' | 'tradesReport' | 'historyReport' | 'fullReport'
+ * @typedef {'plots' | 'report.currency'
+ * | 'report.settings' | 'report.perf'
+ * | 'report.trades' | 'report.history'
* } UpdateChangeType
*/