-
-
Notifications
You must be signed in to change notification settings - Fork 403
/
Copy pathReplayMode.js
103 lines (79 loc) · 2.57 KB
/
ReplayMode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const TradingView = require('../main');
/**
* This example tests the real replay mode by fetching
* indicator data and stores it in a 'periods' variable
*/
if (!process.env.SESSION || !process.env.SIGNATURE) {
throw Error('Please set your sessionid and signature cookies');
}
console.log('----- Testing ReplayMode: -----');
const client = new TradingView.Client({
token: process.env.SESSION,
signature: process.env.SIGNATURE,
});
const chart = new client.Session.Chart();
const config = {
symbol: 'BINANCE:BTCEUR',
timeframe: 'D',
startFrom: Math.round(Date.now() / 1000) - 86400 * 7, // Seven days before now
// startFrom: 1600000000,
};
chart.setMarket(config.symbol, {
timeframe: config.timeframe,
replay: config.startFrom,
range: 1,
});
// chart.onReplayLoaded(() => {
// console.log('STARTING REPLAY MODE');
// chart.replayStart(1000);
// // setTimeout(() => {
// // console.log('STOPPING REPLAY MODE');
// // chart.replayStop();
// // }, 7000);
// });
let loading = 0;
const indicators = [];
const periods = {};
let interval = NaN;
async function step() {
const period = { ...chart.periods[0] };
const times = Object.keys(periods);
const intrval = times[times.length - 1] - times[times.length - 2];
if (Number.isNaN(interval) && times.length >= 2) interval = intrval;
if (!Number.isNaN(interval) && interval !== intrval) {
throw new Error(`Wrong interval: ${intrval} (should be ${interval})`);
}
indicators.forEach(([n, i]) => {
const plots = { ...i.periods[0] };
delete plots.$time;
period[n] = { plots };
Object.keys(i.graphic).forEach((g) => {
if (!i.graphic[g].length) return;
if (!period[n].graphics) period[n].graphics = {};
period[n].graphics[g] = i.graphic[g];
});
});
periods[period.time] = period;
console.log('Next ->', period.time, times.length);
await chart.replayStep(1);
step();
}
chart.onReplayEnd(async () => {
await client.end();
console.log('Done !', Object.keys(periods).length);
});
async function addIndicator(name, pineId, options = {}) {
loading += 1;
const indic = pineId.includes('@')
? new TradingView.BuiltInIndicator(pineId)
: await TradingView.getIndicator(pineId);
Object.keys(options).forEach((o) => { indic.setOption(o, options[o]); });
const std = new chart.Study(indic);
std.onReady(() => {
indicators.push([name, std]);
if (loading === indicators.length) step();
});
}
addIndicator('Volume', 'Volume@tv-basicstudies-241');
addIndicator('EMA_50', 'STD;EMA', { Length: 50 });
addIndicator('EMA_200', 'STD;EMA', { Length: 200 });