ohlcv-indicators 3.4.8 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ohlcv-indicators.min.js +20 -18
- package/index.js +3 -2
- package/package.json +1 -1
- package/src/volatility/atr.js +20 -12
|
@@ -2905,29 +2905,30 @@ var OHLCV_INDICATORS;
|
|
|
2905
2905
|
});
|
|
2906
2906
|
},
|
|
2907
2907
|
atr: function(t, e, r, n) {
|
|
2908
|
-
var o = n.lag, i = t.verticalOhlcv,
|
|
2908
|
+
var o = n.lag, i = n.type, a = t.verticalOhlcv, u = t.instances, l = "percentage" === i ? "atr_".concat(r, "_percentage") : "atr_".concat(r);
|
|
2909
2909
|
if (0 === e) {
|
|
2910
|
-
var
|
|
2911
|
-
l
|
|
2912
|
-
|
|
2910
|
+
var c = t.instances, s = t.verticalOhlcv, f = t.arrayTypes, y = t.len, p = t.priceBased;
|
|
2911
|
+
c[l] = new Yt(r, jt), "price" === i && p.add(l), s[l] = new Float64Array(y).fill(NaN),
|
|
2912
|
+
f[l] = "Float64Array", o > 0 && t.lag([ l ], o);
|
|
2913
2913
|
}
|
|
2914
2914
|
// Retrieve the current price value.
|
|
2915
|
-
var
|
|
2916
|
-
high:
|
|
2917
|
-
low:
|
|
2918
|
-
close:
|
|
2919
|
-
},
|
|
2915
|
+
var h = {
|
|
2916
|
+
high: a.high[e],
|
|
2917
|
+
low: a.low[e],
|
|
2918
|
+
close: a.close[e]
|
|
2919
|
+
}, v = u[l];
|
|
2920
2920
|
// Update the moving average instance.
|
|
2921
|
-
|
|
2922
|
-
var
|
|
2921
|
+
v.update(h);
|
|
2922
|
+
var m = NaN;
|
|
2923
2923
|
try {
|
|
2924
|
-
|
|
2924
|
+
m = v.getResult();
|
|
2925
2925
|
} catch (t) {}
|
|
2926
|
+
return "percentage" === i && (m = Number.isNaN(m) ? NaN : m / h.close),
|
|
2926
2927
|
// Always push the MA value (even if NaN).
|
|
2927
|
-
|
|
2928
|
+
t.pushToMain({
|
|
2928
2929
|
index: e,
|
|
2929
|
-
key:
|
|
2930
|
-
value:
|
|
2930
|
+
key: l,
|
|
2931
|
+
value: m
|
|
2931
2932
|
}), !0;
|
|
2932
2933
|
},
|
|
2933
2934
|
ema: Ce,
|
|
@@ -3931,15 +3932,16 @@ var OHLCV_INDICATORS;
|
|
|
3931
3932
|
max: this.len,
|
|
3932
3933
|
allowDecimals: !1
|
|
3933
3934
|
}, "size", r), s(e, "options", r);
|
|
3934
|
-
var n = e.lag, o = void 0 === n ? 0 : n;
|
|
3935
|
+
var n = e.lag, o = void 0 === n ? 0 : n, i = e.type, a = void 0 === i ? "price" : i;
|
|
3935
3936
|
return p(o, {
|
|
3936
3937
|
min: 0,
|
|
3937
3938
|
max: this.len,
|
|
3938
3939
|
allowDecimals: !1
|
|
3939
|
-
}, "options.lag", r), this.inputParams.push({
|
|
3940
|
+
}, "options.lag", r), f([ "price", "percentage" ], a, "options.type", r), this.inputParams.push({
|
|
3940
3941
|
key: r,
|
|
3941
3942
|
params: [ t, {
|
|
3942
|
-
lag: o
|
|
3943
|
+
lag: o,
|
|
3944
|
+
type: a
|
|
3943
3945
|
} ]
|
|
3944
3946
|
}), this;
|
|
3945
3947
|
}
|
package/index.js
CHANGED
|
@@ -268,11 +268,12 @@ export default class OHLCV_INDICATORS {
|
|
|
268
268
|
validateNumber(size, {min: 1, max: this.len, allowDecimals: false}, 'size', methodName)
|
|
269
269
|
validateObject(options, 'options', methodName)
|
|
270
270
|
|
|
271
|
-
const {lag = 0} = options
|
|
271
|
+
const {lag = 0, type = 'price'} = options
|
|
272
272
|
|
|
273
273
|
validateNumber(lag, {min: 0, max: this.len, allowDecimals: false}, 'options.lag', methodName)
|
|
274
|
+
validateArrayOptions(['price', 'percentage'], type, 'options.type', methodName)
|
|
274
275
|
|
|
275
|
-
this.inputParams.push({key: methodName, params: [size, {lag}]})
|
|
276
|
+
this.inputParams.push({key: methodName, params: [size, {lag, type}]})
|
|
276
277
|
|
|
277
278
|
return this
|
|
278
279
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ohlcv-indicators",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "A JavaScript library featuring optimized trading indicators for OHLCV arrays [{open, high, low, close, value}]. This library is optimized for backtesting and trading bot development.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
package/src/volatility/atr.js
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
|
|
2
2
|
import {FasterATR, FasterWSMA} from 'trading-signals';
|
|
3
3
|
|
|
4
|
-
export const atr = (main, index, size, {lag}) => {
|
|
4
|
+
export const atr = (main, index, size, {lag, type}) => {
|
|
5
5
|
const { verticalOhlcv, instances } = main
|
|
6
|
-
const keyName = `atr_${size}`
|
|
6
|
+
const keyName = (type === 'percentage') ? `atr_${size}_percentage` : `atr_${size}`
|
|
7
7
|
|
|
8
8
|
if (index === 0) {
|
|
9
9
|
|
|
10
10
|
const {instances, verticalOhlcv, arrayTypes, len, priceBased} = main
|
|
11
11
|
|
|
12
12
|
instances[keyName] = new FasterATR(size, FasterWSMA)
|
|
13
|
-
verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
|
|
14
|
-
|
|
15
|
-
priceBased.add(keyName)
|
|
16
13
|
|
|
14
|
+
if(type === 'price')
|
|
15
|
+
{
|
|
16
|
+
priceBased.add(keyName)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
verticalOhlcv[keyName] = new Float64Array(len).fill(NaN)
|
|
20
|
+
arrayTypes[keyName] = 'Float64Array'
|
|
21
|
+
|
|
17
22
|
if(lag > 0)
|
|
18
23
|
{
|
|
19
24
|
main.lag([keyName], lag)
|
|
20
25
|
}
|
|
21
|
-
|
|
22
|
-
arrayTypes[keyName] = 'Float64Array'
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
// Retrieve the current price value.
|
|
26
|
-
const
|
|
29
|
+
const curr = {
|
|
27
30
|
high: verticalOhlcv.high[index],
|
|
28
31
|
low: verticalOhlcv.low[index],
|
|
29
32
|
close: verticalOhlcv.close[index],
|
|
@@ -31,16 +34,21 @@ export const atr = (main, index, size, {lag}) => {
|
|
|
31
34
|
const instance = instances[keyName]
|
|
32
35
|
|
|
33
36
|
// Update the moving average instance.
|
|
34
|
-
instance.update(
|
|
35
|
-
let
|
|
37
|
+
instance.update(curr);
|
|
38
|
+
let currAtr = NaN;
|
|
36
39
|
try {
|
|
37
|
-
|
|
40
|
+
currAtr = instance.getResult();
|
|
38
41
|
} catch (err) {
|
|
39
42
|
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
if(type === 'percentage')
|
|
46
|
+
{
|
|
47
|
+
currAtr = (Number.isNaN(currAtr)) ? NaN : (currAtr / curr.close)
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
// Always push the MA value (even if NaN).
|
|
43
|
-
main.pushToMain({ index, key: keyName, value:
|
|
51
|
+
main.pushToMain({ index, key: keyName, value: currAtr });
|
|
44
52
|
|
|
45
53
|
return true;
|
|
46
54
|
}
|