-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
add-markers-with-autosize-enabled.js
74 lines (68 loc) · 1.75 KB
/
add-markers-with-autosize-enabled.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
function generateCandle(i, target) {
const step = (i % 2) / 150;
const base = i / 5;
target.open = base * (1 - step);
target.high = base * (1 + 2 * step);
target.low = base * (1 - 2 * step);
target.close = base * (1 + step);
}
function generateData() {
const res = [];
const time = new Date(Date.UTC(2018, 0, 1, 0, 0, 0, 0));
for (let i = 0; i < 50; ++i) {
const item = {
time: time.getTime() / 1000,
};
time.setUTCDate(time.getUTCDate() + 1);
if (i % 30 !== 0) {
generateCandle(i, item);
}
res.push(item);
}
return res;
}
function runTestCase(container) {
const chart = window.chart = LightweightCharts.createChart(container, { autoSize: true, layout: { attributionLogo: false } });
const series = chart.addCandlestickSeries();
const data = generateData();
series.setData(data);
const datesForMarkers = [data[data.length - 39], data[data.length - 19]];
let indexOfMinPrice = 0;
for (let i = 1; i < datesForMarkers.length; i++) {
if (datesForMarkers[i].high < datesForMarkers[indexOfMinPrice].high) {
indexOfMinPrice = i;
}
}
const markers = [
{
time: data[data.length - 48].time,
position: 'aboveBar',
color: '#f68410',
shape: 'circle',
text: 'D',
id: 'D',
},
];
for (let i = 0; i < datesForMarkers.length; i++) {
if (i !== indexOfMinPrice) {
markers.push({
time: datesForMarkers[i].time,
position: 'aboveBar',
color: '#e91e63',
shape: 'arrowDown',
text: 'Sell @ ' + Math.floor(datesForMarkers[i].high + 2),
id: 'Sell',
});
} else {
markers.push({
time: datesForMarkers[i].time,
position: 'belowBar',
color: '#2196F3',
shape: 'arrowUp',
text: 'Buy @ ' + Math.floor(datesForMarkers[i].low - 2),
id: 'Buy',
});
}
}
series.setMarkers(markers);
}