-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathautoscale_cb.js
48 lines (41 loc) · 1.67 KB
/
autoscale_cb.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
if (!window._bt_scale_range) {
window._bt_scale_range = function (range, min, max, pad) {
"use strict";
if (min !== Infinity && max !== -Infinity) {
pad = pad ? (max - min) * .03 : 0;
range.start = min - pad;
range.end = max + pad;
} else console.error('backtesting: scale range error:', min, max, range);
};
}
clearTimeout(window._bt_autoscale_timeout);
window._bt_autoscale_timeout = setTimeout(function () {
/**
* @variable cb_obj `fig_ohlc.x_range`.
* @variable source `ColumnDataSource`
* @variable ohlc_range `fig_ohlc.y_range`.
* @variable volume_range `fig_volume.y_range`.
*/
"use strict";
let i = Math.max(Math.floor(cb_obj.start), 0),
j = Math.min(Math.ceil(cb_obj.end), source.data['ohlc_high'].length);
let max = Math.max.apply(null, source.data['ohlc_high'].slice(i, j)),
min = Math.min.apply(null, source.data['ohlc_low'].slice(i, j));
_bt_scale_range(ohlc_range, min, max, true);
if (volume_range) {
max = Math.max.apply(null, source.data['Volume'].slice(i, j));
_bt_scale_range(volume_range, 0, max * 1.03, false);
}
if(indicator_ranges){
let keys = Object.keys(indicator_ranges);
for(var count=0;count<keys.length;count++){
if(keys[count]){
max = Math.max.apply(null, source.data[keys[count]+'_max'].slice(i, j));
min = Math.min.apply(null, source.data[keys[count]+'_min'].slice(i, j));
if(min && max){
_bt_scale_range(indicator_ranges[keys[count]], min, max, true);
}
}
}
}
}, 50);