Skip to content

Commit 9451fff

Browse files
committed
Better strategy parsing
1 parent 5921da0 commit 9451fff

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mathieuc/tradingview",
3-
"version": "3.3.2",
3+
"version": "3.3.3",
44
"description": "Tradingview instant stocks API, indicator alerts, trading bot, and more !",
55
"main": "main.js",
66
"scripts": {

src/chart/study.js

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ function getInputs(options) {
3232
return options.options;
3333
}
3434

35+
const parseTrades = (trades) => trades.reverse().map((t) => ({
36+
entry: {
37+
name: t.e.c,
38+
type: (t.e.tp[0] === 's' ? 'short' : 'long'),
39+
value: t.e.p,
40+
time: t.e.tm,
41+
},
42+
exit: {
43+
name: t.x.c,
44+
value: t.x.p,
45+
time: t.x.tm,
46+
},
47+
quantity: t.q,
48+
profit: t.tp,
49+
cumulative: t.cp,
50+
runup: t.rn,
51+
drawdown: t.dd,
52+
}));
53+
54+
// const historyParser = (history) => history.reverse().map((h) => ({
55+
3556
/**
3657
* @typedef {Object} TradeReport Trade report
3758
@@ -86,9 +107,19 @@ function getInputs(options) {
86107
* @prop {number} totalTrades Total trades
87108
*/
88109

110+
/**
111+
* @typedef {Object} FromTo
112+
* @prop {number} from From timestamp
113+
* @prop {number} to To timestamp
114+
*/
115+
89116
/**
90117
* @typedef {Object} StrategyReport
91118
* @prop {'EUR' | 'USD' | 'JPY' | '' | 'CHF'} [currency] Selected currency
119+
* @prop {Object} [settings] Backtester settings
120+
* @prop {Object} [settings.dateRange] Backtester date range
121+
* @prop {FromTo} [settings.dateRange.backtest] Date range for backtest
122+
* @prop {FromTo} [settings.dateRange.trade] Date range for trade
92123
* @prop {TradeReport[]} trades Trade list starting by the last
93124
* @prop {Object} history History Chart value
94125
* @prop {number[]} [history.buyHold] Buy hold values
@@ -275,60 +306,45 @@ module.exports = (chartSession) => class ChartStudy {
275306
changes.push('graphic');
276307
}
277308

278-
if (parsed.data && parsed.data.report && parsed.data.report.performance) {
279-
this.#strategyReport.performance = parsed.data.report.performance;
280-
changes.push('perfReport');
281-
}
309+
const updateStrategyReport = (report) => {
310+
if (report.currency) {
311+
this.#strategyReport.currency = report.currency;
312+
changes.push('report.currency');
313+
}
282314

283-
if (parsed.data && parsed.data.report && parsed.data.report.trades) {
284-
this.#strategyReport.trades = parsed.data.report.trades;
285-
changes.push('tradesReport');
286-
}
315+
if (report.settings) {
316+
this.#strategyReport.settings = report.settings;
317+
changes.push('report.settings');
318+
}
287319

288-
if (parsed.data && parsed.data.report && parsed.data.report.history) {
289-
this.#strategyReport.history = parsed.data.report.history;
290-
changes.push('historyReport');
291-
}
320+
if (report.performance) {
321+
this.#strategyReport.performance = report.performance;
322+
changes.push('report.perf');
323+
}
324+
325+
if (report.trades) {
326+
this.#strategyReport.trades = parseTrades(report.trades);
327+
changes.push('report.trades');
328+
}
329+
330+
if (report.equity) {
331+
this.#strategyReport.history = {
332+
buyHold: report.buyHold,
333+
buyHoldPercent: report.buyHoldPercent,
334+
drawDown: report.drawDown,
335+
drawDownPercent: report.drawDownPercent,
336+
equity: report.equity,
337+
equityPercent: report.equityPercent,
338+
};
339+
changes.push('report.history');
340+
}
341+
};
292342

293343
if (parsed.dataCompressed) {
294-
const parsedC = await parseCompressed(parsed.dataCompressed);
295-
296-
this.#strategyReport = {
297-
currency: parsedC.report.currency,
298-
299-
trades: parsedC.report.trades.reverse().map((t) => ({
300-
entry: {
301-
name: t.e.c,
302-
type: (t.e.tp[0] === 's' ? 'short' : 'long'),
303-
value: t.e.p,
304-
time: t.e.tm,
305-
},
306-
exit: {
307-
name: t.x.c,
308-
value: t.x.p,
309-
time: t.x.tm,
310-
},
311-
quantity: t.q,
312-
profit: t.tp,
313-
cumulative: t.cp,
314-
runup: t.rn,
315-
drawdown: t.dd,
316-
})),
317-
318-
history: {
319-
buyHold: parsedC.report.buyHold,
320-
buyHoldPercent: parsedC.report.buyHoldPercent,
321-
drawDown: parsedC.report.drawDown,
322-
drawDownPercent: parsedC.report.drawDownPercent,
323-
equity: parsedC.report.equity,
324-
equityPercent: parsedC.report.equityPercent,
325-
},
326-
327-
performance: parsedC.report.performance,
328-
};
329-
330-
changes.push('fullReport');
344+
updateStrategyReport((await parseCompressed(parsed.dataCompressed)).report);
331345
}
346+
347+
if (parsed.data && parsed.data.report) updateStrategyReport(parsed.data.report);
332348
}
333349

334350
if (data.ns.indexes && typeof data.ns.indexes === 'object') {
@@ -383,8 +399,9 @@ module.exports = (chartSession) => class ChartStudy {
383399
}
384400

385401
/**
386-
* @typedef {
387-
* 'plots' | 'perfReport' | 'tradesReport' | 'historyReport' | 'fullReport'
402+
* @typedef {'plots' | 'report.currency'
403+
* | 'report.settings' | 'report.perf'
404+
* | 'report.trades' | 'report.history'
388405
* } UpdateChangeType
389406
*/
390407

0 commit comments

Comments
 (0)